def test_exclude(): if hasattr(os, 'mkfifo') and not os.path.exists('test/project1/testfifo'): os.mkfifo('test/project1/testfifo') cfg = get_default_config() cfg['exclude'] = [ r'invalid', r'\.pyc$', r'project1/module.+$', ] finder = Finder('test/project1', cfg) expected = sorted( fix_paths([ 'data/broken.json', 'data/broken.po', 'data/broken.pot', 'data/broken.rst', 'data/broken.yaml', 'input.yaml', 'pyproject.toml', 'setup.cfg', 'setup.py', 'project1/__init__.py', 'project1/broken.py', 'project1/koi8r.py', 'project1/utf8.py', 'project1b/__init__.py', ])) actual = sorted( [os.path.relpath(f, 'test/project1') for f in finder.files()]) assert expected == actual
def test_execute_tools(capsys): cfg = get_default_config() cfg['pycodestyle']['use'] = False expected_tools = sorted(get_tools()) expected_tools.remove('pycodestyle') progress = QuietProgress() collector = execute_tools(cfg, 'test/project1', progress=progress) assert isinstance(collector, Collector) assert [] == sorted(progress.current_tools) assert expected_tools == sorted(progress.completed_tools) out, err = capsys.readouterr() assert out == '' assert err == '' for tool in get_tools(): cfg[tool]['use'] = False progress = QuietProgress() collector = execute_tools(cfg, 'test/project1', progress=progress) assert isinstance(collector, Collector) assert [] == sorted(progress.current_tools) assert [] == sorted(progress.completed_tools) assert collector.issue_count() == 0 assert collector.issue_count(include_unclean=True) == 0 out, err = capsys.readouterr() assert out == '' assert err == ''
def test_console_execute(capsys): cfg = get_default_config() cfg['reports'] = [{'type': 'console'}] collector = Collector(cfg) execute_reports(cfg, 'someproject', collector) expected = u'No issues found!' if sys.platform != 'win32': expected = u'\u2714 ' + expected out, err = capsys.readouterr() assert out.rstrip() == expected assert err == '' collector.add_issues(ISSUES) execute_reports(cfg, 'someproject', collector) expected = EXPECTED_CONSOLE.rstrip() if sys.platform == 'win32': expected = expected.replace(u'\u2717 ', '') if PY2: expected = expected.replace('\n', '\r\n') out, err = capsys.readouterr() assert out.rstrip() == expected assert err == ''
def test_csv_file_output(capsys, tmpdir): target_dir = tmpdir.mkdir('reports') cfg = get_default_config() cfg['requested_reports'] = [{'type': 'csv'}] collector = Collector(cfg) collector.add_issues(ISSUES) test_file = str(target_dir) + 'test1' with open(test_file, 'w') as fp: execute_reports(cfg, 'someproject', collector, output_file=fp) out, err = capsys.readouterr() assert out == '' assert err == '' assert EXPECTED_CSV == open(test_file, 'r').read() test_file = str(target_dir) + 'test2' cfg['requested_reports'] = [{'type': 'csv', 'file': test_file}] execute_reports(cfg, 'someproject', collector) out, err = capsys.readouterr() assert out == '' assert err == '' assert EXPECTED_CSV == open(test_file, 'r').read()
def test_execute_reports(capsys): cfg = get_default_config() cfg['requested_reports'] = [ { 'type': 'null' }, { 'type': 'doesntexist' }, ] collector = execute_tools(cfg, 'test/project1') executed = [] def on_finish(report): executed.append(report['type']) execute_reports(cfg, 'test/project1', collector, on_report_finish=on_finish) assert ['null'] == sorted(executed) out, err = capsys.readouterr() assert out == '' assert err == ''
def test_get_grouped_issues_custom(): collector = Collector(get_default_config()) expected = { 'test': [ BarIssue('test', 'test message', 'test/file.ext', 2), FooIssue('test', 'test message', 'test/file.ext', 2), ], 'test2': [ BarIssue('test2', 'test message', 'test/file.ext', 2), BarIssue('test2', 'test message', 'test/file.ext', 5), FooIssue('test2', 'test message', 'test/file.ext', 2), FooIssue('test2', 'test message', 'test/file.ext', 5), ], } shuffled = [] for issues in expected.values(): shuffled += issues random.shuffle(shuffled) collector.add_issues(shuffled) assert collector.get_grouped_issues(lambda x: x.code, ('tool', 'line')) == expected assert collector.issue_count() == len(shuffled) assert collector.issue_count(include_unclean=True) == len(shuffled)
def test_sort_issues_default(): collector = Collector(get_default_config()) expected = [ BarIssue('test', 'test message', 'test/file.ext', 2), BarIssue('test2', 'test message', 'test/file.ext', 2), FooIssue('test', 'test message', 'test/file.ext', 2), FooIssue('test2', 'test message', 'test/file.ext', 2), BarIssue('test', 'test message', 'test/file.ext', 2, 5), BarIssue('test2', 'test message', 'test/file.ext', 2, 5), FooIssue('test', 'test message', 'test/file.ext', 2, 5), FooIssue('test2', 'test message', 'test/file.ext', 2, 5), BarIssue('test', 'test message', 'test/file.ext', 5, 3), BarIssue('test2', 'test message', 'test/file.ext', 5, 3), FooIssue('test', 'test message', 'test/file.ext', 5, 3), FooIssue('test2', 'test message', 'test/file.ext', 5, 3), BarIssue('test', 'test message', 'test/file2.ext', 2), BarIssue('test2', 'test message', 'test/file2.ext', 2), FooIssue('test', 'test message', 'test/file2.ext', 2), FooIssue('test2', 'test message', 'test/file2.ext', 2), BarIssue('test', 'test message', 'test/file2.ext', 2, 5), BarIssue('test2', 'test message', 'test/file2.ext', 2, 5), FooIssue('test', 'test message', 'test/file2.ext', 2, 5), FooIssue('test2', 'test message', 'test/file2.ext', 2, 5), BarIssue('test', 'test message', 'test/file2.ext', 5, 3), BarIssue('test2', 'test message', 'test/file2.ext', 5, 3), FooIssue('test', 'test message', 'test/file2.ext', 5, 3), FooIssue('test2', 'test message', 'test/file2.ext', 5, 3), ] shuffled = [] + expected random.shuffle(shuffled) assert collector._sort_issues(shuffled) == expected
def test_topmost_directories(): cfg = get_default_config() finder = Finder('test/project1', cfg) if sys.platform == 'win32': expected = sorted([ 'c:\\foo\\bar', 'c:\\else\\where', ]) actual = sorted( finder.topmost_directories([ 'c:\\foo\\bar', 'c:\\foo\\bar\\baz', 'c:\\foo\\bar\\blah', 'c:\\foo\\bar\\blah\\a\\b', 'c:\\else\\where', ])) else: expected = sorted([ '/foo/bar', '/else/where', ]) actual = sorted( finder.topmost_directories([ '/foo/bar', '/foo/bar/baz', '/foo/bar/blah', '/foo/bar/blah/a/b', '/else/where' ])) assert expected == actual assert finder.topmost_directories([]) == [] assert finder.topmost_directories(None) == []
def test_noqa_disabled(tmpdir): project_dir = tmpdir.mkdir('noqa') py_file = project_dir.join('file.py') py_file.write( '\nsomething # noqa: test1\n\n# NoQA\n\n\n\n# NOqa: tidypy:test6,foobar,@bar' ) yaml_file = project_dir.join('file.yaml') cfg = get_default_config() cfg['merge-issues'] = False cfg['noqa'] = False collector = Collector(cfg) good_issues = [ TidyPyIssue('test', 'test message', str(py_file), 2), TidyPyIssue('test4', 'test message', str(py_file), 6), TidyPyIssue('test5', 'test message', str(yaml_file), 2), TidyPyIssue('test7', 'test message', str(py_file), 8), BarIssue('test8', 'test message', str(py_file), 7), ] filtered_issues = [ TidyPyIssue('test1', 'test message', str(py_file), 2), TidyPyIssue('test2', 'test message', str(py_file), 4), TidyPyIssue('test3', 'test message', str(py_file), 4), TidyPyIssue('test6', 'test message', str(py_file), 8), BarIssue('test9', 'test message', str(py_file), 8), ] collector.add_issues(good_issues) collector.add_issues(filtered_issues) assert good_issues + filtered_issues == collector.get_issues( sortby=collector.NO_SORT) assert collector.issue_count() == len(good_issues + filtered_issues) assert collector.issue_count(include_unclean=True) == len(good_issues + filtered_issues)
def test_quiet_progress(capsys): cfg = get_default_config() for tool in get_tools(): cfg[tool]['use'] = tool in ('pylint', 'pycodestyle') progress = QuietProgress() progress.on_start() progress.on_tool_start('pylint') assert progress.current_tools == ['pylint'] assert progress.completed_tools == [] progress.on_tool_start('pycodestyle') assert progress.current_tools == ['pylint', 'pycodestyle'] assert progress.completed_tools == [] progress.on_tool_finish('pycodestyle') assert progress.current_tools == ['pylint'] assert progress.completed_tools == ['pycodestyle'] progress.on_tool_finish('pylint') assert progress.current_tools == [] assert progress.completed_tools == ['pycodestyle', 'pylint'] progress.on_finish() out, err = capsys.readouterr() assert out == '' assert err == ''
def test_console_progress(capsys): cfg = get_default_config() for tool in get_tools(): cfg[tool]['use'] = tool in ('pylint', 'pycodestyle') progress = ConsoleProgress(cfg) progress.on_start() progress.on_tool_start('pylint') assert progress.current_tools == ['pylint'] assert progress.completed_tools == [] assert progress.currently_executing == '[pylint]' progress.on_tool_start('pycodestyle') assert progress.current_tools == ['pylint', 'pycodestyle'] assert progress.completed_tools == [] assert progress.currently_executing == '[pylint, pycodestyle]' progress.on_tool_finish('pycodestyle') progress.on_tool_finish('pycodestyle') assert progress.current_tools == ['pylint'] assert progress.completed_tools == ['pycodestyle'] assert progress.currently_executing == '[pylint]' progress.on_tool_finish('pylint') assert progress.current_tools == [] assert progress.completed_tools == ['pycodestyle', 'pylint'] assert progress.currently_executing == '' progress.on_finish()
def test_read_file(): cfg = get_default_config() finder = Finder('test/project1', cfg) expected = "# -*- coding: utf-8 -*-\n\ntest = 'ҖՄڇឈ'\n\n" assert expected == finder.read_file('test/project1/project1/utf8.py').replace('\r\n', '\n') expected = "# -*- coding: koi8-r -*-\n\ntest = '©©© ©©©©©© ©©©©©©©©©©©'\n\n#foo = 1\n\n" assert expected == finder.read_file('test/project1/project1/koi8r.py').replace('\r\n', '\n')
def test_get_project_config_default(tmpdir, monkeypatch): user_dir = tmpdir.mkdir('nix') monkeypatch.setattr(sys, 'platform', 'darwin') def mockreturn(path): return path.replace('~', str(user_dir)) monkeypatch.setattr(os.path, 'expanduser', mockreturn) local_dir = tmpdir.mkdir('local') actual = get_project_config(str(local_dir)) assert actual == get_default_config()
def test_execute_parseable(capsys): cfg = get_default_config() cfg['requested_reports'] = [{'type': 'pylint-parseable'}] collector = Collector(cfg) collector.add_issues(ISSUES) execute_reports(cfg, 'someproject', collector) out, err = capsys.readouterr() assert out.replace('\r\n', '\n') == EXPECTED_PARSEABLE assert err == ''
def test_sys_paths(): cfg = get_default_config() finder = Finder('test/project1', cfg) expected = sorted([ '.', ]) actual = sorted( [os.path.relpath(f, 'test/project1') for f in finder.sys_paths()]) assert expected == actual
def test_sort_issues_empty(): collector = Collector(get_default_config()) expected = [ BarIssue('test', 'test message', 'test/file.ext', 2), BarIssue('test2', 'test message', 'test/file.ext', 2), BarIssue('test2', 'test message', 'test/file.ext', 5), FooIssue('test', 'test message', 'test/file.ext', 2), FooIssue('test2', 'test message', 'test/file.ext', 2), FooIssue('test2', 'test message', 'test/file.ext', 5), ] assert collector._sort_issues(expected, collector.NO_SORT) == expected
def test_execute(capsys): cfg = get_default_config() cfg['reports'] = [{'type': 'pycodestyle'}] collector = Collector(cfg) collector.add_issues(ISSUES) execute_reports(cfg, 'someproject', collector) out, err = capsys.readouterr() assert out.replace('\r\n', '\n') == EXPECTED_PYCODESTYLE assert err == ''
def test_json_execute(capsys): cfg = get_default_config() cfg['requested_reports'] = [{'type': 'json'}] collector = Collector(cfg) collector.add_issues(ISSUES) execute_reports(cfg, 'someproject', collector) out, err = capsys.readouterr() assert EXPECTED_JSON == out.replace('\r\n', '\n') assert err == ''
def test_disabled(): cfg = get_default_config() cfg['disabled'] = ['foo'] collector = Collector(cfg) collector.add_issues([ TidyPyIssue('code1', 'message 1', 'test1.py', 2), TidyPyIssue('foo', 'message 2', 'test2.py', 3), ]) assert collector.issue_count() == 1 assert collector.issue_count(include_unclean=True) == 2 assert collector.get_issues()[0].code == 'code1'
def test_packages(): cfg = get_default_config() finder = Finder('test/project1', cfg) expected = sorted(fix_paths([ 'project1', 'project1b', ])) actual = sorted( [os.path.relpath(f, 'test/project1') for f in finder.packages()]) assert expected == actual
def test_directories_filters(): cfg = get_default_config() finder = Finder('test/project1', cfg) expected = sorted(fix_paths([ 'data', ])) actual = sorted([ os.path.relpath(f, 'test/project1') for f in finder.directories(filters=[r'data']) ]) assert expected == actual
def test_modules_filters(): cfg = get_default_config() finder = Finder('test/project1', cfg) expected = sorted(fix_paths([ 'project1/module1.py', ])) actual = sorted([ os.path.relpath(f, 'test/project1') for f in finder.modules(filters=[r'module1']) ]) assert expected == actual
def test_merging_dupes(): collector = Collector(get_default_config()) issues = [ TidyPyIssue('test', 'test message', 'test/file.ext', 2), TidyPyIssue('test', 'test message', 'test/file.ext', 2), TidyPyIssue('test2', 'test message', 'test/file.ext', 2), TidyPyIssue('test', 'test message', 'test/file.ext', 3), TidyPyIssue('test2', 'test message', 'test/file2.ext', 2), ] collector.add_issues(issues) assert collector.get_issues() == [issues[0]] + issues[2:] assert collector.issue_count() == 4 assert collector.issue_count(include_unclean=True) == 5
def test_directories_containing(): cfg = get_default_config() finder = Finder('test/project1', cfg) expected = sorted(fix_paths([ 'project1', ])) actual = sorted([ os.path.relpath(f, 'test/project1') for f in finder.directories(containing=[r'module1.py']) ]) assert expected == actual
def test_basics(): collector = Collector(get_default_config()) assert collector.get_issues() == [] assert collector.issue_count() == 0 assert collector.issue_count(include_unclean=True) == 0 collector.add_issues(FooIssue('test', 'test message', 'test/file.ext', 1), ) collector.add_issues([ FooIssue('test2', 'test message 2', 'test/file.ext', 2), BarIssue('test3', 'test message 3', 'test/file.ext', 3), ]) assert collector.issue_count() == 3 assert collector.issue_count(include_unclean=True) == 3
def test_disabled(): cfg = get_default_config() cfg['disabled'] = ['test'] collector = Collector(cfg) issues = [ TidyPyIssue('test', 'test message', 'test/file.ext', 2), TidyPyIssue('test2', 'test message', 'test/file.ext', 2), TidyPyIssue('test3', 'test message', 'test/file.ext', 2), ] collector.add_issues(issues) assert collector.get_issues() == issues[1:] assert collector.issue_count() == 2 assert collector.issue_count(include_unclean=True) == 3
def test_get_default_config(): actual = get_default_config() assert actual['exclude'] == [] assert actual['merge-issues'] == True assert isinstance(actual['workers'], int) assert actual['workers'] >= 1 assert actual['workers'] <= 4 assert actual['disabled'] == [] assert actual['noqa'] == True assert actual['extends'] == [] assert actual['ignore-missing-extends'] == False for tool in get_tools().keys(): assert tool in actual
def test_modules(): cfg = get_default_config() finder = Finder('test/project1', cfg) expected = sorted( fix_paths([ 'project1/__init__.py', 'project1/broken.py', 'project1/koi8r.py', 'project1/module1.py', 'project1/module2.py', 'project1/utf8.py', 'project1b/__init__.py', 'setup.py' ])) actual = sorted( [os.path.relpath(f, 'test/project1') for f in finder.modules()]) assert expected == actual
def test_sort_issues_custom(): collector = Collector(get_default_config()) expected = [ BarIssue('test', 'test message', 'test/file.ext', 2), BarIssue('test2', 'test message', 'test/file.ext', 2), BarIssue('test2', 'test message', 'test/file.ext', 5), FooIssue('test', 'test message', 'test/file.ext', 2), FooIssue('test2', 'test message', 'test/file.ext', 2), FooIssue('test2', 'test message', 'test/file.ext', 5), ] shuffled = [] + expected random.shuffle(shuffled) assert collector._sort_issues(shuffled, ('tool', 'code', 'line')) == expected
def test_directories(): cfg = get_default_config() cfg['exclude'] = ['project1b'] finder = Finder('test/project1', cfg) expected = sorted(fix_paths([ '.', 'data', 'project1', ])) actual = sorted( [os.path.relpath(f, 'test/project1') for f in finder.directories()]) assert expected == actual