Passed
Push — master ( b29ecb...3caa16 )
by
unknown
01:14
created

test_generate_file_random_ascii_string()   A

Complexity

Conditions 2

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 21
rs 9.45
c 0
b 0
f 0
cc 2
nop 3
1
# -*- coding: utf-8 -*-
2
3
"""
4
test_generate_file
5
------------------
6
7
Tests formerly known from a unittest residing in test_generate.py named
8
TestGenerateFile.test_generate_file
9
TestGenerateFile.test_generate_file_verbose_template_syntax_error
10
"""
11
12
from __future__ import unicode_literals
13
import json
14
import os
15
16
from jinja2 import FileSystemLoader
17
from jinja2.exceptions import TemplateSyntaxError
18
import pytest
19
20
from cookiecutter import generate
21
from cookiecutter.environment import StrictEnvironment
22
23
24
@pytest.fixture(scope='function')
25
def remove_cheese_file(request):
26
    """
27
    Remove the cheese text file which is created by the tests.
28
    """
29
    def fin_remove_cheese_file():
30
        if os.path.exists('tests/files/cheese.txt'):
31
            os.remove('tests/files/cheese.txt')
32
    request.addfinalizer(fin_remove_cheese_file)
33
34
35
@pytest.fixture
36
def env():
37
    environment = StrictEnvironment()
38
    environment.loader = FileSystemLoader('.')
39
    return environment
40
41
42 View Code Duplication
@pytest.mark.usefixtures('remove_cheese_file')
0 ignored issues
show
Duplication introduced by Raphael Pierzina
This code seems to be duplicated in your project.
Loading history...
43
def test_generate_file(env):
44
    infile = 'tests/files/{{generate_file}}.txt'
45
    generate.generate_file(
46
        project_dir=".",
47
        infile=infile,
48
        context={'generate_file': 'cheese'},
49
        env=env
50
    )
51
    assert os.path.isfile('tests/files/cheese.txt')
52
    with open('tests/files/cheese.txt', 'rt') as f:
53
        generated_text = f.read()
54
        assert generated_text == 'Testing cheese'
55
56
57
@pytest.mark.usefixtures('remove_cheese_file')
58
def test_generate_file_with_false_condition(env):
59
    infile = 'tests/files/{% if generate_file == \'y\' %}cheese.txt{% endif %}'
60
    generate.generate_file(
61
        project_dir=".",
62
        infile=infile,
63
        context={'generate_file': 'n'},
64
        env=env
65
    )
66
    assert not os.path.exists('tests/files/cheese.txt')
67
68
69
@pytest.mark.usefixtures('remove_cheese_file')
70
def test_generate_file_jsonify_filter(env):
71
    infile = 'tests/files/{{cookiecutter.jsonify_file}}.txt'
72
    data = {'jsonify_file': 'cheese', 'type': 'roquefort'}
73
    generate.generate_file(
74
        project_dir=".",
75
        infile=infile,
76
        context={'cookiecutter': data},
77
        env=env
78
    )
79
    assert os.path.isfile('tests/files/cheese.txt')
80
    with open('tests/files/cheese.txt', 'rt') as f:
81
        generated_text = f.read()
82
        assert json.loads(generated_text) == data
83
84
85
@pytest.mark.usefixtures('remove_cheese_file')
86
@pytest.mark.parametrize("length", (10, 40))
87
@pytest.mark.parametrize("punctuation", (True, False))
88
def test_generate_file_random_ascii_string(env, length, punctuation):
89
    infile = 'tests/files/{{cookiecutter.random_string_file}}.txt'
90
    data = {'random_string_file': 'cheese'}
91
    context = {
92
        "cookiecutter": data,
93
        "length": length,
94
        "punctuation": punctuation
95
    }
96
    generate.generate_file(
97
        project_dir=".",
98
        infile=infile,
99
        context=context,
100
        env=env
101
    )
102
    assert os.path.isfile('tests/files/cheese.txt')
103
    with open('tests/files/cheese.txt', 'rt') as f:
104
        generated_text = f.read()
105
        assert len(generated_text) == length
106
107
108 View Code Duplication
@pytest.mark.usefixtures('remove_cheese_file')
0 ignored issues
show
Duplication introduced by Abdo Roig-Maranges
This code seems to be duplicated in your project.
Loading history...
109
def test_generate_file_with_true_conditional(env):
110
    infile = 'tests/files/{% if generate_file == \'y\' %}cheese.txt{% endif %}'
111
    generate.generate_file(
112
        project_dir=".",
113
        infile=infile,
114
        context={'generate_file': 'y'},
115
        env=env
116
    )
117
    assert os.path.isfile('tests/files/cheese.txt')
118
    with open('tests/files/cheese.txt', 'rt') as f:
119
        generated_text = f.read()
120
        assert generated_text == 'Testing that generate_file was y'
121
122
123
@pytest.fixture
124
def expected_msg():
125
    msg = (
126
        'Missing end of comment tag\n'
127
        '  File "./tests/files/syntax_error.txt", line 1\n'
128
        '    I eat {{ syntax_error }} {# this comment is not closed}'
129
    )
130
    return msg.replace("/", os.sep)
131
132
133
@pytest.mark.usefixtures('remove_cheese_file')
134
def test_generate_file_verbose_template_syntax_error(env, expected_msg):
135
    try:
136
        generate.generate_file(
137
            project_dir=".",
138
            infile='tests/files/syntax_error.txt',
139
            context={'syntax_error': 'syntax_error'},
140
            env=env
141
        )
142
    except TemplateSyntaxError as exception:
143
        assert str(exception) == expected_msg
144
    except Exception as exception:
145
        pytest.fail('Unexpected exception thrown: {0}'.format(exception))
146
    else:
147
        pytest.fail('TemplateSyntaxError not thrown')
148