def test_show_bears(self): with retrieve_stdout() as stdout: bears = {SomelocalBear: ['default', 'test'], SomeglobalBear: ['default', 'test']} print_bears(self.log_printer.printer, bears, False) expected_string = stdout.getvalue() self.maxDiff = None with retrieve_stdout() as stdout: show_bears(self.local_bears, self.global_bears, False, self.log_printer.printer) self.assertEqual(expected_string, stdout.getvalue()) with retrieve_stdout() as stdout: show_bears(self.local_bears, self.global_bears, True, self.log_printer.printer) self.assertEqual("SomeglobalBear\n" "==============\n" "Some global-bear Description.\n" "\n" "SomelocalBear\n" "=============\n" "Some local-bear Description.\n\n", stdout.getvalue())
def test_print_bears_empty(self): with retrieve_stdout() as stdout: bears = {} print_bears(self.log_printer.printer, bears, True) self.assertEqual("No bears to show.\n", stdout.getvalue()) with retrieve_stdout() as stdout: bears = {} print_bears(self.log_printer.printer, bears, False) self.assertEqual("No bears to show.\n", stdout.getvalue())
def test_print_used_languages(self): with retrieve_stdout() as custom_stdout: print_used_languages(self.printer, [('Python', 100)]) res = custom_stdout.getvalue() self.assertIn("Python", res) self.assertIn("100%", res) with retrieve_stdout() as custom_stdout: print_used_languages(self.printer, [('Python', 75), ('C++', 25)]) self.assertIn("75%\n", custom_stdout.getvalue())
def test_static_functions(self): with retrieve_stdout() as stdout: print_section_beginning(self.console_printer, Section("name")) self.assertEqual(stdout.getvalue(), _("Executing section " "{name}...").format(name="name") + "\n") with retrieve_stdout() as stdout: nothing_done(self.console_printer) self.assertEqual(stdout.getvalue(), _("No existent section was targeted or enabled. " "Nothing to do.") + "\n")
def test_show_bears(self): with retrieve_stdout() as stdout: bears = {KeywordBear: ["default", "test"], LineLengthBear: ["test"], SomeglobalBear: ["default", "test"]} print_bears(self.log_printer.printer, bears, False) expected_string = stdout.getvalue() self.maxDiff = None with retrieve_stdout() as stdout: show_bears(self.local_bears, self.global_bears, False, self.log_printer.printer) self.assertEqual(expected_string, stdout.getvalue()) with retrieve_stdout() as stdout: show_bears(self.local_bears, self.global_bears, True, self.log_printer.printer) self.assertEqual(" * KeywordBear\n" " * LineLengthBear\n" " * SomeglobalBear\n", stdout.getvalue())
def test_show_bears(self): with retrieve_stdout() as stdout: bears = {KeywordBear: ['default', 'test'], LineLengthBear: ['test'], SomeglobalBear: ['default', 'test']} print_bears(self.log_printer.printer, bears) expected_string = stdout.getvalue() self.maxDiff = None with retrieve_stdout() as stdout: show_bears(self.local_bears, self.global_bears, self.log_printer.printer) self.assertEqual(expected_string, stdout.getvalue())
def test_acquire_actions_and_apply(self): with make_temp() as testfile_path: file_dict = {testfile_path: ["1\n", "2\n", "3\n"]} diff = Diff(file_dict[testfile_path]) diff.delete_line(2) diff.change_line(3, "3\n", "3_changed\n") with simulate_console_inputs(1, 0) as generator, \ retrieve_stdout() as sio: ApplyPatchAction.is_applicable = staticmethod( lambda *args: True) acquire_actions_and_apply(self.console_printer, self.log_printer, Section(""), self.file_diff_dict, Result("origin", "message", diffs={ testfile_path: diff}), file_dict) self.assertEqual(generator.last_input, 1) self.assertIn(ApplyPatchAction.success_message, sio.getvalue()) class InvalidateTestAction(ResultAction): is_applicable = staticmethod(lambda *args: True) def apply(*args, **kwargs): ApplyPatchAction.is_applicable = staticmethod( lambda *args: False) old_applypatch_is_applicable = ApplyPatchAction.is_applicable ApplyPatchAction.is_applicable = staticmethod(lambda *args: True) cli_actions = [ApplyPatchAction(), InvalidateTestAction()] with simulate_console_inputs(2, 1, 0) as generator, \ retrieve_stdout() as sio: acquire_actions_and_apply(self.console_printer, self.log_printer, Section(""), self.file_diff_dict, Result("origin", "message", diffs={testfile_path: diff}), file_dict, cli_actions=cli_actions) self.assertEqual(generator.last_input, 2) action_fail = "Failed to execute the action" self.assertNotIn(action_fail, sio.getvalue()) apply_path_desc = ApplyPatchAction().get_metadata().desc self.assertEqual(sio.getvalue().count(apply_path_desc), 1) ApplyPatchAction.is_applicable = old_applypatch_is_applicable
def test_print_results_sorting(self): with retrieve_stdout() as stdout: print_results(self.log_printer, Section(""), [Result("SpaceConsistencyBear", "Trailing whitespace found", "proj/white", line_nr=5), Result("SpaceConsistencyBear", "Trailing whitespace found", "proj/white", line_nr=2)], {"proj/white": ["test line\n", "line 2\n", "line 3\n", "line 4\n", "line 5\n"]}, {}, color=False) self.assertEqual("""\n\nproj/white | 1| 1| test line | 2| 2| line 2 | | | [{}] SpaceConsistencyBear: | | | Trailing whitespace found | 3| 3| line 3 | 4| 4| line 4 | 5| 5| line 5 | | | [{}] SpaceConsistencyBear: | | | Trailing whitespace found """.format(RESULT_SEVERITY.__str__(RESULT_SEVERITY.NORMAL), RESULT_SEVERITY.__str__(RESULT_SEVERITY.NORMAL)), stdout.getvalue())
def test_print_results_missing_line(self): with retrieve_stdout() as stdout: print_results( self.log_printer, Section(""), [ Result.from_values("t", "msg", file="file", line=5), Result.from_values("t", "msg", file="file", line=6), ], {abspath("file"): ["line " + str(i + 1) for i in range(5)]}, {}, color=False, ) self.assertEqual( "\n" "file\n" "| 5| line 5\n" "| | [NORMAL] t:\n" "| | msg\n" "\n" "file\n" "| | {}\n" "| | [NORMAL] t:\n" "| | msg\n".format(STR_LINE_DOESNT_EXIST), stdout.getvalue(), )
def test_show_bear_desc_only(self): with retrieve_stdout() as stdout: show_bear( SomelocalBear, True, False, self.console_printer) self.assertEqual( stdout.getvalue(), 'SomelocalBear\n Some local-bear Description.\n\n')
def test_file_invalid(self): with retrieve_stdout() as sio: self.assertEqual(collect_files(["invalid_path"], self.log_printer), []) self.assertRegex(sio.getvalue(), ".*\\[WARNING\\].*No files matching " "'invalid_path' were found.\n")
def test_print_results_sorting(self): with retrieve_stdout() as stdout: print_results(self.log_printer, Section(""), [Result.from_values("SpaceConsistencyBear", "Trailing whitespace found", file="file", line=5), Result.from_values("SpaceConsistencyBear", "Trailing whitespace found", file="file", line=2)], {abspath("file"): ["test line\n", "line 2\n", "line 3\n", "line 4\n", "line 5\n"]}, {}, color=False) self.assertEqual(""" file | 2| line 2 | | [NORMAL] SpaceConsistencyBear: | | Trailing whitespace found file | 5| line 5 | | [NORMAL] SpaceConsistencyBear: | | Trailing whitespace found """, stdout.getvalue())
def test_print_results_multiple_ranges(self): affected_code = ( SourceRange.from_values("some_file", 5, end_line=7), SourceRange.from_values("another_file", 1, 3, 1, 5), SourceRange.from_values("another_file", 3, 3, 3, 5)) with retrieve_stdout() as stdout: print_results( self.log_printer, Section(""), [Result("ClangCloneDetectionBear", "Clone Found", affected_code)], {abspath("some_file"): ["line " + str(i + 1) + "\n" for i in range(10)], abspath("another_file"): ["line " + str(i + 1) + "\n" for i in range(10)]}, {}, color=False) self.assertEqual(""" another_file | 1| line 1 another_file | 3| line 3 some_file | 5| line 5 | 6| line 6 | 7| line 7 | | [NORMAL] ClangCloneDetectionBear: | | Clone Found """, stdout.getvalue())
def test_apply_with_rename(self): with retrieve_stdout() as stdout: previous_diffs = {"a": Diff(self.file_dict['a'])} previous_diffs["a"].change_line(2, "b\n", "b_changed\n") diff_dict = {"a": Diff(self.file_dict['a'], rename="a.rename"), "b": Diff(self.file_dict['b'], delete=True)} diff_dict["a"].add_lines(1, ["test\n"]) diff_dict["a"].delete_line(3) diff_dict["b"].add_lines(0, ["first\n"]) test_result = Result("origin", "message", diffs=diff_dict) self.assertEqual(self.uut.apply_from_section(test_result, self.file_dict, previous_diffs, self.section), previous_diffs) self.assertEqual(stdout.getvalue(), "|----| | a\n" "| |++++| a.rename\n" "| 1| 1| a\n" "| | 2|+test\n" "| 2| 3| b_changed\n" "| 3| |-c\n" "|----| | b\n" "| |++++| /dev/null\n" "| 1| |-old_first\n")
def test_print_result_no_input(self): with make_temp() as testfile_path: file_dict = {testfile_path: ["1\n", "2\n", "3\n"]} diff = Diff(file_dict[testfile_path]) diff.delete_line(2) diff.change_line(3, "3\n", "3_changed\n") with simulate_console_inputs(1, 2, 3) as generator, retrieve_stdout() as stdout: ApplyPatchAction.is_applicable = staticmethod(lambda *args: True) print_results_no_input( self.log_printer, Section("someSection"), [Result("origin", "message", diffs={testfile_path: diff})], file_dict, self.file_diff_dict, color=False, ) self.assertEqual(generator.last_input, -1) self.assertEqual( stdout.getvalue(), """ Project wide: | | [NORMAL] origin: | | message """, )
def test_print_results_missing_line(self): with retrieve_stdout() as stdout: print_results( self.log_printer, Section(""), [Result.from_values("t", "msg", file="file", line=5), Result.from_values("t", "msg", file="file", line=6)], {abspath("file"): ["line " + str(i + 1) for i in range(5)]}, {}, color=False) self.assertEqual("\n" "file\n" "| 5| {0}\n" "| | [NORMAL] t:\n" "| | {1}\n" "\n" "file\n" "| 6| {2}\n" "| | [NORMAL] t:\n" "| | {1}\n".format( highlight_text('line 5', self.lexer), highlight_text("msg", style=BackgroundMessageStyle), STR_LINE_DOESNT_EXIST), stdout.getvalue())
def test_print_results_sorting(self): with retrieve_stdout() as stdout: print_results(self.log_printer, Section(""), [Result.from_values("SpaceConsistencyBear", "Trailing whitespace found", file="file", line=5), Result.from_values("SpaceConsistencyBear", "Trailing whitespace found", file="file", line=2)], {abspath("file"): ["test line\n", "\t\n", "line 3\n", "line 4\n", "line 5\t\n"]}, {}, color=False) self.assertEqual(""" file | 2| {0} | | [NORMAL] SpaceConsistencyBear: | | {1} file | 5| {2} | | [NORMAL] SpaceConsistencyBear: | | {1}\n""".format(highlight_text('\t', self.lexer), highlight_text("Trailing whitespace found", style=BackgroundMessageStyle), highlight_text('line 5\t', self.lexer)), stdout.getvalue())
def test_nonexistent_coafile(self): old_getcwd = os.getcwd os.getcwd = lambda *args: None with retrieve_stdout() as stdout: retval = coala_delete_orig.main() self.assertIn("Can only delete .orig files if ", stdout.getvalue()) self.assertEqual(retval, 255) os.getcwd = old_getcwd
def test_remove_exception(self, mock_glob): # Non existent file mock_glob.return_value = ["non_existent_file"] with retrieve_stdout() as stdout: retval = coala_delete_orig.main(section=self.section) output = stdout.getvalue() self.assertEqual(retval, 0) self.assertIn("Couldn't delete", output) # Directory instead of file with tempfile.TemporaryDirectory() as filename, \ retrieve_stdout() as stdout: mock_glob.return_value = [filename] retval = coala_delete_orig.main(section=self.section) output = stdout.getvalue() self.assertEqual(retval, 0) self.assertIn("Couldn't delete", output)
def test_default_format(self): expected_string = ( "id:-?[0-9]+:origin:1:file:None:from_line:None:" "from_column:None:to_line:None:to_column:None:" "severity:1:msg:2\n" ) with retrieve_stdout() as stdout: print_results_formatted(self.logger, self.section, [Result("1", "2")], None, None) self.assertRegex(stdout.getvalue(), expected_string)
def test_good_format(self): self.section.append(Setting("format_str", "{origin}")) with retrieve_stdout() as stdout: print_results_formatted(self.logger, self.section, [Result("1", "2")], None, None) self.assertEqual(stdout.getvalue(), "1\n")
def test_apply(self): with retrieve_stdout() as stdout: self.assertEqual(self.uut.apply_from_section(self.test_result, {}, {}, Section("name")), {}) self.assertEqual(stdout.getvalue(), self.test_result.debug_msg+"\n")
def test_print_results_for_file(self): with retrieve_stdout() as stdout: print_results( self.log_printer, Section(""), [Result.from_values("SpaceConsistencyBear", "Trailing whitespace found", file="filename", line=2)], {abspath("filename"): ["test line\n", "line 2\n", "line 3\n"]}, {}, self.console_printer) self.assertEqual("""\nfilename | 2| {} | | [NORMAL] SpaceConsistencyBear: | | {}\n""".format(highlight_text(self.no_color, 'line 2', self.lexer), highlight_text(self.no_color, "Trailing whitespace found", style=BackgroundMessageStyle)), stdout.getvalue()) with retrieve_stdout() as stdout: print_results( self.log_printer, Section(""), [Result.from_values("SpaceConsistencyBear", "Trailing whitespace found", file="filename", line=5)], {abspath("filename"): ["test line\n", "line 2\n", "line 3\n", "line 4\n", "line 5\n"]}, {}, self.console_printer) self.assertEqual("""\nfilename | 5| {} | | [NORMAL] SpaceConsistencyBear: | | {}\n""".format(highlight_text(self.no_color, 'line 5', self.lexer), highlight_text(self.no_color, "Trailing whitespace found", style=BackgroundMessageStyle)), stdout.getvalue())
def test_print_side_by_side(self): with retrieve_stdout() as custom_stdout: print_side_by_side( self.printer, ["Left side content."], ["Right side content", "that is longer than the", "left side."], limit=80) self.assertIn( "side content.\x1b[0m \x1b[34mRight side", custom_stdout.getvalue())
def test_print_results_without_line(self): with retrieve_stdout() as stdout: print_results( self.log_printer, Section(""), [Result.from_values("t", "msg", file="file")], {abspath("file"): []}, {}, color=False, ) self.assertEqual("\nfile\n" "| | [NORMAL] t:\n" "| | msg\n", stdout.getvalue())
def test_show_bear_details_only(self): with retrieve_stdout() as stdout: show_bear( SomelocalBear, [], False, True, self.console_printer) self.assertEqual(stdout.getvalue(), 'SomelocalBear\n' ' The bear does not provide information about ' 'which languages it can analyze.\n\n' ' No sections.\n\n' ' No needed settings.\n\n' ' No optional settings.\n\n')
def test_print_bears_no_sections(self): with retrieve_stdout() as stdout: bears = {SomeBear: []} print_bears(self.log_printer.printer, bears, False) expected_string = "SomeBear:\n" expected_string += " Some Description.\n\n" expected_string += " No sections.\n\n" expected_string += " No needed settings.\n\n" expected_string += " No optional settings.\n\n" self.assertEqual(expected_string, stdout.getvalue())
def test_apply_empty(self): with retrieve_stdout() as stdout: test_result = Result("origin", "message", diffs={'a': Diff([])}) file_dict = {'a': []} self.assertEqual(self.uut.apply_from_section(test_result, file_dict, {}, self.section), {}) self.assertEqual(stdout.getvalue(), '')
def test_print_results_project_wide(self): with retrieve_stdout() as stdout: print_results(self.log_printer, Section(""), [Result("origin", "message")], {}, {}, color=False) self.assertEqual( "\n{}\n| | [NORMAL] origin:\n| | message" "\n".format(STR_PROJECT_WIDE), stdout.getvalue())
def test_remove_exception(self): old_glob = Globbing.glob # Non existent file with retrieve_stdout() as stdout: Globbing.glob = lambda *args: ["non_existent_file"] retval = coala_delete_orig.main(section=self.section) output = stdout.getvalue() self.assertEqual(retval, 0) self.assertIn("Couldn't delete", output) # Directory instead of file with tempfile.TemporaryDirectory() as filename, \ retrieve_stdout() as stdout: Globbing.glob = lambda *args: [filename] retval = coala_delete_orig.main(section=self.section) output = stdout.getvalue() self.assertEqual(retval, 0) self.assertIn("Couldn't delete", output) Globbing.glob = old_glob
def test_nothing_done(self): with retrieve_stdout() as stdout: nothing_done(self.log_printer) self.assertIn( "No existent section was targeted or enabled. " "Nothing to do.\n", stdout.getvalue())
def test_print_results_empty(self): with retrieve_stdout() as stdout: print_results(self.log_printer, Section(""), [], {}, {}) self.assertEqual(stdout.getvalue(), "")
def test_show_bear_minimal(self): with retrieve_stdout() as stdout: show_bear(SomelocalBear, False, False, self.console_printer) self.assertEqual(stdout.getvalue(), 'SomelocalBear\n')
def test_show_bear_desc_only(self): with retrieve_stdout() as stdout: show_bear(SomelocalBear, True, False, self.console_printer) self.assertEqual( stdout.getvalue(), 'SomelocalBear\n Some local-bear Description.\n\n')
def test_print_section_beginning(self): with retrieve_stdout() as stdout: print_section_beginning(self.console_printer, Section('name')) self.assertEqual(stdout.getvalue(), 'Executing section name...\n')
def test_retrieve_stdout(self): with retrieve_stdout() as sio: print("test") self.assertEqual(sio.getvalue(), "test\n")
def test_show_bears_empty(self): with retrieve_stdout() as stdout: show_bears({}, {}, True, True, self.console_printer) self.assertIn("No bears to show.", stdout.getvalue())