Skip to content

Commit

Permalink
Merge pull request cookiecutter#1718 from cookiecutter/code-quality-t…
Browse files Browse the repository at this point in the history
…ests-use-pathlib

Code quality: Tests upgrade: Use pathlib for files read/write
  • Loading branch information
insspb committed Jun 8, 2022
2 parents ca229a3 + b1d4701 commit 7b81306
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 139 deletions.
@@ -1,5 +1,6 @@
"""Tests around detection whether cookiecutter templates are cached locally."""
import os
from pathlib import Path

import pytest

Expand All @@ -20,7 +21,7 @@ def cloned_cookiecutter_path(user_config_data, template):
cloned_template_path = os.path.join(cookiecutters_dir, template)
os.mkdir(cloned_template_path)

open(os.path.join(cloned_template_path, 'cookiecutter.json'), 'w')
Path(cloned_template_path, "cookiecutter.json").touch() # creates file

return cloned_template_path

Expand Down
@@ -1,5 +1,6 @@
"""Tests around locally cached cookiecutter template repositories."""
import os
from pathlib import Path

import pytest

Expand All @@ -24,7 +25,7 @@ def cloned_cookiecutter_path(user_config_data, template):
subdir_template_path = os.path.join(cloned_template_path, 'my-dir')
if not os.path.exists(subdir_template_path):
os.mkdir(subdir_template_path)
open(os.path.join(subdir_template_path, 'cookiecutter.json'), 'w')
Path(subdir_template_path, 'cookiecutter.json').touch() # creates file

return subdir_template_path

Expand Down
41 changes: 19 additions & 22 deletions tests/test_cli.py
Expand Up @@ -3,7 +3,7 @@
import json
import os
import re

from pathlib import Path

import pytest
from click.testing import CliRunner
Expand Down Expand Up @@ -72,8 +72,8 @@ def test_cli(cli_runner):
result = cli_runner('tests/fake-repo-pre/', '--no-input')
assert result.exit_code == 0
assert os.path.isdir('fake-project')
with open(os.path.join('fake-project', 'README.rst')) as f:
assert 'Project name: **Fake Project**' in f.read()
content = Path("fake-project", "README.rst").read_text()
assert 'Project name: **Fake Project**' in content


@pytest.mark.usefixtures('remove_fake_project_dir')
Expand All @@ -82,8 +82,8 @@ def test_cli_verbose(cli_runner):
result = cli_runner('tests/fake-repo-pre/', '--no-input', '-v')
assert result.exit_code == 0
assert os.path.isdir('fake-project')
with open(os.path.join('fake-project', 'README.rst')) as f:
assert 'Project name: **Fake Project**' in f.read()
content = Path("fake-project", "README.rst").read_text()
assert 'Project name: **Fake Project**' in content


@pytest.mark.usefixtures('remove_fake_project_dir')
Expand Down Expand Up @@ -435,10 +435,9 @@ def test_local_extension(tmpdir, cli_runner):
template_path,
)
assert result.exit_code == 0
with open(os.path.join(output_dir, 'Foobar', 'HISTORY.rst')) as f:
data = f.read()
assert 'FoobarFoobar' in data
assert 'FOOBAR' in data
content = Path(output_dir, 'Foobar', 'HISTORY.rst').read_text()
assert 'FoobarFoobar' in content
assert 'FOOBAR' in content


def test_local_extension_not_available(tmpdir, cli_runner):
Expand All @@ -462,8 +461,8 @@ def test_cli_extra_context(cli_runner):
)
assert result.exit_code == 0
assert os.path.isdir('fake-project')
with open(os.path.join('fake-project', 'README.rst')) as f:
assert 'Project name: **Awesomez**' in f.read()
content = Path('fake-project', 'README.rst').read_text()
assert 'Project name: **Awesomez**' in content


