File: PRESUBMIT_test.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (108 lines) | stat: -rwxr-xr-x 4,680 bytes parent folder | download | duplicates (11)
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
#!/usr/bin/env python
# Copyright 2014 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
import sys
import tempfile
import unittest
import PRESUBMIT

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

class HTMLActionAdditionTest(unittest.TestCase):

    def testActionXMLChanged(self):
        mock_input_api = MockInputApi()
        lines = ['<input id="testinput" pref="testpref"',
                 'metric="validaction" type="checkbox" dialog-pref>']
        mock_input_api.files = [MockFile('path/valid.html', lines)]
        mock_input_api.change = MockChange(['path/valid.html', 'actions.xml'])
        action_xml_path = self._createActionXMLFile()
        self.assertEqual([], PRESUBMIT.InternalCheckUserActionUpdate(mock_input_api,
                                                             MockOutputApi(),
                                                             action_xml_path))

    def testValidChange_StartOfLine(self):
        lines = ['<input id="testinput" pref="testpref"',
                 'metric="validaction" type="checkbox" dialog-pref>']
        self.assertEqual([], self._testChange(lines))

    def testValidChange_StartsWithSpace(self):
        lines = ['<input id="testinput" pref="testpref"',
                 '  metric="validaction" type="checkbox" dialog-pref>']
        self.assertEqual([], self._testChange(lines))

    def testValidChange_Radio(self):
        lines = ['<input id="testinput" pref="testpref"',
                 '  metric="validaction" type="radio" dialog-pref value="true">']
        self.assertEqual([], self._testChange(lines))

    def testValidChange_UsingDatatype(self):
        lines = ['<input id="testinput" pref="testpref"',
                 '  metric="validaction" datatype="boolean" dialog-pref>']
        self.assertEqual([], self._testChange(lines))

    def testValidChange_NotBoolean(self):
        lines = ['<input id="testinput" pref="testpref"',
                 '  metric="notboolean_validaction" dialog-pref>']
        self.assertEqual([], self._testChange(lines))

    def testInvalidChange(self):
        lines = ['<input id="testinput" pref="testpref"',
                 'metric="invalidaction" type="checkbox" dialog-pref>']
        warnings = self._testChange(lines)
        self.assertEqual(1, len(warnings), warnings)

    def testInValidChange_Radio(self):
        lines = ['<input id="testinput" pref="testpref"',
                 '  metric="validaction" type="radio" dialog-pref value="string">']
        warnings = self._testChange(lines)
        self.assertEqual(1, len(warnings), warnings)

    def testValidChange_MultilineType(self):
        lines = ['<input id="testinput" pref="testpref"\n'
                 ' metric="validaction" type=\n'
                 ' "radio" dialog-pref value=\n'
                 ' "false">']
        warnings = self._testChange(lines)
        self.assertEqual([], self._testChange(lines))

    def _testChange(self, lines):
        mock_input_api = MockInputApi()
        mock_input_api.files = [MockFile('path/test.html', lines)]

        action_xml_path = self._createActionXMLFile()
        return PRESUBMIT.InternalCheckUserActionUpdate(mock_input_api,
                                               MockOutputApi(),
                                               action_xml_path)

    def _createActionXMLFile(self):
        content = ('<actions>'
            '<action name="validaction_Disable">'
            ' <owner>Please list the metric\'s owners.</owner>'
            ' <description>Enter the description of this user action.</description>'
            '</action>'
            '<action name="validaction_Enable">'
            ' <owner>Please list the metric\'s owners. </owner>'
            ' <description>Enter the description of this user action.</description>'
            '</action>'
            '<action name="notboolean_validaction">'
            ' <owner>Please list the metric\'s owners.</owner>'
            ' <description>Enter the description of this user action.</description>'
            '</action>'
            '</actions>')
        sys_temp = tempfile.gettempdir()
        action_xml_path = os.path.join(sys_temp, 'actions_test.xml')
        if not os.path.exists(action_xml_path):
            with open(action_xml_path, 'w+') as action_file:
                action_file.write(content)

        return action_xml_path

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