1- # stdlib
2- import sys
3-
41# 3rd party
52import pytest
6- import tox # type: ignore
7- import tox .reporter # type: ignore
83from coincidence .selectors import not_pypy
9- from domdf_python_tools .paths import in_directory
10-
4+ from testing_tox import run_tox
115
12- def test_rmdir_docs (tmp_pathplus , capsys ):
13- tox .reporter ._INSTANCE .tw ._file = sys .stdout
146
7+ @pytest .fixture ()
8+ def basic_docs_testenv (tmp_pathplus ):
159 build_dir = tmp_pathplus / "doc-source" / "build"
16-
1710 build_dir .mkdir (parents = True )
1811
1912 (tmp_pathplus / "tox.ini" ).write_lines ([
2013 "[testenv:docs]" ,
2114 "deps = sphinx" ,
2215 "skip_install = True" ,
2316 "commands = sphinx-build --version" ,
24- 'recreate_hook = builtin.rmdir(r"{toxinidir}/doc-source/build")' ,
2517 ])
2618
27- try :
28- with pytest .raises (SystemExit ), in_directory (tmp_pathplus ):
29- tox .cmdline (["-e" , "docs" , "-r" ])
19+ return build_dir
3020
21+
22+ def test_rmdir_docs (basic_docs_testenv , tmp_pathplus , capsys ):
23+ with (tmp_pathplus / "tox.ini" ).open ('a' ) as fp :
24+ fp .write ('recreate_hook = builtin.rmdir(r"{toxinidir}/doc-source/build")\n ' )
25+
26+ try :
27+ run_tox (["-e" , "docs" , "-r" ], tmp_pathplus )
3128 finally :
3229 stdout = capsys .readouterr ().out
3330 print (stdout )
3431
3532 assert (tmp_pathplus / "doc-source" ).is_dir ()
36- assert not build_dir .is_dir ()
33+ assert not basic_docs_testenv .is_dir ()
3734
38- assert f"docs recreate hook: removing { build_dir } " in stdout
35+ assert f"docs recreate hook: removing { basic_docs_testenv } " in stdout
3936
4037
4138@not_pypy ("mypy does noy support PyPy" )
4239def test_rmdir_mypy (tmp_pathplus , capsys ):
43- tox .reporter ._INSTANCE .tw ._file = sys .stdout
4440
4541 cache_dir = tmp_pathplus / ".mypy_cache"
46-
4742 cache_dir .mkdir ()
4843
4944 (tmp_pathplus / "tox.ini" ).write_lines ([
@@ -55,127 +50,70 @@ def test_rmdir_mypy(tmp_pathplus, capsys):
5550 ])
5651
5752 try :
58- with pytest .raises (SystemExit ), in_directory (tmp_pathplus ):
59- tox .cmdline (["-e" , "mypy" , "-r" ])
60-
53+ run_tox (["-e" , "mypy" , "-r" ], tmp_pathplus )
6154 finally :
6255 stdout = capsys .readouterr ().out
6356 print (stdout )
6457
65- stdout = capsys .readouterr ().out
66- print (stdout )
67-
6858 assert not cache_dir .is_dir ()
6959
7060 assert f"mypy recreate hook: removing { cache_dir } " in stdout
7161
7262
73- def test_simple_custom_hook (tmp_pathplus , capsys ):
74- tox .reporter ._INSTANCE .tw ._file = sys .stdout
75-
76- (tmp_pathplus / "tox.ini" ).write_lines ([
77- "[testenv:docs]" ,
78- "deps = sphinx" ,
79- "skip_install = True" ,
80- "commands = sphinx-build --version" ,
81- 'recreate_hook = "hello world"' ,
82- ])
63+ def test_simple_custom_hook (basic_docs_testenv , tmp_pathplus , capsys ):
64+ with (tmp_pathplus / "tox.ini" ).open ('a' ) as fp :
65+ fp .write ('recreate_hook = "hello world"' )
8366
8467 try :
85- with pytest .raises (SystemExit ), in_directory (tmp_pathplus ):
86- tox .cmdline (["-e" , "docs" , "-r" ])
87-
68+ run_tox (["-e" , "docs" , "-r" ], tmp_pathplus )
8869 finally :
8970 stdout = capsys .readouterr ().out
9071 print (stdout )
9172
92- stdout = capsys .readouterr ().out
93- print (stdout )
94-
9573 assert f"docs recreate hook: hello world" in stdout
9674
9775
98- def test_custom_hook (tmp_pathplus , capsys ):
99- tox .reporter ._INSTANCE .tw ._file = sys .stdout
100-
101- (tmp_pathplus / "tox.ini" ).write_lines ([
102- "[testenv:docs]" ,
103- "deps = sphinx" ,
104- "skip_install = True" ,
105- "commands = sphinx-build --version" ,
106- "recreate_hook = custom_hook.custom_hook()" ,
107- ])
76+ def test_custom_hook (basic_docs_testenv , tmp_pathplus , capsys ):
77+ with (tmp_pathplus / "tox.ini" ).open ('a' ) as fp :
78+ fp .write ('recreate_hook = custom_hook.custom_hook()\n ' )
10879
10980 (tmp_pathplus / "custom_hook.py" ).write_lines ([
11081 "def custom_hook() -> str:" ,
11182 '\t return "this is a custom hook"' ,
11283 ])
11384
11485 try :
115- with pytest .raises (SystemExit ), in_directory (tmp_pathplus ):
116- tox .cmdline (["-e" , "docs" , "-r" ])
117-
86+ run_tox (["-e" , "docs" , "-r" ], tmp_pathplus )
11887 finally :
11988 stdout = capsys .readouterr ().out
12089 print (stdout )
12190
122- stdout = capsys .readouterr ().out
123- print (stdout )
124-
12591 assert f"docs recreate hook: this is a custom hook" in stdout
12692
12793
128- def test_no_hook (tmp_pathplus , capsys ):
129- tox .reporter ._INSTANCE .tw ._file = sys .stdout
130-
131- (tmp_pathplus / "tox.ini" ).write_lines ([
132- "[testenv:docs]" ,
133- "deps = sphinx" ,
134- "skip_install = True" ,
135- "commands = sphinx-build --version" ,
136- ])
94+ def test_no_hook (basic_docs_testenv , tmp_pathplus , capsys ):
13795
13896 try :
139- with pytest .raises (SystemExit ), in_directory (tmp_pathplus ):
140- tox .cmdline (["-e" , "docs" , "-r" ])
97+ run_tox (["-e" , "docs" , "-r" ], tmp_pathplus )
14198
14299 finally :
143100 stdout = capsys .readouterr ().out
144101 print (stdout )
145102
146- stdout = capsys .readouterr ().out
147- print (stdout )
148-
149103 assert "docs recreate hook: " not in stdout
150104
151105
152- def test_not_recreate (tmp_pathplus , capsys ):
153- tox .reporter ._INSTANCE .tw ._file = sys .stdout
154-
155- build_dir = tmp_pathplus / "doc-source" / "build"
156-
157- build_dir .mkdir (parents = True )
158-
159- (tmp_pathplus / "tox.ini" ).write_lines ([
160- "[testenv:docs]" ,
161- "deps = sphinx" ,
162- "skip_install = True" ,
163- "commands = sphinx-build --version" ,
164- 'recreate_hook = builtin.rmdir(r"{toxinidir}/doc-source/build")' ,
165- ])
106+ def test_not_recreate (basic_docs_testenv , tmp_pathplus , capsys ):
107+ with (tmp_pathplus / "tox.ini" ).open ('a' ) as fp :
108+ fp .write ('recreate_hook = builtin.rmdir(r"{toxinidir}/doc-source/build")\n ' )
166109
167110 try :
168- with pytest .raises (SystemExit ), in_directory (tmp_pathplus ):
169- tox .cmdline (["-e" , "docs" ])
170-
111+ run_tox (["-e" , "docs" ], tmp_pathplus )
171112 finally :
172113 stdout = capsys .readouterr ().out
173114 print (stdout )
174115
175- stdout = capsys .readouterr ().out
176- print (stdout )
177-
178116 assert (tmp_pathplus / "doc-source" ).is_dir ()
179- assert build_dir .is_dir ()
117+ assert basic_docs_testenv .is_dir ()
180118
181119 assert "docs recreate hook: " not in stdout
0 commit comments