def test_always_report(capsys): logging.getLogger().setLevel(logging.INFO) with patch.object(sys, "stdin", io.StringIO(DIFF_OUTPUT)), \ NamedTemporaryFile() as conf: conf.write(b""" [main] debug=True [pylint] always_report=W0613 """) conf.flush() sys.argv = ["whatever"] with patch("lint_diffs.USER_CONFIG", conf.name): try: main() except SystemExit as ex: assert ex.code != 0 cap = capsys.readouterr() log.info(cap.out) assert 'W0613' in cap.out assert 'E0602' in cap.out
def test_not_strict(): sys.argv = ["whatever", "-o", "pylint:command=no-such-command"] with patch.object(sys, "stdin", io.StringIO(DIFF_OUTPUT)), patch("sys.exit") as exited: main() exited.assert_not_called()
def test_custom_opts(caplog): sys.argv = [ "whatever", "--debug", "-o", "txt:extensions=.txt", "-o", "txt:command=echo hi", "-o", r"txt:regex=(?P<file>[^:]+):(?P<line>\d+):[^:]+: (?P<err>[^ :]+)" ] with patch.object(sys, "stdin", io.StringIO(NOT_LINT_TXT_DIFF)): main() assert "echo" in caplog.text
def test_parallel(capsys): sys.argv = ["whatever", "--parallel", "2", "-o", "flake8:extensions=.py"] with patch.object(sys, "stdin", io.StringIO(DIFF_OUTPUT)), patch("sys.exit") as exited: main() cap = capsys.readouterr() assert 'E0602' in cap.out # pylint assert 'F821' in cap.out # flake8
def test_clang(): sys.argv = ["whatever", "-o", "clang-tidy:extensions=.cpp .hpp"] class Ret: # pylint: disable=all stdout = CLANG_OUTPUT returncode = 1 def __init__(self, *a, **k): pass with patch.object(sys, "stdin", io.StringIO(DIFF_CPP)), patch( "subprocess.run", Ret), patch("sys.exit") as exited: main() exited.assert_called_once_with(1)
def test_noline_noerr(capsys): logging.getLogger().setLevel(logging.INFO) # One bad code line, but not in diff pylint_output = """************* Module badcode test/badcode.py:1:10: E0602: Undefined variable 'bar' (undefined-variable) ---------------------------------------------------------------------- Your code has been rated at -40.00/10 (previous run: -40.00/10, +0.00)""" class Ret: # pylint: disable=all stdout = pylint_output returncode = 1 def __init__(self, *a, **k): pass with patch.object(sys, "stdin", io.StringIO(DIFF_OUTPUT)): with patch("subprocess.run", Ret): sys.argv = ["whatever"] main() cap = capsys.readouterr() assert "test/badcode.py" not in cap.out
def test_output_file(): logging.getLogger().setLevel(logging.INFO) with patch.object(sys, "stdin", io.StringIO(DIFF_OUTPUT)), \ NamedTemporaryFile() as conf: conf.write(b""" [main] debug=True [pylint] always_report=W0613 output_file=./outtest.lint """) conf.flush() sys.argv = ["whatever"] with patch("lint_diffs.USER_CONFIG", conf.name): try: main() except SystemExit as ex: assert ex.code != 0 assert path.isfile('./outtest.lint') with open('./outtest.lint') as outfile: content = outfile.read() assert 'W0613' in content
def test_debug_mode(caplog): sys.argv = ["whatever", "--debug"] with patch.object(sys, "stdin", io.StringIO(GOOD_DIFF_OUTPUT)): main() assert "DEBUG" in caplog.text
def test_nolint_txt(caplog): with patch.object(sys, "stdin", io.StringIO(NOT_LINT_TXT_DIFF)): sys.argv = ["whatever", "--debug"] main() assert "no files need linting" in caplog.text