55import pytest
66
77
8- def successfully_created_project (result ):
9- """Basic assertions that indicate the copier was able to create a project"""
10- return result .exit_code == 0 and result .exception is None and result .project_dir .is_dir ()
8+ def create_project_with_basic_checks (copie , extra_answers , package_name = "example_package" ):
9+ """Create the project using copier. Perform a handful of basic checks on the created directory."""
10+ # run copier to hydrate a temporary project
11+ result = copie .copy (extra_answers = extra_answers )
12+
13+ ## Initializes local git repository (required to run pre-commit)
14+ subprocess .call (["git" , "init" , "." ], cwd = result .project_dir )
15+
16+ # successfully_created_project
17+ assert (
18+ result .exit_code == 0 and result .exception is None and result .project_dir .is_dir ()
19+ ), "Did not successfully create project"
20+
21+ # pyproject_toml_is_valid
22+ precommit_results = subprocess .run (
23+ ["pre-commit" , "run" , "validate-pyproject" ], cwd = result .project_dir , check = False
24+ )
25+ assert precommit_results .returncode == 0
1126
27+ # directory_structure_is_correct
28+ assert (result .project_dir / f"src/{ package_name } " ).is_dir () and (
29+ result .project_dir / f"tests/{ package_name } "
30+ ).is_dir (), "Directory structure is incorrect"
1231
13- def contains_required_files (result ):
14- """Utility method to confirm that the required project files exist in the
15- copier result."""
32+ # contains_required_files
1633 required_files = [
1734 ".copier-answers.yml" ,
1835 ".git_archival.txt" ,
@@ -29,14 +46,9 @@ def contains_required_files(result):
2946 if not (result .project_dir / file ).is_file ():
3047 all_found = False
3148 print ("Required file not generated:" , file )
32- return all_found
49+ assert all_found
3350
34-
35- def directory_structure_is_correct (result , package_name = "example_package" ):
36- """Test to ensure that the default directory structure ws created correctly"""
37- return (result .project_dir / f"src/{ package_name } " ).is_dir () and (
38- result .project_dir / f"tests/{ package_name } "
39- ).is_dir ()
51+ return result
4052
4153
4254def black_runs_successfully (result ):
@@ -70,18 +82,6 @@ def pylint_runs_successfully(result):
7082 return pylint_results .returncode == 0
7183
7284
73- def unit_tests_in_project_run_successfully (result ):
74- """Test to ensure that the unit tests run successfully on the project
75-
76- !!! NOTE - This doesn't currently work because we need to `pip install` the hydrated
77- project before running the tests. And we don't have a way to create a temporary
78- virtual environment for the project.
79- """
80- pytest_results = subprocess .run (["python" , "-m" , "pytest" ], cwd = result .project_dir , check = False )
81-
82- return pytest_results .returncode == 0
83-
84-
8585def docs_build_successfully (result ):
8686 """Test that we can build the doc tree.
8787
@@ -116,23 +116,14 @@ def github_workflows_are_valid(result):
116116 return workflows_results .returncode == 0
117117
118118
119- def initialize_git_project (result ):
120- """Initializes local git repository (required to run pre-commit)"""
121- subprocess .call (["git" , "init" , "." ], cwd = result .project_dir )
122-
123-
124119def test_all_defaults (copie ):
125120 """Test that the default values are used when no arguments are given.
126121 Ensure that the project is created and that the basic files exist.
127122 """
128-
129123 # run copier to hydrate a temporary project
130- result = copie . copy ( )
124+ result = create_project_with_basic_checks ( copie , {} )
131125
132- assert successfully_created_project (result )
133- assert directory_structure_is_correct (result )
134126 assert not pylint_runs_successfully (result )
135- assert contains_required_files (result )
136127
137128 # check to see if the README file was hydrated with copier answers.
138129 found_line = False
@@ -155,14 +146,8 @@ def test_use_black_and_no_example_modules(copie):
155146 "enforce_style" : ["black" , "pylint" , "isort" ],
156147 "create_example_module" : False ,
157148 }
158-
159- # run copier to hydrate a temporary project
160- result = copie .copy (extra_answers = extra_answers )
161-
162- assert successfully_created_project (result )
163- assert directory_structure_is_correct (result )
149+ result = create_project_with_basic_checks (copie , extra_answers )
164150 assert pylint_runs_successfully (result )
165- assert contains_required_files (result )
166151
167152 # make sure that the files that were not requested were not created
168153 assert not (result .project_dir / "src/example_package/example_module.py" ).is_file ()
@@ -200,10 +185,8 @@ def test_code_style_combinations(copie, enforce_style):
200185 extra_answers = {
201186 "enforce_style" : enforce_style ,
202187 }
203- result = copie .copy (extra_answers = extra_answers )
204- assert successfully_created_project (result )
205- assert directory_structure_is_correct (result )
206- assert contains_required_files (result )
188+ result = create_project_with_basic_checks (copie , extra_answers )
189+
207190 # black would still run successfully.
208191 assert black_runs_successfully (result )
209192
@@ -227,12 +210,30 @@ def test_smoke_test_notification(copie, notification):
227210 }
228211
229212 # run copier to hydrate a temporary project
230- result = copie .copy (extra_answers = extra_answers )
213+ result = create_project_with_basic_checks (copie , extra_answers )
214+ assert black_runs_successfully (result )
215+
216+
217+ @pytest .mark .parametrize (
218+ "license" ,
219+ [
220+ [],
221+ ["MIT" ],
222+ ["BSD" ],
223+ ["GPL3" ],
224+ ["none" ],
225+ ],
226+ )
227+ def test_license (copie , license ):
228+ """Confirm we get a valid project for different license options."""
229+
230+ # provide a dictionary of the non-default answers to use
231+ extra_answers = {"license" : license }
232+
233+ # run copier to hydrate a temporary project
234+ result = create_project_with_basic_checks (copie , extra_answers )
231235
232- assert successfully_created_project (result )
233- assert directory_structure_is_correct (result )
234236 assert black_runs_successfully (result )
235- assert contains_required_files (result )
236237
237238
238239@pytest .mark .parametrize (
@@ -252,12 +253,9 @@ def test_doc_combinations(copie, doc_answers):
252253 """Confirm the docs directory is well-formed, when including docs."""
253254
254255 # run copier to hydrate a temporary project
255- result = copie . copy ( extra_answers = doc_answers )
256+ result = create_project_with_basic_checks ( copie , doc_answers )
256257
257- assert successfully_created_project (result )
258- assert directory_structure_is_correct (result )
259258 assert black_runs_successfully (result )
260- assert contains_required_files (result )
261259 assert docs_build_successfully (result )
262260 assert (result .project_dir / "docs" ).is_dir ()
263261
@@ -279,12 +277,9 @@ def test_doc_combinations_no_docs(copie, doc_answers):
279277 """Confirm there is no 'docs' directory, if not including docs."""
280278
281279 # run copier to hydrate a temporary project
282- result = copie . copy ( extra_answers = doc_answers )
280+ result = create_project_with_basic_checks ( copie , doc_answers )
283281
284- assert successfully_created_project (result )
285- assert directory_structure_is_correct (result )
286282 assert black_runs_successfully (result )
287- assert contains_required_files (result )
288283 assert not (result .project_dir / "docs" ).is_dir ()
289284
290285
@@ -299,12 +294,9 @@ def test_test_lowest_version(copie, test_lowest_version):
299294 }
300295
301296 # run copier to hydrate a temporary project
302- result = copie . copy ( extra_answers = extra_answers )
297+ result = create_project_with_basic_checks ( copie , extra_answers )
303298
304- assert successfully_created_project (result )
305- assert directory_structure_is_correct (result )
306299 assert black_runs_successfully (result )
307- assert contains_required_files (result )
308300
309301
310302def test_github_workflows_schema (copie ):
@@ -313,7 +305,6 @@ def test_github_workflows_schema(copie):
313305 "include_benchmarks" : True ,
314306 "include_docs" : True ,
315307 }
316- result = copie . copy ( extra_answers = extra_answers )
317- initialize_git_project ( result )
308+ result = create_project_with_basic_checks ( copie , extra_answers )
309+
318310 assert github_workflows_are_valid (result )
319- assert contains_required_files (result )
0 commit comments