Skip to content

Commit

Permalink
Add stderr capture argument to conanfile's run() method (#15121)
Browse files Browse the repository at this point in the history
Add stderr capture to conanfile.run method
  • Loading branch information
AbrilRBS authored Nov 20, 2023
1 parent 6834956 commit 7f80c3b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
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)

0 comments on commit 7f80c3b

Please sign in to comment.