-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[llvm-lit] Add redirection handling for env
command without args and write a lit test to check behavior with lit internal shell
#106629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -746,11 +746,25 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper): | |||||||||||||||||||||||||||||||||||||||||||||||||||
env_str = "\n".join( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
f"{key}={value}" for key, value in sorted(cmd_shenv.env.items()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
results.append( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
ShellCommandResult( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
j, env_str, "", 0, timeoutHelper.timeoutReached(), [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
# Process redirections. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
stdin, stdout, stderr = processRedirects( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
j, default_stdin, cmd_shenv, opened_files | ||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
if stdout != default_stdin: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
# Write directly to the redirected file (stdout). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
stdout.write(env_str) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
results.append( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
ShellCommandResult( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
j, "", "", 0, timeoutHelper.timeoutReached(), [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
# Capture the output for cases without redirection. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
results.append( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
ShellCommandResult( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
j, env_str, "", 0, timeoutHelper.timeoutReached(), [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+754
to
+767
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This is a bit shorter and avoids all end empty lines with closing parens. |
||||||||||||||||||||||||||||||||||||||||||||||||||||
return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
elif args[0] == "not": | ||||||||||||||||||||||||||||||||||||||||||||||||||||
not_args.append(args.pop(0)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
## Test the env command with output redirection to a file. | ||
# RUN: rm -f %t | ||
# RUN: env > %t | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can see from the code that the redirect is only consumed if there are no arguments, but could we also check that |
||
# RUN: FileCheck %s < %t | ||
|
||
# CHECK: BAR=2 | ||
# CHECK: FOO=1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this always true? I admit I didn't try to understand all of processRedirects, but it doesn't look like the input argument is ever returned for
stdout
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The input argument (
default_stdin
, which is set tosubprocess.PIPE
) is not always returned directly asstdout
by theprocessRedirects
function. The function's purpose is to handle any redirections specified in the command. If there's a redirection forstdout
(like>
or>>
),processRedirects
will open a file and return a file descriptor for that file instead of returning the originalsubprocess.PIPE
.So, when you pass
subprocess.PIPE
as the default forstdout
,processRedirects
might override it with a different file descriptor if redirection is specified. If no redirection is specified, then it could returnsubprocess.PIPE
as it was originally passed. This means the conditionif stdout != default_stdin
is designed to check whetherstdout
was redirected to something other than the pipe, and it's not always true—it depends on whether any redirection was applied.Did that answer your question?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked at processRedirects() and could not see the
stdin_source
parameter (what you are passing asdefault_stdin
) ever being returned as the second tuple element.As you say the check is actually
if stdout != subprocess.PIPE
and the way it is spelled now is very confusing since it seems to imply some relationship between stdout and stdin.As I understand your comment the
!= default_stdin
check isn't actually checking anything related to what was stdin value for the process, but just relies on the fact thatdefault_stdin
holdssubprocess.PIPE
. With the following check the code makes a lot more sense to me: