[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 <sad@odoo.com>
This commit is contained in:
co-authored by
Samuel Degueldre
parent
b7d9c445c0
commit
7c032cb969
@@ -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
|
||||
|
||||
@@ -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())
|
||||
Reference in New Issue
Block a user