@pytest.mark.usefixtures('remove_fake_project_dir')
Expand Down Expand Up @@ -544,13 +543,12 @@ def test_debug_list_installed_templates(cli_runner, debug_file, user_config_path
"""Verify --list-installed command correct invocation."""
fake_template_dir = os.path.dirname(os.path.abspath('fake-project'))
os.makedirs(os.path.dirname(user_config_path))
with open(user_config_path, 'w') as config_file:
# In YAML, double quotes mean to use escape sequences.
# Single quotes mean we will have unescaped backslahes.
# http://blogs.perl.org/users/tinita/2018/03/
# strings-in-yaml---to-quote-or-not-to-quote.html
config_file.write("cookiecutters_dir: '%s'" % fake_template_dir)
open(os.path.join('fake-project', 'cookiecutter.json'), 'w').write('{}')
# In YAML, double quotes mean to use escape sequences.
# Single quotes mean we will have unescaped backslahes.
# http://blogs.perl.org/users/tinita/2018/03/
# strings-in-yaml---to-quote-or-not-to-quote.html
Path(user_config_path).write_text(f"cookiecutters_dir: '{fake_template_dir}'")
Path("fake-project", "cookiecutter.json").write_text('{}')

result = cli_runner(
'--list-installed',
Expand All @@ -568,8 +566,7 @@ def test_debug_list_installed_templates_failure(
):
"""Verify --list-installed command error on invocation."""
os.makedirs(os.path.dirname(user_config_path))
with open(user_config_path, 'w') as config_file:
config_file.write('cookiecutters_dir: "/notarealplace/"')
Path(user_config_path).write_text('cookiecutters_dir: "/notarealplace/"')

result = cli_runner(
'--list-installed', '--config-file', user_config_path, str(debug_file)
Expand All @@ -590,8 +587,8 @@ def test_directory_repo(cli_runner):
)
assert result.exit_code == 0
assert os.path.isdir("fake-project")
with open(os.path.join("fake-project", "README.rst")) as f:
assert "Project name: **Fake Project**" in f.read()
content = Path("fake-project", "README.rst").read_text()
assert "Project name: **Fake Project**" in content


cli_accept_hook_arg_testdata = [
Expand Down
12 changes: 5 additions & 7 deletions tests/test_cookiecutter_local_no_input.py
Expand Up @@ -5,6 +5,7 @@
"""
import os
import textwrap
from pathlib import Path

import pytest

Expand Down Expand Up @@ -65,9 +66,8 @@ def test_cookiecutter_no_input_return_rendered_file():
"""Verify Jinja2 templating correctly works in `cookiecutter.json` file."""
project_dir = main.cookiecutter('tests/fake-repo-pre', no_input=True)
assert project_dir == os.path.abspath('fake-project')
with open(os.path.join(project_dir, 'README.rst')) as fh:
contents = fh.read()
assert "Project name: **Fake Project**" in contents
content = Path(project_dir, 'README.rst').read_text()
assert "Project name: **Fake Project**" in content


@pytest.mark.usefixtures('clean_system', 'remove_additional_dirs')
Expand All @@ -76,11 +76,9 @@ def test_cookiecutter_dict_values_in_context():
project_dir = main.cookiecutter('tests/fake-repo-dict', no_input=True)
assert project_dir == os.path.abspath('fake-project-dict')

with open(os.path.join(project_dir, 'README.md')) as fh:
contents = fh.read()

content = Path(project_dir, 'README.md').read_text()
assert (
contents
content
== textwrap.dedent(
"""
# README
Expand Down
11 changes: 3 additions & 8 deletions tests/test_custom_extensions_in_hooks.py
Expand Up @@ -4,8 +4,7 @@
Tests to ensure custom cookiecutter extensions are properly made available to
pre- and post-gen hooks.
"""
import codecs
import os
from pathlib import Path

import pytest

Expand Down Expand Up @@ -40,9 +39,5 @@ def test_hook_with_extension(template, output_dir):
extra_context={'project_slug': 'foobar', 'name': 'Cookiemonster'},
)

readme_file = os.path.join(project_dir, 'README.rst')

with codecs.open(readme_file, encoding='utf8') as f:
readme = f.read().strip()

assert readme == 'Hello Cookiemonster!'
readme = Path(project_dir, 'README.rst').read_text(encoding="utf-8")
assert readme.strip() == 'Hello Cookiemonster!'
7 changes: 4 additions & 3 deletions tests/test_default_extensions.py
@@ -1,9 +1,10 @@
"""Verify Jinja2 filters/extensions are available from pre-gen/post-gen hooks."""
import os
import uuid
from pathlib import Path

import freezegun
import pytest
import uuid

from cookiecutter.main import cookiecutter

Expand All @@ -25,7 +26,7 @@ def test_jinja2_time_extension(tmp_path):
changelog_file = os.path.join(project_dir, 'HISTORY.rst')
assert os.path.isfile(changelog_file)

with open(changelog_file, encoding='utf-8') as f:
with Path(changelog_file).open(encoding='utf-8') as f:
changelog_lines = f.readlines()

expected_lines = [
Expand Down Expand Up @@ -57,7 +58,7 @@ def test_jinja2_uuid_extension(tmp_path):
changelog_file = os.path.join(project_dir, 'id')
assert os.path.isfile(changelog_file)

with open(changelog_file, encoding='utf-8') as f:
with Path(changelog_file).open(encoding='utf-8') as f:
changelog_lines = f.readlines()

uuid.UUID(changelog_lines[0], version=4)
37 changes: 19 additions & 18 deletions tests/test_generate_copy_without_render.py
@@ -1,5 +1,6 @@
"""Verify correct work of `_copy_without_render` context option."""
import os
from pathlib import Path

import pytest

Expand Down Expand Up @@ -43,33 +44,33 @@ def test_generate_copy_without_render_extensions():
assert 'test_copy_without_render-not-rendered' in dir_contents
assert 'test_copy_without_render-rendered' in dir_contents

with open('test_copy_without_render/README.txt') as f:
assert '{{cookiecutter.render_test}}' in f.read()
file_1 = Path('test_copy_without_render/README.txt').read_text()
assert '{{cookiecutter.render_test}}' in file_1

with open('test_copy_without_render/README.rst') as f:
assert 'I have been rendered!' in f.read()
file_2 = Path('test_copy_without_render/README.rst').read_text()
assert 'I have been rendered!' in file_2

with open(
file_3 = Path(
'test_copy_without_render/test_copy_without_render-rendered/README.txt'
) as f:
assert '{{cookiecutter.render_test}}' in f.read()
).read_text()
assert '{{cookiecutter.render_test}}' in file_3

with open(
file_4 = Path(
'test_copy_without_render/test_copy_without_render-rendered/README.rst'
) as f:
assert 'I have been rendered' in f.read()
).read_text()
assert 'I have been rendered' in file_4

with open(
file_5 = Path(
'test_copy_without_render/'
'test_copy_without_render-not-rendered/'
'README.rst'
) as f:
assert '{{cookiecutter.render_test}}' in f.read()
).read_text()
assert '{{cookiecutter.render_test}}' in file_5

with open('test_copy_without_render/rendered/not_rendered.yml') as f:
assert '{{cookiecutter.render_test}}' in f.read()
file_6 = Path('test_copy_without_render/rendered/not_rendered.yml').read_text()
assert '{{cookiecutter.render_test}}' in file_6

with open(
file_7 = Path(
'test_copy_without_render/' 'test_copy_without_render-rendered/' 'README.md'
) as f:
assert '{{cookiecutter.render_test}}' in f.read()
).read_text()
assert '{{cookiecutter.render_test}}' in file_7
37 changes: 19 additions & 18 deletions tests/test_generate_copy_without_render_override.py
@@ -1,5 +1,6 @@
"""Verify correct work of `_copy_without_render` context option."""
import os
from pathlib import Path

import pytest

Expand Down Expand Up @@ -62,33 +63,33 @@ def test_generate_copy_without_render_extensions():
assert 'test_copy_without_render-not-rendered' in dir_contents
assert 'test_copy_without_render-rendered' in dir_contents

with open('test_copy_without_render/README.txt') as f:
assert '{{cookiecutter.render_test}}' in f.read()
file_1 = Path('test_copy_without_render/README.txt').read_text()
assert '{{cookiecutter.render_test}}' in file_1

with open('test_copy_without_render/README.rst') as f:
assert 'I have been rendered!' in f.read()
file_2 = Path('test_copy_without_render/README.rst').read_text()
assert 'I have been rendered!' in file_2

with open(
file_3 = Path(
'test_copy_without_render/test_copy_without_render-rendered/README.txt'
) as f:
assert '{{cookiecutter.render_test}}' in f.read()
).read_text()
assert '{{cookiecutter.render_test}}' in file_3

with open(
file_4 = Path(
'test_copy_without_render/test_copy_without_render-rendered/README.rst'
) as f:
assert 'I have been rendered' in f.read()
).read_text()
assert 'I have been rendered' in file_4

with open(
file_5 = Path(
'test_copy_without_render/'
'test_copy_without_render-not-rendered/'
'README.rst'
) as f:
assert '{{cookiecutter.render_test}}' in f.read()
).read_text()
assert '{{cookiecutter.render_test}}' in file_5

with open('test_copy_without_render/rendered/not_rendered.yml') as f:
assert '{{cookiecutter.render_test}}' in f.read()
file_6 = Path('test_copy_without_render/rendered/not_rendered.yml').read_text()
assert '{{cookiecutter.render_test}}' in file_6

with open(
file_7 = Path(
'test_copy_without_render/' 'test_copy_without_render-rendered/' 'README.md'
) as f:
assert '{{cookiecutter.render_test}}' in f.read()
).read_text()
assert '{{cookiecutter.render_test}}' in file_7

0 comments on commit 7b81306

Please sign in to comment.