Skip to content

Commit ca23618

Browse files
authored
Use new license format in pyproject.toml (#534)
* Use new license format in pyproject.toml * Ah! Also use new format for PPT project itself.
1 parent 9849b8f commit ca23618

File tree

4 files changed

+69
-73
lines changed

4 files changed

+69
-73
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[project]
22
name = "python-project-template"
3-
license = {file = "LICENSE"}
3+
license = "BSD-3-Clause"
4+
license-files = ["LICENSE", "CITATION.bib"]
45
readme = "README.md"
56
authors = [
67
{ name = "LINCC Frameworks", email = "[email protected]" }

python-project-template/.pre-commit-config.yaml.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ repos:
3434
args: ['--maxkb=500']
3535
# Verify that pyproject.toml is well formed
3636
- repo: https://github.com/abravalheri/validate-pyproject
37-
rev: v0.12.1
37+
rev: v0.24.1
3838
hooks:
3939
- id: validate-pyproject
4040
name: Validate pyproject.toml

python-project-template/pyproject.toml.jinja

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
{%- import 'python-versions.jinja' as py %}
22
[project]
33
name = "{{project_name}}"
4-
license = {file = "LICENSE"}
4+
{%- if project_license == 'BSD' %}
5+
license = "BSD-3-Clause"
6+
{%- elif project_license == 'MIT' %}
7+
license = "MIT"
8+
{%- elif project_license == 'GPL3' %}
9+
license = "GPL-3.0-only"
10+
{%- endif %}
11+
{%- if project_license != 'none' %}
12+
license_file = ["LICENSE"]
13+
{%- endif %}
514
readme = "README.md"
615
authors = [
716
{ name = "{{author_name}}"{% if author_email %}, email = "{{author_email}}"{% endif %} }
817
]
918
classifiers = [
1019
"Development Status :: 4 - Beta",
11-
{%- if project_license == 'MIT' %}
12-
"License :: OSI Approved :: MIT License",
13-
{%- elif project_license == 'BSD' %}
14-
"License :: OSI Approved :: BSD License",
15-
{%- endif %}
1620
"Intended Audience :: Developers",
1721
"Intended Audience :: Science/Research",
1822
"Operating System :: OS Independent",

tests/test_package_creation.py

Lines changed: 56 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,31 @@
55
import 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

4254
def 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-
8585
def 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-
124119
def 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

310302
def 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

Comments
 (0)