|
4 | 4 |
|
5 | 5 | import sys |
6 | 6 |
|
| 7 | +from copier import run_copy |
| 8 | + |
| 9 | +from tests.helpers import DEFAULT_DATA, TEMPLATE_ROOT |
| 10 | + |
7 | 11 | if sys.version_info >= (3, 11): |
8 | 12 | import tomllib |
9 | 13 | else: |
@@ -41,3 +45,71 @@ def test_pyproject_license_field(self, license_project): |
41 | 45 | (project_path / "pyproject.toml").read_text(encoding="utf-8") |
42 | 46 | ) |
43 | 47 | assert pyproject["project"]["license"] == license_name |
| 48 | + |
| 49 | + |
| 50 | +class TestApacheDefault: |
| 51 | + """The default license when the user accepts all defaults is Apache-2.0.""" |
| 52 | + |
| 53 | + def test_default_is_apache(self, tmp_path): |
| 54 | + # Build data without a `license` key so copier falls back to the |
| 55 | + # question's default (Apache-2.0). |
| 56 | + data = {k: v for k, v in DEFAULT_DATA.items() if k != "license"} |
| 57 | + run_copy( |
| 58 | + str(TEMPLATE_ROOT), |
| 59 | + tmp_path, |
| 60 | + data=data, |
| 61 | + defaults=True, |
| 62 | + unsafe=True, |
| 63 | + vcs_ref="HEAD", |
| 64 | + ) |
| 65 | + license_text = (tmp_path / "LICENSE").read_text(encoding="utf-8") |
| 66 | + assert LICENSE_MARKERS["Apache-2.0"] in license_text |
| 67 | + pyproject = tomllib.loads( |
| 68 | + (tmp_path / "pyproject.toml").read_text(encoding="utf-8") |
| 69 | + ) |
| 70 | + assert pyproject["project"]["license"] == "Apache-2.0" |
| 71 | + |
| 72 | + |
| 73 | +class TestExistingLicenseFile: |
| 74 | + """When existing_license_file is set, no LICENSE is generated, the user's |
| 75 | + existing file is preserved, pyproject records `LicenseRef-Custom` and |
| 76 | + points `license-files` at the user's file.""" |
| 77 | + |
| 78 | + def _data_with_existing(self, filename: str) -> dict: |
| 79 | + # Omit `license` so copier computes the dynamic default |
| 80 | + # ("LicenseRef-Custom" when existing_license_file is set). |
| 81 | + data = {k: v for k, v in DEFAULT_DATA.items() if k != "license"} |
| 82 | + data["existing_license_file"] = filename |
| 83 | + return data |
| 84 | + |
| 85 | + def test_no_license_generated_when_existing_file_named(self, tmp_path): |
| 86 | + data = self._data_with_existing("LICENSE.md") |
| 87 | + run_copy( |
| 88 | + str(TEMPLATE_ROOT), |
| 89 | + tmp_path, |
| 90 | + data=data, |
| 91 | + defaults=True, |
| 92 | + unsafe=True, |
| 93 | + vcs_ref="HEAD", |
| 94 | + ) |
| 95 | + assert not (tmp_path / "LICENSE").exists() |
| 96 | + pyproject = tomllib.loads( |
| 97 | + (tmp_path / "pyproject.toml").read_text(encoding="utf-8") |
| 98 | + ) |
| 99 | + assert pyproject["project"]["license"] == "LicenseRef-Custom" |
| 100 | + assert pyproject["project"]["license-files"] == ["LICENSE.md"] |
| 101 | + |
| 102 | + def test_preexisting_license_md_is_preserved(self, tmp_path): |
| 103 | + sentinel = "MY EXISTING LICENSE — DO NOT TOUCH\n" |
| 104 | + (tmp_path / "LICENSE.md").write_text(sentinel, encoding="utf-8") |
| 105 | + data = self._data_with_existing("LICENSE.md") |
| 106 | + run_copy( |
| 107 | + str(TEMPLATE_ROOT), |
| 108 | + tmp_path, |
| 109 | + data=data, |
| 110 | + defaults=True, |
| 111 | + unsafe=True, |
| 112 | + vcs_ref="HEAD", |
| 113 | + ) |
| 114 | + assert not (tmp_path / "LICENSE").exists() |
| 115 | + assert (tmp_path / "LICENSE.md").read_text(encoding="utf-8") == sentinel |
0 commit comments