def test_javascript(self) -> None: """Tests JavaScriptLinter.""" linter = linters.JavaScriptLinter() self.assertEqual( linter.run_one('test.js', b' function x ( ){a;b;c;};\n'), linters.SingleResult(b'function x() {\n a;\n b;\n c;\n}\n', ['javascript'])) self.assertEqual( linter.run_one('test.js', b'#!/usr/bin/node\nreturn;\n'), linters.SingleResult(b'#!/usr/bin/node\nreturn;\n', ['javascript']))
def run_one(self, filename: str, contents: bytes) -> linters.SingleResult: '''Runs the linter against |contents|.''' diagnostics: List[linters.Diagnostic] = [] for lineno, line in enumerate(contents.decode('utf-8').split('\n'), start=1): if filename.endswith('.tpl'): regex = _TEMPLATE_RE else: regex = _FRONTEND_RE for match in regex.finditer(line): if match[1] in self.__valid_strings: continue diagnostics.append( linters.Diagnostic( f'Missing translation string {match[1]!r}', filename, lineno=lineno, line=line, col=match.start(1) + 1, col_end=match.end(1) + 1)) if diagnostics: raise linters.LinterException('Missing translation strings', fixable=False, diagnostics=diagnostics) return linters.SingleResult(contents, ['translation_strings'])
def test_vue(self) -> None: """Tests VueLinter.""" linter = linters.VueLinter() with self.assertRaisesRegex(linters.LinterException, r'Found Vue errors') as lex: linter.run_one('test.vue', b'<template>\n<b></span>\n</template>\n') self.assertEqual(lex.exception.diagnostics, [ linters.Diagnostic(message='Unclosed tag', filename='test.vue', line='<b></span>', lineno=2, col=4), linters.Diagnostic('Unclosed tag', filename='test.vue', line='</template>', lineno=3, col=1), ]) self.assertEqual( linter.run_one('test.vue', b'<template>\n<b></b>\n</template>\n'), linters.SingleResult(b'<template>\n <b></b>\n</template>\n', ['vue']))
def test_php(self) -> None: """Tests PHPLinter.""" linter = linters.PHPLinter() self.assertEqual( linter.run_one('test.php', b'<?php\necho array("foo");'), linters.SingleResult(b'<?php\necho [\'foo\'];\n', ['php']))
def test_command(self) -> None: """Tests CommandLinter.""" linter = linters.CommandLinter({ 'commands': ['python3 test/uppercase_linter.py'], }) self.assertEqual(linter.run_one('test.txt', b'Hello, World!\n'), linters.SingleResult(b'HELLO, WORLD!\n', ['command']))
def test_html(self) -> None: """Tests HTMLLinter.""" linter = linters.HTMLLinter() self.assertEqual( linter.run_one( 'test.html', b'<!DOCTYPE html>\n<html><head><title /></head>' b'<body>\n<input/></body></html>\n'), linters.SingleResult( b'<!DOCTYPE html>\n<html>\n <head>\n <title></title>\n' b' </head>\n <body>\n <input />\n </body>\n</html>\n', ['html']))
def test_whitespace(self) -> None: """Tests WhitespaceLinter.""" linter = linters.WhitespaceLinter() self.assertEqual( linter.run_one('test.txt', b'Hello\r\nWorld!\n'), linters.SingleResult(b'Hello\nWorld!\n', ['Windows-style EOF'])) self.assertEqual( linter.run_one('test.txt', b'Hello\n\n\nWorld!\n'), linters.SingleResult(b'Hello\n\nWorld!\n', ['consecutive empty lines'])) self.assertEqual( linter.run_one('test.txt', b'function() {\n\n}\n'), linters.SingleResult(b'function() {\n}\n', ['empty lines after an opening brace'])) self.assertEqual( linter.run_one('test.txt', b'function() {\n//\n\n}\n'), linters.SingleResult(b'function() {\n//\n}\n', ['empty lines before a closing brace'])) self.assertEqual( linter.run_one('test.txt', 'hello \u200b\n'.encode('utf-8')), linters.SingleResult(b'hello\n', [ 'trailing whitespace', ])) self.assertEqual( linter.run_one('test.txt', b'function() {\r\n\n\n// \n\n}\n'), linters.SingleResult(b'function() {\n//\n}\n', [ 'Windows-style EOF', 'trailing whitespace', 'consecutive empty lines', 'empty lines after an opening brace', 'empty lines before a closing brace', ]))