Skip to content

Commit 0d91bf2

Browse files
committed
ENH: Increase coverage
1 parent b266720 commit 0d91bf2

File tree

5 files changed

+201
-28
lines changed

5 files changed

+201
-28
lines changed

.coveragerc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[run]
2+
branch = True
3+
source = codespell_lib
4+
include = */codespell_lib/*
5+
omit =

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ before_install:
2727
- source venv/bin/activate
2828
- python --version # just to check
2929
- pip install -U pip wheel # upgrade to latest pip find 3.5 wheels; wheel to avoid errors
30-
- retry pip install nose flake8 coverage codecov
30+
- retry pip install nose flake8 coverage codecov chardet
3131
- cd $SRC_DIR
3232

3333
install:

codespell_lib/_codespell.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ def init_chardet(self):
129129
try:
130130
from chardet.universaldetector import UniversalDetector
131131
except ImportError:
132-
raise Exception("There's no chardet installed to import from. "
133-
"Please, install it and check your PYTHONPATH "
134-
"environment variable")
132+
raise ImportError("There's no chardet installed to import from. "
133+
"Please, install it and check your PYTHONPATH "
134+
"environment variable")
135135

136136
self.encdetector = UniversalDetector()
137137

@@ -433,8 +433,7 @@ def parse_file(filename, colors, summary):
433433

434434
if options.write_changes and fix:
435435
changed = True
436-
lines[i - 1] = re.sub(r'\b%s\b' % word,
437-
fixword, lines[i - 1])
436+
lines[i] = re.sub(r'\b%s\b' % word, fixword, lines[i])
438437
fixed_words.add(word)
439438
continue
440439

@@ -506,7 +505,7 @@ def main(*args):
506505
print('ERROR: cannot find dictionary file:\n%s' % options.dictionary,
507506
file=sys.stderr)
508507
parser.print_help()
509-
sys.exit(1)
508+
return 1
510509

511510
build_dict(options.dictionary)
512511
colors = TermColors()
@@ -536,17 +535,9 @@ def main(*args):
536535

537536
if os.path.isdir(filename):
538537
for root, dirs, files in os.walk(filename):
539-
# i = 0
540-
# for d in dirs:
541-
# if is_hidden(d):
542-
# del dirs[i]
543-
# else:
544-
# i += 1
545538
for file_ in files:
546539
fname = os.path.join(root, file_)
547-
if not os.path.isfile(fname):
548-
continue
549-
if not os.path.getsize(fname):
540+
if not os.path.isfile(fname) or not os.path.getsize(fname):
550541
continue
551542
if glob_match.match(root): # skips also match directories
552543
continue
@@ -561,7 +552,3 @@ def main(*args):
561552
print("\n-------8<-------\nSUMMARY:")
562553
print(summary)
563554
return bad_count
564-
565-
566-
if __name__ == '__main__':
567-
sys.exit(main(*sys.argv))

codespell_lib/tests/test_basic.py

Lines changed: 188 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,179 @@
1+
# -*- coding: utf-8 -*-
2+
13
from __future__ import print_function
24

5+
import contextlib
36
import os
47
import os.path as op
58
import sys
69
import tempfile
710
import warnings
811

9-
from nose.tools import assert_equal
12+
from nose.tools import assert_equal, assert_true
1013

1114
from codespell_lib import main
1215

1316

1417
def test_basic():
1518
"""Test some basic functionality"""
1619
assert_equal(main('_does_not_exist_'), 0)
17-
with tempfile.NamedTemporaryFile() as f:
18-
assert_equal(main(f.name,), 0) # empty file
19-
f.write('this is a test file\n'.encode('utf-8'))
20+
with tempfile.NamedTemporaryFile('w') as f:
21+
with CaptureStdout() as sio:
22+
assert_equal(main('-D', 'foo', f.name), 1) # missing dictionary
23+
assert_true('cannot find dictionary' in sio[1])
24+
assert_equal(main(f.name), 0) # empty file
25+
f.write('this is a test file\n')
2026
f.flush()
21-
assert_equal(main(f.name,), 0) # good
22-
f.write('abandonned\n'.encode('utf-8'))
27+
assert_equal(main(f.name), 0) # good
28+
f.write('abandonned\n')
2329
f.flush()
2430
assert_equal(main(f.name), 1) # bad
25-
f.write('abandonned\n'.encode('utf-8'))
31+
f.write('abandonned\n')
2632
f.flush()
2733
assert_equal(main(f.name), 2) # worse
34+
with TemporaryDirectory() as d:
35+
with open(op.join(d, 'bad.txt'), 'w') as f:
36+
f.write('abandonned\nAbandonned\nABANDONNED\nAbAnDoNnEd')
37+
assert_equal(main(d), 4)
38+
with CaptureStdout() as sio:
39+
assert_equal(main('-w', d), 0)
40+
assert_true('FIXED:' in sio[1])
41+
with open(op.join(d, 'bad.txt')) as f:
42+
new_content = f.read()
43+
assert_equal(main(d), 0)
44+
assert_equal(new_content, 'abandoned\nAbandoned\nABANDONED\nabandoned')
45+
46+
with open(op.join(d, 'bad.txt'), 'w') as f:
47+
f.write('abandonned abandonned\n')
48+
assert_equal(main(d), 2)
49+
with CaptureStdout() as sio:
50+
assert_equal(main('-q', '16', '-w', d), 0)
51+
assert_equal(sio[0], '')
52+
assert_equal(main(d), 0)
53+
54+
# empty directory
55+
os.mkdir(op.join(d, 'test'))
56+
assert_equal(main(d), 0)
57+
58+
# hidden file
59+
with open(op.join(d, 'test.txt'), 'w') as f:
60+
f.write('abandonned\n')
61+
assert_equal(main(op.join(d, 'test.txt')), 1)
62+
os.rename(op.join(d, 'test.txt'), op.join(d, '.test.txt'))
63+
assert_equal(main(op.join(d, '.test.txt')), 0)
64+
65+
66+
def test_interactivity():
67+
"""Test interaction"""
68+
with tempfile.NamedTemporaryFile('w') as f:
69+
assert_equal(main(f.name), 0) # empty file
70+
f.write('abandonned\n')
71+
f.flush()
72+
assert_equal(main('-i', '-1', f.name), 1) # bad
73+
with FakeStdin('y\n'):
74+
assert_equal(main('-i', '3', f.name), 1)
75+
with CaptureStdout() as sio:
76+
with FakeStdin('n\n'):
77+
assert_equal(main('-w', '-i', '3', f.name), 0)
78+
assert_true('==>' in sio[0])
79+
with CaptureStdout():
80+
with FakeStdin('x\ny\n'):
81+
assert_equal(main('-w', '-i', '3', f.name), 0)
82+
assert_equal(main(f.name), 0)
83+
with tempfile.NamedTemporaryFile('w') as f:
84+
f.write('abandonned\n')
85+
f.flush()
86+
assert_equal(main(f.name), 1)
87+
with CaptureStdout():
88+
with FakeStdin(' '): # blank input -> Y
89+
assert_equal(main('-w', '-i', '3', f.name), 0)
90+
assert_equal(main(f.name), 0)
91+
# multiple options
92+
with tempfile.NamedTemporaryFile('w') as f:
93+
f.write('ackward\n')
94+
f.flush()
95+
assert_equal(main(f.name), 1)
96+
with CaptureStdout():
97+
with FakeStdin(' \n'): # blank input -> nothing
98+
assert_equal(main('-w', '-i', '3', f.name), 0)
99+
assert_equal(main(f.name), 1)
100+
with CaptureStdout():
101+
with FakeStdin('0\n'): # blank input -> nothing
102+
assert_equal(main('-w', '-i', '3', f.name), 0)
103+
assert_equal(main(f.name), 0)
104+
with open(f.name, 'r') as f_read:
105+
assert_equal(f_read.read(), 'awkward\n')
106+
f.seek(0)
107+
f.write('ackward\n')
108+
f.flush()
109+
assert_equal(main(f.name), 1)
110+
with CaptureStdout() as sio:
111+
with FakeStdin('x\n1\n'): # blank input -> nothing
112+
assert_equal(main('-w', '-i', '3', f.name), 0)
113+
assert_true('a valid option' in sio[0])
114+
assert_equal(main(f.name), 0)
115+
with open(f.name, 'r') as f:
116+
assert_equal(f.read(), 'backward\n')
117+
118+
119+
def test_summary():
120+
"""Test summary functionality"""
121+
with tempfile.NamedTemporaryFile('w') as f:
122+
with CaptureStdout() as sio:
123+
main(f.name)
124+
for ii in range(2):
125+
assert_equal(sio[ii], '') # no output
126+
with CaptureStdout() as sio:
127+
main(f.name, '--summary')
128+
assert_equal(sio[1], '') # stderr
129+
assert_true('SUMMARY' in sio[0])
130+
assert_equal(len(sio[0].split('\n')), 5) # no output
131+
f.write('abandonned\nabandonned')
132+
f.flush()
133+
with CaptureStdout() as sio:
134+
main(f.name, '--summary')
135+
assert_equal(sio[1], '') # stderr
136+
assert_true('SUMMARY' in sio[0])
137+
assert_equal(len(sio[0].split('\n')), 7)
138+
assert_true('abandonned' in sio[0].split()[-2])
139+
140+
141+
def test_exclude_file():
142+
"""Test exclude file functionality"""
143+
with TemporaryDirectory() as d:
144+
with open(op.join(d, 'bad.txt'), 'w') as f:
145+
f.write('abandonned 1\nabandonned 2\n')
146+
with tempfile.NamedTemporaryFile('w') as f:
147+
f.write('abandonned 1\n')
148+
f.flush()
149+
assert_equal(main(d), 2)
150+
assert_equal(main('-x', f.name, d), 1)
151+
152+
153+
def test_encoding():
154+
"""Test encoding handling"""
155+
# Some simple Unicode things
156+
with tempfile.NamedTemporaryFile('wb') as f:
157+
# with CaptureStdout() as sio:
158+
assert_equal(main(f.name), 0)
159+
f.write(u'naïve\n'.encode('utf-8'))
160+
f.flush()
161+
assert_equal(main(f.name), 0)
162+
assert_equal(main('-e', f.name), 0)
163+
f.write(u'naieve\n'.encode('utf-8'))
164+
f.flush()
165+
assert_equal(main(f.name), 1)
166+
# Binary file warning
167+
with tempfile.NamedTemporaryFile('wb') as f:
168+
assert_equal(main(f.name), 0)
169+
f.write(b'\x00\x00naiive\x00\x00')
170+
f.flush()
171+
with CaptureStdout() as sio:
172+
assert_equal(main(f.name), 0)
173+
assert_true('WARNING: Binary file' in sio[1])
174+
with CaptureStdout() as sio:
175+
assert_equal(main('-q', '2', f.name), 0)
176+
assert_equal(sio[1], '')
28177

29178

30179
def test_ignore():
@@ -114,3 +263,35 @@ def _rmtree(self, path):
114263
self._rmdir(path)
115264
except OSError:
116265
pass
266+
267+
268+
@contextlib.contextmanager
269+
def CaptureStdout():
270+
if sys.version[0] == '2':
271+
from StringIO import StringIO
272+
else:
273+
from io import StringIO
274+
oldout, olderr = sys.stdout, sys.stderr
275+
try:
276+
out = [StringIO(), StringIO()]
277+
sys.stdout, sys.stderr = out
278+
yield out
279+
finally:
280+
sys.stdout, sys.stderr = oldout, olderr
281+
out[0] = out[0].getvalue()
282+
out[1] = out[1].getvalue()
283+
284+
285+
@contextlib.contextmanager
286+
def FakeStdin(text):
287+
if sys.version[0] == '2':
288+
from StringIO import StringIO
289+
else:
290+
from io import StringIO
291+
oldin = sys.stdin
292+
try:
293+
in_ = StringIO(text)
294+
sys.stdin = in_
295+
yield
296+
finally:
297+
sys.stdin = oldin

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
with-coverage = 1
33
cover-package = codespell_lib
44
detailed-errors = 1
5-
verbose = 3
5+
verbosity = 2
66

77
[flake8]
88
exclude = build

0 commit comments

Comments
 (0)