99from contextlib import contextmanager
1010import requests
1111import pretext
12+ from typing import cast , Generator
13+ from pytest_console_scripts import ScriptRunner
1214
1315EXAMPLES_DIR = Path (__file__ ).parent / "examples"
1416
15- PTX_CMD = shutil .which ("pretext" )
17+ PTX_CMD = cast (str , shutil .which ("pretext" ))
18+ assert PTX_CMD is not None
1619PY_CMD = sys .executable
1720
1821
1922@contextmanager
20- def pretext_view (* args ) :
23+ def pretext_view (* args : str ) -> Generator :
2124 process = subprocess .Popen (
2225 [PTX_CMD , "-v" , "debug" , "view" , "--no-launch" ] + list (args )
2326 )
@@ -29,8 +32,8 @@ def pretext_view(*args):
2932 process .wait ()
3033
3134
32- def test_entry_points (script_runner ) :
33- ret = script_runner .run (PTX_CMD , "-v" , "debug" , "-h" )
35+ def test_entry_points (script_runner : ScriptRunner ) -> None :
36+ ret = script_runner .run ([ PTX_CMD , "-v" , "debug" , "-h" ] )
3437 assert ret .success
3538 assert (
3639 subprocess .run (
@@ -40,33 +43,33 @@ def test_entry_points(script_runner):
4043 )
4144
4245
43- def test_version (script_runner ) :
44- ret = script_runner .run (PTX_CMD , "-v" , "debug" , "--version" )
46+ def test_version (script_runner : ScriptRunner ) -> None :
47+ ret = script_runner .run ([ PTX_CMD , "-v" , "debug" , "--version" ] )
4548 assert ret .stdout .strip () == pretext .VERSION
4649
4750
48- def test_new (tmp_path : Path , script_runner ) :
49- assert script_runner .run (PTX_CMD , "-v" , "debug" , "new" , cwd = tmp_path ).success
51+ def test_new (tmp_path : Path , script_runner : ScriptRunner ) -> None :
52+ assert script_runner .run ([ PTX_CMD , "-v" , "debug" , "new" ] , cwd = tmp_path ).success
5053 assert (tmp_path / "new-pretext-project" / "project.ptx" ).exists ()
5154
5255
53- def test_devscript (script_runner ) :
56+ def test_devscript (script_runner : ScriptRunner ) -> None :
5457 """
5558 Test that `pretext devscript -h` aliases `python /path/to/.ptx/pretext/pretext -h`.
5659 """
57- result = script_runner .run (PTX_CMD , "devscript" , "-h" )
60+ result = script_runner .run ([ PTX_CMD , "devscript" , "-h" ] )
5861 assert result .success
5962 assert "PreTeXt utility script" in result .stdout
6063
6164
62- def test_build (tmp_path : Path , script_runner ) :
65+ def test_build (tmp_path : Path , script_runner : ScriptRunner ) -> None :
6366 path_with_spaces = "test path with spaces"
6467 project_path = tmp_path / path_with_spaces
6568 assert script_runner .run (
66- PTX_CMD , "-v" , "debug" , "new" , "demo" , "-d" , path_with_spaces , cwd = tmp_path
69+ [ PTX_CMD , "-v" , "debug" , "new" , "demo" , "-d" , path_with_spaces ] , cwd = tmp_path
6770 ).success
6871 assert script_runner .run (
69- PTX_CMD , "-v" , "debug" , "build" , "web" , cwd = project_path
72+ [ PTX_CMD , "-v" , "debug" , "build" , "web" ] , cwd = project_path
7073 ).success
7174 web_path = project_path / "output" / "web"
7275 assert web_path .exists ()
@@ -92,74 +95,72 @@ def test_build(tmp_path: Path, script_runner):
9295 f"{ source_prefix } backmatter.ptx" : ["backmatter" ],
9396 }
9497 assert script_runner .run (
95- PTX_CMD ,
96- "-v" ,
97- "debug" ,
98- "build" ,
99- "subset" ,
100- "-x" ,
101- "ch-first-without-spaces" ,
98+ [PTX_CMD , "-v" , "debug" , "build" , "subset" , "-x" , "ch-first-without-spaces" ],
10299 cwd = project_path ,
103100 ).success
104101 assert (project_path / "output" / "subset" ).exists ()
105102 assert not (project_path / "output" / "subset" / "ch-empty.html" ).exists ()
106103 assert (
107104 project_path / "output" / "subset" / "ch-first-without-spaces.html"
108105 ).exists ()
109- assert script_runner .run (PTX_CMD , "build" , "print-latex" , cwd = project_path ).success
106+ assert script_runner .run (
107+ [PTX_CMD , "build" , "print-latex" ], cwd = project_path
108+ ).success
110109 assert (project_path / "output" / "print-latex" ).exists ()
111110 assert script_runner .run (
112- PTX_CMD , "-v" , "debug" , "build" , "-g" , cwd = project_path
111+ [ PTX_CMD , "-v" , "debug" , "build" , "-g" ] , cwd = project_path
113112 ).success
114113 assert (project_path / "generated-assets" ).exists ()
115114 shutil .rmtree (project_path / "generated-assets" )
116115 assert script_runner .run (
117- PTX_CMD , "-v" , "debug" , "build" , "-g" , "webwork" , cwd = project_path
116+ [ PTX_CMD , "-v" , "debug" , "build" , "-g" , "webwork" ] , cwd = project_path
118117 ).success
119118 assert (project_path / "generated-assets" ).exists ()
120119
121120
122- def test_init (tmp_path : Path , script_runner ) :
123- assert script_runner .run (PTX_CMD , "-v" , "debug" , "init" , cwd = tmp_path ).success
121+ def test_init (tmp_path : Path , script_runner : ScriptRunner ) -> None :
122+ assert script_runner .run ([ PTX_CMD , "-v" , "debug" , "init" ] , cwd = tmp_path ).success
124123 assert (tmp_path / "project.ptx" ).exists ()
125124 assert (tmp_path / "requirements.txt" ).exists ()
126125 assert (tmp_path / ".gitignore" ).exists ()
127126 assert (tmp_path / "publication" / "publication.ptx" ).exists ()
128127 assert len ([* tmp_path .glob ("project-*.ptx" )]) == 0 # need to refresh
129- assert script_runner .run (PTX_CMD , "-v" , "debug" , "init" , "-r" , cwd = tmp_path ).success
128+ assert script_runner .run (
129+ [PTX_CMD , "-v" , "debug" , "init" , "-r" ], cwd = tmp_path
130+ ).success
130131 assert len ([* tmp_path .glob ("project-*.ptx" )]) > 0
131132 assert len ([* tmp_path .glob ("requirements-*.txt" )]) > 0
132133 assert len ([* tmp_path .glob (".gitignore-*" )]) > 0
133134 assert len ([* tmp_path .glob ("publication/publication-*.ptx" )]) > 0
134135
135136
136- def test_generate_asymptote (tmp_path : Path , script_runner ) :
137- assert script_runner .run (PTX_CMD , "-v" , "debug" , "init" , cwd = tmp_path ).success
137+ def test_generate_asymptote (tmp_path : Path , script_runner : ScriptRunner ) -> None :
138+ assert script_runner .run ([ PTX_CMD , "-v" , "debug" , "init" ] , cwd = tmp_path ).success
138139 (tmp_path / "source" ).mkdir ()
139140 shutil .copyfile (EXAMPLES_DIR / "asymptote.ptx" , tmp_path / "source" / "main.ptx" )
140141 assert script_runner .run (
141- PTX_CMD , "-v" , "debug" , "generate" , "asymptote" , cwd = tmp_path
142+ [ PTX_CMD , "-v" , "debug" , "generate" , "asymptote" ] , cwd = tmp_path
142143 ).success
143144 assert (tmp_path / "generated-assets" / "asymptote" / "test.html" ).exists ()
144145 os .remove (tmp_path / "generated-assets" / "asymptote" / "test.html" )
145146 assert script_runner .run (
146- PTX_CMD , "-v" , "debug" , "generate" , "-x" , "test" , cwd = tmp_path
147+ [ PTX_CMD , "-v" , "debug" , "generate" , "-x" , "test" ] , cwd = tmp_path
147148 ).success
148149 assert (tmp_path / "generated-assets" / "asymptote" / "test.html" ).exists ()
149150 os .remove (tmp_path / "generated-assets" / "asymptote" / "test.html" )
150151 assert script_runner .run (
151- PTX_CMD , "-v" , "debug" , "generate" , "asymptote" , "-t" , "web" , cwd = tmp_path
152+ [ PTX_CMD , "-v" , "debug" , "generate" , "asymptote" , "-t" , "web" ] , cwd = tmp_path
152153 ).success
153154 os .remove (tmp_path / "generated-assets" / "asymptote" / "test.html" )
154155
155156
156157# @pytest.mark.skip(
157158# reason="Waiting on upstream changes to interactive preview generation"
158159# )
159- def test_generate_interactive (tmp_path : Path , script_runner ) :
160+ def test_generate_interactive (tmp_path : Path , script_runner : ScriptRunner ) -> None :
160161 int_path = tmp_path / "interactive"
161162 shutil .copytree (EXAMPLES_DIR / "projects" / "interactive" , int_path )
162- assert script_runner .run (PTX_CMD , "-v" , "debug" , "generate" , cwd = int_path ).success
163+ assert script_runner .run ([ PTX_CMD , "-v" , "debug" , "generate" ] , cwd = int_path ).success
163164 preview_file = (
164165 int_path / "generated-assets" / "preview" / "interactive-infinity-preview.png"
165166 )
@@ -168,49 +169,49 @@ def test_generate_interactive(tmp_path: Path, script_runner):
168169 assert qrcode_file .exists ()
169170
170171
171- def test_view (tmp_path : Path , script_runner ) :
172+ def test_view (tmp_path : Path , script_runner : ScriptRunner ) -> None :
172173 os .chdir (tmp_path )
173174 port = random .randint (10_000 , 65_536 )
174175 with pretext_view ("-d" , "." , "-p" , f"{ port } " ):
175176 assert requests .get (f"http://localhost:{ port } /" ).status_code == 200
176- assert script_runner .run (PTX_CMD , "-v" , "debug" , "new" , "-d" , "1" ).success
177+ assert script_runner .run ([ PTX_CMD , "-v" , "debug" , "new" , "-d" , "1" ] ).success
177178 os .chdir (Path ("1" ))
178- assert script_runner .run (PTX_CMD , "-v" , "debug" , "build" ).success
179+ assert script_runner .run ([ PTX_CMD , "-v" , "debug" , "build" ] ).success
179180 port = random .randint (10_000 , 65_536 )
180181 with pretext_view ("-p" , f"{ port } " ):
181182 assert requests .get (f"http://localhost:{ port } /" ).status_code == 200
182183 os .chdir (tmp_path )
183- assert script_runner .run (PTX_CMD , "-v" , "debug" , "new" , "-d" , "2" ).success
184+ assert script_runner .run ([ PTX_CMD , "-v" , "debug" , "new" , "-d" , "2" ] ).success
184185 os .chdir (Path ("2" ))
185186 port = random .randint (10_000 , 65_536 )
186187 with pretext_view ("-p" , f"{ port } " , "-b" , "-g" ):
187188 assert requests .get (f"http://localhost:{ port } /" ).status_code == 200
188189
189190
190- def test_custom_xsl (tmp_path : Path , script_runner ) :
191+ def test_custom_xsl (tmp_path : Path , script_runner : ScriptRunner ) -> None :
191192 custom_path = tmp_path / "custom"
192193 shutil .copytree (EXAMPLES_DIR / "projects" / "custom-xsl" , custom_path )
193- assert script_runner .run (PTX_CMD , "-v" , "debug" , "build" , cwd = custom_path ).success
194+ assert script_runner .run ([ PTX_CMD , "-v" , "debug" , "build" ] , cwd = custom_path ).success
194195 assert (custom_path / "output" / "test" ).exists ()
195196
196197
197- def test_custom_webwork_server (tmp_path : Path , script_runner ) :
198+ def test_custom_webwork_server (tmp_path : Path , script_runner : ScriptRunner ) -> None :
198199 custom_path = tmp_path / "custom"
199200 shutil .copytree (EXAMPLES_DIR / "projects" / "custom-wwserver" , custom_path )
200201 result = script_runner .run (
201- PTX_CMD , "-v" , "debug" , "generate" , "webwork" , cwd = custom_path
202+ [ PTX_CMD , "-v" , "debug" , "generate" , "webwork" ] , cwd = custom_path
202203 )
203204 assert result .success
204205 assert "webwork-dev" in result .stderr
205- result = script_runner .run (PTX_CMD , "-v" , "debug" , "build" , cwd = custom_path )
206+ result = script_runner .run ([ PTX_CMD , "-v" , "debug" , "build" ] , cwd = custom_path )
206207 assert result .success
207208
208209
209- def test_slideshow (tmp_path : Path , script_runner ) :
210+ def test_slideshow (tmp_path : Path , script_runner : ScriptRunner ) -> None :
210211 assert script_runner .run (
211- PTX_CMD , "-v" , "debug" , "new" , "slideshow" , "-d" , "." , cwd = tmp_path
212+ [ PTX_CMD , "-v" , "debug" , "new" , "slideshow" , "-d" , "." ] , cwd = tmp_path
212213 ).success
213214 assert script_runner .run (
214- PTX_CMD , "-v" , "debug" , "build" , "web" , cwd = tmp_path
215+ [ PTX_CMD , "-v" , "debug" , "build" , "web" ] , cwd = tmp_path
215216 ).success
216217 assert (tmp_path / "output" / "web" / "slides.html" ).exists ()
0 commit comments