11"""Checks that the cookiecutter works."""
22
33import difflib
4- import os
54import pathlib
65import shutil
76import subprocess
109import pytest
1110import pytest_venv # type: ignore[import-not-found]
1211
12+ from .helpers import ( # type: ignore[import-not-found]
13+ DEFAULT_CONFIG ,
14+ get_all_files_folders ,
15+ )
1316
14- def get_all_files_folders (root_path : pathlib .Path ) -> set [pathlib .Path ]:
15- """
16- Get all files and folders under a directory.
1717
18- The paths are returned relative to the root path given.
19- __pycache__ directories and .DS_Store files are ignored.
20- """
21- file_set : set [pathlib .Path ] = set ()
22- for dirpath , _ , filenames in os .walk (root_path ):
23- dirpath_path = pathlib .Path (dirpath ).relative_to (root_path )
24- if dirpath_path .name in ["__pycache__" ]:
25- continue
26-
27- # Add this directory
28- file_set .update ((dirpath_path ,))
29- # Add any files in it
30- for filename in filenames :
31- if filename in [".DS_Store" ]:
32- continue
33- file_set .update ((dirpath_path / filename ,))
34-
35- return file_set
36-
37-
38- def test_package_generation (
39- tmp_path : pathlib .Path ,
40- generate_package : typing .Callable ,
41- ) -> None :
18+ def test_package_generation (generate_package : typing .Callable ) -> None :
4219 """Test package generation."""
43- test_config = {
44- "github_owner" : "test-user" ,
45- "project_short_description" : "description" ,
46- "project_name" : "Cookiecutter Test" ,
47- # Not having a git repo makes it easier to check in/out reference
48- # data files to the main python-tooling git repository
49- "initialise_git_repository" : False ,
50- }
51- generate_package (config = test_config , path = tmp_path )
20+ test_config = DEFAULT_CONFIG .copy ()
21+ # Not having a git repo makes it easier to check in/out reference
22+ # data files to the main python-tooling git repository
23+ test_config ["initialise_git_repository" ] = "False"
24+ _ , test_project_dir = generate_package (config = test_config )
5225
5326 expected_package_dir = (
5427 pathlib .Path (__file__ ).parent / "data" / "test_package_generation"
5528 )
56- # Check project directory exists
57- test_project_dir = tmp_path / "cookiecutter-test"
58- assert test_project_dir .exists ()
29+ assert test_project_dir .exists (), "Project directory does not exist."
5930
6031 actual_files = get_all_files_folders (test_project_dir )
6132 expected_files = get_all_files_folders (expected_package_dir )
@@ -94,18 +65,11 @@ def test_package_generation(
9465
9566
9667def test_pip_installable (
97- tmp_path : pathlib .Path ,
9868 venv : pytest_venv .VirtualEnvironment ,
9969 generate_package : typing .Callable ,
10070) -> None :
10171 """Test generated package is pip installable."""
102- test_config = {
103- "github_owner" : "test-user" ,
104- "project_short_description" : "description" ,
105- "project_name" : "Cookiecutter Test" ,
106- }
107- generate_package (config = test_config , path = tmp_path )
108- test_project_dir = tmp_path / "cookiecutter-test"
72+ _ , test_project_dir = generate_package ()
10973 # Try to install package in virtual environment with pip
11074 pipinstall = subprocess .run ( # noqa: S603
11175 [
@@ -124,21 +88,13 @@ def test_pip_installable(
12488 )
12589
12690
127- @pytest .mark .parametrize ("funder" , ["" , "STFC" ])
128- def test_optional_funder (
129- tmp_path : pathlib .Path , generate_package : typing .Callable , funder : str
130- ) -> None :
91+ @pytest .mark .parametrize ("funder" , ["" , "STFC" , "UKRI" , "Wellcome Trust" ])
92+ def test_optional_funder (generate_package : typing .Callable , funder : str ) -> None :
13193 """Test specifying funder or not in package generation."""
132- config = {
133- "github_owner" : "test-user" ,
134- "project_short_description" : "description" ,
135- "project_name" : "Cookiecutter Test" ,
136- "funder" : funder ,
137- }
138-
139- generate_package (config , tmp_path )
94+ config = DEFAULT_CONFIG .copy ()
95+ config ["funder" ] = funder
96+ _ , test_project_dir = generate_package (config )
14097
141- test_project_dir = tmp_path / "cookiecutter-test"
14298 with (test_project_dir / "README.md" ).open () as f :
14399 readme_text = "" .join (f .readlines ())
144100
@@ -152,18 +108,11 @@ def test_optional_funder(
152108
153109
154110def test_docs_build (
155- tmp_path : pathlib .Path ,
156111 venv : pytest_venv .VirtualEnvironment ,
157112 generate_package : typing .Callable ,
158113) -> None :
159114 """Test documentation build from package created from template."""
160- config = {
161- "github_owner" : "test-user" ,
162- "project_short_description" : "description" ,
163- "project_name" : "Cookiecutter Test" ,
164- }
165- generate_package (config , tmp_path )
166- test_project_dir = tmp_path / "cookiecutter-test"
115+ _ , test_project_dir = generate_package ()
167116 venv .install ("tox" )
168117 tox_docs_process = subprocess .run ( # noqa: S603
169118 [
0 commit comments