Skip to content

Commit

Permalink
Fix default values being loaded with wrong encoding on Windows (#1414)
Browse files Browse the repository at this point in the history
Explicitly set the encoding to utf-8 when reading the context file to
ensure values are correctly loaded.

Co-authored-by: Andrey Shpak <insspb@users.noreply.github.com>
  • Loading branch information
agateau and insspb committed May 29, 2020
1 parent c156337 commit 7f6804c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cookiecutter/generate.py
Expand Up @@ -82,7 +82,7 @@ def generate_context(
context = OrderedDict([])

try:
with open(context_file) as file_handle:
with open(context_file, encoding='utf-8') as file_handle:
obj = json.load(file_handle, object_pairs_hook=OrderedDict)
except ValueError as e:
# JSON decoding error. Let's throw a new exception that is more
Expand Down
3 changes: 3 additions & 0 deletions tests/test-generate-context/non_ascii.json
@@ -0,0 +1,3 @@
{
"full_name": "éèà"
}
11 changes: 11 additions & 0 deletions tests/test_generate_context.py
Expand Up @@ -108,6 +108,17 @@ def test_default_context_replacement_in_generate_context():
assert generated_context == expected_context


def test_generate_context_decodes_non_ascii_chars():
"""Verify `generate_context` correctly decodes non-ascii chars."""
expected_context = {'non_ascii': OrderedDict([('full_name', 'éèà'),])}

generated_context = generate.generate_context(
context_file='tests/test-generate-context/non_ascii.json'
)

assert generated_context == expected_context


@pytest.fixture
def template_context():
"""Fixture. Populates template content for future tests."""
Expand Down

0 comments on commit 7f6804c

Please sign in to comment.