Skip to content

Commit cfc954c

Browse files
committed
refactor test_odfdo_show.py
1 parent d2a401c commit cfc954c

File tree

1 file changed

+148
-79
lines changed

1 file changed

+148
-79
lines changed

tests/scripts/test_odfdo_show.py

Lines changed: 148 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# Copyright 2018-2025 Jérôme Dumonteil
22
# Authors (odfdo project): [email protected]
3+
from __future__ import annotations
34

45
import subprocess
56
import sys
67
from pathlib import Path
78

9+
import pytest
10+
811
from odfdo.scripts import show
12+
from odfdo.scripts.show import main as main_script
13+
from odfdo.scripts.show import main_show, parse_cli_args
914

1015
SCRIPT = Path(show.__file__)
1116

@@ -22,134 +27,197 @@ def run_params(params: list):
2227
return out, err, proc.returncode
2328

2429

25-
def test_no_param():
30+
def test_show_no_param():
2631
params = []
2732
_out, err, exitcode = run_params(params)
2833
assert exitcode == 2
2934
assert "odfdo-show: error: the following arguments" in err
3035
assert "usage" in err
3136

3237

33-
def test_help():
34-
params = ["--help"]
35-
out, _err, exitcode = run_params(params)
36-
assert exitcode == 0
37-
assert "Dump text from an OpenDocument" in out
38+
# direct access to internal function
39+
40+
41+
def test_show_2_no_param_on_main_function(monkeypatch):
42+
with pytest.raises(SystemExit) as result:
43+
monkeypatch.setattr(sys, "argv", [])
44+
main_script()
45+
assert result.value.code >= 1
46+
47+
48+
def test_show_2_no_param():
49+
with pytest.raises(SystemExit) as result:
50+
params = parse_cli_args([])
51+
main_show(params)
52+
assert result.value.code >= 1
53+
54+
55+
def test_show_2_version(capsys):
56+
with pytest.raises(SystemExit) as result:
57+
parse_cli_args(["--version"])
58+
assert result.value.code == 0
59+
captured = capsys.readouterr()
60+
61+
assert "odfdo-show v3" in captured.out
62+
63+
64+
def test_show_2_help(capsys):
65+
with pytest.raises(SystemExit) as result:
66+
parse_cli_args(["--help"])
67+
assert result.value.code == 0
68+
captured = capsys.readouterr()
69+
70+
assert "Dump text from an OpenDocument" in captured.out
3871

3972

40-
def test_no_file():
41-
params = ["none_file"]
42-
out, _err, exitcode = run_params(params)
43-
assert exitcode == 1
44-
assert "FileNotFoundError" in out
73+
def test_show_2_no_file():
74+
params = parse_cli_args(["none_file"])
4575

76+
with pytest.raises(SystemExit) as result:
77+
main_show(params)
78+
assert result.value.code >= 1
4679

47-
def test_base(samples):
80+
81+
def test_show_2_base_on_main_function(capsys, monkeypatch, samples):
4882
source = str(samples("base_text.odt"))
49-
params = [source]
50-
out, _err, exitcode = run_params(params)
51-
assert exitcode == 0
52-
assert "This is the second paragraph." in out
83+
monkeypatch.setattr(sys, "argv", ["odfdo-replace", source])
84+
85+
main_script()
86+
captured = capsys.readouterr()
87+
88+
assert "This is the second paragraph." in captured.out
5389

5490

55-
def test_no_text(samples):
91+
def test_show_2_base(capsys, samples):
5692
source = str(samples("base_text.odt"))
57-
params = ["-n", source]
58-
out, _err, exitcode = run_params(params)
59-
assert exitcode == 0
60-
assert not out.strip()
93+
params = parse_cli_args([source])
6194

95+
main_show(params)
96+
captured = capsys.readouterr()
6297

63-
def test_meta(samples):
98+
assert "This is the second paragraph." in captured.out
99+
100+
101+
def test_show_2_no_text(capsys, samples):
64102
source = str(samples("base_text.odt"))
65-
params = ["-nm", source]
66-
out, _err, exitcode = run_params(params)
67-
assert exitcode == 0
68-
assert "This is the second paragraph." not in out
69-
assert "Keyword: These are the keywords" in out
70-
assert "Generator: LibreOffice" in out
103+
params = parse_cli_args(["-n", source])
104+
105+
main_show(params)
106+
captured = capsys.readouterr()
107+
108+
assert not captured.out.strip()
109+
110+
111+
def test_show_2_meta(capsys, samples):
112+
source = str(samples("base_text.odt"))
113+
params = parse_cli_args(["-nm", source])
114+
115+
main_show(params)
116+
captured = capsys.readouterr()
71117

118+
assert "This is the second paragraph." not in captured.out
119+
assert "Keyword: These are the keywords" in captured.out
120+
assert "Generator: LibreOffice" in captured.out
72121

73-
def test_style(samples):
122+
123+
def test_show_2_style(capsys, samples):
74124
source = str(samples("base_text.odt"))
75-
params = ["-s", source]
76-
out, _err, exitcode = run_params(params)
77-
assert exitcode == 0
78-
assert "This is the second paragraph." in out
79-
assert "used:y family:paragraph" in out
125+
params = parse_cli_args(["-s", source])
126+
127+
main_show(params)
128+
captured = capsys.readouterr()
129+
130+
assert "This is the second paragraph." in captured.out
131+
assert "used:y family:paragraph" in captured.out
80132

81133

