From 7c032cb96922f4ca348fd627270b7cd321aeec46 Mon Sep 17 00:00:00 2001 From: Jorge Pinna Puissant Date: Wed, 28 Apr 2021 13:30:37 +0000 Subject: [PATCH] [IMP] test_lint: Add ESLint to the test suite This commit allows using ESLint on the code JS to disallow undeclared variables (no-undef) and disallow specific global variables (no-restricted-globals) (error, event and self). Co-authored-by: Samuel Degueldre --- odoo/addons/test_lint/tests/__init__.py | 1 + odoo/addons/test_lint/tests/test_eslint.py | 59 ++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 odoo/addons/test_lint/tests/test_eslint.py diff --git a/odoo/addons/test_lint/tests/__init__.py b/odoo/addons/test_lint/tests/__init__.py index f9bf576a3ff..a7e2c1d6aa7 100644 --- a/odoo/addons/test_lint/tests/__init__.py +++ b/odoo/addons/test_lint/tests/__init__.py @@ -1,6 +1,7 @@ from . import test_pylint from . import test_pofile from . import test_ecmascript +from . import test_eslint from . import test_markers from . import test_onchange_domains from . import test_dunderinit diff --git a/odoo/addons/test_lint/tests/test_eslint.py b/odoo/addons/test_lint/tests/test_eslint.py new file mode 100644 index 00000000000..c19eb244ab3 --- /dev/null +++ b/odoo/addons/test_lint/tests/test_eslint.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import logging +import re +import subprocess +from unittest import skipIf +from odoo import tools + +from . import lint_case + +RULES = ('{' + '"no-undef": "error",' + '"no-restricted-globals": ["error", "event", "self"]' + '}' +) +PARSER_OPTIONS = '{ecmaVersion: 2019, sourceType: module}' +GLOBAL = ','.join([ + 'owl', + 'odoo', + '$', + 'jQuery', + '_', + 'Chart', + 'fuzzy', + 'QWeb2', + 'Popover', + 'StackTrace', + 'QUnit', + 'moment', + 'py', + 'ClipboardJS', +]) + +_logger = logging.getLogger(__name__) + +try: + eslint = tools.misc.find_in_path('eslint') +except IOError: + eslint = None + +@skipIf(eslint is None, "eslint tool not found on this system") +class TestESLint(lint_case.LintCase): + + longMessage = True + + def test_eslint_version(self): + """ Test that there are no eslint errors in javascript files """ + + files_to_check = [ + p for p in self.iter_module_files('**/static/**/*.js') + if not re.match('.*/libs?/.*', p) # don't check libraries + ] + + _logger.info('Testing %s js files', len(files_to_check)) + # https://eslint.org/docs/user-guide/command-line-interface + cmd = [eslint, '--no-eslintrc', '--env', 'browser', '--env', 'es2017', '--parser-options', PARSER_OPTIONS, '--rule', RULES, '--global', GLOBAL] + files_to_check + process = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, check=False) + self.assertEqual(process.returncode, 0, msg=process.stdout.decode())