diff --git a/tests/repository/test_determine_repo_dir_finds_existing_cookiecutter.py b/tests/repository/test_determine_repo_dir_finds_existing_cookiecutter.py index 107e64aa3..3810668f6 100644 --- a/tests/repository/test_determine_repo_dir_finds_existing_cookiecutter.py +++ b/tests/repository/test_determine_repo_dir_finds_existing_cookiecutter.py @@ -1,5 +1,6 @@ """Tests around detection whether cookiecutter templates are cached locally.""" import os +from pathlib import Path import pytest @@ -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 diff --git a/tests/repository/test_determine_repo_dir_finds_subdirectories.py b/tests/repository/test_determine_repo_dir_finds_subdirectories.py index f40e6063d..bcea1b387 100644 --- a/tests/repository/test_determine_repo_dir_finds_subdirectories.py +++ b/tests/repository/test_determine_repo_dir_finds_subdirectories.py @@ -1,5 +1,6 @@ """Tests around locally cached cookiecutter template repositories.""" import os +from pathlib import Path import pytest @@ -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 diff --git a/tests/test_cli.py b/tests/test_cli.py index 1bc2fdd55..39e924093 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -3,7 +3,7 @@ import json import os import re - +from pathlib import Path import pytest from click.testing import CliRunner @@ -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') @@ -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') @@ -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): @@ -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') @@ -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', @@ -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) @@ -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 = [ diff --git a/tests/test_cookiecutter_local_no_input.py b/tests/test_cookiecutter_local_no_input.py index 0031cbcbf..9e89c9539 100644 --- a/tests/test_cookiecutter_local_no_input.py +++ b/tests/test_cookiecutter_local_no_input.py @@ -5,6 +5,7 @@ """ import os import textwrap +from pathlib import Path import pytest @@ -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') @@ -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 diff --git a/tests/test_custom_extensions_in_hooks.py b/tests/test_custom_extensions_in_hooks.py index 4300bbf4d..fdf2861be 100644 --- a/tests/test_custom_extensions_in_hooks.py +++ b/tests/test_custom_extensions_in_hooks.py @@ -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 @@ -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!' diff --git a/tests/test_default_extensions.py b/tests/test_default_extensions.py index 85924a68e..d229e3d8c 100644 --- a/tests/test_default_extensions.py +++ b/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 @@ -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 = [ @@ -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) diff --git a/tests/test_generate_copy_without_render.py b/tests/test_generate_copy_without_render.py index 7d614824c..9e6039787 100644 --- a/tests/test_generate_copy_without_render.py +++ b/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 @@ -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 diff --git a/tests/test_generate_copy_without_render_override.py b/tests/test_generate_copy_without_render_override.py index af85c4b04..c2d836e3d 100644 --- a/tests/test_generate_copy_without_render_override.py +++ b/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 @@ -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 diff --git a/tests/test_generate_file.py b/tests/test_generate_file.py index 18c811eea..9ff622168 100644 --- a/tests/test_generate_file.py +++ b/tests/test_generate_file.py @@ -2,6 +2,7 @@ import json import os import re +from pathlib import Path import pytest from jinja2 import FileSystemLoader @@ -45,9 +46,8 @@ def test_generate_file(env): env=env, ) assert os.path.isfile('tests/files/cheese.txt') - with open('tests/files/cheese.txt') as f: - generated_text = f.read() - assert generated_text == 'Testing cheese' + generated_text = Path('tests/files/cheese.txt').read_text() + assert generated_text == 'Testing cheese' def test_generate_file_jsonify_filter(env): @@ -58,9 +58,8 @@ def test_generate_file_jsonify_filter(env): project_dir=".", infile=infile, context={'cookiecutter': data}, env=env ) assert os.path.isfile('tests/files/cheese.txt') - with open('tests/files/cheese.txt') as f: - generated_text = f.read() - assert json.loads(generated_text) == data + generated_text = Path('tests/files/cheese.txt').read_text() + assert json.loads(generated_text) == data @pytest.mark.parametrize("length", (10, 40)) @@ -72,9 +71,8 @@ def test_generate_file_random_ascii_string(env, length, punctuation): context = {"cookiecutter": data, "length": length, "punctuation": punctuation} generate.generate_file(project_dir=".", infile=infile, context=context, env=env) assert os.path.isfile('tests/files/cheese.txt') - with open('tests/files/cheese.txt') as f: - generated_text = f.read() - assert len(generated_text) == length + generated_text = Path('tests/files/cheese.txt').read_text() + assert len(generated_text) == length def test_generate_file_with_true_condition(env): @@ -92,9 +90,8 @@ def test_generate_file_with_true_condition(env): env=env, ) assert os.path.isfile('tests/files/cheese.txt') - with open('tests/files/cheese.txt') as f: - generated_text = f.read() - assert generated_text == 'Testing that generate_file was y' + generated_text = Path('tests/files/cheese.txt').read_text() + assert generated_text == 'Testing that generate_file was y' def test_generate_file_with_false_condition(env): @@ -148,7 +145,7 @@ def test_generate_file_does_not_translate_lf_newlines_to_crlf(env, tmp_path): # this generated file should have a LF line ending gf = 'tests/files/cheese_lf_newlines.txt' - with open(gf, encoding='utf-8', newline='') as f: + with Path(gf).open(encoding='utf-8', newline='') as f: simple_text = f.readline() assert simple_text == 'newline is LF\n' assert f.newlines == '\n' @@ -166,7 +163,7 @@ def test_generate_file_does_not_translate_crlf_newlines_to_lf(env): # this generated file should have a CRLF line ending gf = 'tests/files/cheese_crlf_newlines.txt' - with open(gf, encoding='utf-8', newline='') as f: + with Path(gf).open(encoding='utf-8', newline='') as f: simple_text = f.readline() assert simple_text == 'newline is CRLF\r\n' assert f.newlines == '\r\n' diff --git a/tests/test_generate_files.py b/tests/test_generate_files.py index 9cf4929aa..bb4075cae 100644 --- a/tests/test_generate_files.py +++ b/tests/test_generate_files.py @@ -44,7 +44,7 @@ def test_generate_files(tmp_path): assert simple_file.exists() assert simple_file.is_file() - simple_text = open(simple_file, encoding='utf-8').read() + simple_text = Path(simple_file).read_text(encoding='utf-8') assert simple_text == 'I eat pizzä' @@ -60,7 +60,7 @@ def test_generate_files_with_linux_newline(tmp_path): assert newline_file.is_file() assert newline_file.exists() - with open(newline_file, encoding='utf-8', newline='') as f: + with Path(newline_file).open(encoding='utf-8', newline='') as f: simple_text = f.readline() assert simple_text == 'newline is LF\n' assert f.newlines == '\n' @@ -83,7 +83,7 @@ def test_generate_files_with_jinja2_environment(tmp_path): assert conditions_file.is_file() assert conditions_file.exists() - simple_text = conditions_file.open('rt', encoding='utf-8').read() + simple_text = conditions_file.read_text(encoding='utf-8') assert simple_text == 'I eat pizzä\n' @@ -100,7 +100,7 @@ def test_generate_files_with_trailing_newline_forced_to_linux_by_context(tmp_pat assert newline_file.is_file() assert newline_file.exists() - with open(newline_file, encoding='utf-8', newline='') as f: + with Path(newline_file).open(encoding='utf-8', newline='') as f: simple_text = f.readline() assert simple_text == 'newline is LF\r\n' assert f.newlines == '\r\n' @@ -118,7 +118,7 @@ def test_generate_files_with_windows_newline(tmp_path): assert newline_file.is_file() assert newline_file.exists() - with open(newline_file, encoding='utf-8', newline='') as f: + with Path(newline_file).open(encoding='utf-8', newline='') as f: simple_text = f.readline() assert simple_text == 'newline is CRLF\r\n' assert f.newlines == '\r\n' @@ -136,7 +136,7 @@ def test_generate_files_with_windows_newline_forced_to_linux_by_context(tmp_path assert newline_file.is_file() assert newline_file.exists() - with open(newline_file, encoding='utf-8', newline='') as f: + with Path(newline_file).open(encoding='utf-8', newline='') as f: simple_text = f.readline() assert simple_text == 'newline is CRLF\n' @@ -202,7 +202,6 @@ def test_generate_files_permissions(tmp_path): output_dir=tmp_path, ) - assert Path(tmp_path, 'inputpermissions/simple.txt').exists() assert Path(tmp_path, 'inputpermissions/simple.txt').is_file() # Verify source simple.txt should still be 0o644 @@ -241,7 +240,7 @@ def test_generate_files_with_overwrite_if_exists_with_skip_if_file_exists(tmp_pa simple_with_new_line_file = Path(tmp_path, 'inputpizzä/simple-with-newline.txt') Path(tmp_path, 'inputpizzä').mkdir(parents=True) - with open(simple_file, 'w') as f: + with Path(simple_file).open('w') as f: f.write('temp') generate.generate_files( @@ -257,7 +256,7 @@ def test_generate_files_with_overwrite_if_exists_with_skip_if_file_exists(tmp_pa assert Path(simple_with_new_line_file).is_file() assert Path(simple_with_new_line_file).exists() - simple_text = open(simple_file, encoding='utf-8').read() + simple_text = Path(simple_file).read_text(encoding='utf-8') assert simple_text == 'temp' @@ -267,8 +266,7 @@ def test_generate_files_with_skip_if_file_exists(tmp_path): simple_with_new_line_file = Path(tmp_path, 'inputpizzä/simple-with-newline.txt') Path(tmp_path, 'inputpizzä').mkdir(parents=True) - with open(simple_file, 'w') as f: - f.write('temp') + Path(simple_file).write_text('temp') with pytest.raises(exceptions.OutputDirExistsException): generate.generate_files( @@ -279,11 +277,10 @@ def test_generate_files_with_skip_if_file_exists(tmp_path): ) assert Path(simple_file).is_file() - assert Path(simple_file).exists() assert not Path(simple_with_new_line_file).is_file() assert not Path(simple_with_new_line_file).exists() - simple_text = open(simple_file, encoding='utf-8').read() + simple_text = Path(simple_file).read_text(encoding='utf-8') assert simple_text == 'temp' @@ -293,8 +290,7 @@ def test_generate_files_with_overwrite_if_exists(tmp_path): simple_with_new_line_file = Path(tmp_path, 'inputpizzä/simple-with-newline.txt') Path(tmp_path, 'inputpizzä').mkdir(parents=True) - with open(simple_file, 'w') as f: - f.write('temp') + Path(simple_file).write_text('temp') generate.generate_files( context={'cookiecutter': {'food': 'pizzä'}}, @@ -308,7 +304,7 @@ def test_generate_files_with_overwrite_if_exists(tmp_path): assert Path(simple_with_new_line_file).is_file() assert Path(simple_with_new_line_file).exists() - simple_text = open(simple_file, encoding='utf-8').read() + simple_text = Path(simple_file).read_text(encoding='utf-8') assert simple_text == 'I eat pizzä' diff --git a/tests/test_generate_hooks.py b/tests/test_generate_hooks.py index 9624bb8ae..a57e0dbde 100644 --- a/tests/test_generate_hooks.py +++ b/tests/test_generate_hooks.py @@ -2,6 +2,7 @@ import errno import os import sys +from pathlib import Path import pytest @@ -123,7 +124,7 @@ def test_run_failing_hook_removes_output_directory(): hook_path = os.path.join(hooks_path, 'pre_gen_project.py') - with open(hook_path, 'w') as f: + with Path(hook_path).open('w') as f: f.write("#!/usr/bin/env python\n") f.write("import sys; sys.exit(1)\n") @@ -152,7 +153,7 @@ def test_run_failing_hook_preserves_existing_output_directory(): hook_path = os.path.join(hooks_path, 'pre_gen_project.py') - with open(hook_path, 'w') as f: + with Path(hook_path).open('w') as f: f.write("#!/usr/bin/env python\n") f.write("import sys; sys.exit(1)\n") diff --git a/tests/test_hooks.py b/tests/test_hooks.py index d214e714b..9abd66af2 100644 --- a/tests/test_hooks.py +++ b/tests/test_hooks.py @@ -1,9 +1,10 @@ """Tests for `cookiecutter.hooks` module.""" -import os import errno +import os import stat import sys import textwrap +from pathlib import Path import pytest @@ -18,10 +19,9 @@ def make_test_repo(name, multiple_hooks=False): os.mkdir(hook_dir) os.mkdir(template) - with open(os.path.join(template, 'README.rst'), 'w') as f: - f.write("foo\n===\n\nbar\n") + Path(template, 'README.rst').write_text("foo\n===\n\nbar\n") - with open(os.path.join(hook_dir, 'pre_gen_project.py'), 'w') as f: + with Path(hook_dir, 'pre_gen_project.py').open('w') as f: f.write("#!/usr/bin/env python\n") f.write("# -*- coding: utf-8 -*-\n") f.write("from __future__ import print_function\n") @@ -32,7 +32,7 @@ def make_test_repo(name, multiple_hooks=False): if sys.platform.startswith('win'): post = 'post_gen_project.bat' - with open(os.path.join(hook_dir, post), 'w') as f: + with Path(hook_dir, post).open('w') as f: f.write("@echo off\n") f.write("\n") f.write("echo post generation hook\n") @@ -40,7 +40,7 @@ def make_test_repo(name, multiple_hooks=False): else: post = 'post_gen_project.sh' filename = os.path.join(hook_dir, post) - with open(filename, 'w') as f: + with Path(filename).open('w') as f: f.write("#!/bin/bash\n") f.write("\n") f.write("echo 'post generation hook';\n") @@ -52,7 +52,7 @@ def make_test_repo(name, multiple_hooks=False): if multiple_hooks: if sys.platform.startswith('win'): pre = 'pre_gen_project.bat' - with open(os.path.join(hook_dir, pre), 'w') as f: + with Path(hook_dir, pre).open('w') as f: f.write("@echo off\n") f.write("\n") f.write("echo post generation hook\n") @@ -60,7 +60,7 @@ def make_test_repo(name, multiple_hooks=False): else: pre = 'pre_gen_project.sh' filename = os.path.join(hook_dir, pre) - with open(filename, 'w') as f: + with Path(filename).open('w') as f: f.write("#!/bin/bash\n") f.write("\n") f.write("echo 'post generation hook';\n") @@ -182,13 +182,13 @@ def test_run_script_with_context(self): if sys.platform.startswith('win'): post = 'post_gen_project.bat' - with open(os.path.join(self.hooks_path, post), 'w') as f: + with Path(self.hooks_path, post).open('w') as f: f.write("@echo off\n") f.write("\n") f.write("echo post generation hook\n") f.write("echo. >{{cookiecutter.file}}\n") else: - with open(hook_path, 'w') as fh: + with Path(hook_path).open('w') as fh: fh.write("#!/bin/bash\n") fh.write("\n") fh.write("echo 'post generation hook';\n") @@ -221,7 +221,7 @@ def test_run_failing_hook(self): hook_path = os.path.join(self.hooks_path, 'pre_gen_project.py') tests_dir = os.path.join(self.repo_path, 'input{{hooks}}') - with open(hook_path, 'w') as f: + with Path(hook_path).open('w') as f: f.write("#!/usr/bin/env python\n") f.write("import sys; sys.exit(1)\n") diff --git a/tests/test_output_folder.py b/tests/test_output_folder.py index 166a45005..00c4f7846 100644 --- a/tests/test_output_folder.py +++ b/tests/test_output_folder.py @@ -5,6 +5,7 @@ TestOutputFolder.test_output_folder """ import os +from pathlib import Path import pytest @@ -32,11 +33,11 @@ def test_output_folder(): something = """Hi! My name is Audrey Greenfeld. It is 2014.""" - something2 = open('output_folder/something.txt').read() + something2 = Path('output_folder/something.txt').read_text() assert something == something2 in_folder = "The color is green and the letter is D." - in_folder2 = open('output_folder/folder/in_folder.txt').read() + in_folder2 = Path('output_folder/folder/in_folder.txt').read_text() assert in_folder == in_folder2 assert os.path.isdir('output_folder/im_a.dir') diff --git a/tests/test_templates.py b/tests/test_templates.py index 27f40428b..44b9475d2 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -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 @@ -31,12 +30,9 @@ def test_build_templates(template, output_dir): output_dir=output_dir, ) - readme_file = os.path.join(project_dir, 'requirements.txt') + readme = Path(project_dir, 'requirements.txt').read_text() - with codecs.open(readme_file, encoding='utf8') as f: - readme = f.read().splitlines() - - assert readme == [ + assert readme.splitlines() == [ "pip", "Click", "pytest", diff --git a/tests/test_utils.py b/tests/test_utils.py index 537f1017f..5e93d5ad4 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -17,9 +17,7 @@ def make_readonly(path): def test_force_delete(mocker, tmp_path): """Verify `utils.force_delete` makes files writable.""" ro_file = Path(tmp_path, 'bar') - - with open(ro_file, "w") as f: - f.write("Test data") + ro_file.write_text("Test data") make_readonly(ro_file) rmtree = mocker.Mock() @@ -33,9 +31,9 @@ def test_force_delete(mocker, tmp_path): def test_rmtree(tmp_path): """Verify `utils.rmtree` remove files marked as read-only.""" - with open(Path(tmp_path, 'bar'), "w") as f: - f.write("Test data") - make_readonly(Path(tmp_path, 'bar')) + file_path = Path(tmp_path, "bar") + file_path.write_text("Test data") + make_readonly(file_path) utils.rmtree(tmp_path) diff --git a/tests/zipfile/test_unzip.py b/tests/zipfile/test_unzip.py index 2bf58b00f..9d4448e59 100644 --- a/tests/zipfile/test_unzip.py +++ b/tests/zipfile/test_unzip.py @@ -1,8 +1,9 @@ """Tests for function unzip() from zipfile module.""" +import shutil import tempfile +from pathlib import Path import pytest -import shutil from cookiecutter import zipfile from cookiecutter.exceptions import InvalidZipRepository @@ -10,7 +11,7 @@ def mock_download(): """Fake download function.""" - with open('tests/files/fake-repo-tmpl.zip', 'rb') as zf: + with Path('tests/files/fake-repo-tmpl.zip').open('rb') as zf: chunk = zf.read(1024) while chunk: yield chunk @@ -20,7 +21,7 @@ def mock_download(): def mock_download_with_empty_chunks(): """Fake download function.""" yield - with open('tests/files/fake-repo-tmpl.zip', 'rb') as zf: + with Path('tests/files/fake-repo-tmpl.zip').open('rb') as zf: chunk = zf.read(1024) while chunk: yield chunk