def _build_lint_target( path, config_dict): # type: (Path, Dict[str, Any]) -> AbstractLintTarget if path == _stdin_symbol: stdin_alt_path = get_config_value(config_dict, ['cmdargs', 'stdin_display_name']) # NOTE: In Python 3, sys.stdin is a string not bytes. Then we can get bytes by sys.stdin.buffer. # But in Python 2, sys.stdin.buffer is not defined. But we can get bytes by sys.stdin directly. is_python_3 = hasattr(sys.stdin, 'buffer') if is_python_3: lint_target = LintTargetBufferedStream( alternate_path=Path(stdin_alt_path), buffered_io=sys.stdin.buffer) else: # NOTE: Python 2 on Windows opens sys.stdin in text mode, and # binary data that read from it becomes corrupted on \r\n # SEE: https://stackoverflow.com/questions/2850893/reading-binary-data-from-stdin/38939320#38939320 if sys.platform == 'win32': # set sys.stdin to binary mode import os, msvcrt msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) lint_target = LintTargetBufferedStream( alternate_path=Path(stdin_alt_path), buffered_io=sys.stdin) return CachedLintTarget(lint_target) else: lint_target = LintTargetFile(path) return CachedLintTarget(lint_target)
def assertFoundViolationsEqual(self, path, Policy, expected_violations, policy_options=None): policy_name = Policy.__name__ policy_set = PolicySet([Policy]) config_dict = { 'cmdargs': { 'severity': Level.STYLE_PROBLEM, }, 'policies': { policy_name: { 'enabled': True, } }, } if policy_options is not None: config_dict['policies'][policy_name] = policy_options linter = Linter(policy_set, config_dict) violations = linter.lint(LintTargetFile(path)) pprint(violations) self.assertEqual(len(violations), len(expected_violations)) for violation, expected_violation in zip_longest( violations, expected_violations): self.assertViolation(violation, expected_violation)
def test_lint(self): policy_set = TestLinterIntegral.StubPolicySet() config_dict_global = { 'cmdargs': { 'verbose': True, 'severity': Level.WARNING, 'error-limit': 10, }, 'policies': { 'StubPolicy1': { 'enabled': True, }, 'StubPolicy2': { 'enabled': False, }, } } linter = Linter(policy_set, config_dict_global) got_violations = linter.lint(LintTargetFile(INVALID_VIM_SCRIPT)) expected_violations = [ { 'name': 'StubPolicy1', 'level': Level.WARNING, 'description': 'desc1', 'reference': 'ref1', 'position': { 'line': 1, 'column': 1, 'path': INVALID_VIM_SCRIPT }, }, { 'name': 'StubPolicy1', 'level': Level.WARNING, 'description': 'desc1', 'reference': 'ref1', 'position': { 'line': 7, 'column': 1, 'path': INVALID_VIM_SCRIPT }, }, { 'name': 'StubPolicy2', 'level': Level.WARNING, 'description': 'desc2', 'reference': 'ref2', 'position': { 'line': 8, 'column': 1, 'path': INVALID_VIM_SCRIPT }, }, ] self.maxDiff = 1024 self.assertEqual(got_violations, expected_violations)
def create_ast(self, file_path): parser = Parser() ast = parser.parse(LintTargetFile(file_path)) id_classifier = IdentifierClassifier() attached_ast = id_classifier.attach_identifier_attributes(ast) return attached_ast
def test_simple_example(self): global_config_dict = {'cmdargs': {'severity': Level.ERROR}} policy_set = PolicySet([TestConfigNextLineCommentSource.ProhibitStringPolicy]) linter = Linter(policy_set, global_config_dict) lint_target = LintTargetFile(Fixtures.SIMPLE.value) reported_string_node_values = [violation['description'] for violation in linter.lint(lint_target)] self.assertEqual(reported_string_node_values, [ "'report me because I have no line config comments'", "'report me because I have no line config comments, but the previous line have it'", ])
def test_lambda_string_expr(self): global_config_dict = {'cmdargs': {'severity': Level.ERROR}} policy_set = PolicySet([TestConfigNextLineCommentSource.ProhibitStringPolicy]) linter = Linter(policy_set, global_config_dict) lint_target = LintTargetFile(Fixtures.LAMBDA_STRING_EXPR.value) reported_string_node_values = [violation['description'] for violation in linter.lint(lint_target)] self.assertEqual(reported_string_node_values, [ "'report me because I have no line config comments'", "'report me because I have no line config comments, but the previous line have it'", # NOTE: In the current implementation, string in string will reported twice. "'\"report me because I have no line config comments, but the parent node have it\"'", '"report me because I have no line config comments, but the parent node have it"', ])
def test_lint_with_broken_file(self): policy_set = TestLinterIntegral.StubPolicySet() config_dict_global = { 'cmdargs': { 'verbose': True, 'severity': Level.WARNING, 'error-limit': 10, }, 'policies': { 'StubPolicy1': { 'enabled': True, }, 'StubPolicy2': { 'enabled': False, }, } } linter = Linter(policy_set, config_dict_global) got_violations = linter.lint(LintTargetFile(BROKEN_VIM_SCRIPT)) expected_violations = [ { 'name': 'SyntaxError', 'level': Level.ERROR, 'description': 'unexpected token: ==', 'reference': 'vim-jp/vim-vimlparser', 'position': { 'line': 1, 'column': 6, 'path': BROKEN_VIM_SCRIPT }, }, ] self.maxDiff = 1000 self.assertEqual(got_violations, expected_violations)
def create_ast(self, file_path): parser = Parser() ast = parser.parse(LintTargetFile(file_path.value)) return ast
def test_file(self): lint_target = LintTargetFile(Fixture.FILE.value) bytes_seq = lint_target.read() self.assertIsNotNone(bytes_seq) self.assertEqual(Fixture.FILE.value, lint_target.path)
def test_parse_empty_file(self): parser = Parser() ast = parser.parse(LintTargetFile(FIXTURE_FILE_EMPTY)) self.assertIs(ast['type'], 1)
def test_parse_file_when_neovim_enabled(self): parser = Parser(enable_neovim=True) ast = parser.parse(LintTargetFile(FIXTURE_FILE_NEOVIM)) self.assertIs(ast['type'], 1)
def test_parse_file_on_ff_dos_and_fenc_cp932(self): parser = Parser() ast = parser.parse(LintTargetFile(FIXTURE_FILE_FF_DOS_FENC_CP932)) self.assertIs(ast['type'], 1)
def create_ast(self, file_path): parser = Parser() lint_target = LintTargetFile(file_path.value) ast = parser.parse(lint_target) return ast
def setUp(self): parser = Parser() self.ast = parser.parse(LintTargetFile(FIXTURE_FILE))