[llvm] [llvm-lit] Add redirection handling for `env` command without args and write a lit test to check behavior with lit internal shell (PR #106629)

Alexander Richardson via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 29 23:40:55 PDT 2024


================
@@ -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(), []
+                            )
+                        )
----------------
arichardson wrote:

```suggestion
                        # Write directly to the redirected file (stdout).
                        stdout.write(env_str)
                        captured_stdout = ""
                    else:
                        # Capture the output for cases without redirection.
                        captured_stdout = env_str
                    results.append(
                        ShellCommandResult(
                            j, captured_stdout, "", 0, timeoutHelper.timeoutReached(), []
                        )
                    )
```

This is a bit shorter and avoids all end empty lines with closing parens.

https://github.com/llvm/llvm-project/pull/106629


More information about the llvm-commits mailing list