-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
409 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# vim: set ai et ts=4 sw=4 tw=80: | ||
# SPDX-FileCopyrightText: 2024-present Atri Bhattacharya <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
"""Common fixtures""" | ||
|
||
import os | ||
import shutil | ||
|
||
import pytest | ||
|
||
from tartex.tartex import TarTeX | ||
|
||
|
||
@pytest.fixture | ||
def sample_texfile(): | ||
"""Pytest fixture: TarTeX with just a tex file for parameter""" | ||
return TarTeX(["some_file.tex"]) | ||
|
||
|
||
# Data copying to tmpdir to allow using with pytest | ||
# Note that relative paths to the data do not work as it is never certain which | ||
# location pytest is run from | ||
# Based on https://stackoverflow.com/a/29631801 | ||
@pytest.fixture | ||
def datadir(tmpdir, request): | ||
''' | ||
Fixture responsible for searching a folder with the same name of test | ||
module and, if available, moving all contents to a temporary directory so | ||
tests can use them freely. | ||
''' | ||
filename = request.module.__file__ | ||
test_dir, _ = os.path.splitext(filename) | ||
|
||
if os.path.isdir(test_dir): | ||
shutil.copytree(test_dir, tmpdir, dirs_exist_ok=True) | ||
|
||
return tmpdir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
# SPDX-FileCopyrightText: 2024-present Atri Bhattacharya <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
# | ||
"""Tests for tarball generation from basic latex files""" | ||
import os | ||
import tarfile as tar | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from tartex.tartex import TAR_DEFAULT_COMP, TarTeX | ||
|
||
|
||
@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(), | ||
"-v", | ||
"-s", | ||
"-o", | ||
default_target.as_posix(), | ||
] | ||
) | ||
|
||
|
||
class TestBasicLaTeX: | ||
"""Tests checking tar file generation from a basic latex file""" | ||
|
||
def test_gen_tar(self, default_target, default_tartex_obj): | ||
"""Should include a single file in tarball""" | ||
output = default_target.with_suffix(".tar.gz") | ||
t = default_tartex_obj | ||
t.tar_files() | ||
assert output.exists() is True | ||
with tar.open(output) as rat: | ||
assert len(rat.getnames()) == 1 | ||
|
||
def test_diff_target_dir(self, tmpdir, datadir): | ||
# Make a new dir inside tmpdir | ||
destdir = tmpdir / "dest" | ||
os.mkdir(destdir) | ||
t = TarTeX([(Path(datadir) / "basic_latex.tex").as_posix(), | ||
"-v", | ||
"-s", | ||
"-o", | ||
str(destdir / "output.tar.gz")]) | ||
t.tar_files() | ||
dest = t.tar_file.with_suffix(f".tar.{t.tar_ext}") | ||
print(dest) | ||
assert t.tar_file.with_suffix(f".tar.{t.tar_ext}").exists() | ||
|
||
|
||
# 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_sol_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 _: "") | ||
|
||
# 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_sol_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 _: "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_sol_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 _: "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_sol_newname_ok(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 _: 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_sol_newname_old(self, default_tartex_obj, tmpdir, monkeypatch): | ||
"""Test error when entering new name that is same as the old name""" | ||
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 _: 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 | ||
|
||
def test_sol_newext(self, default_tartex_obj, tmpdir, monkeypatch): | ||
"""Test new name with just the file ext changed""" | ||
t_con = default_tartex_obj | ||
t_con.tar_files() | ||
|
||
output = str(tmpdir / "test.tar.gz") | ||
assert Path(output).exists() is True | ||
|
||
output = output.replace(".gz", ".xz") | ||
# Monkeypatch responses for choosing file name same as original | ||
user_inputs = iter(['c', output]) | ||
monkeypatch.setattr("builtins.input", lambda _: next(user_inputs)) | ||
t_con.tar_files() | ||
assert Path(output).exists() is True | ||
assert t_con.tar_ext == "xz" | ||
with tar.open(output) as rat: | ||
assert len(rat.getnames()) == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
\documentclass{article} | ||
|
||
\begin{document} | ||
\section{Test document section} | ||
This is a test document. | ||
\end{document} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# vim:set et sw=4 ts=4 tw=80: | ||
""" | ||
Test inclusion and exclusion of .bib files | ||
""" | ||
|
||
import tarfile as tar | ||
from tartex.tartex import TarTeX, TAR_DEFAULT_COMP | ||
|
||
|
||
def test_bib(datadir, tmpdir): | ||
"""Test if tar file contains .bib when -b is passed""" | ||
t = TarTeX( | ||
[ | ||
str(datadir / "main_bib.tex"), | ||
"-b", | ||
"-s", | ||
"-v", | ||
"-o", | ||
f"{str(tmpdir)}/main_bib", | ||
] | ||
) | ||
t.tar_files() | ||
with tar.open(t.tar_file.with_suffix(f".tar.{TAR_DEFAULT_COMP}")) as f: | ||
assert "refs.bib" in f.getnames() # Check: .bib file is in tarball | ||
# Check main_bib.bbl file is in tarball even though not in srcdir | ||
assert "main_bib.bbl" in f.getnames() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
\documentclass[11pt,a4paper]{article} | ||
|
||
\author{No Author} | ||
\title{Test bib} | ||
\date{\today} | ||
\begin{document} | ||
\maketitle | ||
\tableofcontents | ||
|
||
\section{Only Section} | ||
|
||
This is a test document with a single citation \cite{testarticle}. | ||
|
||
\subsection{Empty subsection} | ||
|
||
\bibliographystyle{plain} | ||
\bibliography{refs} | ||
|
||
\end{document} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@ARTICLE{testarticle, | ||
author = {No Name}, | ||
title = {No Title}, | ||
journal = {No Journal}, | ||
year = {2024}, | ||
otherinfo = {Purely for testing} | ||
} | ||
|
Oops, something went wrong.