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)]).start()
86
87    def tearDown(self):
88        """Tear down."""
89        mock.patch.stopall()
90
91    def test_get_test_reference_types(self):
92        """Test _get_test_reference_types parses reference types correctly."""
93        self.assertEqual(
94            test_finder_handler._get_test_reference_types('ModuleOrClassName'),
95            [REF_TYPE.INTEGRATION, REF_TYPE.MODULE, REF_TYPE.CLASS]
96        )
97        self.assertEqual(
98            test_finder_handler._get_test_reference_types('Module_or_Class_name'),
99            [REF_TYPE.INTEGRATION, REF_TYPE.MODULE, REF_TYPE.CLASS]
100        )
101        self.assertEqual(
102            test_finder_handler._get_test_reference_types('some.package'),
103            [REF_TYPE.PACKAGE]
104        )
105        self.assertEqual(
106            test_finder_handler._get_test_reference_types('fully.q.Class'),
107            [REF_TYPE.QUALIFIED_CLASS]
108        )
109        self.assertEqual(
110            test_finder_handler._get_test_reference_types('Integration.xml'),
111            [REF_TYPE.INTEGRATION_FILE_PATH]
112        )
113        self.assertEqual(
114            test_finder_handler._get_test_reference_types('SomeClass.java'),
115            [REF_TYPE.MODULE_FILE_PATH]
116        )
117        self.assertEqual(
118            test_finder_handler._get_test_reference_types('Android.mk'),
119            [REF_TYPE.MODULE_FILE_PATH]
120        )
121        self.assertEqual(
122            test_finder_handler._get_test_reference_types('Android.bp'),
123            [REF_TYPE.MODULE_FILE_PATH]
124        )
125        self.assertEqual(
126            test_finder_handler._get_test_reference_types('module:Class'),
127            [REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS]
128        )
129        self.assertEqual(
130            test_finder_handler._get_test_reference_types('module:f.q.Class'),
131            [REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS]
132        )
133        self.assertEqual(
134            test_finder_handler._get_test_reference_types('module:a.package'),
135            [REF_TYPE.MODULE_PACKAGE]
136        )
137        self.assertEqual(
138            test_finder_handler._get_test_reference_types('.'),
139            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH]
140        )
141        self.assertEqual(
142            test_finder_handler._get_test_reference_types('..'),
143            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH]
144        )
145        self.assertEqual(
146            test_finder_handler._get_test_reference_types('./rel/path/to/test'),
147            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH]
148        )
149        self.assertEqual(
150            test_finder_handler._get_test_reference_types('rel/path/to/test'),
151            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH,
152             REF_TYPE.INTEGRATION]
153        )
154        self.assertEqual(
155            test_finder_handler._get_test_reference_types('/abs/path/to/test'),
156            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH]
157        )
158        self.assertEqual(
159            test_finder_handler._get_test_reference_types('int/test'),
160            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH,
161             REF_TYPE.INTEGRATION]
162        )
163        self.assertEqual(
164            test_finder_handler._get_test_reference_types('int/test:fully.qual.Class#m'),
165            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH,
166             REF_TYPE.INTEGRATION]
167        )
168        self.assertEqual(
169            test_finder_handler._get_test_reference_types('int/test:Class#method'),
170            [REF_TYPE.INTEGRATION_FILE_PATH, REF_TYPE.MODULE_FILE_PATH,
171             REF_TYPE.INTEGRATION]
172        )
173        self.assertEqual(
174            test_finder_handler._get_test_reference_types('int_name_no_slash:Class#m'),
175            [REF_TYPE.INTEGRATION, REF_TYPE.MODULE_CLASS]
176        )
177
178    def test_get_registered_find_methods(self):
179        """Test that we get the registered find methods."""
180        empty_mod_info = None
181        example_finder_a_instance = test_finder_handler._get_finder_instance_dict(
182            empty_mod_info)[_EXAMPLE_FINDER_A]
183        should_equal = [
184            test_finder_base.Finder(
185                example_finder_a_instance,
186                ExampleFinderA.registered_find_method_from_example_finder)]
187        should_not_equal = [
188            test_finder_base.Finder(
189                example_finder_a_instance,
190                ExampleFinderA.unregistered_find_method_from_example_finder)]
191        # Let's make sure we see the registered method.
192        self.assertEqual(
193            should_equal,
194            test_finder_handler._get_registered_find_methods(empty_mod_info)
195        )
196        # Make sure we don't see the unregistered method here.
197        self.assertNotEqual(
198            should_not_equal,
199            test_finder_handler._get_registered_find_methods(empty_mod_info)
200        )
201
202    def test_get_find_methods_for_test(self):
203        """Test that we get the find methods we expect."""
204        # Let's see that we get the unregistered and registered find methods in
205        # the order we expect.
206        test = ''
207        registered_find_methods = [
208            test_finder_base.Finder(
209                _FINDER_INSTANCES[_EXAMPLE_FINDER_A],
210                ExampleFinderA.registered_find_method_from_example_finder)]
211        default_find_methods = [
212            test_finder_base.Finder(
213                _FINDER_INSTANCES[_EXAMPLE_FINDER_A],
214                ExampleFinderA.unregistered_find_method_from_example_finder)]
215        should_equal = registered_find_methods + default_find_methods
216        self.assertEqual(
217            should_equal,
218            test_finder_handler.get_find_methods_for_test(self.empty_mod_info,
219                                                          test))
220
221
222if __name__ == '__main__':
223    unittest.main()
224