"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "codespell_lib/tests/test_basic.py" between
codespell-1.17.1.tar.gz and codespell-2.0.0.tar.gz

About: codespell checks source code and other text files for common misspellings.

test_basic.py  (codespell-1.17.1):test_basic.py  (codespell-2.0.0)
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import print_function from __future__ import print_function
import contextlib import contextlib
import inspect
import os import os
import os.path as op import os.path as op
from shutil import copyfile
import subprocess import subprocess
import sys import sys
import codespell_lib as cs import pytest
import codespell_lib as cs_
from codespell_lib._codespell import EX_USAGE, EX_OK, EX_DATAERR
def test_constants():
"""Test our EX constants."""
assert EX_OK == 0
assert EX_USAGE == 64
assert EX_DATAERR == 65
class MainWrapper(object):
"""Compatibility wrapper for when we used to return the count."""
def main(self, *args, count=True, std=False, **kwargs):
if count:
args = ('--count',) + args
code = cs_.main(*args, **kwargs)
capsys = inspect.currentframe().f_back.f_locals['capsys']
stdout, stderr = capsys.readouterr()
assert code in (EX_OK, EX_USAGE, EX_DATAERR)
if code == EX_DATAERR: # have some misspellings
code = int(stderr.split('\n')[-2])
elif code == EX_OK and count:
code = int(stderr.split('\n')[-2])
assert code == 0
if std:
return (code, stdout, stderr)
else:
return code
cs = MainWrapper()
def run_codespell(args=(), cwd=None): def run_codespell(args=(), cwd=None):
"""Helper to run codespell""" """Run codespell."""
return subprocess.Popen( args = ('--count',) + args
proc = subprocess.Popen(
['codespell'] + list(args), cwd=cwd, ['codespell'] + list(args), cwd=cwd,
stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait() stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stderr = proc.communicate()[1].decode('utf-8')
count = int(stderr.split('\n')[-2])
return count
def test_command(tmpdir): def test_command(tmpdir):
"""Test running the codespell executable""" """Test running the codespell executable."""
# With no arguments does "." # With no arguments does "."
d = str(tmpdir) d = str(tmpdir)
assert run_codespell(cwd=d) == 0 assert run_codespell(cwd=d) == 0
with open(op.join(d, 'bad.txt'), 'w') as f: with open(op.join(d, 'bad.txt'), 'w') as f:
f.write('abandonned\nAbandonned\nABANDONNED\nAbAnDoNnEd') f.write('abandonned\nAbandonned\nABANDONNED\nAbAnDoNnEd')
assert run_codespell(cwd=d) == 4 assert run_codespell(cwd=d) == 4
def test_basic(tmpdir, capsys): def test_basic(tmpdir, capsys):
"""Test some basic functionality""" """Test some basic functionality."""
assert cs.main('_does_not_exist_') == 0 assert cs.main('_does_not_exist_') == 0
fname = op.join(str(tmpdir), 'tmp') fname = op.join(str(tmpdir), 'tmp')
with open(fname, 'w') as f: with open(fname, 'w') as f:
pass pass
assert cs.main('-D', 'foo', f.name) == 1, 'missing dictionary' code, _, stderr = cs.main('-D', 'foo', f.name, std=True)
assert 'cannot find dictionary' in capsys.readouterr()[1] assert code == EX_USAGE, 'missing dictionary'
assert 'cannot find dictionary' in stderr
assert cs.main(fname) == 0, 'empty file' assert cs.main(fname) == 0, 'empty file'
with open(fname, 'a') as f: with open(fname, 'a') as f:
f.write('this is a test file\n') f.write('this is a test file\n')
assert cs.main(fname) == 0, 'good' assert cs.main(fname) == 0, 'good'
with open(fname, 'a') as f: with open(fname, 'a') as f:
f.write('abandonned\n') f.write('abandonned\n')
assert cs.main(fname) == 1, 'bad' assert cs.main(fname) == 1, 'bad'
with open(fname, 'a') as f: with open(fname, 'a') as f:
f.write('abandonned\n') f.write('abandonned\n')
assert cs.main(fname) == 2, 'worse' assert cs.main(fname) == 2, 'worse'
with open(fname, 'a') as f: with open(fname, 'a') as f:
f.write('tim\ngonna\n') f.write('tim\ngonna\n')
assert cs.main(fname) == 2, 'with a name' assert cs.main(fname) == 2, 'with a name'
assert cs.main('--builtin', 'clear,rare,names,informal', fname) == 4 assert cs.main('--builtin', 'clear,rare,names,informal', fname) == 4
capsys.readouterr() code, _, stderr = cs.main(fname, '--builtin', 'foo', std=True)
assert cs.main(fname, '--builtin', 'foo') == 1 # bad type sys.exit(1) assert code == EX_USAGE # bad type
stdout = capsys.readouterr()[1] assert 'Unknown builtin dictionary' in stderr
assert 'Unknown builtin dictionary' in stdout d = str(tmpdir)
d = str(tmpdir) code, _, stderr = cs.main(fname, '-D', op.join(d, 'foo'), std=True)
assert cs.main(fname, '-D', op.join(d, 'foo')) == 1 # bad dict assert code == EX_USAGE # bad dict
stdout = capsys.readouterr()[1] assert 'cannot find dictionary' in stderr
assert 'cannot find dictionary' in stdout
os.remove(fname) os.remove(fname)
with open(op.join(d, 'bad.txt'), 'w') as f: with open(op.join(d, 'bad.txt'), 'w') as f:
f.write('abandonned\nAbandonned\nABANDONNED\nAbAnDoNnEd') f.write('abandonned\nAbandonned\nABANDONNED\nAbAnDoNnEd')
assert cs.main(d) == 4 assert cs.main(d) == 4
capsys.readouterr() code, _, stderr = cs.main('-w', d, std=True)
assert cs.main('-w', d) == 0 assert code == 0
assert 'FIXED:' in capsys.readouterr()[1] assert 'FIXED:' in stderr
with open(op.join(d, 'bad.txt')) as f: with open(op.join(d, 'bad.txt')) as f:
new_content = f.read() new_content = f.read()
assert cs.main(d) == 0 assert cs.main(d) == 0
assert new_content == 'abandoned\nAbandoned\nABANDONED\nabandoned' assert new_content == 'abandoned\nAbandoned\nABANDONED\nabandoned'
with open(op.join(d, 'bad.txt'), 'w') as f: with open(op.join(d, 'bad.txt'), 'w') as f:
f.write('abandonned abandonned\n') f.write('abandonned abandonned\n')
assert cs.main(d) == 2 assert cs.main(d) == 2
capsys.readouterr() code, stdout, stderr = cs.main(
assert cs.main('-q', '16', '-w', d) == 0 '-q', '16', '-w', d, count=False, std=True)
assert capsys.readouterr() == ('', '') assert code == 0
assert stdout == stderr == ''
assert cs.main(d) == 0 assert cs.main(d) == 0
# empty directory # empty directory
os.mkdir(op.join(d, 'test')) os.mkdir(op.join(d, 'test'))
assert cs.main(d) == 0 assert cs.main(d) == 0
def test_interactivity(tmpdir, capsys): def test_interactivity(tmpdir, capsys):
"""Test interaction""" """Test interaction"""
# Windows can't read a currently-opened file, so here we use # Windows can't read a currently-opened file, so here we use
# NamedTemporaryFile just to get a good name # NamedTemporaryFile just to get a good name
with open(op.join(str(tmpdir), 'tmp'), 'w') as f: with open(op.join(str(tmpdir), 'tmp'), 'w') as f:
pass pass
try: try:
assert cs.main(f.name) == 0, 'empty file' assert cs.main(f.name) == 0, 'empty file'
with open(f.name, 'w') as f: with open(f.name, 'w') as f:
f.write('abandonned\n') f.write('abandonned\n')
assert cs.main('-i', '-1', f.name) == 1, 'bad' assert cs.main('-i', '-1', f.name) == 1, 'bad'
with FakeStdin('y\n'): with FakeStdin('y\n'):
assert cs.main('-i', '3', f.name) == 1 assert cs.main('-i', '3', f.name) == 1
with FakeStdin('n\n'): with FakeStdin('n\n'):
assert cs.main('-w', '-i', '3', f.name) == 0 code, stdout, _ = cs.main('-w', '-i', '3', f.name, std=True)
assert '==>' in capsys.readouterr()[0] assert code == 0
assert '==>' in stdout
with FakeStdin('x\ny\n'): with FakeStdin('x\ny\n'):
assert cs.main('-w', '-i', '3', f.name) == 0 assert cs.main('-w', '-i', '3', f.name) == 0
assert cs.main(f.name) == 0 assert cs.main(f.name) == 0
finally: finally:
os.remove(f.name) os.remove(f.name)
# New example # New example
with open(op.join(str(tmpdir), 'tmp2'), 'w') as f: with open(op.join(str(tmpdir), 'tmp2'), 'w') as f:
pass pass
try: try:
skipping to change at line 137 skipping to change at line 176
assert cs.main('-w', '-i', '3', f.name) == 0 assert cs.main('-w', '-i', '3', f.name) == 0
assert cs.main(f.name) == 1 assert cs.main(f.name) == 1
with FakeStdin('0\n'): # blank input -> nothing with FakeStdin('0\n'): # blank input -> nothing
assert cs.main('-w', '-i', '3', f.name) == 0 assert cs.main('-w', '-i', '3', f.name) == 0
assert cs.main(f.name) == 0 assert cs.main(f.name) == 0
with open(f.name, 'r') as f_read: with open(f.name, 'r') as f_read:
assert f_read.read() == 'awkward\n' assert f_read.read() == 'awkward\n'
with open(f.name, 'w') as f: with open(f.name, 'w') as f:
f.write('ackward\n') f.write('ackward\n')
assert cs.main(f.name) == 1 assert cs.main(f.name) == 1
capsys.readouterr()
with FakeStdin('x\n1\n'): # blank input -> nothing with FakeStdin('x\n1\n'): # blank input -> nothing
assert cs.main('-w', '-i', '3', f.name) == 0 code, stdout, _ = cs.main('-w', '-i', '3', f.name, std=True)
assert 'a valid option' in capsys.readouterr()[0] assert code == 0
assert 'a valid option' in stdout
assert cs.main(f.name) == 0 assert cs.main(f.name) == 0
with open(f.name, 'r') as f: with open(f.name, 'r') as f:
assert f.read() == 'backward\n' assert f.read() == 'backward\n'
finally: finally:
os.remove(f.name) os.remove(f.name)
def test_summary(tmpdir, capsys): def test_summary(tmpdir, capsys):
"""Test summary functionality""" """Test summary functionality."""
with open(op.join(str(tmpdir), 'tmp'), 'w') as f: with open(op.join(str(tmpdir), 'tmp'), 'w') as f:
pass pass
try: code, stdout, stderr = cs.main(f.name, std=True, count=False)
cs.main(f.name) assert code == 0
assert capsys.readouterr() == ('', ''), 'no output' assert stdout == stderr == '', 'no output'
cs.main(f.name, '--summary') code, stdout, stderr = cs.main(f.name, '--summary', std=True)
stdout, stderr = capsys.readouterr() assert code == 0
assert stderr == '' assert stderr == '0\n'
assert 'SUMMARY' in stdout assert 'SUMMARY' in stdout
assert len(stdout.split('\n')) == 5 assert len(stdout.split('\n')) == 5
with open(f.name, 'w') as f: with open(f.name, 'w') as f:
f.write('abandonned\nabandonned') f.write('abandonned\nabandonned')
cs.main(f.name, '--summary') assert code == 0
stdout, stderr = capsys.readouterr() code, stdout, stderr = cs.main(f.name, '--summary', std=True)
assert stderr == '' assert stderr == '2\n'
assert 'SUMMARY' in stdout assert 'SUMMARY' in stdout
assert len(stdout.split('\n')) == 7 assert len(stdout.split('\n')) == 7
assert 'abandonned' in stdout.split()[-2] assert 'abandonned' in stdout.split()[-2]
finally:
os.remove(f.name)
def test_ignore_dictionary(tmpdir): def test_ignore_dictionary(tmpdir, capsys):
"""Test ignore dictionary functionality.""" """Test ignore dictionary functionality."""
d = str(tmpdir) d = str(tmpdir)
with open(op.join(d, 'bad.txt'), 'w') as f: with open(op.join(d, 'bad.txt'), 'w') as f:
f.write('1 abandonned 1\n2 abandonned 2\nabondon\n') f.write('1 abandonned 1\n2 abandonned 2\nabondon\n')
bad_name = f.name bad_name = f.name
assert cs.main(bad_name) == 3 assert cs.main(bad_name) == 3
with open(op.join(d, 'ignore.txt'), 'w') as f: with open(op.join(d, 'ignore.txt'), 'w') as f:
f.write('abandonned\n') f.write('abandonned\n')
assert cs.main('-I', f.name, bad_name) == 1 assert cs.main('-I', f.name, bad_name) == 1
def test_ignore_word_list(tmpdir): def test_ignore_word_list(tmpdir, capsys):
"""Test ignore word list functionality""" """Test ignore word list functionality."""
d = str(tmpdir) d = str(tmpdir)
with open(op.join(d, 'bad.txt'), 'w') as f: with open(op.join(d, 'bad.txt'), 'w') as f:
f.write('abandonned\nabondon\nabilty\n') f.write('abandonned\nabondon\nabilty\n')
assert cs.main(d) == 3 assert cs.main(d) == 3
assert cs.main('-Labandonned,someword', '-Labilty', d) == 1 assert cs.main('-Labandonned,someword', '-Labilty', d) == 1
def test_custom_regex(tmpdir, capsys): def test_custom_regex(tmpdir, capsys):
"""Test custom word regex.""" """Test custom word regex."""
d = str(tmpdir) d = str(tmpdir)
with open(op.join(d, 'bad.txt'), 'w') as f: with open(op.join(d, 'bad.txt'), 'w') as f:
f.write('abandonned_abondon\n') f.write('abandonned_abondon\n')
assert cs.main(d) == 0 assert cs.main(d) == 0
assert cs.main('-r', "[a-z]+", d) == 2 assert cs.main('-r', "[a-z]+", d) == 2
capsys.readouterr() code, stdout, _ = cs.main('-r', '[a-z]+', '--write-changes', d, std=True)
assert cs.main('-r', '[a-z]+', '--write-changes', d) == 1 assert code == EX_USAGE
assert 'ERROR:' in capsys.readouterr()[0] assert 'ERROR:' in stdout
def test_exclude_file(tmpdir): def test_exclude_file(tmpdir, capsys):
"""Test exclude file functionality.""" """Test exclude file functionality."""
d = str(tmpdir) d = str(tmpdir)
with open(op.join(d, 'bad.txt'), 'wb') as f: with open(op.join(d, 'bad.txt'), 'wb') as f:
f.write('1 abandonned 1\n2 abandonned 2\n'.encode('utf-8')) f.write('1 abandonned 1\n2 abandonned 2\n'.encode('utf-8'))
bad_name = f.name bad_name = f.name
assert cs.main(bad_name) == 2 assert cs.main(bad_name) == 2
with open(op.join(d, 'tmp.txt'), 'wb') as f: with open(op.join(d, 'tmp.txt'), 'wb') as f:
f.write('1 abandonned 1\n'.encode('utf-8')) f.write('1 abandonned 1\n'.encode('utf-8'))
assert cs.main(bad_name) == 2 assert cs.main(bad_name) == 2
assert cs.main('-x', f.name, bad_name) == 1 assert cs.main('-x', f.name, bad_name) == 1
def test_encoding(tmpdir, capsys): def test_encoding(tmpdir, capsys):
"""Test encoding handling.""" """Test encoding handling."""
# Some simple Unicode things # Some simple Unicode things
with open(op.join(str(tmpdir), 'tmp'), 'w') as f: with open(op.join(str(tmpdir), 'tmp'), 'w') as f:
pass pass
# with CaptureStdout() as sio: # with CaptureStdout() as sio:
assert cs.main(f.name) == 0 assert cs.main(f.name) == 0
try: with open(f.name, 'wb') as f:
with open(f.name, 'wb') as f: f.write(u'naïve\n'.encode('utf-8'))
f.write(u'naïve\n'.encode('utf-8')) assert cs.main(f.name) == 0
assert cs.main(f.name) == 0 assert cs.main('-e', f.name) == 0
assert cs.main('-e', f.name) == 0 with open(f.name, 'ab') as f:
with open(f.name, 'ab') as f: f.write(u'naieve\n'.encode('utf-8'))
f.write(u'naieve\n'.encode('utf-8')) assert cs.main(f.name) == 1
assert cs.main(f.name) == 1 # Binary file warning
# Binary file warning with open(f.name, 'wb') as f:
with open(f.name, 'wb') as f: f.write(b'\x00\x00naiive\x00\x00')
f.write(b'\x00\x00naiive\x00\x00') code, stdout, stderr = cs.main(f.name, std=True, count=False)
capsys.readouterr() assert code == 0
assert cs.main(f.name) == 0 assert stdout == stderr == ''
assert 'WARNING: Binary file' in capsys.readouterr()[1] code, stdout, stderr = cs.main('-q', '0', f.name, std=True, count=False)
assert cs.main('-q', '2', f.name) == 0 assert code == 0
assert capsys.readouterr() == ('', '') assert stdout == ''
finally: assert 'WARNING: Binary file' in stderr
os.remove(f.name)
def test_ignore(tmpdir): def test_ignore(tmpdir, capsys):
"""Test ignoring of files and directories.""" """Test ignoring of files and directories."""
d = str(tmpdir) d = str(tmpdir)
with open(op.join(d, 'good.txt'), 'w') as f: with open(op.join(d, 'good.txt'), 'w') as f:
f.write('this file is okay') f.write('this file is okay')
assert cs.main(d) == 0 assert cs.main(d) == 0
with open(op.join(d, 'bad.txt'), 'w') as f: with open(op.join(d, 'bad.txt'), 'w') as f:
f.write('abandonned') f.write('abandonned')
assert cs.main(d) == 1 assert cs.main(d) == 1
assert cs.main('--skip=bad*', d) == 0 assert cs.main('--skip=bad*', d) == 0
assert cs.main('--skip=bad.txt', d) == 0 assert cs.main('--skip=bad.txt', d) == 0
subdir = op.join(d, 'ignoredir') subdir = op.join(d, 'ignoredir')
os.mkdir(subdir) os.mkdir(subdir)
with open(op.join(subdir, 'bad.txt'), 'w') as f: with open(op.join(subdir, 'bad.txt'), 'w') as f:
f.write('abandonned') f.write('abandonned')
assert cs.main(d) == 2 assert cs.main(d) == 2
assert cs.main('--skip=bad*', d) == 0 assert cs.main('--skip=bad*', d) == 0
assert cs.main('--skip=*ignoredir*', d) == 1 assert cs.main('--skip=*ignoredir*', d) == 1
assert cs.main('--skip=ignoredir', d) == 1 assert cs.main('--skip=ignoredir', d) == 1
assert cs.main('--skip=*ignoredir/bad*', d) == 1 assert cs.main('--skip=*ignoredir/bad*', d) == 1
def test_check_filename(tmpdir): def test_check_filename(tmpdir, capsys):
"""Test filename check.""" """Test filename check."""
d = str(tmpdir) d = str(tmpdir)
# Empty file
with open(op.join(d, 'abandonned.txt'), 'w') as f:
f.write('')
assert cs.main('-f', d) == 1
# Normal file with contents
with open(op.join(d, 'abandonned.txt'), 'w') as f: with open(op.join(d, 'abandonned.txt'), 'w') as f:
f.write('.') f.write('.')
assert cs.main('-f', d) == 1 assert cs.main('-f', d) == 1
# Normal file with binary contents
with open(op.join(d, 'abandonned.txt'), 'wb') as f:
f.write(b'\x00\x00naiive\x00\x00')
assert cs.main('-f', d) == 1
def test_check_hidden(tmpdir): @pytest.mark.skipif((not hasattr(os, "mkfifo") or not callable(os.mkfifo)),
reason='requires os.mkfifo')
def test_check_filename_irregular_file(tmpdir, capsys):
"""Test irregular file filename check."""
# Irregular file (!isfile())
d = str(tmpdir)
os.mkfifo(op.join(d, 'abandonned'))
assert cs.main('-f', d) == 1
d = str(tmpdir)
def test_check_hidden(tmpdir, capsys):
"""Test ignoring of hidden files.""" """Test ignoring of hidden files."""
d = str(tmpdir) d = str(tmpdir)
# hidden file # visible file
with open(op.join(d, 'test.txt'), 'w') as f: with open(op.join(d, 'test.txt'), 'w') as f:
f.write('abandonned\n') f.write('abandonned\n')
assert cs.main(op.join(d, 'test.txt')) == 1 assert cs.main(op.join(d, 'test.txt')) == 1
assert cs.main(d) == 1
# hidden file
os.rename(op.join(d, 'test.txt'), op.join(d, '.test.txt')) os.rename(op.join(d, 'test.txt'), op.join(d, '.test.txt'))
assert cs.main(op.join(d, '.test.txt')) == 0 assert cs.main(op.join(d, '.test.txt')) == 0
assert cs.main(d) == 0
assert cs.main('--check-hidden', op.join(d, '.test.txt')) == 1 assert cs.main('--check-hidden', op.join(d, '.test.txt')) == 1
assert cs.main('--check-hidden', d) == 1
# hidden file with typo in name
os.rename(op.join(d, '.test.txt'), op.join(d, '.abandonned.txt')) os.rename(op.join(d, '.test.txt'), op.join(d, '.abandonned.txt'))
assert cs.main(op.join(d, '.abandonned.txt')) == 0 assert cs.main(op.join(d, '.abandonned.txt')) == 0
assert cs.main(d) == 0
assert cs.main('--check-hidden', op.join(d, '.abandonned.txt')) == 1 assert cs.main('--check-hidden', op.join(d, '.abandonned.txt')) == 1
assert cs.main('--check-hidden', d) == 1
assert cs.main('--check-hidden', '--check-filenames', assert cs.main('--check-hidden', '--check-filenames',
op.join(d, '.abandonned.txt')) == 2 op.join(d, '.abandonned.txt')) == 2
assert cs.main('--check-hidden', '--check-filenames', d) == 2
# hidden directory
assert cs.main(d) == 0
assert cs.main('--check-hidden', d) == 1
assert cs.main('--check-hidden', '--check-filenames', d) == 2
os.mkdir(op.join(d, '.abandonned'))
copyfile(op.join(d, '.abandonned.txt'),
op.join(d, '.abandonned', 'abandonned.txt'))
assert cs.main(d) == 0
assert cs.main('--check-hidden', d) == 2
assert cs.main('--check-hidden', '--check-filenames', d) == 5
def test_case_handling(tmpdir, capsys): def test_case_handling(tmpdir, capsys):
"""Test that capitalized entries get detected properly.""" """Test that capitalized entries get detected properly."""
# Some simple Unicode things # Some simple Unicode things
with open(op.join(str(tmpdir), 'tmp'), 'w') as f: with open(op.join(str(tmpdir), 'tmp'), 'w') as f:
pass pass
# with CaptureStdout() as sio: # with CaptureStdout() as sio:
assert cs.main(f.name) == 0 assert cs.main(f.name) == 0
try: with open(f.name, 'wb') as f:
with open(f.name, 'wb') as f: f.write('this has an ACII error'.encode('utf-8'))
f.write('this has an ACII error'.encode('utf-8')) code, stdout, _ = cs.main(f.name, std=True)
assert cs.main(f.name) == 1 assert code == 1
assert 'ASCII' in capsys.readouterr()[0] assert 'ASCII' in stdout
assert cs.main('-w', f.name) == 0 code, _, stderr = cs.main('-w', f.name, std=True)
assert 'FIXED' in capsys.readouterr()[1] assert code == 0
with open(f.name, 'rb') as f: assert 'FIXED' in stderr
assert f.read().decode('utf-8') == 'this has an ASCII error' with open(f.name, 'rb') as f:
finally: assert f.read().decode('utf-8') == 'this has an ASCII error'
os.remove(f.name)
def test_context(tmpdir, capsys): def test_context(tmpdir, capsys):
"""Test context options.""" """Test context options."""
d = str(tmpdir) d = str(tmpdir)
with open(op.join(d, 'context.txt'), 'w') as f: with open(op.join(d, 'context.txt'), 'w') as f:
f.write('line 1\nline 2\nline 3 abandonned\nline 4\nline 5') f.write('line 1\nline 2\nline 3 abandonned\nline 4\nline 5')
# symmetric context, fully within file # symmetric context, fully within file
cs.main('-C', '1', d) code, stdout, _ = cs.main('-C', '1', d, std=True)
lines = capsys.readouterr()[0].split('\n') assert code == 1
lines = stdout.split('\n')
assert len(lines) == 5 assert len(lines) == 5
assert lines[0] == ': line 2' assert lines[0] == ': line 2'
assert lines[1] == '> line 3 abandonned' assert lines[1] == '> line 3 abandonned'
assert lines[2] == ': line 4' assert lines[2] == ': line 4'
# requested context is bigger than the file # requested context is bigger than the file
cs.main('-C', '10', d) code, stdout, _ = cs.main('-C', '10', d, std=True)
lines = capsys.readouterr()[0].split('\n') assert code == 1
lines = stdout.split('\n')
assert len(lines) == 7 assert len(lines) == 7
assert lines[0] == ': line 1' assert lines[0] == ': line 1'
assert lines[1] == ': line 2' assert lines[1] == ': line 2'
assert lines[2] == '> line 3 abandonned' assert lines[2] == '> line 3 abandonned'
assert lines[3] == ': line 4' assert lines[3] == ': line 4'
assert lines[4] == ': line 5' assert lines[4] == ': line 5'
# only before context # only before context
cs.main('-B', '2', d) code, stdout, _ = cs.main('-B', '2', d, std=True)
lines = capsys.readouterr()[0].split('\n') assert code == 1
lines = stdout.split('\n')
assert len(lines) == 5 assert len(lines) == 5
assert lines[0] == ': line 1' assert lines[0] == ': line 1'
assert lines[1] == ': line 2' assert lines[1] == ': line 2'
assert lines[2] == '> line 3 abandonned' assert lines[2] == '> line 3 abandonned'
# only after context # only after context
cs.main('-A', '1', d) code, stdout, _ = cs.main('-A', '1', d, std=True)
lines = capsys.readouterr()[0].split('\n') assert code == 1
lines = stdout.split('\n')
assert len(lines) == 4 assert len(lines) == 4
assert lines[0] == '> line 3 abandonned' assert lines[0] == '> line 3 abandonned'
assert lines[1] == ': line 4' assert lines[1] == ': line 4'
# asymmetric context # asymmetric context
cs.main('-B', '2', '-A', '1', d) code, stdout, _ = cs.main('-B', '2', '-A', '1', d, std=True)
lines = capsys.readouterr()[0].split('\n') assert code == 1
lines = stdout.split('\n')
assert len(lines) == 6 assert len(lines) == 6
assert lines[0] == ': line 1' assert lines[0] == ': line 1'
assert lines[1] == ': line 2' assert lines[1] == ': line 2'
assert lines[2] == '> line 3 abandonned' assert lines[2] == '> line 3 abandonned'
assert lines[3] == ': line 4' assert lines[3] == ': line 4'
# both '-C' and '-A' on the command line # both '-C' and '-A' on the command line
cs.main('-C', '2', '-A', '1', d) code, stdout, _ = cs.main('-C', '2', '-A', '1', d, std=True)
lines = capsys.readouterr()[0].split('\n') assert code == EX_USAGE
lines = stdout.split('\n')
assert 'ERROR' in lines[0] assert 'ERROR' in lines[0]
# both '-C' and '-B' on the command line # both '-C' and '-B' on the command line
cs.main('-C', '2', '-B', '1', d) code, stdout, stderr = cs.main('-C', '2', '-B', '1', d, std=True)
lines = capsys.readouterr()[0].split('\n') assert code == EX_USAGE
lines = stdout.split('\n')
assert 'ERROR' in lines[0] assert 'ERROR' in lines[0]
def test_ignore_regex_flag(tmpdir, capsys):
"""Test ignore regex flag functionality."""
d = str(tmpdir)
# Invalid regex.
code, stdout, _ = cs.main('--ignore-regex=(', std=True)
assert code == EX_USAGE
assert 'usage:' in stdout
with open(op.join(d, 'flag.txt'), 'w') as f:
f.write('# Please see http://example.com/abandonned for info\n')
# Test file has 1 invalid entry, and it's not ignored by default.
assert cs.main(f.name) == 1
# An empty regex is the default value, and nothing is ignored.
assert cs.main(f.name, '--ignore-regex=') == 1
assert cs.main(f.name, '--ignore-regex=""') == 1
# Non-matching regex results in nothing being ignored.
assert cs.main(f.name, '--ignore-regex=^$') == 1
# A word can be ignored.
assert cs.main(f.name, '--ignore-regex=abandonned') == 0
# Ignoring part of the word can result in odd behavior.
assert cs.main(f.name, '--ignore-regex=nn') == 0
with open(op.join(d, 'flag.txt'), 'w') as f:
f.write('abandonned donn\n')
# Test file has 2 invalid entries.
assert cs.main(f.name) == 2
# Ignoring donn breaks them both.
assert cs.main(f.name, '--ignore-regex=donn') == 0
# Adding word breaks causes only one to be ignored.
assert cs.main(f.name, r'--ignore-regex=\Wdonn\W') == 1
def test_config(tmpdir, capsys):
"""
Tests loading options from a config file.
"""
d = str(tmpdir)
# Create sample files.
with open(op.join(d, 'bad.txt'), 'w') as f:
f.write('abandonned donn\n')
with open(op.join(d, 'good.txt'), 'w') as f:
f.write("good")
# Create a config file.
conffile = op.join(d, 'config.cfg')
with open(conffile, 'w') as f:
f.write(
'[codespell]\n'
'skip = bad.txt\n'
'count = \n'
)
# Should fail when checking both.
code, stdout, _ = cs.main(d, count=True, std=True)
# Code in this case is not exit code, but count of misspellings.
assert code == 2
assert 'bad.txt' in stdout
# Should pass when skipping bad.txt
code, stdout, _ = cs.main('--config', conffile, d, count=True, std=True)
assert code == 0
assert 'bad.txt' not in stdout
@contextlib.contextmanager @contextlib.contextmanager
def FakeStdin(text): def FakeStdin(text):
if sys.version[0] == '2': if sys.version[0] == '2':
from StringIO import StringIO from StringIO import StringIO
else: else:
from io import StringIO from io import StringIO
oldin = sys.stdin oldin = sys.stdin
try: try:
in_ = StringIO(text) in_ = StringIO(text)
sys.stdin = in_ sys.stdin = in_
 End of changes. 42 change blocks. 
100 lines changed or deleted 243 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)