• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright 2018, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Unittests for test_finder_handler."""
18
19# pylint: disable=line-too-long
20
21import unittest
22
23from unittest import mock
24
25import atest_error
26import test_finder_handler
27
28from test_finders import test_info
29from test_finders import test_finder_base
30
31#pylint: disable=protected-access
32REF_TYPE = test_finder_handler._REFERENCE_TYPE
33
34_EXAMPLE_FINDER_A = 'EXAMPLE_A'
35
36
37#pylint: disable=no-self-use
38@test_finder_base.find_method_register
39class ExampleFinderA(test_finder_base.TestFinderBase):
40    """Example finder class A."""
41    NAME = _EXAMPLE_FINDER_A
42    _TEST_RUNNER = 'TEST_RUNNER'
43
44    @test_finder_base.register()
45    def registered_find_method_from_example_finder(self, test):
46        """Registered Example find method."""
47        if test == 'ExampleFinderATrigger':
48            return test_info.TestInfo(test_name=test,
49                                      test_runner=self._TEST_RUNNER,
50                                      build_targets=set())
51        return None
52
53    def unregistered_find_method_from_example_finder(self, _test):
54        """Unregistered Example find method, should never be called."""
55        raise atest_error.ShouldNeverBeCalledError()
56
57
58_TEST_FINDERS_PATCH = {
59    ExampleFinderA,
60}
61
62
63_FINDER_INSTANCES = {
64    _EXAMPLE_FINDER_A: ExampleFinderA(),
65}
66
67
68class TestFinderHandlerUnittests(unittest.TestCase):
69    """Unit tests for test_finder_handler.py"""
70
71    def setUp(self):
72        """Set up for testing."""
73        # pylint: disable=invalid-name
74        # This is so we can see the full diffs when there are mismatches.
75        self.maxDiff = None
76        self.empty_mod_info = None
77        # We want to control the finders we return.
78        mock.patch('test_finder_handler._get_test_finders',
79                   lambda: _TEST_FINDERS_PATCH).start()
80        # Since we're going to be comparing instance objects, we'll need to keep
81        # track of the objects so they align.
82        mock.patch('test_finder_handler._get_finder_instance_dict',
83                   lambda x: _FINDER_INSTANCES).start()
84        # We want to mock out the default find methods to make sure we got all
85        # the methods we expect.
86        mock.patch('test_finder_handler._get_default_find_methods',
87                   lambda x, y: [test_finder_base.Finder(
88                       _FINDER_INSTANCES[_EXAMPLE_FINDER_A],
89                       ExampleFinderA.unregistered_find_method_from_example_finder,
90                       _EXAMPLE_FINDER_A)]).start()
91
92    def tearDown(self):
93        """Tear down."""
94        mock.patch.stopall()
95
96    def test_get_test_reference_types(self):
97        """Test _get_test_reference_types parses reference types correctly."""
98        self.assertEqual(
99            test_finder_handler._get_test_reference_types('ModuleOrClassName'),
100            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE,
101             REF_TYPE.CONFIG, REF_TYPE.SUITE_PLAN, REF_TYPE.CLASS,
102             REF_TYPE.CC_CLASS]
103        )
104        self.assertEqual(
105            test_finder_handler._get_test_reference_types('Module_or_Class_name'),
106            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE,
107             REF_TYPE.CONFIG, REF_TYPE.SUITE_PLAN, REF_TYPE.CLASS,
108             REF_TYPE.CC_CLASS]
109        )
110        self.assertEqual(
111            test_finder_handler._get_test_reference_types('SuiteName'),
112            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE,
113             REF_TYPE.CONFIG, REF_TYPE.SUITE_PLAN, REF_TYPE.CLASS,
114             REF_TYPE.CC_CLASS]
115        )
116        self.assertEqual(
117            test_finder_handler._get_test_reference_types('Suite-Name'),
118            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE,
119             REF_TYPE.CONFIG, REF_TYPE.SUITE_PLAN, REF_TYPE.CLASS,
120             REF_TYPE.CC_CLASS]
121        )
122        self.assertEqual(
123            test_finder_handler._get_test_reference_types('some.package'),
124            [REF_TYPE.CACHE, REF_TYPE.MODULE, REF_TYPE.PACKAGE]
125        )
126        self.assertEqual(
127            test_finder_handler._get_test_reference_types('fully.q.Class'),
128            [REF_TYPE.CACHE, REF_TYPE.QUALIFIED_CLASS]
129        )
130        self.assertEqual(
131            test_finder_handler._get_test_reference_types('Integration.xml'),
132            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
133             REF_TYPE.SUITE_PLAN_FILE_PATH]
134        )
135        self.assertEqual(
136            test_finder_handler._get_test_reference_types('SomeClass.java'),
137            [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH]
138        )
139        self.assertEqual(
140            test_finder_handler._get_test_reference_types('SomeClass.kt'),
141            [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH]
142        )
143        self.assertEqual(
144            test_finder_handler._get_test_reference_types('Android.mk'),
145            [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH]
146        )
147        self.assertEqual(
148            test_finder_handler._get_test_reference_types('Android.bp'),
149            [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH]
150        )
151        self.assertEqual(
152            test_finder_handler._get_test_reference_types('SomeTest.cc'),
153            [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH]
154        )
155        self.assertEqual(
156            test_finder_handler._get_test_reference_types('SomeTest.cpp'),
157            [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH]
158        )
159        self.assertEqual(
160            test_finder_handler._get_test_reference_types('SomeTest.cc#method'),
161            [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH]
162        )
163        self.assertEqual(
164            test_finder_handler._get_test_reference_types('module:Class'),
165            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS]
166        )
167        self.assertEqual(
168            test_finder_handler._get_test_reference_types('module:f.q.Class'),
169            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS]
170        )
171        self.assertEqual(
172            test_finder_handler._get_test_reference_types('module:a.package'),
173            [REF_TYPE.CACHE, REF_TYPE.MODULE_PACKAGE, REF_TYPE.MODULE_CLASS]
174        )
175        self.assertEqual(
176            test_finder_handler._get_test_reference_types('.'),
177            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
178             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.SUITE_PLAN_FILE_PATH]
179        )
180        self.assertEqual(
181            test_finder_handler._get_test_reference_types('..'),
182            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
183             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.SUITE_PLAN_FILE_PATH]
184        )
185        self.assertEqual(
186            test_finder_handler._get_test_reference_types('./rel/path/to/test'),
187            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
188             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.SUITE_PLAN_FILE_PATH]
189        )
190        self.assertEqual(
191            test_finder_handler._get_test_reference_types('rel/path/to/test'),
192            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
193             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.INTEGRATION,
194             REF_TYPE.SUITE_PLAN_FILE_PATH, REF_TYPE.CC_CLASS]
195        )
196        self.assertEqual(
197            test_finder_handler._get_test_reference_types('/abs/path/to/test'),
198            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
199             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.SUITE_PLAN_FILE_PATH]
200        )
201        self.assertEqual(
202            test_finder_handler._get_test_reference_types('int/test'),
203            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
204             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.INTEGRATION,
205             REF_TYPE.SUITE_PLAN_FILE_PATH, REF_TYPE.CC_CLASS]
206        )
207        self.assertEqual(
208            test_finder_handler._get_test_reference_types('int/test:fully.qual.Class#m'),
209            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
210             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.INTEGRATION,
211             REF_TYPE.SUITE_PLAN_FILE_PATH, REF_TYPE.MODULE_CLASS]
212        )
213        self.assertEqual(
214            test_finder_handler._get_test_reference_types('int/test:Class#method'),
215            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
216             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.INTEGRATION,
217             REF_TYPE.SUITE_PLAN_FILE_PATH, REF_TYPE.MODULE_CLASS]
218        )
219        self.assertEqual(
220            test_finder_handler._get_test_reference_types('int_name_no_slash:Class#m'),
221            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS]
222        )
223        self.assertEqual(
224            test_finder_handler._get_test_reference_types(
225                'gtest_module:Class_Prefix/Class#Method/Method_Suffix'),
226            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
227             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.INTEGRATION,
228             REF_TYPE.SUITE_PLAN_FILE_PATH, REF_TYPE.MODULE_CLASS]
229        )
230        self.assertEqual(
231            test_finder_handler._get_test_reference_types(
232                'Class_Prefix/Class#Method/Method_Suffix'),
233            [REF_TYPE.CACHE,
234             REF_TYPE.INTEGRATION_FILE_PATH,
235             REF_TYPE.MODULE_FILE_PATH,
236             REF_TYPE.INTEGRATION,
237             REF_TYPE.SUITE_PLAN_FILE_PATH,
238             REF_TYPE.CC_CLASS]
239        )
240
241    def test_get_registered_find_methods(self):
242        """Test that we get the registered find methods."""
243        empty_mod_info = None
244        example_finder_a_instance = test_finder_handler._get_finder_instance_dict(
245            empty_mod_info)[_EXAMPLE_FINDER_A]
246        should_equal = [
247            test_finder_base.Finder(
248                example_finder_a_instance,
249                ExampleFinderA.registered_find_method_from_example_finder,
250                _EXAMPLE_FINDER_A)]
251        should_not_equal = [
252            test_finder_base.Finder(
253                example_finder_a_instance,
254                ExampleFinderA.unregistered_find_method_from_example_finder,
255                _EXAMPLE_FINDER_A)]
256        # Let's make sure we see the registered method.
257        self.assertEqual(
258            should_equal,
259            test_finder_handler._get_registered_find_methods(empty_mod_info)
260        )
261        # Make sure we don't see the unregistered method here.
262        self.assertNotEqual(
263            should_not_equal,
264            test_finder_handler._get_registered_find_methods(empty_mod_info)
265        )
266
267    def test_get_find_methods_for_test(self):
268        """Test that we get the find methods we expect."""
269        # Let's see that we get the unregistered and registered find methods in
270        # the order we expect.
271        test = ''
272        registered_find_methods = [
273            test_finder_base.Finder(
274                _FINDER_INSTANCES[_EXAMPLE_FINDER_A],
275                ExampleFinderA.registered_find_method_from_example_finder,
276                _EXAMPLE_FINDER_A)]
277        default_find_methods = [
278            test_finder_base.Finder(
279                _FINDER_INSTANCES[_EXAMPLE_FINDER_A],
280                ExampleFinderA.unregistered_find_method_from_example_finder,
281                _EXAMPLE_FINDER_A)]
282        should_equal = registered_find_methods + default_find_methods
283        self.assertEqual(
284            should_equal,
285            test_finder_handler.get_find_methods_for_test(self.empty_mod_info,
286                                                          test))
287
288
289if __name__ == '__main__':
290    unittest.main()
291