def test_custom_should_analyze_file(): """Check that we can write custom should_analyze_file that work even for arguments. """ class CustomPyLinter(PyLinter): def should_analyze_file(self, modname, path, is_argument=False): if os.path.basename(path) == "wrong.py": return False return super(CustomPyLinter, self).should_analyze_file( modname, path, is_argument=is_argument ) package_dir = os.path.join(HERE, "regrtest_data", "bad_package") wrong_file = os.path.join(package_dir, "wrong.py") for jobs in [1, 2]: reporter = testutils.TestReporter() linter = CustomPyLinter() linter.config.jobs = jobs linter.config.persistent = 0 linter.open() linter.set_reporter(reporter) try: sys.path.append(os.path.dirname(package_dir)) linter.check([package_dir, wrong_file]) finally: sys.path.pop() messages = reporter.messages assert len(messages) == 1 assert "invalid syntax" in messages[0]
def test_multiprocessing(jobs): """Check that multiprocessing does not create duplicates.""" # For the bug (#3584) to show up we need more than one file with issues # per process filenames = [ "special_attr_scope_lookup_crash.py", "syntax_error.py", "unused_variable.py", "wildcard.py", "wrong_import_position.py", ] reporter = testutils.TestReporter() linter = PyLinter() linter.config.jobs = jobs linter.config.persistent = 0 linter.open() linter.set_reporter(reporter) try: sys.path.append(os.path.dirname(REGRTEST_DATA_DIR)) linter.check( [os.path.join(REGRTEST_DATA_DIR, fname) for fname in filenames]) finally: sys.path.pop() messages = reporter.messages assert len(messages) == len(set(messages))
def test_addmessage(linter): linter.set_reporter(testutils.TestReporter()) linter.open() linter.set_current_module('0123') linter.add_message('C0301', line=1, args=(1, 2)) linter.add_message('line-too-long', line=2, args=(3, 4)) assert ['C: 1: Line too long (1/2)', 'C: 2: Line too long (3/4)'] == \ linter.reporter.messages
def linter(): test_reporter = testutils.TestReporter() linter = lint.PyLinter() linter.set_reporter(test_reporter) linter.disable('I') linter.config.persistent = 0 checkers.initialize(linter) return linter
def linter(): linter = PyLinter() linter.disable('I') linter.config.persistent = 0 # register checkers checkers.initialize(linter) linter.set_reporter(testutils.TestReporter()) return linter
def test_addmessage(linter): linter.set_reporter(testutils.TestReporter()) linter.open() linter.set_current_module("0123") linter.add_message("C0301", line=1, args=(1, 2)) linter.add_message("line-too-long", line=2, args=(3, 4)) assert [ "C: 1: Line too long (1/2)", "C: 2: Line too long (3/4)", ] == linter.reporter.messages
def test_filename_with__init__(init_linter): # This tracks a regression where a file whose name ends in __init__.py, # such as flycheck__init__.py, would accidentally lead to linting the # entire containing directory. reporter = testutils.TestReporter() linter = init_linter linter.open() linter.set_reporter(reporter) filepath = join(INPUT_DIR, "not__init__.py") linter.check([filepath]) messages = reporter.messages assert len(messages) == 0
def test_addmessage_invalid(linter): linter.set_reporter(testutils.TestReporter()) linter.open() linter.set_current_module('0123') with pytest.raises(InvalidMessageError) as cm: linter.add_message('line-too-long', args=(1, 2)) assert str(cm.value) == "Message C0301 must provide line, got None" with pytest.raises(InvalidMessageError) as cm: linter.add_message('line-too-long', line=2, node='fake_node', args=(1, 2)) assert str(cm.value) == "Message C0301 must only provide line, got line=2, node=fake_node" with pytest.raises(InvalidMessageError) as cm: linter.add_message('C0321') assert str(cm.value) == "Message C0321 must provide Node, got None"
def test_custom_should_analyze_file(): """Check that we can write custom should_analyze_file that work even for arguments. """ package_dir = os.path.join(REGRTEST_DATA_DIR, "bad_package") wrong_file = os.path.join(package_dir, "wrong.py") for jobs in [1, 2]: reporter = testutils.TestReporter() linter = _CustomPyLinter() linter.config.jobs = jobs linter.config.persistent = 0 linter.open() linter.set_reporter(reporter) try: sys.path.append(os.path.dirname(package_dir)) linter.check([package_dir, wrong_file]) finally: sys.path.pop() messages = reporter.messages assert len(messages) == 1 assert "invalid syntax" in messages[0]
def test_analyze_explicit_script(linter): linter.set_reporter(testutils.TestReporter()) linter.check(os.path.join(DATA_DIR, "ascript")) assert ["C: 2: Line too long (175/100)"] == linter.reporter.messages
def test_analyze_explicit_script(linter): linter.set_reporter(testutils.TestReporter()) linter.check(os.path.join(os.path.dirname(__file__), "data", "ascript")) assert ["C: 2: Line too long (175/100)"] == linter.reporter.messages
def linter(): l = PyLinter(reporter=testutils.TestReporter()) initialize(l) return l