1+ """Test cases for CLI functionality."""
2+
3+ import pytest
4+ from click .testing import CliRunner
5+ from pathlib import Path
6+ from tex_minify .cli import cli
7+
8+
9+ def test_minify_command (tmp_path ):
10+ runner = CliRunner ()
11+
12+ # Create a test input file
13+ input_file = tmp_path / "test.tex"
14+ input_file .write_text (r"\input{chapter1}" )
15+
16+ # Create the input file that will be referenced
17+ chapter_file = tmp_path / "chapter1.tex"
18+ chapter_file .write_text ("Chapter 1 content" )
19+
20+ # Test with output to file
21+ output_file = tmp_path / "output.tex"
22+ result = runner .invoke (cli , ["minify" , str (input_file ), "-o" , str (output_file )])
23+ assert result .exit_code == 0
24+ assert output_file .read_text () == "Chapter 1 content"
25+
26+ # Test with output to stdout
27+ result = runner .invoke (cli , ["minify" , str (input_file )])
28+ assert result .exit_code == 0
29+ assert result .output == "Chapter 1 content\n "
30+
31+
32+ def test_filter_bib_command (tmp_path ):
33+ runner = CliRunner ()
34+
35+ # Create test input files
36+ tex_file = tmp_path / "test.tex"
37+ tex_file .write_text (r"""
38+ This is a test document.
39+ \cite{key1,key2}
40+ \citep{key3}
41+ """ )
42+
43+ bib_file = tmp_path / "test.bib"
44+ bib_file .write_text ("""
45+ @article{key1,
46+ title = {Title 1},
47+ author = {Author 1},
48+ }
49+
50+ @book{key2,
51+ title = {Title 2},
52+ author = {Author 2},
53+ }
54+
55+ @inproceedings{key3,
56+ title = {Title 3},
57+ author = {Author 3},
58+ }
59+
60+ @misc{key4,
61+ title = {Title 4},
62+ author = {Author 4},
63+ }
64+ """ )
65+
66+ # Test with output to file
67+ output_file = tmp_path / "filtered.bib"
68+ result = runner .invoke (cli , ["filter-bib" , str (tex_file ), str (bib_file ), "-o" , str (output_file )])
69+ assert result .exit_code == 0
70+
71+ filtered_content = output_file .read_text ()
72+ assert "@article{key1," in filtered_content
73+ assert "@book{key2," in filtered_content
74+ assert "@inproceedings{key3," in filtered_content
75+ assert "@misc{key4," not in filtered_content
76+
77+ # Test with output to stdout
78+ result = runner .invoke (cli , ["filter-bib" , str (tex_file ), str (bib_file )])
79+ assert result .exit_code == 0
80+ assert "@article{key1," in result .output
81+ assert "@book{key2," in result .output
82+ assert "@inproceedings{key3," in result .output
83+ assert "@misc{key4," not in result .output
84+
85+
86+ def test_filter_bib_command_no_citations (tmp_path ):
87+ runner = CliRunner ()
88+
89+ # Create test input files with no citations
90+ tex_file = tmp_path / "test.tex"
91+ tex_file .write_text ("This is a test document with no citations." )
92+
93+ bib_file = tmp_path / "test.bib"
94+ bib_file .write_text ("""
95+ @article{key1,
96+ title = {Title 1},
97+ author = {Author 1},
98+ }
99+ """ )
100+
101+ # Test with output to file
102+ output_file = tmp_path / "filtered.bib"
103+ result = runner .invoke (cli , ["filter-bib" , str (tex_file ), str (bib_file ), "-o" , str (output_file )])
104+ assert result .exit_code == 0
105+ assert output_file .read_text ().strip () == ""
106+
107+
108+ def test_filter_bib_command_missing_files (tmp_path ):
109+ runner = CliRunner ()
110+
111+ # Test with non-existent TeX file
112+ result = runner .invoke (cli , ["filter-bib" , "nonexistent.tex" , "test.bib" ])
113+ assert result .exit_code != 0
114+
115+ # Test with non-existent BibTeX file
116+ tex_file = tmp_path / "test.tex"
117+ tex_file .write_text (r"\cite{key1}" )
118+ result = runner .invoke (cli , ["filter-bib" , str (tex_file ), "nonexistent.bib" ])
119+ assert result .exit_code != 0
0 commit comments