From 7f4211ab16ae735fdf7a35e84b177f0c85cc2909 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Mon, 20 Jan 2025 13:28:59 +0000 Subject: [PATCH] Migrate test generation to pathlib (#5649) --- tests/test_cookiecutter_generation.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/tests/test_cookiecutter_generation.py b/tests/test_cookiecutter_generation.py index 9669d78b..1c03015c 100755 --- a/tests/test_cookiecutter_generation.py +++ b/tests/test_cookiecutter_generation.py @@ -2,6 +2,8 @@ import glob import os import re import sys +from collections.abc import Iterable +from pathlib import Path import pytest @@ -146,19 +148,19 @@ def _fixture_id(ctx): 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.""" - 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.""" # Assert that no match is found in any of the files for path in paths: - if is_binary(path): + if is_binary(str(path)): continue - for line in open(path): + for line in path.open(): match = RE_OBJ.search(line) 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.is_dir() - paths = build_files_list(str(result.project_path)) + paths = build_files_list(result.project_path) assert 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.is_dir() - with open(f"{result.project_path}/.travis.yml") as travis_yml: + with (result.project_path / ".travis.yml").open() as travis_yml: try: yml = yaml.safe_load(travis_yml)["jobs"]["include"] 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.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: gitlab_config = yaml.safe_load(gitlab_yml) 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.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: github_config = yaml.safe_load(github_yml) linter_present = False @@ -389,9 +391,9 @@ def test_pycharm_docs_removed(cookies, context, editor, pycharm_docs_exist): context.update({"editor": editor}) result = cookies.bake(extra_context=context) - with open(f"{result.project_path}/docs/index.rst") as f: - has_pycharm_docs = "pycharm/configuration" in f.read() - assert has_pycharm_docs is pycharm_docs_exist + index_rst = result.project_path / "docs" / "index.rst" + has_pycharm_docs = "pycharm/configuration" in index_rst.read_text() + assert has_pycharm_docs is pycharm_docs_exist def test_trim_domain_email(cookies, context):