Skip to content

Commit

Permalink
Merge branch 'test-tar-from-tex'
Browse files Browse the repository at this point in the history
  • Loading branch information
badshah400 committed Feb 11, 2024
2 parents 1689ea5 + 38ce6fa commit 4286bae
Show file tree
Hide file tree
Showing 16 changed files with 409 additions and 36 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/test_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: Python package

on:
push:
branches: [ "main", "feature-github-actions" ]
branches: [ "main", "test-tar-from-tex" ]
pull_request:
branches: [ "main" ]

Expand All @@ -30,6 +30,11 @@ jobs:
python -m pip install flake8 pytest
python -m pip install hatch
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Install TeX Live
uses: zauguin/install-texlive@v3
with:
packages: >
latex-bin latexmk scheme-basic
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
Expand All @@ -38,4 +43,7 @@ jobs:
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with hatch run pytest
run: |
# TEMPDIR is needed tor tex compilation based tests
export TMPDIR=$(mktemp -d -p .)
hatch run pytest -v
rm -fr ${TMPDIR}
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,7 @@ exclude_lines = [
addopts = [
"--import-mode=importlib",
]

markers = [
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
]
39 changes: 39 additions & 0 deletions tests/conftest.py
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
44 changes: 9 additions & 35 deletions tests/test_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest

from tartex.__about__ import __version__
from tartex.tartex import TAR_DEFAULT_COMP, TarTeX, make_tar
from tartex.tartex import TarTeX, make_tar


class TestArgs:
Expand All @@ -21,11 +21,10 @@ def test_no_args(self):

assert exc.value.code == 2

def test_only_file(self):
def test_only_file(self, sample_texfile):
"""Test success with one arg: file name"""
t = TarTeX(["some_file.tex"])
assert t.main_file.stem == "some_file"
assert t.tar_file.name == "some_file.tar"
assert sample_texfile.main_file.stem == "some_file"
assert sample_texfile.tar_file.name == "some_file.tar"

def test_version(self, capsys):
"""Test version string against version from __about.py__"""
Expand All @@ -37,38 +36,13 @@ def test_version(self, capsys):
assert f"{__version__}" in output
assert exc.value.code == 0

def test_taropts_conflict(self, capsys):
"""Test exit status when both --bzip2 and --gzip are passed"""
@pytest.mark.parametrize(("tar_opt1", "tar_opt2"),
[("-J", "-z"), ("-j", "-J"), ("-z", "-J")])
def test_taropts_conflict(self, capsys, tar_opt1, tar_opt2):
"""Test exit status when two conflicting tar options are passed"""
with pytest.raises(SystemExit) as exc:
TarTeX(["--bzip2", "--gzip"])
TarTeX([tar_opt1, tar_opt2, "some_file.tex"])

assert exc.value.code == 2
output = capsys.readouterr().err
assert "not allowed with" in output


class TestTarExt:
"""
Class of tests checking automatic tar compression detection based on user
specified outout tarfile name
"""
def test_default(self):
""""Test default tarball extension"""
t = TarTeX(["some_file.tex"])
assert t.tar_ext == TAR_DEFAULT_COMP

def test_output_bzip2(self):
"""Test user output file as .tar.bz2"""
t = TarTeX(["-o", "dest.tar.bz2", "some_file.tex"])
assert t.tar_ext == "bz2"
t = TarTeX(["-o", "dest.tar.bz2", "-J", "some_file.tex"])
assert t.tar_ext == "xz"
assert t.tar_file.name == "dest.tar"

def test_output_xz(self):
"""Test user output file as .tar.xz"""
t = TarTeX(["-o", "dest.tar.xz", "some_file.tex"])
assert t.tar_ext == "xz"
t = TarTeX(["-o", "dest.tar.xz", "-j", "some_file.tex"])
assert t.tar_ext == "bz2"
assert t.tar_file.name == "dest.tar"
154 changes: 154 additions & 0 deletions tests/test_basic_latex.py
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
6 changes: 6 additions & 0 deletions tests/test_basic_latex/basic_latex.tex
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}
26 changes: 26 additions & 0 deletions tests/test_bib.py
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()
19 changes: 19 additions & 0 deletions tests/test_bib/main_bib.tex
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}
8 changes: 8 additions & 0 deletions tests/test_bib/refs.bib
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}
}

Loading

0 comments on commit 4286bae

Please sign in to comment.