"Fossies" - the Fresh Open Source Software Archive

Member "codespell-1.17.1/codespell_lib/tests/test_basic.py" (22 May 2020, 12742 Bytes) of package /linux/misc/codespell-1.17.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_basic.py": 1.16.0_vs_1.17.1.

    1 # -*- coding: utf-8 -*-
    2 
    3 from __future__ import print_function
    4 
    5 import contextlib
    6 import os
    7 import os.path as op
    8 import subprocess
    9 import sys
   10 
   11 import codespell_lib as cs
   12 
   13 
   14 def run_codespell(args=(), cwd=None):
   15     """Helper to run codespell"""
   16     return subprocess.Popen(
   17         ['codespell'] + list(args), cwd=cwd,
   18         stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait()
   19 
   20 
   21 def test_command(tmpdir):
   22     """Test running the codespell executable"""
   23     # With no arguments does "."
   24     d = str(tmpdir)
   25     assert run_codespell(cwd=d) == 0
   26     with open(op.join(d, 'bad.txt'), 'w') as f:
   27         f.write('abandonned\nAbandonned\nABANDONNED\nAbAnDoNnEd')
   28     assert run_codespell(cwd=d) == 4
   29 
   30 
   31 def test_basic(tmpdir, capsys):
   32     """Test some basic functionality"""
   33     assert cs.main('_does_not_exist_') == 0
   34     fname = op.join(str(tmpdir), 'tmp')
   35     with open(fname, 'w') as f:
   36         pass
   37     assert cs.main('-D', 'foo', f.name) == 1, 'missing dictionary'
   38     assert 'cannot find dictionary' in capsys.readouterr()[1]
   39     assert cs.main(fname) == 0, 'empty file'
   40     with open(fname, 'a') as f:
   41         f.write('this is a test file\n')
   42     assert cs.main(fname) == 0, 'good'
   43     with open(fname, 'a') as f:
   44         f.write('abandonned\n')
   45     assert cs.main(fname) == 1, 'bad'
   46     with open(fname, 'a') as f:
   47         f.write('abandonned\n')
   48     assert cs.main(fname) == 2, 'worse'
   49     with open(fname, 'a') as f:
   50         f.write('tim\ngonna\n')
   51     assert cs.main(fname) == 2, 'with a name'
   52     assert cs.main('--builtin', 'clear,rare,names,informal', fname) == 4
   53     capsys.readouterr()
   54     assert cs.main(fname, '--builtin', 'foo') == 1  # bad type sys.exit(1)
   55     stdout = capsys.readouterr()[1]
   56     assert 'Unknown builtin dictionary' in stdout
   57     d = str(tmpdir)
   58     assert cs.main(fname, '-D', op.join(d, 'foo')) == 1  # bad dict
   59     stdout = capsys.readouterr()[1]
   60     assert 'cannot find dictionary' in stdout
   61     os.remove(fname)
   62 
   63     with open(op.join(d, 'bad.txt'), 'w') as f:
   64         f.write('abandonned\nAbandonned\nABANDONNED\nAbAnDoNnEd')
   65     assert cs.main(d) == 4
   66     capsys.readouterr()
   67     assert cs.main('-w', d) == 0
   68     assert 'FIXED:' in capsys.readouterr()[1]
   69     with open(op.join(d, 'bad.txt')) as f:
   70         new_content = f.read()
   71     assert cs.main(d) == 0
   72     assert new_content == 'abandoned\nAbandoned\nABANDONED\nabandoned'
   73 
   74     with open(op.join(d, 'bad.txt'), 'w') as f:
   75         f.write('abandonned abandonned\n')
   76     assert cs.main(d) == 2
   77     capsys.readouterr()
   78     assert cs.main('-q', '16', '-w', d) == 0
   79     assert capsys.readouterr() == ('', '')
   80     assert cs.main(d) == 0
   81 
   82     # empty directory
   83     os.mkdir(op.join(d, 'test'))
   84     assert cs.main(d) == 0
   85 
   86 
   87 def test_interactivity(tmpdir, capsys):
   88     """Test interaction"""
   89     # Windows can't read a currently-opened file, so here we use
   90     # NamedTemporaryFile just to get a good name
   91     with open(op.join(str(tmpdir), 'tmp'), 'w') as f:
   92         pass
   93     try:
   94         assert cs.main(f.name) == 0, 'empty file'
   95         with open(f.name, 'w') as f:
   96             f.write('abandonned\n')
   97         assert cs.main('-i', '-1', f.name) == 1, 'bad'
   98         with FakeStdin('y\n'):
   99             assert cs.main('-i', '3', f.name) == 1
  100         with FakeStdin('n\n'):
  101             assert cs.main('-w', '-i', '3', f.name) == 0
  102         assert '==>' in capsys.readouterr()[0]
  103         with FakeStdin('x\ny\n'):
  104             assert cs.main('-w', '-i', '3', f.name) == 0
  105         assert cs.main(f.name) == 0
  106     finally:
  107         os.remove(f.name)
  108 
  109     # New example
  110     with open(op.join(str(tmpdir), 'tmp2'), 'w') as f:
  111         pass
  112     try:
  113         with open(f.name, 'w') as f:
  114             f.write('abandonned\n')
  115         assert cs.main(f.name) == 1
  116         with FakeStdin(' '):  # blank input -> Y
  117             assert cs.main('-w', '-i', '3', f.name) == 0
  118         assert cs.main(f.name) == 0
  119     finally:
  120         os.remove(f.name)
  121 
  122     # multiple options
  123     with open(op.join(str(tmpdir), 'tmp3'), 'w') as f:
  124         pass
  125     try:
  126         with open(f.name, 'w') as f:
  127             f.write('ackward\n')
  128 
  129         assert cs.main(f.name) == 1
  130         with FakeStdin(' \n'):  # blank input -> nothing
  131             assert cs.main('-w', '-i', '3', f.name) == 0
  132         assert cs.main(f.name) == 1
  133         with FakeStdin('0\n'):  # blank input -> nothing
  134             assert cs.main('-w', '-i', '3', f.name) == 0
  135         assert cs.main(f.name) == 0
  136         with open(f.name, 'r') as f_read:
  137             assert f_read.read() == 'awkward\n'
  138         with open(f.name, 'w') as f:
  139             f.write('ackward\n')
  140         assert cs.main(f.name) == 1
  141         capsys.readouterr()
  142         with FakeStdin('x\n1\n'):  # blank input -> nothing
  143             assert cs.main('-w', '-i', '3', f.name) == 0
  144         assert 'a valid option' in capsys.readouterr()[0]
  145         assert cs.main(f.name) == 0
  146         with open(f.name, 'r') as f:
  147             assert f.read() == 'backward\n'
  148     finally:
  149         os.remove(f.name)
  150 
  151 
  152 def test_summary(tmpdir, capsys):
  153     """Test summary functionality"""
  154     with open(op.join(str(tmpdir), 'tmp'), 'w') as f:
  155         pass
  156     try:
  157         cs.main(f.name)
  158         assert capsys.readouterr() == ('', ''), 'no output'
  159         cs.main(f.name, '--summary')
  160         stdout, stderr = capsys.readouterr()
  161         assert stderr == ''
  162         assert 'SUMMARY' in stdout
  163         assert len(stdout.split('\n')) == 5
  164         with open(f.name, 'w') as f:
  165             f.write('abandonned\nabandonned')
  166         cs.main(f.name, '--summary')
  167         stdout, stderr = capsys.readouterr()
  168         assert stderr == ''
  169         assert 'SUMMARY' in stdout
  170         assert len(stdout.split('\n')) == 7
  171         assert 'abandonned' in stdout.split()[-2]
  172     finally:
  173         os.remove(f.name)
  174 
  175 
  176 def test_ignore_dictionary(tmpdir):
  177     """Test ignore dictionary functionality."""
  178     d = str(tmpdir)
  179     with open(op.join(d, 'bad.txt'), 'w') as f:
  180         f.write('1 abandonned 1\n2 abandonned 2\nabondon\n')
  181     bad_name = f.name
  182     assert cs.main(bad_name) == 3
  183     with open(op.join(d, 'ignore.txt'), 'w') as f:
  184         f.write('abandonned\n')
  185     assert cs.main('-I', f.name, bad_name) == 1
  186 
  187 
  188 def test_ignore_word_list(tmpdir):
  189     """Test ignore word list functionality"""
  190     d = str(tmpdir)
  191     with open(op.join(d, 'bad.txt'), 'w') as f:
  192         f.write('abandonned\nabondon\nabilty\n')
  193     assert cs.main(d) == 3
  194     assert cs.main('-Labandonned,someword', '-Labilty', d) == 1
  195 
  196 
  197 def test_custom_regex(tmpdir, capsys):
  198     """Test custom word regex."""
  199     d = str(tmpdir)
  200     with open(op.join(d, 'bad.txt'), 'w') as f:
  201         f.write('abandonned_abondon\n')
  202     assert cs.main(d) == 0
  203     assert cs.main('-r', "[a-z]+", d) == 2
  204     capsys.readouterr()
  205     assert cs.main('-r', '[a-z]+', '--write-changes', d) == 1
  206     assert 'ERROR:' in capsys.readouterr()[0]
  207 
  208 
  209 def test_exclude_file(tmpdir):
  210     """Test exclude file functionality."""
  211     d = str(tmpdir)
  212     with open(op.join(d, 'bad.txt'), 'wb') as f:
  213         f.write('1 abandonned 1\n2 abandonned 2\n'.encode('utf-8'))
  214     bad_name = f.name
  215     assert cs.main(bad_name) == 2
  216     with open(op.join(d, 'tmp.txt'), 'wb') as f:
  217         f.write('1 abandonned 1\n'.encode('utf-8'))
  218     assert cs.main(bad_name) == 2
  219     assert cs.main('-x', f.name, bad_name) == 1
  220 
  221 
  222 def test_encoding(tmpdir, capsys):
  223     """Test encoding handling."""
  224     # Some simple Unicode things
  225     with open(op.join(str(tmpdir), 'tmp'), 'w') as f:
  226         pass
  227     # with CaptureStdout() as sio:
  228     assert cs.main(f.name) == 0
  229     try:
  230         with open(f.name, 'wb') as f:
  231             f.write(u'naïve\n'.encode('utf-8'))
  232         assert cs.main(f.name) == 0
  233         assert cs.main('-e', f.name) == 0
  234         with open(f.name, 'ab') as f:
  235             f.write(u'naieve\n'.encode('utf-8'))
  236         assert cs.main(f.name) == 1
  237         # Binary file warning
  238         with open(f.name, 'wb') as f:
  239             f.write(b'\x00\x00naiive\x00\x00')
  240         capsys.readouterr()
  241         assert cs.main(f.name) == 0
  242         assert 'WARNING: Binary file' in capsys.readouterr()[1]
  243         assert cs.main('-q', '2', f.name) == 0
  244         assert capsys.readouterr() == ('', '')
  245     finally:
  246         os.remove(f.name)
  247 
  248 
  249 def test_ignore(tmpdir):
  250     """Test ignoring of files and directories."""
  251     d = str(tmpdir)
  252     with open(op.join(d, 'good.txt'), 'w') as f:
  253         f.write('this file is okay')
  254     assert cs.main(d) == 0
  255     with open(op.join(d, 'bad.txt'), 'w') as f:
  256         f.write('abandonned')
  257     assert cs.main(d) == 1
  258     assert cs.main('--skip=bad*', d) == 0
  259     assert cs.main('--skip=bad.txt', d) == 0
  260     subdir = op.join(d, 'ignoredir')
  261     os.mkdir(subdir)
  262     with open(op.join(subdir, 'bad.txt'), 'w') as f:
  263         f.write('abandonned')
  264     assert cs.main(d) == 2
  265     assert cs.main('--skip=bad*', d) == 0
  266     assert cs.main('--skip=*ignoredir*', d) == 1
  267     assert cs.main('--skip=ignoredir', d) == 1
  268     assert cs.main('--skip=*ignoredir/bad*', d) == 1
  269 
  270 
  271 def test_check_filename(tmpdir):
  272     """Test filename check."""
  273     d = str(tmpdir)
  274     with open(op.join(d, 'abandonned.txt'), 'w') as f:
  275         f.write('.')
  276     assert cs.main('-f', d) == 1
  277 
  278 
  279 def test_check_hidden(tmpdir):
  280     """Test ignoring of hidden files."""
  281     d = str(tmpdir)
  282     # hidden file
  283     with open(op.join(d, 'test.txt'), 'w') as f:
  284         f.write('abandonned\n')
  285     assert cs.main(op.join(d, 'test.txt')) == 1
  286     os.rename(op.join(d, 'test.txt'), op.join(d, '.test.txt'))
  287     assert cs.main(op.join(d, '.test.txt')) == 0
  288     assert cs.main('--check-hidden', op.join(d, '.test.txt')) == 1
  289     os.rename(op.join(d, '.test.txt'), op.join(d, '.abandonned.txt'))
  290     assert cs.main(op.join(d, '.abandonned.txt')) == 0
  291     assert cs.main('--check-hidden', op.join(d, '.abandonned.txt')) == 1
  292     assert cs.main('--check-hidden', '--check-filenames',
  293                    op.join(d, '.abandonned.txt')) == 2
  294 
  295 
  296 def test_case_handling(tmpdir, capsys):
  297     """Test that capitalized entries get detected properly."""
  298     # Some simple Unicode things
  299     with open(op.join(str(tmpdir), 'tmp'), 'w') as f:
  300         pass
  301     # with CaptureStdout() as sio:
  302     assert cs.main(f.name) == 0
  303     try:
  304         with open(f.name, 'wb') as f:
  305             f.write('this has an ACII error'.encode('utf-8'))
  306         assert cs.main(f.name) == 1
  307         assert 'ASCII' in capsys.readouterr()[0]
  308         assert cs.main('-w', f.name) == 0
  309         assert 'FIXED' in capsys.readouterr()[1]
  310         with open(f.name, 'rb') as f:
  311             assert f.read().decode('utf-8') == 'this has an ASCII error'
  312     finally:
  313         os.remove(f.name)
  314 
  315 
  316 def test_context(tmpdir, capsys):
  317     """Test context options."""
  318     d = str(tmpdir)
  319     with open(op.join(d, 'context.txt'), 'w') as f:
  320         f.write('line 1\nline 2\nline 3 abandonned\nline 4\nline 5')
  321 
  322     # symmetric context, fully within file
  323     cs.main('-C', '1', d)
  324     lines = capsys.readouterr()[0].split('\n')
  325     assert len(lines) == 5
  326     assert lines[0] == ': line 2'
  327     assert lines[1] == '> line 3 abandonned'
  328     assert lines[2] == ': line 4'
  329 
  330     # requested context is bigger than the file
  331     cs.main('-C', '10', d)
  332     lines = capsys.readouterr()[0].split('\n')
  333     assert len(lines) == 7
  334     assert lines[0] == ': line 1'
  335     assert lines[1] == ': line 2'
  336     assert lines[2] == '> line 3 abandonned'
  337     assert lines[3] == ': line 4'
  338     assert lines[4] == ': line 5'
  339 
  340     # only before context
  341     cs.main('-B', '2', d)
  342     lines = capsys.readouterr()[0].split('\n')
  343     assert len(lines) == 5
  344     assert lines[0] == ': line 1'
  345     assert lines[1] == ': line 2'
  346     assert lines[2] == '> line 3 abandonned'
  347 
  348     # only after context
  349     cs.main('-A', '1', d)
  350     lines = capsys.readouterr()[0].split('\n')
  351     assert len(lines) == 4
  352     assert lines[0] == '> line 3 abandonned'
  353     assert lines[1] == ': line 4'
  354 
  355     # asymmetric context
  356     cs.main('-B', '2', '-A', '1', d)
  357     lines = capsys.readouterr()[0].split('\n')
  358     assert len(lines) == 6
  359     assert lines[0] == ': line 1'
  360     assert lines[1] == ': line 2'
  361     assert lines[2] == '> line 3 abandonned'
  362     assert lines[3] == ': line 4'
  363 
  364     # both '-C' and '-A' on the command line
  365     cs.main('-C', '2', '-A', '1', d)
  366     lines = capsys.readouterr()[0].split('\n')
  367     assert 'ERROR' in lines[0]
  368 
  369     # both '-C' and '-B' on the command line
  370     cs.main('-C', '2', '-B', '1', d)
  371     lines = capsys.readouterr()[0].split('\n')
  372     assert 'ERROR' in lines[0]
  373 
  374 
  375 @contextlib.contextmanager
  376 def FakeStdin(text):
  377     if sys.version[0] == '2':
  378         from StringIO import StringIO
  379     else:
  380         from io import StringIO
  381     oldin = sys.stdin
  382     try:
  383         in_ = StringIO(text)
  384         sys.stdin = in_
  385         yield
  386     finally:
  387         sys.stdin = oldin