• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
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
19import unittest
20import mock
21
22import atest_error
23import test_finder_handler
24from test_finders import test_info
25from test_finders import test_finder_base
26
27#pylint: disable=protected-access
28REF_TYPE = test_finder_handler._REFERENCE_TYPE
29
30_EXAMPLE_FINDER_A = 'EXAMPLE_A'
31
32
33#pylint: disable=no-self-use
34@test_finder_base.find_method_register
35class ExampleFinderA(test_finder_base.TestFinderBase):
36    """Example finder class A."""
37    NAME = _EXAMPLE_FINDER_A
38    _TEST_RUNNER = 'TEST_RUNNER'
39
40    @test_finder_base.register()
41    def registered_find_method_from_example_finder(self, test):
42        """Registered Example find method."""
43        if test == 'ExampleFinderATrigger':
44            return test_info.TestInfo(test_name=test,
45                                      test_runner=self._TEST_RUNNER,
46                                      build_targets=set())
47        return None
48
49    def unregistered_find_method_from_example_finder(self, _test):
50        """Unregistered Example find method, should never be called."""
51        raise atest_error.ShouldNeverBeCalledError()
52
53
54_TEST_FINDERS_PATCH = {
55    ExampleFinderA,
56}
57
58
59_FINDER_INSTANCES = {
60    _EXAMPLE_FINDER_A: ExampleFinderA(),
61}
62
63
64class TestFinderHandlerUnittests(unittest.TestCase):
65    """Unit tests for test_finder_handler.py"""
66
67    def setUp(self):
68        """Set up for testing."""
69        # pylint: disable=invalid-name
70        # This is so we can see the full diffs when there are mismatches.
71        self.maxDiff = None
72        self.empty_mod_info = None
73        # We want to control the finders we return.
74        mock.patch('test_finder_handler._get_test_finders',
75                   lambda: _TEST_FINDERS_PATCH).start()
76        # Since we're going to be comparing instance objects, we'll need to keep
77        # track of the objects so they align.
78        mock.patch('test_finder_handler._get_finder_instance_dict',
79                   lambda x: _FINDER_INSTANCES).start()
80        # We want to mock out the default find methods to make sure we got all
81        # the methods we expect.
82        mock.patch('test_finder_handler._get_default_find_methods',
83                   lambda x, y: [test_finder_base.Finder(
84                       _FINDER_INSTANCES[_EXAMPLE_FINDER_A],
85                       ExampleFinderA.unregistered_find_method_from_example_finder,
86                       _EXAMPLE_FINDER_A)]).start()
87
88    def tearDown(self):
89        """Tear down."""
90        mock.patch.stopall()
91
92    def test_get_test_reference_types(self):
93        """Test _get_test_reference_types parses reference types correctly."""
94        self.assertEqual(
95            test_finder_handler._get_test_reference_types('ModuleOrClassName'),
96            [REF_TYPE.INTEGRATION, REF_TYPE.MODULE, REF_TYPE.SUITE_PLAN,
97             REF_TYPE.CLASS, REF_TYPE.CC_CLASS]
98        )
99        self.assertEqual(
100            test_finder_handler._get_test_reference_types('Module_or_Class_name'),
101            [REF_TYPE.INTEGRATION, REF_TYPE.MODULE, REF_TYPE.SUITE_PLAN,
102             REF_TYPE.CLASS, REF_TYPE.CC_CLASS]
103        )
104        self.assertEqual(
105            test_finder_handler._get_test_reference_types('SuiteName'),
106            [REF_TYPE.INTEGRATION, REF_TYPE.MODULE, REF_TYPE.SUITE_PLAN,
107             REF_TYPE.CLASS, REF_TYPE.CC_CLASS]
108        )
109        self.assertEqual(
110            test_finder_handler._get_test_reference_types('Suite-Name'),
111            [REF_TYPE.INTEGRATION, REF_TYPE.MODULE, REF_TYPE.SUITE_PLAN,
112             REF_TYPE.CLASS, REF_TYPE.CC_CLASS]
113        )
114        self.assertEqual(
115            test_finder_handler._get_test_reference_types('some.package'),
116            [REF_TYPE.MODULE, REF_TYPE.PACKAGE]
117        )
118        self.assertEqual(
119            test_finder_handler._get_test_reference_types('fully.q.Class'),
120            [REF_TYPE.QUALIFIED_CLASS]
121        )
122        self.assertEqual(
123            test_finder_handler._get_test_reference_types('Integration.xml'),
124            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.SUITE_PLAN_FILE_PATH]
125        )
126        self.assertEqual(
127            test_finder_handler._get_test_reference_types('SomeClass.java'),
128            [REF_TYPE.MODULE_FILE_PATH]
129        )
130        self.assertEqual(
131            test_finder_handler._get_test_reference_types('SomeClass.kt'),
132            [REF_TYPE.MODULE_FILE_PATH]
133        )
134        self.assertEqual(
135            test_finder_handler._get_test_reference_types('Android.mk'),
136            [REF_TYPE.MODULE_FILE_PATH]
137        )
138        self.assertEqual(
139            test_finder_handler._get_test_reference_types('Android.bp'),
140            [REF_TYPE.MODULE_FILE_PATH]
141        )
142        self.assertEqual(
143            test_finder_handler._get_test_reference_types('SomeTest.cc'),
144            [REF_TYPE.MODULE_FILE_PATH]
145        )
146        self.assertEqual(
147            test_finder_handler._get_test_reference_types('SomeTest.cpp'),
148            [REF_TYPE.MODULE_FILE_PATH]
149        )
150        self.assertEqual(
151            test_finder_handler._get_test_reference_types('SomeTest.cc#method'),
152            [REF_TYPE.MODULE_FILE_PATH]
153        )
154        self.assertEqual(
155            test_finder_handler._get_test_reference_types('module:Class'),
156            [REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS]
157        )
158        self.assertEqual(
159            test_finder_handler._get_test_reference_types('module:f.q.Class'),
160            [REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS]
161        )
162        self.assertEqual(
163            test_finder_handler._get_test_reference_types('module:a.package'),
164            [REF_TYPE.MODULE_PACKAGE]
165        )
166        self.assertEqual(
167            test_finder_handler._get_test_reference_types('.'),
168            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH,
169             REF_TYPE.SUITE_PLAN_FILE_PATH]
170        )
171        self.assertEqual(
172            test_finder_handler._get_test_reference_types('..'),
173            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH,
174             REF_TYPE.SUITE_PLAN_FILE_PATH]
175        )
176        self.assertEqual(
177            test_finder_handler._get_test_reference_types('./rel/path/to/test'),
178            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH,
179             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.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH,
184             REF_TYPE.INTEGRATION, REF_TYPE.SUITE_PLAN_FILE_PATH]
185        )
186        self.assertEqual(
187            test_finder_handler._get_test_reference_types('/abs/path/to/test'),
188            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH,
189             REF_TYPE.SUITE_PLAN_FILE_PATH]
190        )
191        self.assertEqual(
192            test_finder_handler._get_test_reference_types('int/test'),
193            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH,
194             REF_TYPE.INTEGRATION, REF_TYPE.SUITE_PLAN_FILE_PATH]
195        )
196        self.assertEqual(
197            test_finder_handler._get_test_reference_types('int/test:fully.qual.Class#m'),
198            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH,
199             REF_TYPE.INTEGRATION, REF_TYPE.SUITE_PLAN_FILE_PATH]
200        )
201        self.assertEqual(
202            test_finder_handler._get_test_reference_types('int/test:Class#method'),
203            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH,
204             REF_TYPE.INTEGRATION, REF_TYPE.SUITE_PLAN_FILE_PATH]
205        )
206        self.assertEqual(
207            test_finder_handler._get_test_reference_types('int_name_no_slash:Class#m'),
208            [REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS]
209        )
210
211    def test_get_registered_find_methods(self):
212        """Test that we get the registered find methods."""
213        empty_mod_info = None
214        example_finder_a_instance = test_finder_handler._get_finder_instance_dict(
215            empty_mod_info)[_EXAMPLE_FINDER_A]
216        should_equal = [
217            test_finder_base.Finder(
218                example_finder_a_instance,
219                ExampleFinderA.registered_find_method_from_example_finder,
220                _EXAMPLE_FINDER_A)]
221        should_not_equal = [
222            test_finder_base.Finder(
223                example_finder_a_instance,
224                ExampleFinderA.unregistered_find_method_from_example_finder,
225                _EXAMPLE_FINDER_A)]
226        # Let's make sure we see the registered method.
227        self.assertEqual(
228            should_equal,
229            test_finder_handler._get_registered_find_methods(empty_mod_info)
230        )
231        # Make sure we don't see the unregistered method here.
232        self.assertNotEqual(
233            should_not_equal,
234            test_finder_handler._get_registered_find_methods(empty_mod_info)
235        )
236
237    def test_get_find_methods_for_test(self):
238        """Test that we get the find methods we expect."""
239        # Let's see that we get the unregistered and registered find methods in
240        # the order we expect.
241        test = ''
242        registered_find_methods = [
243            test_finder_base.Finder(
244                _FINDER_INSTANCES[_EXAMPLE_FINDER_A],
245                ExampleFinderA.registered_find_method_from_example_finder,
246                _EXAMPLE_FINDER_A)]
247        default_find_methods = [
248            test_finder_base.Finder(
249                _FINDER_INSTANCES[_EXAMPLE_FINDER_A],
250                ExampleFinderA.unregistered_find_method_from_example_finder,
251                _EXAMPLE_FINDER_A)]
252        should_equal = registered_find_methods + default_find_methods
253        self.assertEqual(
254            should_equal,
255            test_finder_handler.get_find_methods_for_test(self.empty_mod_info,
256                                                          test))
257
258
259if __name__ == '__main__':
260    unittest.main()
261