diff --git a/norminette/__main__.py b/norminette/__main__.py index 264dcca..8ee3120 100644 --- a/norminette/__main__.py +++ b/norminette/__main__.py @@ -136,7 +136,7 @@ def main(): except KeyboardInterrupt: sys.exit(1) errors = format(files) - print(errors) + print(errors, end='') sys.exit(1 if len(file.errors) else 0) diff --git a/norminette/errors.py b/norminette/errors.py index 6af05b9..e8818f7 100644 --- a/norminette/errors.py +++ b/norminette/errors.py @@ -179,6 +179,7 @@ def __str__(self) -> str: highlight = error.highlights[0] output += f"\n{error.level}: {error.name:<20} " output += f"(line: {highlight.lineno:>3}, col: {highlight.column:>3}):\t{error.text}" + output += '\n' return output @@ -194,7 +195,7 @@ def __str__(self): output = { "files": files, } - return json.dumps(output, separators=(',', ':')) + return json.dumps(output, separators=(',', ':')) + '\n' formatters = ( diff --git a/tests/rules/rules_generator_test.py b/tests/rules/rules_generator_test.py index 979b9e6..19f86ca 100644 --- a/tests/rules/rules_generator_test.py +++ b/tests/rules/rules_generator_test.py @@ -25,7 +25,7 @@ def test_rule_for_file(file, capsys): context = Context(file, list(lexer), debug=2) registry.run(context) errors = HumanizedErrorsFormatter(file) - print(errors) + print(errors, end='') captured = capsys.readouterr() assert captured.out == out_content diff --git a/tests/test_errors.py b/tests/test_errors.py index a65b999..be6636d 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -3,6 +3,7 @@ from dataclasses import astuple import pytest +from unittest.mock import patch from norminette.file import File from norminette.lexer import Lexer @@ -10,6 +11,61 @@ from norminette.registry import Registry from norminette.errors import JSONErrorsFormatter from norminette.errors import Error, Errors, Highlight as H +from norminette.errors import HumanizedErrorsFormatter + + +@pytest.mark.parametrize("files, expected_result, ", [it.values() for it in [ + { + "files": [ + File("/nium/a.c", "#include "), + File("/nium/b.c", "int\tmain(void)\n{\n\treturn (1);\n}\n"), + File("/nium/c.c", "int\tfn(int n);\n"), + ], + "expected_result": "a.c: OK!\nb.c: OK!\nc.c: OK!\n", + }, + { + "files": [ + File("skyfall.c", "// Hello"), + ], + "expected_result": "skyfall.c: OK!\n", + }, + { + "files": [ + File("/nium/mortari.c", "#define TRUE 1"), + File("/nium/gensler.c", "int\tmain();\n"), + ], + "expected_result": ( + "mortari.c: OK!\n" + "gensler.c: Error!\n" + "Error: NO_ARGS_VOID (line: 1, col: 10):\tEmpty function argument requires void\n" + ) + }, + { + "files": [ + File("/nium/john.c", "#define x"), + File("/nium/galt.c", "#define x"), + ], + "expected_result": ( + "john.c: Error!\n" + "Error: MACRO_NAME_CAPITAL (line: 1, col: 9):\tMacro name must be capitalized\n" + "galt.c: Error!\n" + "Error: MACRO_NAME_CAPITAL (line: 1, col: 9):\tMacro name must be capitalized\n" + ) + }, + ] +]) +def test_humanized_formatter_errored_file(files, expected_result): + registry = Registry() + + with patch("norminette.rules.check_header.CheckHeader.run") as _: + for file in files: + lexer = Lexer(file) + context = Context(file, lexer.get_tokens()) + registry.run(context) + + formatter = HumanizedErrorsFormatter(files) + assert str(formatter) == expected_result + tests = [ { @@ -47,7 +103,7 @@ def test_json_formatter_errored_file(file, test): Registry().run(context) formatter = JSONErrorsFormatter(file) - assert str(formatter) == json.dumps(test, separators=(',', ':')) + assert str(formatter) == json.dumps(test, separators=(',', ':')) + '\n' def test_error_from_name():