|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +MINIMAL = { |
| 6 | + "include_github_actions": "n", |
| 7 | + "publish_to_pypi": "n", |
| 8 | + "deptry": "n", |
| 9 | + "mkdocs": "n", |
| 10 | + "codecov": "n", |
| 11 | + "dockerfile": "n", |
| 12 | + "devcontainer": "n", |
| 13 | +} |
| 14 | + |
| 15 | +COMBINATIONS = [ |
| 16 | + pytest.param({}, id="all-defaults"), |
| 17 | + pytest.param(MINIMAL, id="minimal"), |
| 18 | + pytest.param({"layout": "src"}, id="src-layout-defaults"), |
| 19 | + pytest.param({**MINIMAL, "layout": "src"}, id="src-layout-minimal"), |
| 20 | + pytest.param({"publish_to_pypi": "n", "mkdocs": "n"}, id="no-publish-no-mkdocs"), |
| 21 | + pytest.param({"include_github_actions": "n"}, id="no-github-actions"), |
| 22 | + pytest.param({"type_checker": "ty"}, id="ty-type-checker"), |
| 23 | + pytest.param({"mkdocs": "y", "codecov": "n"}, id="mkdocs-no-codecov"), |
| 24 | + pytest.param({"codecov": "n", "include_github_actions": "n"}, id="no-codecov-no-actions"), |
| 25 | + pytest.param({"layout": "src", "type_checker": "ty", "publish_to_pypi": "n"}, id="src-ty-no-publish"), |
| 26 | +] |
| 27 | + |
| 28 | +# Defaults from cookiecutter.json (first item in each list) |
| 29 | +DEFAULTS = { |
| 30 | + "layout": "flat", |
| 31 | + "include_github_actions": "y", |
| 32 | + "publish_to_pypi": "y", |
| 33 | + "deptry": "y", |
| 34 | + "mkdocs": "y", |
| 35 | + "codecov": "y", |
| 36 | + "dockerfile": "y", |
| 37 | + "devcontainer": "y", |
| 38 | + "type_checker": "mypy", |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +def resolve_options(options: dict[str, str]) -> dict[str, str]: |
| 43 | + """Return the full set of resolved options (defaults merged with overrides).""" |
| 44 | + return {**DEFAULTS, **options} |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize("options", COMBINATIONS) |
| 48 | +class TestStructure: |
| 49 | + """Validate file presence/absence for each option combination.""" |
| 50 | + |
| 51 | + def test_always_present_files(self, bake, options): |
| 52 | + EXPECTED_FILES = [ |
| 53 | + ".gitignore", |
| 54 | + ".pre-commit-config.yaml", |
| 55 | + "CONTRIBUTING.md", |
| 56 | + "LICENSE", |
| 57 | + "Makefile", |
| 58 | + "README.md", |
| 59 | + "pyproject.toml", |
| 60 | + "tests", |
| 61 | + "tox.ini", |
| 62 | + ] |
| 63 | + project = bake(**options) |
| 64 | + for rel_path in EXPECTED_FILES: |
| 65 | + assert (project.path / rel_path).exists(), f"Expected {rel_path} to exist" |
| 66 | + |
| 67 | + def test_conditional_files(self, bake, options): |
| 68 | + project = bake(**options) |
| 69 | + effective = resolve_options(options) |
| 70 | + |
| 71 | + if effective["dockerfile"] == "y": |
| 72 | + assert project.has_file("Dockerfile") |
| 73 | + else: |
| 74 | + assert not project.has_file("Dockerfile") |
| 75 | + |
| 76 | + if effective["mkdocs"] == "y": |
| 77 | + assert project.has_dir("docs") |
| 78 | + assert project.has_file("mkdocs.yml") |
| 79 | + else: |
| 80 | + assert not project.has_dir("docs") |
| 81 | + assert not project.has_file("mkdocs.yml") |
| 82 | + |
| 83 | + if effective["codecov"] == "y": |
| 84 | + assert project.has_file("codecov.yaml") |
| 85 | + else: |
| 86 | + assert not project.has_file("codecov.yaml") |
| 87 | + |
| 88 | + if effective["devcontainer"] == "y": |
| 89 | + assert project.has_dir(".devcontainer") |
| 90 | + else: |
| 91 | + assert not project.has_dir(".devcontainer") |
| 92 | + |
| 93 | + if effective["include_github_actions"] == "y": |
| 94 | + assert project.has_dir(".github") |
| 95 | + else: |
| 96 | + assert not project.has_dir(".github") |
| 97 | + |
| 98 | + def test_layout(self, bake, options): |
| 99 | + effective = resolve_options(options) |
| 100 | + project = bake(**options) |
| 101 | + if effective["layout"] == "src": |
| 102 | + assert project.has_dir("src/example_project") |
| 103 | + assert not project.has_dir("example_project") |
| 104 | + else: |
| 105 | + assert project.has_dir("example_project") |
| 106 | + assert not project.has_dir("src") |
| 107 | + |
| 108 | + def test_release_workflow(self, bake, options): |
| 109 | + effective = resolve_options(options) |
| 110 | + project = bake(**options) |
| 111 | + if effective["include_github_actions"] != "y": |
| 112 | + return # no .github at all |
| 113 | + has_release = effective["publish_to_pypi"] == "y" or effective["mkdocs"] == "y" |
| 114 | + workflow = ".github/workflows/on-release-main.yml" |
| 115 | + if has_release: |
| 116 | + assert project.has_file(workflow), "Expected release workflow to exist" |
| 117 | + else: |
| 118 | + assert not project.has_file(workflow), "Expected release workflow to be absent" |
| 119 | + |
| 120 | + def test_yaml_validity(self, bake, options): |
| 121 | + effective = resolve_options(options) |
| 122 | + project = bake(**options) |
| 123 | + if effective["include_github_actions"] == "y": |
| 124 | + assert project.is_valid_yaml(".github/workflows/main.yml") |
| 125 | + |
| 126 | + def test_pyproject_type_checker(self, bake, options): |
| 127 | + effective = resolve_options(options) |
| 128 | + project = bake(**options) |
| 129 | + content = project.read_file("pyproject.toml") |
| 130 | + if effective["type_checker"] == "mypy": |
| 131 | + assert '"mypy' in content |
| 132 | + assert '"ty' not in content |
| 133 | + else: |
| 134 | + assert '"ty' in content |
| 135 | + assert '"mypy' not in content |
| 136 | + |
| 137 | + def test_makefile_targets(self, bake, options): |
| 138 | + effective = resolve_options(options) |
| 139 | + project = bake(**options) |
| 140 | + content = project.read_file("Makefile") |
| 141 | + |
| 142 | + if effective["publish_to_pypi"] == "y": |
| 143 | + assert "build-and-publish" in content |
| 144 | + else: |
| 145 | + assert "build-and-publish" not in content |
| 146 | + |
| 147 | + if effective["mkdocs"] == "y": |
| 148 | + assert "docs:" in content |
| 149 | + else: |
| 150 | + assert "docs:" not in content |
| 151 | + |
| 152 | + def test_codecov_workflow(self, bake, options): |
| 153 | + effective = resolve_options(options) |
| 154 | + project = bake(**options) |
| 155 | + if effective["include_github_actions"] == "y": |
| 156 | + if effective["codecov"] == "y": |
| 157 | + assert project.has_file(".github/workflows/validate-codecov-config.yml") |
| 158 | + assert project.has_file("codecov.yaml") |
| 159 | + else: |
| 160 | + assert not project.has_file(".github/workflows/validate-codecov-config.yml") |
| 161 | + assert not project.has_file("codecov.yaml") |
| 162 | + |
| 163 | + |
| 164 | +@pytest.mark.slow |
| 165 | +@pytest.mark.parametrize("options", COMBINATIONS) |
| 166 | +def test_install_and_run_tests(bake, options): |
| 167 | + """Bake, install dependencies, and run the generated project's test suite.""" |
| 168 | + project = bake(**options) |
| 169 | + project.install() |
| 170 | + project.run_tests() |
| 171 | + |
| 172 | + |
| 173 | +@pytest.mark.slow |
| 174 | +def test_check_passes_on_default_project(bake): |
| 175 | + """A freshly baked default project should pass its own ``make check``.""" |
| 176 | + project = bake() |
| 177 | + project.install() |
| 178 | + project.run_check() |
0 commit comments