82-
def test_style_no_text(samples):
134+
def test_show_2_style_no_text(capsys, samples):
83135
source = str(samples("base_text.odt"))
84-
params = ["-ns", source]
85-
out, _err, exitcode = run_params(params)
86-
assert exitcode == 0
87-
assert "This is the second paragraph." not in out
88-
assert "used:y family:paragraph" in out
136+
params = parse_cli_args(["-ns", source])
89137

138+
main_show(params)
139+
captured = capsys.readouterr()
90140

91-
def test_style_rst(samples):
141+
assert "This is the second paragraph." not in captured.out
142+
assert "used:y family:paragraph" in captured.out
143+
144+
145+
def test_show_2_style_rst(capsys, samples):
92146
source = str(samples("base_text.odt"))
93-
params = ["-r", source]
94-
out, _err, exitcode = run_params(params)
95-
assert exitcode == 0
96-
assert "Level 2 Title\n=============" in out
147+
params = parse_cli_args(["-r", source])
148+
149+
main_show(params)
150+
captured = capsys.readouterr()
97151

152+
assert "Level 2 Title\n=============" in captured.out
98153

99-
def test_style_ods(samples):
154+
155+
def test_show_2_style_ods(capsys, samples):
100156
source = str(samples("styled_table.ods"))
101-
params = [source]
102-
out, _err, exitcode = run_params(params)
103-
print(out)
104-
assert exitcode == 0
105-
assert "1,2,3,4" in out
157+
params = parse_cli_args([source])
158+
159+
main_show(params)
160+
captured = capsys.readouterr()
161+
162+
assert "1,2,3,4" in captured.out
106163

107164

108-
def test_not_style_ods(samples):
165+
def test_show_2_not_style_ods(capsys, samples):
109166
source = str(samples("styled_table.ods"))
110-
params = ["-n", source]
111-
out, _err, exitcode = run_params(params)
112-
assert exitcode == 0
113-
assert "1,2,3,4" not in out
167+
params = parse_cli_args(["-n", source])
168+
169+
main_show(params)
170+
captured = capsys.readouterr()
171+
172+
assert "1,2,3,4" not in captured.out
114173

115174

116-
def test_unsupported(samples):
175+
def test_show_2_unsupported(capsys, samples):
117176
source = str(samples("base_shapes.odg"))
118-
params = [source]
119-
out, err, exitcode = run_params(params)
120-
assert exitcode == 1
121-
print(out)
122-
assert "format 'graphics' is not supported" in err
177+
params = parse_cli_args([source])
123178

179+
with pytest.raises(SystemExit) as result:
180+
main_show(params)
181+
assert result.value.code >= 1
182+
captured = capsys.readouterr()
124183

125-
def test_folder_unsupported(tmp_path, samples):
184+
assert "format 'graphics' is not supported" in captured.err
185+
186+
187+
def test_show_2_folder_unsupported(tmp_path, capsys, samples):
126188
source = str(samples("base_shapes.odg"))
127189
dest = tmp_path / "test_show"
128-
params = ["-o", f"{dest}", source]
129-
_out, err, exitcode = run_params(params)
130-
assert exitcode == 1
131-
assert "format 'graphics' is not supported" in err
190+
params = parse_cli_args(["-o", str(dest), source])
132191

192+
with pytest.raises(SystemExit) as result:
193+
main_show(params)
194+
assert result.value.code >= 1
195+
captured = capsys.readouterr()
133196

134-
def test_folder_text(tmp_path, samples):
197+
assert "format 'graphics' is not supported" in captured.err
198+
199+
200+
def test_show_2_folder_text(tmp_path, samples):
135201
source = str(samples("base_text.odt"))
136202
dest = tmp_path / "test_show"
137-
params = ["-o", f"{dest}", source]
138-
_out, _err, exitcode = run_params(params)
139-
assert exitcode == 0
203+
params = parse_cli_args(["-o", str(dest), source])
204+
205+
main_show(params)
206+
140207
assert dest.is_dir()
141208
assert (dest / "meta.txt").is_file()
142209
assert (dest / "styles.txt").is_file()
143210
assert (dest / "content.rst").is_file()
144211
assert not (dest / "Pictures").exists()
145212

146213

147-
def test_folder_image(tmp_path, samples):
214+
def test_show_2_folder_image(tmp_path, samples):
148215
source = str(samples("background.odp"))
149216
dest = tmp_path / "test_show"
150-
params = ["-o", f"{dest}", source]
151-
_out, _err, exitcode = run_params(params)
152-
assert exitcode == 0
217+
params = parse_cli_args(["-o", str(dest), source])
218+
219+
main_show(params)
220+
153221
assert dest.is_dir()
154222
assert (dest / "meta.txt").is_file()
155223
assert (dest / "styles.txt").is_file()
@@ -160,12 +228,13 @@ def test_folder_image(tmp_path, samples):
160228
).is_file()
161229

162230

163-
def test_folder_ods(tmp_path, samples):
231+
def test_show_2_folder_ods(tmp_path, samples):
164232
source = str(samples("styled_table.ods"))
165233
dest = tmp_path / "test_show_ods"
166-
params = ["-o", f"{dest}", source]
167-
_out, _err, exitcode = run_params(params)
168-
assert exitcode == 0
234+
params = parse_cli_args(["-o", str(dest), source])
235+
236+
main_show(params)
237+
169238
assert dest.is_dir()
170239
assert (dest / "meta.txt").is_file()
171240
assert (dest / "styles.txt").is_file()

0 commit comments

Comments
 (0)