log tests use tmpdir properly

This commit is contained in:
Todd Gamblin 2017-10-23 14:13:33 +02:00
parent 0bb1eb32f2
commit b98fc48273

View File

@ -32,36 +32,39 @@
def test_log_python_output_with_python_stream(capsys, tmpdir): def test_log_python_output_with_python_stream(capsys, tmpdir):
# pytest's DontReadFromInput object does not like what we do here, so # pytest's DontReadFromInput object does not like what we do here, so
# disable capsys or things hang. # disable capsys or things hang.
with capsys.disabled(): with tmpdir.as_cwd():
with log_output('foo.txt'): with capsys.disabled():
print('logged') with log_output('foo.txt'):
print('logged')
with open('foo.txt') as f: with open('foo.txt') as f:
assert f.read() == 'logged\n' assert f.read() == 'logged\n'
assert capsys.readouterr() == ('', '') assert capsys.readouterr() == ('', '')
def test_log_python_output_with_fd_stream(capfd, tmpdir): def test_log_python_output_with_fd_stream(capfd, tmpdir):
with log_output('foo.txt'): with tmpdir.as_cwd():
print('logged') with log_output('foo.txt'):
print('logged')
with open('foo.txt') as f: with open('foo.txt') as f:
assert f.read() == 'logged\n' assert f.read() == 'logged\n'
assert capfd.readouterr() == ('', '') assert capfd.readouterr() == ('', '')
def test_log_python_output_and_echo_output(capfd, tmpdir): def test_log_python_output_and_echo_output(capfd, tmpdir):
with log_output('foo.txt') as logger: with tmpdir.as_cwd():
with logger.force_echo(): with log_output('foo.txt') as logger:
print('echo') with logger.force_echo():
print('logged') print('echo')
print('logged')
assert capfd.readouterr() == ('echo\n', '') assert capfd.readouterr() == ('echo\n', '')
with open('foo.txt') as f: with open('foo.txt') as f:
assert f.read() == 'echo\nlogged\n' assert f.read() == 'echo\nlogged\n'
@pytest.mark.skipif(not which('echo'), reason="needs echo command") @pytest.mark.skipif(not which('echo'), reason="needs echo command")
@ -72,24 +75,26 @@ def test_log_subproc_output(capsys, tmpdir):
# TODO: figure out why this is and whether it means we're doing # TODO: figure out why this is and whether it means we're doing
# sometihng wrong with OUR redirects. Seems like it should work even # sometihng wrong with OUR redirects. Seems like it should work even
# with capsys enabled. # with capsys enabled.
with capsys.disabled(): with tmpdir.as_cwd():
with log_output('foo.txt'): with capsys.disabled():
echo('logged') with log_output('foo.txt'):
echo('logged')
with open('foo.txt') as f: with open('foo.txt') as f:
assert f.read() == 'logged\n' assert f.read() == 'logged\n'
@pytest.mark.skipif(not which('echo'), reason="needs echo command") @pytest.mark.skipif(not which('echo'), reason="needs echo command")
def test_log_subproc_and_echo_output(capfd, tmpdir): def test_log_subproc_and_echo_output(capfd, tmpdir):
echo = which('echo') echo = which('echo')
with log_output('foo.txt') as logger: with tmpdir.as_cwd():
with logger.force_echo(): with log_output('foo.txt') as logger:
echo('echo') with logger.force_echo():
print('logged') echo('echo')
print('logged')
assert capfd.readouterr() == ('echo\n', '') assert capfd.readouterr() == ('echo\n', '')
with open('foo.txt') as f: with open('foo.txt') as f:
assert f.read() == 'logged\n' assert f.read() == 'logged\n'