| 1 | import pytest |
| 2 | import subprocess |
| 3 | import textwrap |
| 4 | |
| 5 | from labgrid.util.helper import processwrapper |
| 6 | |
| 7 | |
| 8 | def test_processwrapper_passes(): |
| 9 | processwrapper.check_output(["true"]) |
| 10 | |
| 11 | |
| 12 | def test_processwrapper_fails(): |
| 13 | with pytest.raises(subprocess.CalledProcessError): |
| 14 | processwrapper.check_output(["false"]) |
| 15 | |
| 16 | |
| 17 | def test_processwrapper_output(tmpdir): |
| 18 | content = textwrap.dedent( |
| 19 | """\ |
| 20 | A |
| 21 | B |
| 22 | C |
| 23 | """ |
| 24 | ) |
| 25 | |
| 26 | tmpfile = tmpdir.join("output.txt") |
| 27 | tmpfile.write(content) |
| 28 | |
| 29 | output = processwrapper.check_output(["cat", str(tmpfile)]) |
| 30 | |
| 31 | assert output.decode("utf-8") == content |
| 32 | |
| 33 | |
| 34 | def test_processwrapper_input(): |
| 35 | content = textwrap.dedent( |
| 36 | """\ |
| 37 | A |
| 38 | B |
| 39 | C |
| 40 | """ |
| 41 | ) |
| 42 | |
| 43 | output = processwrapper.check_output(["cat", "-"], input=content.encode("utf-8")) |
| 44 | |
| 45 | assert output.decode("utf-8") == content |
| 46 | |
| 47 | |
| 48 | def test_processwrapper_empty_input(): |
| 49 | output = processwrapper.check_output(["cat", "-"], input=b"") |
| 50 | assert output.decode("utf-8") == "" |