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]
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]
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]
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]
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
220    def test_get_registered_find_methods(self):
221        """Test that we get the registered find methods."""
222        empty_mod_info = None
223        example_finder_a_instance = test_finder_handler._get_finder_instance_dict(
224            empty_mod_info)[_EXAMPLE_FINDER_A]
225        should_equal = [
226            test_finder_base.Finder(
227                example_finder_a_instance,
228                ExampleFinderA.registered_find_method_from_example_finder,
229                _EXAMPLE_FINDER_A)]
230        should_not_equal = [
231            test_finder_base.Finder(
232                example_finder_a_instance,
233                ExampleFinderA.unregistered_find_method_from_example_finder,
234                _EXAMPLE_FINDER_A)]
235        # Let's make sure we see the registered method.
236        self.assertEqual(
237            should_equal,
238            test_finder_handler._get_registered_find_methods(empty_mod_info)
239        )
240        # Make sure we don't see the unregistered method here.
241        self.assertNotEqual(
242            should_not_equal,
243            test_finder_handler._get_registered_find_methods(empty_mod_info)
244        )
245
246    def test_get_find_methods_for_test(self):
247        """Test that we get the find methods we expect."""
248        # Let's see that we get the unregistered and registered find methods in
249        # the order we expect.
250        test = ''
251        registered_find_methods = [
252            test_finder_base.Finder(
253                _FINDER_INSTANCES[_EXAMPLE_FINDER_A],
254                ExampleFinderA.registered_find_method_from_example_finder,
255                _EXAMPLE_FINDER_A)]
256        default_find_methods = [
257            test_finder_base.Finder(
258                _FINDER_INSTANCES[_EXAMPLE_FINDER_A],
259                ExampleFinderA.unregistered_find_method_from_example_finder,
260                _EXAMPLE_FINDER_A)]
261        should_equal = registered_find_methods + default_find_methods
262        self.assertEqual(
263            should_equal,
264            test_finder_handler.get_find_methods_for_test(self.empty_mod_info,
265                                                          test))
266
267
268if __name__ == '__main__':
269    unittest.main()
270