Skip to content

Commit

Permalink
run_command output to console if capture: false
Browse files Browse the repository at this point in the history
  • Loading branch information
julianneswinoga committed Nov 28, 2024
1 parent 467da05 commit c30fb05
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
12 changes: 7 additions & 5 deletions mesonbuild/interpreter/interpreterobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,20 @@ def run_command(self,
child_env = os.environ.copy()
child_env.update(menv)
child_env = env.get_env(child_env)
stdout = subprocess.PIPE if self.capture else subprocess.DEVNULL
stdout = subprocess.PIPE if self.capture else None
stderr = subprocess.PIPE if self.capture else None
mlog.debug('Running command:', mesonlib.join_args(command_array))
try:
p, o, e = Popen_safe(command_array, stdout=stdout, env=child_env, cwd=cwd)
p, o, e = Popen_safe(command_array, stdout=stdout, stderr=stderr, env=child_env, cwd=cwd)
if self.capture:
mlog.debug('--- stdout ---')
mlog.debug(o)
mlog.debug('--- stderr ---')
mlog.debug(e)
else:
o = ''
mlog.debug('--- stdout disabled ---')
mlog.debug('--- stderr ---')
mlog.debug(e)
e = ''
mlog.debug('--- output disabled ---')
mlog.debug('')

if check and p.returncode != 0:
Expand Down
24 changes: 18 additions & 6 deletions test cases/common/33 run program/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,26 @@ if c.returncode() != 0
endif

py3 = import('python3').find_python()

ret = run_command(py3, '-c', 'print("some output")', check: false)
ret = run_command(
py3,
'-c',
'import sys; print("capture: true stdout"); print("capture: true stderr", file=sys.stderr)',
check: false,
)
assert(ret.returncode() == 0, 'failed to run python3: ' + ret.stderr())
assert(ret.stdout() == 'some output\n', 'failed to run python3')

ret = run_command(py3, '-c', 'print("some output")', check: false, capture: false)
assert(ret.stdout() == 'capture: true stdout\n', 'failed to run python3')
assert(ret.stderr() == 'capture: true stderr\n', 'failed to run python3')

ret = run_command(
py3,
'-c',
'import sys; print("capture: false stdout"); print("capture: false stderr", file=sys.stderr)',
check: false,
capture: false,
)
assert(ret.returncode() == 0, 'failed to run python3: ' + ret.stderr())
assert(ret.stdout() == '', 'stdout is "@0@" instead of empty'.format(ret.stdout()))
assert(ret.stderr() == '', 'stderr is "@0@" instead of empty'.format(ret.stderr()))

c_env = environment()
c_env.append('CUSTOM_ENV_VAR', 'FOOBAR')
Expand All @@ -76,7 +88,7 @@ assert(ret.stdout() == 'FOOBAR\n', 'stdout is "@0@" instead of FOOBAR'.format(re

dd = find_program('dd', required : false)
if dd.found()
ret = run_command(dd, 'if=/dev/urandom', 'bs=10', 'count=1', check: false, capture: false)
ret = run_command(dd, 'if=/dev/urandom', 'bs=10', 'count=1', 'of=/dev/null', check: false, capture: false)
assert(ret.returncode() == 0, 'failed to run dd: ' + ret.stderr())
assert(ret.stdout() == '', 'stdout is "@0@" instead of empty'.format(ret.stdout()))
endif
Expand Down
18 changes: 18 additions & 0 deletions unittests/allplatformstests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5058,3 +5058,21 @@ def test_rsp_support(self):
'link', 'lld-link', 'mwldarm', 'mwldeppc', 'optlink', 'xilink',
}
self.assertEqual(cc.linker.get_accepts_rsp(), has_rsp)

def test_run_command_output(self):
'''
Test that run_command will output to stdout/stderr if `check: false`.
'''
testdir = os.path.join(self.common_test_dir, '33 run program')

# inprocess=False uses BasePlatformTests::_run, which by default
# redirects all stderr to stdout, so we look for the expected stderr
# in the merged stdout.
# inprocess=True captures stderr and stdout separatly, but doesn't
# return stderr (only printing it on failure) so unless we change the
# function signature we can't get at the stderr outupt
out = self.init(testdir, inprocess=False)
assert('capture: true stdout' not in out)
assert('capture: true stderr' not in out)
assert('capture: false stdout' in out)
assert('capture: false stderr' in out)

0 comments on commit c30fb05

Please sign in to comment.