/
test_cmdline.py
284 lines (232 loc) · 9.64 KB
/
test_cmdline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from textwrap import dedent
from click.testing import CliRunner
import pytest
from dennis.cmdline import cli
from tests import build_po_string, nix_header
@pytest.fixture
def runner():
"""Creates a CliRunner"""
return CliRunner()
def build_key_val(text):
pairs = {}
for line in text.splitlines():
if ':' in line:
key, val = line.split(':', 1)
pairs[key.strip()] = val.strip()
return pairs
class TestStatus:
def test_help(self, runner):
result = runner.invoke(cli, ('status', '--help'))
assert result.exit_code == 0
assert 'Usage' in result.output.splitlines()[0]
def test_status_untranslated(self, runner, tmpdir):
po_file = build_po_string(
'#: foo/foo.py:5\n'
'msgid "Foo bar baz"\n'
'msgstr ""\n')
fn = tmpdir.join('messages.po')
fn.write(po_file)
result = runner.invoke(cli, ('status', str(fn)))
assert result.exit_code == 0
# FIXME: This would be a lot easier if the status was
# structured and parseable.
pairs = build_key_val(result.output)
assert pairs['Total strings'] == '1'
assert pairs['Total translateable words'] == '3'
assert pairs['Percentage'] == '0%'
def test_status_translated(self, runner, tmpdir):
po_file = build_po_string(
'#: foo/foo.py:5\n'
'msgid "Foo bar baz"\n'
'msgstr "Feh"\n')
fn = tmpdir.join('messages.po')
fn.write(po_file)
result = runner.invoke(cli, ('status', str(fn)))
assert result.exit_code == 0
# FIXME: This would be a lot easier if the status was
# structured and parseable.
pairs = build_key_val(result.output)
assert pairs['Total strings'] == '1'
assert pairs['Total translateable words'] == '3'
assert pairs['Percentage'] == '100% COMPLETE!'
# FIXME: test --showuntranslated on .po file
# FIXME: test --showfuzzy on .po file
class TestTranslate:
def test_help(self, runner):
result = runner.invoke(cli, ('translate', '--help'))
assert result.exit_code == 0
assert 'Usage' in result.output.splitlines()[0]
def test_strings(self, runner):
result = runner.invoke(cli, ('translate', '-p', 'shouty', '-s', 'ou812'))
assert result.exit_code == 0
assert 'OU812' in result.output
def test_no_paths(self, runner):
result = runner.invoke(cli, ('translate', '-p', 'shouty'))
assert result.exit_code == 2
last_line = result.output.splitlines()[-1]
assert last_line == 'Error: nothing to work on. Use --help for help.'
def test_pipe(self, runner):
result = runner.invoke(cli, ('translate', '-p', 'shouty', '-'), input='foo bar')
assert result.exit_code == 0
assert result.output == 'FOO BAR\n'
def test_pipeline(self, runner, tmpdir):
po_file = build_po_string(
'#: foo/foo.py:5\n'
'msgid "Foo bar baz"\n'
'msgstr ""\n')
fn = tmpdir.join('messages.po')
fn.write(po_file)
# Verify the last line is untranslated
assert po_file.endswith('msgstr ""\n')
result = runner.invoke(cli, ('translate', '-p', 'shouty', str(fn)))
assert result.exit_code == 0
last_line = fn.read().splitlines()[-1]
# Verify the last line is now translated
assert last_line == 'msgstr "FOO BAR BAZ"'
def test_bad_pipeline(self, runner, tmpdir):
po_file = build_po_string(
'#: foo/foo.py:5\n'
'msgid "Foo bar baz"\n'
'msgstr ""\n')
fn = tmpdir.join('messages.po')
fn.write(po_file)
result = runner.invoke(cli, ('translate', '-p', 'triangle', str(fn)))
assert result.exit_code == 2
last_line = result.output.splitlines()[-1]
assert last_line == 'Error: pipeline "triangle" is not valid'
def test_nonexistent_file(self, runner, tmpdir):
fn = tmpdir.join('missingfile.po')
result = runner.invoke(cli, ('translate', '-p', 'shouty', str(fn)))
assert result.exit_code == 2
last_line = result.output.splitlines()[-1]
assert last_line == 'Error: file %s does not exist.' % str(fn)
def test_plurals(self, runner, tmpdir):
po_file = build_po_string(
'#: test_project/base/views.py:12 test_project/base/views.py:22\n'
'#, python-format\n'
'msgid "%(num)s apple"\n'
'msgid_plural "%(num)s apples"\n'
'msgstr[0] ""\n'
'msgstr[1] ""\n'
)
fn = tmpdir.join('django.po')
fn.write(po_file)
result = runner.invoke(cli, ('translate', '-p', 'shouty', str(fn)))
assert result.exit_code == 0
pot_file = nix_header(fn.read())
# FIXME: This has the wrong variables, too. We need to fix that, too.
assert (
pot_file ==
dedent("""\
#: test_project/base/views.py:12 test_project/base/views.py:22
#, python-format
msgid "%(num)s apple"
msgid_plural "%(num)s apples"
msgstr[0] "%(NUM)S APPLE"
msgstr[1] "%(NUM)S APPLES"
""")
)
class TestLint:
def test_help(self, runner):
result = runner.invoke(cli, ('lint', '--help'))
assert result.exit_code == 0
assert 'Usage' in result.output.splitlines()[0]
def test_basic_linting(self, runner, tmpdir):
po_file = build_po_string(
'#: foo/foo.py:5\n'
'msgid "Foo bar baz"\n'
'msgstr "Foo"\n')
fn = tmpdir.join('messages.po')
fn.write(po_file)
result = runner.invoke(cli, ('lint', str(fn)))
assert result.exit_code == 0
output_lines = result.output.splitlines()
assert len(output_lines) == 1
assert output_lines[0].startswith('dennis version')
def test_linting_fail(self, runner, tmpdir):
po_file = build_po_string(
'#: foo/foo.py:5\n'
'msgid "Foo %(foo)s bar baz"\n'
'msgstr "Foo %(bar)s"\n')
fn = tmpdir.join('messages.po')
fn.write(po_file)
result = runner.invoke(cli, ('lint', str(fn)))
assert result.exit_code == 1
output_lines = result.output.splitlines()
assert len(output_lines) == 16
assert 'E201: invalid variables: %(bar)s' in result.output
def test_no_files_to_work_on(self, runner):
result = runner.invoke(cli, ('lint', 'foo'))
assert result.exit_code == 2
assert 'nothing to work on.' in result.output
def test_nonexistent_file(self, runner, tmpdir):
fn = tmpdir.join('missingfile.po')
result = runner.invoke(cli, ('lint', str(fn)))
assert result.exit_code == 2
last_line = result.output.splitlines()[-1]
assert last_line == 'Error: file "%s" does not exist.' % str(fn)
def test_quiet(self, runner, tmpdir):
po_file = build_po_string(
'#: foo/foo.py:5\n'
'msgid "Foo bar baz"\n'
'msgstr "Foo"\n')
fn = tmpdir.join('messages.po')
fn.write(po_file)
result = runner.invoke(cli, ('lint', '--quiet', str(fn)))
assert result.exit_code == 0
assert result.output == ''
def test_rules(self, runner, tmpdir):
po_file = build_po_string(
'#: foo/foo.py:5\n'
'msgid "Foo %(bar)s baz"\n'
'msgstr "FOO %(bar) BAZ"\n')
fn = tmpdir.join('messages.po')
fn.write(po_file)
result = runner.invoke(cli, ('lint', '--rules', 'E101', str(fn)))
assert result.exit_code == 1
assert 'E101: type missing: %(bar)' in result.output
def test_template_rules(self, runner, tmpdir):
po_file = build_po_string(
'#: foo/foo.py:5\n'
'msgid "Foo %(o)s baz"\n'
'msgstr ""\n')
fn = tmpdir.join('messages.pot')
fn.write(po_file)
result = runner.invoke(cli, ('lint', '--rules', 'W501', str(fn)))
assert result.exit_code == 0
assert 'W501: one character variable name "o"' in result.output
def test_nonexisting_rule(self, runner, tmpdir):
po_file = build_po_string(
'#: foo/foo.py:5\n'
'msgid "Foo %(o)s baz"\n'
'msgstr ""\n')
fn = tmpdir.join('messages.pot')
fn.write(po_file)
result = runner.invoke(cli, ('lint', '--rules', 'foo', str(fn)))
assert result.exit_code == 2
assert 'Error: invalid rules: foo.' in result.output
def test_excludes(self, runner, tmpdir):
po_file = build_po_string(
'#: foo/foo.py:5\n'
'msgid "Foo %(o)s baz"\n'
'msgstr ""\n')
fn = tmpdir.join('messages.pot')
fn.write(po_file)
result = runner.invoke(cli, ('lint', '--excluderules', 'W501', str(fn)))
assert result.exit_code == 0
# The rule that generates this error is excluded, so this error shouldn't show up.
assert 'W501: one character variable name "o"' not in result.output
def test_varformat_no_value(self, runner, tmpdir):
po_file = build_po_string(
'#: foo/foo.py:5\n'
'msgid "Foo %(o)s baz"\n'
'msgstr ""\n')
fn = tmpdir.join('messages.pot')
fn.write(po_file)
result = runner.invoke(cli, ('lint', '--varformat', '', str(fn)))
assert result.exit_code == 0
# We're not looking at any variables, so we should never have a variable related warning.
assert 'W501: one character variable name "o"' not in result.output
# FIXME: test --varformat with values
# FIXME: test --reporter
# FIXME: test --errorsonly