summaryrefslogtreecommitdiff
path: root/util/zephyr_check_compliance_unittest.py
blob: 1d4be968d031cf7cb5982bca326985d1600b08fb (plain)
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
#!/usr/bin/env vpython3

# Copyright 2023 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Unit tests for check_zephyr_project_config.py"""

# [VPYTHON:BEGIN]
# python_version: "3.8"
# wheel: <
#   name: "infra/python/wheels/mock-py3"
#   version: "version:4.0.3"
# >
# wheel: <
#   name: "infra/python/wheels/junitparser-py2_py3"
#   version: "version:2.8.0"
# >
# wheel: <
#   name: "infra/python/wheels/future-py2_py3"
#   version: "version:0.18.2"
# >
# wheel: <
#   name: "infra/python/wheels/python-magic-py2_py3"
#   version: "version:0.4.24"
# >
# wheel: <
#   name: "infra/python/wheels/pyyaml-py3"
#   version: "version:5.3.1"
# >
# wheel: <
#   name: "infra/python/wheels/yamllint-py3"
#   version: "version:1.29.0"
# >
# wheel: <
#   name: "infra/python/wheels/pathspec-py3"
#   version: "version:0.9.0"
# >
# wheel: <
#   name: "infra/python/wheels/lxml/${vpython_platform}"
#   version: "version:4.6.3"
# >
# [VPYTHON:END]

import unittest

import mock  # pylint:disable=import-error
import zephyr_check_compliance


# pylint:disable=protected-access,no-self-use


class TestZephyrCheckCompliance(unittest.TestCase):
    """Tests for zephyr_check_compliance."""

    @mock.patch("check_compliance.get_files")
    def test_changed_files(self, get_files_mock):
        """Test _changed_files."""
        get_files_mock.return_value = [
            "file1",
            "file2",
        ]

        zephyr_check_compliance._patch_get_files()
        out = zephyr_check_compliance._changed_files("ref")
        self.assertFalse(out)

        get_files_mock.return_value.append("zephyr/file3")

        zephyr_check_compliance._patch_get_files()
        out = zephyr_check_compliance._changed_files("ref")
        self.assertTrue(out)

    @mock.patch("zephyr_check_compliance._changed_files")
    @mock.patch("check_compliance.main")
    def test_main(self, main_mock, changed_files_mock):
        """Tests the main function."""
        changed_files_mock.return_value = True

        zephyr_check_compliance.main(["ref"])

        changed_files_mock.assert_called_with("ref~1..ref")
        main_mock.assert_called_with(
            [
                "--output=",
                "--no-case-output",
                "-m",
                "YAMLLint",
                "-c",
                "ref~1..ref",
            ]
        )

    @mock.patch("zephyr_check_compliance._changed_files")
    @mock.patch("check_compliance.main")
    def test_main_skip_presubmit(self, main_mock, changed_files_mock):
        """Tests the main function."""
        changed_files_mock.return_value = False

        zephyr_check_compliance.main([zephyr_check_compliance.PRE_SUBMIT_REF])

        self.assertEqual(changed_files_mock.call_count, 0)
        self.assertEqual(main_mock.call_count, 0)

    @mock.patch("zephyr_check_compliance._changed_files")
    @mock.patch("check_compliance.main")
    def test_main_skip(self, main_mock, changed_files_mock):
        """Tests the main function."""
        changed_files_mock.return_value = False

        zephyr_check_compliance.main(["ref"])

        changed_files_mock.assert_called_with("ref~1..ref")
        self.assertEqual(main_mock.call_count, 0)


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