Tests: Use pathlib for files read/write (test_generate_file.py)
import json
import os
import re
+from pathlib import Path
import pytest
from jinja2 import FileSystemLoader
@@ -45,7 +46,7 @@ def test_generate_file(env):
env=env,
)
assert os.path.isfile('tests/files/cheese.txt')
- with open('tests/files/cheese.txt') as f:
+ with Path('tests/files/cheese.txt').open() as f:
generated_text = f.read()
assert generated_text == 'Testing cheese'
@@ -58,7 +59,7 @@ 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:
+ with Path('tests/files/cheese.txt').open() as f:
generated_text = f.read()
assert json.loads(generated_text) == data
@@ -72,7 +73,7 @@ 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:
+ with Path('tests/files/cheese.txt').open() as f:
generated_text = f.read()
assert len(generated_text) == length
@@ -92,7 +93,7 @@ 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:
+ with Path('tests/files/cheese.txt').open() as f:
generated_text = f.read()
assert generated_text == 'Testing that generate_file was y'
@@ -148,7 +149,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(newline='') as f:
simple_text = f.readline()
assert simple_text == 'newline is LF\n'
assert f.newlines == '\n'
@@ -166,7 +167,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(newline='') as f:
simple_text = f.readline()
assert simple_text == 'newline is CRLF\r\n'
assert f.newlines == '\r\n'
