mirror of
https://github.com/cookiecutter/cookiecutter-django.git
synced 2025-02-09 08:00:50 +03:00
Migrate test generation to pathlib (#5649)
This commit is contained in:
parent
1cad7df6cc
commit
7f4211ab16
|
@ -2,6 +2,8 @@ import glob
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
from collections.abc import Iterable
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
@ -146,19 +148,19 @@ def _fixture_id(ctx):
|
||||||
return "-".join(f"{key}:{value}" for key, value in ctx.items())
|
return "-".join(f"{key}:{value}" for key, value in ctx.items())
|
||||||
|
|
||||||
|
|
||||||
def build_files_list(base_dir):
|
def build_files_list(base_path: Path):
|
||||||
"""Build a list containing absolute paths to the generated files."""
|
"""Build a list containing absolute paths to the generated files."""
|
||||||
return [os.path.join(dirpath, file_path) for dirpath, subdirs, files in os.walk(base_dir) for file_path in files]
|
return [dirpath / file_path for dirpath, subdirs, files in base_path.walk() for file_path in files]
|
||||||
|
|
||||||
|
|
||||||
def check_paths(paths):
|
def check_paths(paths: Iterable[Path]):
|
||||||
"""Method to check all paths have correct substitutions."""
|
"""Method to check all paths have correct substitutions."""
|
||||||
# Assert that no match is found in any of the files
|
# Assert that no match is found in any of the files
|
||||||
for path in paths:
|
for path in paths:
|
||||||
if is_binary(path):
|
if is_binary(str(path)):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for line in open(path):
|
for line in path.open():
|
||||||
match = RE_OBJ.search(line)
|
match = RE_OBJ.search(line)
|
||||||
assert match is None, f"cookiecutter variable not replaced in {path}"
|
assert match is None, f"cookiecutter variable not replaced in {path}"
|
||||||
|
|
||||||
|
@ -173,7 +175,7 @@ def test_project_generation(cookies, context, context_override):
|
||||||
assert result.project_path.name == context["project_slug"]
|
assert result.project_path.name == context["project_slug"]
|
||||||
assert result.project_path.is_dir()
|
assert result.project_path.is_dir()
|
||||||
|
|
||||||
paths = build_files_list(str(result.project_path))
|
paths = build_files_list(result.project_path)
|
||||||
assert paths
|
assert paths
|
||||||
check_paths(paths)
|
check_paths(paths)
|
||||||
|
|
||||||
|
@ -286,7 +288,7 @@ def test_travis_invokes_pytest(cookies, context, use_docker, expected_test_scrip
|
||||||
assert result.project_path.name == context["project_slug"]
|
assert result.project_path.name == context["project_slug"]
|
||||||
assert result.project_path.is_dir()
|
assert result.project_path.is_dir()
|
||||||
|
|
||||||
with open(f"{result.project_path}/.travis.yml") as travis_yml:
|
with (result.project_path / ".travis.yml").open() as travis_yml:
|
||||||
try:
|
try:
|
||||||
yml = yaml.safe_load(travis_yml)["jobs"]["include"]
|
yml = yaml.safe_load(travis_yml)["jobs"]["include"]
|
||||||
assert yml[0]["script"] == ["ruff check ."]
|
assert yml[0]["script"] == ["ruff check ."]
|
||||||
|
@ -311,7 +313,7 @@ def test_gitlab_invokes_precommit_and_pytest(cookies, context, use_docker, expec
|
||||||
assert result.project_path.name == context["project_slug"]
|
assert result.project_path.name == context["project_slug"]
|
||||||
assert result.project_path.is_dir()
|
assert result.project_path.is_dir()
|
||||||
|
|
||||||
with open(f"{result.project_path}/.gitlab-ci.yml") as gitlab_yml:
|
with (result.project_path / ".gitlab-ci.yml").open() as gitlab_yml:
|
||||||
try:
|
try:
|
||||||
gitlab_config = yaml.safe_load(gitlab_yml)
|
gitlab_config = yaml.safe_load(gitlab_yml)
|
||||||
assert gitlab_config["precommit"]["script"] == [
|
assert gitlab_config["precommit"]["script"] == [
|
||||||
|
@ -338,7 +340,7 @@ def test_github_invokes_linter_and_pytest(cookies, context, use_docker, expected
|
||||||
assert result.project_path.name == context["project_slug"]
|
assert result.project_path.name == context["project_slug"]
|
||||||
assert result.project_path.is_dir()
|
assert result.project_path.is_dir()
|
||||||
|
|
||||||
with open(f"{result.project_path}/.github/workflows/ci.yml") as github_yml:
|
with (result.project_path / ".github" / "workflows" / "ci.yml").open() as github_yml:
|
||||||
try:
|
try:
|
||||||
github_config = yaml.safe_load(github_yml)
|
github_config = yaml.safe_load(github_yml)
|
||||||
linter_present = False
|
linter_present = False
|
||||||
|
@ -389,9 +391,9 @@ def test_pycharm_docs_removed(cookies, context, editor, pycharm_docs_exist):
|
||||||
context.update({"editor": editor})
|
context.update({"editor": editor})
|
||||||
result = cookies.bake(extra_context=context)
|
result = cookies.bake(extra_context=context)
|
||||||
|
|
||||||
with open(f"{result.project_path}/docs/index.rst") as f:
|
index_rst = result.project_path / "docs" / "index.rst"
|
||||||
has_pycharm_docs = "pycharm/configuration" in f.read()
|
has_pycharm_docs = "pycharm/configuration" in index_rst.read_text()
|
||||||
assert has_pycharm_docs is pycharm_docs_exist
|
assert has_pycharm_docs is pycharm_docs_exist
|
||||||
|
|
||||||
|
|
||||||
def test_trim_domain_email(cookies, context):
|
def test_trim_domain_email(cookies, context):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user