From 5dc6f3d58cd04ce659a548975ba5692b73a8bdf6 Mon Sep 17 00:00:00 2001 From: Atri Bhattacharya Date: Sat, 10 Feb 2024 19:40:56 +0530 Subject: [PATCH] test: Add tests for target name conflicts. --- tests/test_basic_latex.py | 107 +++++++++++++++++++++++++++++++++++--- 1 file changed, 100 insertions(+), 7 deletions(-) diff --git a/tests/test_basic_latex.py b/tests/test_basic_latex.py index d05623e..f3484ea 100644 --- a/tests/test_basic_latex.py +++ b/tests/test_basic_latex.py @@ -6,19 +6,112 @@ import tarfile as tar from pathlib import Path import pytest -from tartex.tartex import TarTeX +from tartex.tartex import TarTeX, TAR_DEFAULT_COMP + + +@pytest.fixture +def default_target(datadir): + return Path(datadir) / "test" + + +@pytest.fixture +def default_tartex_obj(datadir, default_target): + return TarTeX( + [ + (Path(datadir) / "basic_latex.tex").as_posix(), + "-o", + default_target.as_posix(), + ] + ) class TestBasicLaTeX: """Tests checking tar file generation from a basic latex file""" - def test_gen_tar(self, datadir, tmpdir, capsys): + + def test_gen_tar(self, default_target, default_tartex_obj, capsys): """Should include a single file in tarball""" - output = Path(f"{tmpdir}/test.tar.gz") - t = TarTeX([(Path(datadir) / "basic_latex.tex").as_posix(), - "-o", - output.as_posix()]) + output = default_target.with_suffix(".tar.gz") + t = default_tartex_obj t.tar_files() - print(capsys.readouterr().out) assert output.exists() is True with tar.open(output) as rat: assert len(rat.getnames()) == 1 + + +# These tests involve repeatedly compiling LaTeX files, thus can be slow +@pytest.mark.slow +class TestTarConflict: + """Tests checking resolutions for tar file name conflicts""" + + def test_resolve_default(self, default_tartex_obj, monkeypatch): + """Test when user response is not workable""" + t_con = default_tartex_obj + t_con.tar_files() + + # Monkeypatch empty response for input + monkeypatch.setattr("builtins.input", lambda resolve: "") + + # Trying to create tar file again will lead to conflic res dialog + # Blank user input (from monkeypatch) will raise SystemExit + with pytest.raises(SystemExit) as exc: + t_con.tar_files() + + assert "Not overwriting existing tar file" in exc.value.code + + def test_resolve_quit(self, default_tartex_obj, monkeypatch): + """Test when user response is not workable""" + t_con = default_tartex_obj + t_con.tar_files() + + # Monkeypatch empty response for input + monkeypatch.setattr("builtins.input", lambda resolve: "q") + + # Trying to create tar file again will lead to conflic res dialog + # Blank user input (from monkeypatch) will raise SystemExit + with pytest.raises(SystemExit) as exc: + t_con.tar_files() + + assert "Not overwriting existing tar file" in exc.value.code + + def test_resolve_overwrite(self, default_tartex_obj, monkeypatch): + """Test overwrite resolution""" + t_con = default_tartex_obj + t_con.tar_files() + + # Monkeypatch empty response for input + monkeypatch.setattr("builtins.input", lambda resolve: "o") + t_con.tar_files() + output = t_con.tar_file.with_suffix(f".tar.{TAR_DEFAULT_COMP}") + assert output.exists() is True + with tar.open(output) as rat: + assert len(rat.getnames()) == 1 + + def test_resolve_newname_good(self, default_tartex_obj, tmpdir, monkeypatch): + """Test entering new name that works""" + t_con = default_tartex_obj + t_con.tar_files() + + output = str(tmpdir / "new.tar.gz") + # Monkeypatch responses for choosing a new file name + user_inputs = iter(['c', output]) + monkeypatch.setattr("builtins.input", lambda resolve: next(user_inputs)) + t_con.tar_files() + assert Path(output).exists() is True + with tar.open(output) as rat: + assert len(rat.getnames()) == 1 + + def test_resolve_newname_bad(self, default_tartex_obj, tmpdir, monkeypatch): + """Test overwrite resolution""" + t_con = default_tartex_obj + t_con.tar_files() + + output = str(tmpdir / "test.tar.gz") + assert Path(output).exists() is True + + # Monkeypatch responses for choosing file name same as original + user_inputs = iter(['c', output]) + monkeypatch.setattr("builtins.input", lambda resolve: next(user_inputs)) + with pytest.raises(SystemExit) as exc: + t_con.tar_files() + + assert "New name entered is also the same" in exc.value.code