diff --git a/tests/test_extract_odf_links.py b/tests/test_extract_odf_links.py index 46619ae30af84e332f8de5ab8d00164fbbf16989..1e4f2b8801f92b80a4622f963f6750c8ed6d7f28 100644 --- a/tests/test_extract_odf_links.py +++ b/tests/test_extract_odf_links.py @@ -15,6 +15,7 @@ # along with this program. If not, see . import io +import sys import pytest @@ -38,6 +39,14 @@ def expected_links(rel_path): for path in INCLUDED_FILE_LINKS ) +def check_output(stdout, sep, rel_path): + actual = stdout.getvalue().split(sep) + if actual and not actual[-1]: + actual.pop() + expected = expected_links(rel_path) + assert len(actual) == len(expected) + assert set(actual) == expected + @pytest.mark.parametrize('arglist,sep', [ (['-0'], '\0'), (['-d', '\\v'], '\v'), @@ -50,15 +59,33 @@ def test_extract_file_links(arglist, sep, caplog): exitcode = extract_odf_links.main(arglist, stdout, stderr) assert exitcode == 0 assert not stderr.getvalue() - actual = stdout.getvalue().split(sep) - if actual and not actual[-1]: - actual.pop() - expected = expected_links(SRC_PATH.parent) - assert len(actual) == len(expected) - assert set(actual) == expected + check_output(stdout, sep, SRC_PATH.parent) assert caplog.records assert any( log.levelname == 'WARNING' and log.message.endswith('/Bad Link.txt not found') for log in caplog.records ) + +@pytest.mark.parametrize('rel_path', [ + Path('/run'), + Path('/tmp'), +]) +def test_extract_relative_to(rel_path): + arglist = ['--relative', str(rel_path), '-0', '-'] + stdout = io.StringIO() + stderr = io.StringIO() + orig_stdin = sys.stdin + try: + sys.stdin = SRC_PATH.open('rb') + exitcode = extract_odf_links.main(arglist, stdout, stderr) + finally: + sys.stdin = orig_stdin + assert exitcode == 0 + assert not stderr.getvalue() + check_output(stdout, '\0', rel_path) + +def test_reading_stdin_requires_relative_to(): + with pytest.raises(SystemExit) as exc_check: + extract_odf_links.main(['-']) + assert exc_check.value.args[0] == 2