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.SUITE_PLAN, REF_TYPE.CLASS, REF_TYPE.CC_CLASS]
102        )
103        self.assertEqual(
104            test_finder_handler._get_test_reference_types('Module_or_Class_name'),
105            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE,
106             REF_TYPE.SUITE_PLAN, REF_TYPE.CLASS, REF_TYPE.CC_CLASS]
107        )
108        self.assertEqual(
109            test_finder_handler._get_test_reference_types('SuiteName'),
110            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE,
111             REF_TYPE.SUITE_PLAN, REF_TYPE.CLASS, REF_TYPE.CC_CLASS]
112        )
113        self.assertEqual(
114            test_finder_handler._get_test_reference_types('Suite-Name'),
115            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE,
116             REF_TYPE.SUITE_PLAN, REF_TYPE.CLASS, REF_TYPE.CC_CLASS]
117        )
118        self.assertEqual(
119            test_finder_handler._get_test_reference_types('some.package'),
120            [REF_TYPE.CACHE, REF_TYPE.MODULE, REF_TYPE.PACKAGE]
121        )
122        self.assertEqual(
123            test_finder_handler._get_test_reference_types('fully.q.Class'),
124            [REF_TYPE.CACHE, REF_TYPE.QUALIFIED_CLASS]
125        )
126        self.assertEqual(
127            test_finder_handler._get_test_reference_types('Integration.xml'),
128            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
129             REF_TYPE.SUITE_PLAN_FILE_PATH]
130        )
131        self.assertEqual(
132            test_finder_handler._get_test_reference_types('SomeClass.java'),
133            [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH]
134        )
135        self.assertEqual(
136            test_finder_handler._get_test_reference_types('SomeClass.kt'),
137            [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH]
138        )
139        self.assertEqual(
140            test_finder_handler._get_test_reference_types('Android.mk'),
141            [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH]
142        )
143        self.assertEqual(
144            test_finder_handler._get_test_reference_types('Android.bp'),
145            [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH]
146        )
147        self.assertEqual(
148            test_finder_handler._get_test_reference_types('SomeTest.cc'),
149            [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH]
150        )
151        self.assertEqual(
152            test_finder_handler._get_test_reference_types('SomeTest.cpp'),
153            [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH]
154        )
155        self.assertEqual(
156            test_finder_handler._get_test_reference_types('SomeTest.cc#method'),
157            [REF_TYPE.CACHE, REF_TYPE.MODULE_FILE_PATH]
158        )
159        self.assertEqual(
160            test_finder_handler._get_test_reference_types('module:Class'),
161            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS]
162        )
163        self.assertEqual(
164            test_finder_handler._get_test_reference_types('module:f.q.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:a.package'),
169            [REF_TYPE.CACHE, REF_TYPE.MODULE_PACKAGE, REF_TYPE.MODULE_CLASS]
170        )
171        self.assertEqual(
172            test_finder_handler._get_test_reference_types('.'),
173            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
174             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.SUITE_PLAN_FILE_PATH]
175        )
176        self.assertEqual(
177            test_finder_handler._get_test_reference_types('..'),
178            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
179             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.SUITE_PLAN_FILE_PATH]
180        )
181        self.assertEqual(
182            test_finder_handler._get_test_reference_types('./rel/path/to/test'),
183            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
184             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.SUITE_PLAN_FILE_PATH]
185        )
186        self.assertEqual(
187            test_finder_handler._get_test_reference_types('rel/path/to/test'),
188            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
189             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.INTEGRATION,
190             REF_TYPE.SUITE_PLAN_FILE_PATH, REF_TYPE.CC_CLASS]
191        )
192        self.assertEqual(
193            test_finder_handler._get_test_reference_types('/abs/path/to/test'),
194            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
195             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.SUITE_PLAN_FILE_PATH]
196        )
197        self.assertEqual(
198            test_finder_handler._get_test_reference_types('int/test'),
199            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
200             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.INTEGRATION,
201             REF_TYPE.SUITE_PLAN_FILE_PATH, REF_TYPE.CC_CLASS]
202        )
203        self.assertEqual(
204            test_finder_handler._get_test_reference_types('int/test:fully.qual.Class#m'),
205            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
206             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.INTEGRATION,
207             REF_TYPE.SUITE_PLAN_FILE_PATH, REF_TYPE.MODULE_CLASS]
208        )
209        self.assertEqual(
210            test_finder_handler._get_test_reference_types('int/test:Class#method'),
211            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
212             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.INTEGRATION,
213             REF_TYPE.SUITE_PLAN_FILE_PATH, REF_TYPE.MODULE_CLASS]
214        )
215        self.assertEqual(
216            test_finder_handler._get_test_reference_types('int_name_no_slash:Class#m'),
217            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS]
218        )
219        self.assertEqual(
220            test_finder_handler._get_test_reference_types(
221                'gtest_module:Class_Prefix/Class#Method/Method_Suffix'),
222            [REF_TYPE.CACHE, REF_TYPE.INTEGRATION_FILE_PATH,
223             REF_TYPE.MODULE_FILE_PATH, REF_TYPE.INTEGRATION,
224             REF_TYPE.SUITE_PLAN_FILE_PATH, REF_TYPE.MODULE_CLASS]
225        )
226        self.assertEqual(
227            test_finder_handler._get_test_reference_types(
228                'Class_Prefix/Class#Method/Method_Suffix'),
229            [REF_TYPE.CACHE,
230             REF_TYPE.INTEGRATION_FILE_PATH,
231             REF_TYPE.MODULE_FILE_PATH,
232             REF_TYPE.INTEGRATION,
233             REF_TYPE.SUITE_PLAN_FILE_PATH,
234             REF_TYPE.CC_CLASS]
235        )
236
237    def test_get_registered_find_methods(self):
238        """Test that we get the registered find methods."""
239        empty_mod_info = None
240        example_finder_a_instance = test_finder_handler._get_finder_instance_dict(
241            empty_mod_info)[_EXAMPLE_FINDER_A]
242        should_equal = [
243            test_finder_base.Finder(
244                example_finder_a_instance,
245                ExampleFinderA.registered_find_method_from_example_finder,
246                _EXAMPLE_FINDER_A)]
247        should_not_equal = [
248            test_finder_base.Finder(
249                example_finder_a_instance,
250                ExampleFinderA.unregistered_find_method_from_example_finder,
251                _EXAMPLE_FINDER_A)]
252        # Let's make sure we see the registered method.
253        self.assertEqual(
254            should_equal,
255            test_finder_handler._get_registered_find_methods(empty_mod_info)
256        )
257        # Make sure we don't see the unregistered method here.
258        self.assertNotEqual(
259            should_not_equal,
260            test_finder_handler._get_registered_find_methods(empty_mod_info)
261        )
262
263    def test_get_find_methods_for_test(self):
264        """Test that we get the find methods we expect."""
265        # Let's see that we get the unregistered and registered find methods in
266        # the order we expect.
267        test = ''
268        registered_find_methods = [
269            test_finder_base.Finder(
270                _FINDER_INSTANCES[_EXAMPLE_FINDER_A],
271                ExampleFinderA.registered_find_method_from_example_finder,
272                _EXAMPLE_FINDER_A)]
273        default_find_methods = [
274            test_finder_base.Finder(
275                _FINDER_INSTANCES[_EXAMPLE_FINDER_A],
276                ExampleFinderA.unregistered_find_method_from_example_finder,
277                _EXAMPLE_FINDER_A)]
278        should_equal = registered_find_methods + default_find_methods
279        self.assertEqual(
280            should_equal,
281            test_finder_handler.get_find_methods_for_test(self.empty_mod_info,
282                                                          test))
283
284
285if __name__ == '__main__':
286    unittest.main()
287