| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2017 The Chromium Authors. All rights reserved. | 2 # Copyright 2017 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import js_checker | 6 import js_checker |
| 7 import json | 7 import json |
| 8 import os | 8 import os |
| 9 import sys | 9 import sys |
| 10 import unittest | 10 import unittest |
| 11 import tempfile | 11 import tempfile |
| 12 | 12 |
| 13 | 13 |
| 14 _HERE_PATH = os.path.dirname(__file__) | 14 _HERE_PATH = os.path.dirname(__file__) |
| 15 sys.path.append(os.path.join(_HERE_PATH, '..', '..')) | 15 sys.path.append(os.path.join(_HERE_PATH, '..', '..')) |
| 16 | 16 |
| 17 from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi, MockFile | 17 from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi, MockFile |
| 18 | 18 |
| 19 | 19 |
| 20 class JsCheckerEsLintTest(unittest.TestCase): | 20 class JsCheckerEsLintTest(unittest.TestCase): |
| 21 def tearDown(self): | 21 def tearDown(self): |
| 22 os.remove(self._tmp_file) | 22 os.remove(self._tmp_file) |
| 23 | 23 |
| 24 def testGetElementByIdCheck(self): | 24 def _runChecks(self, file_contents): |
| 25 tmp_args = {'suffix': '.js', 'dir': _HERE_PATH, 'delete': False} | 25 tmp_args = {'suffix': '.js', 'dir': _HERE_PATH, 'delete': False} |
| 26 with tempfile.NamedTemporaryFile(**tmp_args) as f: | 26 with tempfile.NamedTemporaryFile(**tmp_args) as f: |
| 27 self._tmp_file = f.name | 27 self._tmp_file = f.name |
| 28 f.write('var a = document.getElementById(\'foo\');') | 28 f.write(file_contents) |
| 29 | 29 |
| 30 input_api = MockInputApi() | 30 input_api = MockInputApi() |
| 31 input_api.files = [MockFile(os.path.abspath(self._tmp_file), '')] | 31 input_api.files = [MockFile(os.path.abspath(self._tmp_file), '')] |
| 32 input_api.presubmit_local_path = _HERE_PATH | 32 input_api.presubmit_local_path = _HERE_PATH |
| 33 | 33 |
| 34 checker = js_checker.JSChecker(input_api, MockOutputApi()) | 34 checker = js_checker.JSChecker(input_api, MockOutputApi()) |
| 35 results_json = checker.RunEsLintChecks( | 35 return checker.RunEsLintChecks(input_api.AffectedFiles(), format='json') |
| 36 input_api.AffectedFiles(), format='json') | 36 |
| 37 def _assertError(self, results_json, rule_id, line): |
| 37 self.assertEqual(1, len(results_json)) | 38 self.assertEqual(1, len(results_json)) |
| 38 | |
| 39 results = json.loads(results_json[0].message) | 39 results = json.loads(results_json[0].message) |
| 40 self.assertEqual(1, len(results)) | 40 self.assertEqual(1, len(results)) |
| 41 | |
| 42 self.assertEqual(1, len(results[0].get('messages'))) | 41 self.assertEqual(1, len(results[0].get('messages'))) |
| 43 message = results[0].get('messages')[0] | 42 message = results[0].get('messages')[0] |
| 44 self.assertEqual('no-restricted-properties', message.get('ruleId')) | 43 self.assertEqual(rule_id, message.get('ruleId')) |
| 45 self.assertEqual(1, message.get('line')) | 44 self.assertEqual(line, message.get('line')) |
| 45 |
| 46 def testGetElementByIdCheck(self): |
| 47 results_json = self._runChecks('var a = document.getElementById(\'foo\');') |
| 48 self._assertError(results_json, 'no-restricted-properties', 1) |
| 49 |
| 50 def testPrimitiveWrappersCheck(self): |
| 51 results_json = self._runChecks('var a = new Number(1);') |
| 52 self._assertError(results_json, 'no-new-wrappers', 1) |
| 46 | 53 |
| 47 | 54 |
| 48 if __name__ == '__main__': | 55 if __name__ == '__main__': |
| 49 unittest.main() | 56 unittest.main() |
| OLD | NEW |