Skip to content
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

Add stderr capture argument to conanfile's run() method #15121

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions conans/model/conan_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def generators_path(self) -> Path:
return Path(self.generators_folder)

def run(self, command, stdout=None, cwd=None, ignore_errors=False, env="", quiet=False,
shell=True, scope="build"):
shell=True, scope="build", stderr=None):
# NOTE: "self.win_bash" is the new parameter "win_bash" for Conan 2.0
command = self._conan_helpers.cmd_wrapper.wrap(command, conanfile=self)
if env == "": # This default allows not breaking for users with ``env=None`` indicating
Expand All @@ -332,7 +332,7 @@ def run(self, command, stdout=None, cwd=None, ignore_errors=False, env="", quiet
from conans.util.runners import conan_run
if not quiet:
ConanOutput().writeln(f"{self.display_name}: RUN: {command}", fg=Color.BRIGHT_BLUE)
retcode = conan_run(wrapped_cmd, cwd=cwd, stdout=stdout, shell=shell)
retcode = conan_run(wrapped_cmd, cwd=cwd, stdout=stdout, stderr=stderr, shell=shell)
if not quiet:
ConanOutput().writeln("")

Expand Down
15 changes: 15 additions & 0 deletions conans/test/functional/conanfile/runner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,18 @@ def source(self):
client.save({"conanfile.py": conanfile})
client.run("source .")
self.assertIn('conanfile.py: Buffer got msgs Hello', client.out)

def test_custom_stream_stderr(self):
conanfile = textwrap.dedent("""
from io import StringIO
from conan import ConanFile
class Pkg(ConanFile):
def source(self):
my_buf = StringIO()
self.run('echo Hello 1>&2', stderr=my_buf)
self.output.info("Buffer got stderr msgs {}".format(my_buf.getvalue()))
""")
client = TestClient()
client.save({"conanfile.py": conanfile})
client.run("source .")
self.assertIn('conanfile.py: Buffer got stderr msgs Hello', client.out)