File: presubmit_support_test.py

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (122 lines) | stat: -rwxr-xr-x 4,560 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
# Copyright 2022 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os.path
import unittest
import sys

from presubmit_support import _CheckNoDirectLitImport, _CheckBannedTsTags

sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi, MockFile


class NoDirectLitImportPresubmit(unittest.TestCase):
    def setUp(self):
        self.mock_input_api = MockInputApi()
        self.mock_input_api.change.RepositoryRoot = lambda: os.path.join(
            os.path.dirname(__file__), '..', '..', '..')

        self.mock_output_api = MockOutputApi()

    def emulateJsAndTsFiles(self, lines):
        ts_path = os.path.join('some', 'path', 'foo.ts')
        js_path = os.path.join('some', 'path', 'foo.js')
        foo_ts = MockFile(ts_path, lines)
        foo_js = MockFile(js_path, lines)
        self.mock_input_api.files.extend((foo_ts, foo_js))
        return ts_path, js_path

    def testWarningWithDirectLitImport(self):
        """
        If a TS file foo.ts or a JS file foo.js is changed, and there's direct
        Lit import in the file, show warnings.
        """
        lines = [
            "import {aaa} from 'a.js';",
            "import {css} from 'chrome://resources/mwc/lit/index.js';",
        ]
        ts_path, js_path = self.emulateJsAndTsFiles(lines)
        errors = _CheckNoDirectLitImport(self.mock_input_api,
                                         self.mock_output_api)
        self.assertEqual(2, len(errors))
        self.assertTrue(ts_path + ':2' in errors[0].message)
        self.assertTrue(js_path + ':2' in errors[1].message)

    def testNoWarningWithDirectLitImportInXfBase(self):
        """
        If ui/file_manager/file_manager/widgets/xf_base.ts is changed, and
        there's direct lit import in the file, no warnings.
        """
        lines = [
            "import {aaa} from 'a.js';",
            "import {css} from 'chrome://resources/mwc/lit/index.js';",
        ]
        xf_base_ts = MockFile(
            os.path.join('ui', 'file_manager', 'file_manager', 'widgets',
                         'xf_base.ts'), lines)
        self.mock_input_api.files.append(xf_base_ts)
        errors = _CheckNoDirectLitImport(self.mock_input_api,
                                         self.mock_output_api)
        self.assertEqual([], errors)

    def testNoWarningWithoutDirectLitImport(self):
        """
        If a TS file foo.ts is changed, and there is no direct Lit import
        in the file, no warnings.
        """
        self.emulateJsAndTsFiles(lines=[])
        errors = _CheckNoDirectLitImport(self.mock_input_api,
                                         self.mock_output_api)
        self.assertEqual([], errors)


class BannedTsTagsTest(unittest.TestCase):
    def setUp(self):
        self.mock_input_api = MockInputApi()
        self.mock_input_api.change.RepositoryRoot = lambda: os.path.join(
            os.path.dirname(__file__), '..', '..', '..')

        self.mock_output_api = MockOutputApi()

    def emulateJsAndTsFiles(self, lines):
        ts_path = os.path.join('some', 'path', 'foo.ts')
        js_path = os.path.join('some', 'path', 'foo.js')
        foo_ts = MockFile(ts_path, lines)
        foo_js = MockFile(js_path, lines)
        self.mock_input_api.files.extend((foo_ts, foo_js))
        return ts_path, js_path

    def testNoTags(self):
        lines = [
            "import {aaa} from 'a.js';",
            "import {css} from 'chrome://resources/mwc/lit/index.js';",
        ]
        self.emulateJsAndTsFiles(lines)
        errors = _CheckBannedTsTags(self.mock_input_api, self.mock_output_api)
        self.assertEqual(0, len(errors))

    def testTagOutsideComment(self):
        lines = [
            "import {aaa} from 'a.js';",
            "@ts-ignore randomly in the code",
        ]
        self.emulateJsAndTsFiles(lines)
        errors = _CheckBannedTsTags(self.mock_input_api, self.mock_output_api)
        self.assertEqual(0, len(errors))

    def testDetectInComments(self):
        lines = [
            "// @ts-ignore: It should block for TS",
            "import {aaa} from 'a.js';",
        ]
        ts_path, js_path = self.emulateJsAndTsFiles(lines)
        errors = _CheckBannedTsTags(self.mock_input_api, self.mock_output_api)
        self.assertEqual(1, len(errors))
        self.assertIn(ts_path, errors[0].items[0])


if __name__ == '__main__':
    unittest.main()