xref: /aosp_15_r20/tools/asuite/atest/cli_translator_unittest.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c2e18aaaSAndroid Build Coastguard Worker#
3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2017, The Android Open Source Project
4*c2e18aaaSAndroid Build Coastguard Worker#
5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*c2e18aaaSAndroid Build Coastguard Worker#
9*c2e18aaaSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*c2e18aaaSAndroid Build Coastguard Worker#
11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License.
16*c2e18aaaSAndroid Build Coastguard Worker
17*c2e18aaaSAndroid Build Coastguard Worker"""Unittests for cli_translator."""
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Workerfrom importlib import reload
20*c2e18aaaSAndroid Build Coastguard Workerfrom io import StringIO
21*c2e18aaaSAndroid Build Coastguard Workerimport json
22*c2e18aaaSAndroid Build Coastguard Workerimport os
23*c2e18aaaSAndroid Build Coastguard Workerimport re
24*c2e18aaaSAndroid Build Coastguard Workerimport sys
25*c2e18aaaSAndroid Build Coastguard Workerimport tempfile
26*c2e18aaaSAndroid Build Coastguard Workerimport unittest
27*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock
28*c2e18aaaSAndroid Build Coastguard Workerfrom atest import arg_parser
29*c2e18aaaSAndroid Build Coastguard Workerfrom atest import atest_utils
30*c2e18aaaSAndroid Build Coastguard Workerfrom atest import cli_translator as cli_t
31*c2e18aaaSAndroid Build Coastguard Workerfrom atest import constants
32*c2e18aaaSAndroid Build Coastguard Workerfrom atest import module_info
33*c2e18aaaSAndroid Build Coastguard Workerfrom atest import test_finder_handler
34*c2e18aaaSAndroid Build Coastguard Workerfrom atest import test_mapping
35*c2e18aaaSAndroid Build Coastguard Workerfrom atest import unittest_constants as uc
36*c2e18aaaSAndroid Build Coastguard Workerfrom atest import unittest_utils
37*c2e18aaaSAndroid Build Coastguard Workerfrom atest.metrics import metrics
38*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_finders import module_finder
39*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_finders import test_finder_base
40*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_finders import test_finder_utils
41*c2e18aaaSAndroid Build Coastguard Workerfrom pyfakefs import fake_filesystem_unittest
42*c2e18aaaSAndroid Build Coastguard Worker
43*c2e18aaaSAndroid Build Coastguard Worker
44*c2e18aaaSAndroid Build Coastguard Worker# TEST_MAPPING related consts
45*c2e18aaaSAndroid Build Coastguard WorkerTEST_MAPPING_TOP_DIR = os.path.join(uc.TEST_DATA_DIR, 'test_mapping')
46*c2e18aaaSAndroid Build Coastguard WorkerTEST_MAPPING_DIR = os.path.join(TEST_MAPPING_TOP_DIR, 'folder1')
47*c2e18aaaSAndroid Build Coastguard WorkerTEST_1 = test_mapping.TestDetail({'name': 'test1', 'host': True})
48*c2e18aaaSAndroid Build Coastguard WorkerTEST_2 = test_mapping.TestDetail({'name': 'test2'})
49*c2e18aaaSAndroid Build Coastguard WorkerTEST_3 = test_mapping.TestDetail({'name': 'test3'})
50*c2e18aaaSAndroid Build Coastguard WorkerTEST_4 = test_mapping.TestDetail({'name': 'test4'})
51*c2e18aaaSAndroid Build Coastguard WorkerTEST_5 = test_mapping.TestDetail({'name': 'test5'})
52*c2e18aaaSAndroid Build Coastguard WorkerTEST_6 = test_mapping.TestDetail({'name': 'test6'})
53*c2e18aaaSAndroid Build Coastguard WorkerTEST_7 = test_mapping.TestDetail({'name': 'test7'})
54*c2e18aaaSAndroid Build Coastguard WorkerTEST_8 = test_mapping.TestDetail({'name': 'test8'})
55*c2e18aaaSAndroid Build Coastguard WorkerTEST_9 = test_mapping.TestDetail({'name': 'test9'})
56*c2e18aaaSAndroid Build Coastguard WorkerTEST_10 = test_mapping.TestDetail({'name': 'test10'})
57*c2e18aaaSAndroid Build Coastguard Worker
58*c2e18aaaSAndroid Build Coastguard WorkerSEARCH_DIR_RE = re.compile(r'^find ([^ ]*).*$')
59*c2e18aaaSAndroid Build Coastguard WorkerBUILD_TOP_DIR = tempfile.TemporaryDirectory().name
60*c2e18aaaSAndroid Build Coastguard WorkerPRODUCT_OUT_DIR = os.path.join(BUILD_TOP_DIR, 'out/target/product/vsoc_x86_64')
61*c2e18aaaSAndroid Build Coastguard WorkerHOST_OUT_DIR = os.path.join(BUILD_TOP_DIR, 'out/host/linux-x86')
62*c2e18aaaSAndroid Build Coastguard Worker
63*c2e18aaaSAndroid Build Coastguard Worker
64*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=unused-argument
65*c2e18aaaSAndroid Build Coastguard Workerdef gettestinfos_side_effect(
66*c2e18aaaSAndroid Build Coastguard Worker    test_names, test_mapping_test_details=None, is_rebuild_module_info=False
67*c2e18aaaSAndroid Build Coastguard Worker):
68*c2e18aaaSAndroid Build Coastguard Worker  """Mock return values for _get_test_info."""
69*c2e18aaaSAndroid Build Coastguard Worker  test_infos = []
70*c2e18aaaSAndroid Build Coastguard Worker  for test_name in test_names:
71*c2e18aaaSAndroid Build Coastguard Worker    if test_name == uc.MODULE_NAME:
72*c2e18aaaSAndroid Build Coastguard Worker      test_infos.append(uc.MODULE_INFO)
73*c2e18aaaSAndroid Build Coastguard Worker    if test_name == uc.CLASS_NAME:
74*c2e18aaaSAndroid Build Coastguard Worker      test_infos.append(uc.CLASS_INFO)
75*c2e18aaaSAndroid Build Coastguard Worker    if test_name == uc.HOST_UNIT_TEST_NAME_1:
76*c2e18aaaSAndroid Build Coastguard Worker      test_infos.append(uc.MODULE_INFO_HOST_1)
77*c2e18aaaSAndroid Build Coastguard Worker    if test_name == uc.HOST_UNIT_TEST_NAME_2:
78*c2e18aaaSAndroid Build Coastguard Worker      test_infos.append(uc.MODULE_INFO_HOST_2)
79*c2e18aaaSAndroid Build Coastguard Worker  return test_infos
80*c2e18aaaSAndroid Build Coastguard Worker
81*c2e18aaaSAndroid Build Coastguard Worker
82*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=protected-access
83*c2e18aaaSAndroid Build Coastguard Workerclass CLITranslatorUnittests(unittest.TestCase):
84*c2e18aaaSAndroid Build Coastguard Worker  """Unit tests for cli_t.py"""
85*c2e18aaaSAndroid Build Coastguard Worker
86*c2e18aaaSAndroid Build Coastguard Worker  def setUp(self):
87*c2e18aaaSAndroid Build Coastguard Worker    """Run before execution of every test"""
88*c2e18aaaSAndroid Build Coastguard Worker    self.ctr = cli_t.CLITranslator()
89*c2e18aaaSAndroid Build Coastguard Worker
90*c2e18aaaSAndroid Build Coastguard Worker    # Create a mock of args.
91*c2e18aaaSAndroid Build Coastguard Worker    self.args = arg_parser.create_atest_arg_parser().parse_args()
92*c2e18aaaSAndroid Build Coastguard Worker    self.args.tests = []
93*c2e18aaaSAndroid Build Coastguard Worker    # Test mapping related args
94*c2e18aaaSAndroid Build Coastguard Worker    self.args.test_mapping = False
95*c2e18aaaSAndroid Build Coastguard Worker    self.args.include_subdirs = False
96*c2e18aaaSAndroid Build Coastguard Worker    self.args.enable_file_patterns = False
97*c2e18aaaSAndroid Build Coastguard Worker    self.args.rebuild_module_info = False
98*c2e18aaaSAndroid Build Coastguard Worker    # Cache finder related args
99*c2e18aaaSAndroid Build Coastguard Worker    self.args.clear_cache = False
100*c2e18aaaSAndroid Build Coastguard Worker    self.ctr.mod_info = mock.Mock
101*c2e18aaaSAndroid Build Coastguard Worker    self.ctr.mod_info.name_to_module_info = {}
102*c2e18aaaSAndroid Build Coastguard Worker
103*c2e18aaaSAndroid Build Coastguard Worker  def tearDown(self):
104*c2e18aaaSAndroid Build Coastguard Worker    """Run after execution of every test"""
105*c2e18aaaSAndroid Build Coastguard Worker    reload(uc)
106*c2e18aaaSAndroid Build Coastguard Worker
107*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(atest_utils, 'update_test_info_cache')
108*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch('builtins.input', return_value='n')
109*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(module_finder.ModuleFinder, 'find_test_by_module_name')
110*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(module_finder.ModuleFinder, 'get_fuzzy_searching_results')
111*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(metrics, 'FindTestFinishEvent')
112*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(test_finder_handler, 'get_find_methods_for_test')
113*c2e18aaaSAndroid Build Coastguard Worker  # pylint: disable=too-many-locals
114*c2e18aaaSAndroid Build Coastguard Worker  def test_get_test_infos(
115*c2e18aaaSAndroid Build Coastguard Worker      self,
116*c2e18aaaSAndroid Build Coastguard Worker      mock_getfindmethods,
117*c2e18aaaSAndroid Build Coastguard Worker      _metrics,
118*c2e18aaaSAndroid Build Coastguard Worker      mock_getfuzzyresults,
119*c2e18aaaSAndroid Build Coastguard Worker      mock_findtestbymodule,
120*c2e18aaaSAndroid Build Coastguard Worker      mock_input,
121*c2e18aaaSAndroid Build Coastguard Worker      _mock_update_test_info,
122*c2e18aaaSAndroid Build Coastguard Worker  ):
123*c2e18aaaSAndroid Build Coastguard Worker    """Test _get_test_infos method."""
124*c2e18aaaSAndroid Build Coastguard Worker    ctr = cli_t.CLITranslator()
125*c2e18aaaSAndroid Build Coastguard Worker    find_method_return_module_info = lambda x, y: uc.MODULE_INFOS
126*c2e18aaaSAndroid Build Coastguard Worker    # pylint: disable=invalid-name
127*c2e18aaaSAndroid Build Coastguard Worker    find_method_return_module_class_info = (
128*c2e18aaaSAndroid Build Coastguard Worker        lambda x, test: uc.MODULE_INFOS
129*c2e18aaaSAndroid Build Coastguard Worker        if test == uc.MODULE_NAME
130*c2e18aaaSAndroid Build Coastguard Worker        else uc.CLASS_INFOS
131*c2e18aaaSAndroid Build Coastguard Worker    )
132*c2e18aaaSAndroid Build Coastguard Worker    find_method_return_nothing = lambda x, y: None
133*c2e18aaaSAndroid Build Coastguard Worker    one_test = [uc.MODULE_NAME]
134*c2e18aaaSAndroid Build Coastguard Worker    mult_test = [uc.MODULE_NAME, uc.CLASS_NAME]
135*c2e18aaaSAndroid Build Coastguard Worker
136*c2e18aaaSAndroid Build Coastguard Worker    # Let's make sure we return what we expect.
137*c2e18aaaSAndroid Build Coastguard Worker    expected_test_infos = [uc.MODULE_INFO]
138*c2e18aaaSAndroid Build Coastguard Worker    mock_getfindmethods.return_value = [
139*c2e18aaaSAndroid Build Coastguard Worker        test_finder_base.Finder(None, find_method_return_module_info, None)
140*c2e18aaaSAndroid Build Coastguard Worker    ]
141*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_equal_testinfo_lists(
142*c2e18aaaSAndroid Build Coastguard Worker        self, ctr._get_test_infos(one_test), expected_test_infos
143*c2e18aaaSAndroid Build Coastguard Worker    )
144*c2e18aaaSAndroid Build Coastguard Worker
145*c2e18aaaSAndroid Build Coastguard Worker    # Check we receive multiple test infos.
146*c2e18aaaSAndroid Build Coastguard Worker    expected_test_infos = [uc.MODULE_INFO, uc.CLASS_INFO]
147*c2e18aaaSAndroid Build Coastguard Worker    mock_getfindmethods.return_value = [
148*c2e18aaaSAndroid Build Coastguard Worker        test_finder_base.Finder(
149*c2e18aaaSAndroid Build Coastguard Worker            None, find_method_return_module_class_info, None
150*c2e18aaaSAndroid Build Coastguard Worker        )
151*c2e18aaaSAndroid Build Coastguard Worker    ]
152*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_equal_testinfo_lists(
153*c2e18aaaSAndroid Build Coastguard Worker        self, ctr._get_test_infos(mult_test), expected_test_infos
154*c2e18aaaSAndroid Build Coastguard Worker    )
155*c2e18aaaSAndroid Build Coastguard Worker
156*c2e18aaaSAndroid Build Coastguard Worker    # Check return null set when we have no tests found or multiple results.
157*c2e18aaaSAndroid Build Coastguard Worker    mock_getfindmethods.return_value = [
158*c2e18aaaSAndroid Build Coastguard Worker        test_finder_base.Finder(None, find_method_return_nothing, None)
159*c2e18aaaSAndroid Build Coastguard Worker    ]
160*c2e18aaaSAndroid Build Coastguard Worker    null_test_info = []
161*c2e18aaaSAndroid Build Coastguard Worker    mock_getfuzzyresults.return_value = []
162*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(null_test_info, ctr._get_test_infos(one_test))
163*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(null_test_info, ctr._get_test_infos(mult_test))
164*c2e18aaaSAndroid Build Coastguard Worker
165*c2e18aaaSAndroid Build Coastguard Worker    # Check returning test_info when the user says Yes.
166*c2e18aaaSAndroid Build Coastguard Worker    mock_input.return_value = 'Y'
167*c2e18aaaSAndroid Build Coastguard Worker    mock_getfindmethods.return_value = [
168*c2e18aaaSAndroid Build Coastguard Worker        test_finder_base.Finder(None, find_method_return_module_info, None)
169*c2e18aaaSAndroid Build Coastguard Worker    ]
170*c2e18aaaSAndroid Build Coastguard Worker    mock_getfuzzyresults.return_value = one_test
171*c2e18aaaSAndroid Build Coastguard Worker    mock_findtestbymodule.return_value = uc.MODULE_INFO
172*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_equal_testinfo_lists(
173*c2e18aaaSAndroid Build Coastguard Worker        self, ctr._get_test_infos([uc.TYPO_MODULE_NAME]), [uc.MODULE_INFO]
174*c2e18aaaSAndroid Build Coastguard Worker    )
175*c2e18aaaSAndroid Build Coastguard Worker
176*c2e18aaaSAndroid Build Coastguard Worker    # Check the method works for test mapping.
177*c2e18aaaSAndroid Build Coastguard Worker    test_detail1 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST)
178*c2e18aaaSAndroid Build Coastguard Worker    test_detail2 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_OPTION)
179*c2e18aaaSAndroid Build Coastguard Worker    expected_test_infos = [uc.MODULE_INFO, uc.CLASS_INFO]
180*c2e18aaaSAndroid Build Coastguard Worker    mock_getfindmethods.return_value = [
181*c2e18aaaSAndroid Build Coastguard Worker        test_finder_base.Finder(
182*c2e18aaaSAndroid Build Coastguard Worker            None, find_method_return_module_class_info, None
183*c2e18aaaSAndroid Build Coastguard Worker        )
184*c2e18aaaSAndroid Build Coastguard Worker    ]
185*c2e18aaaSAndroid Build Coastguard Worker    test_infos = ctr._get_test_infos(mult_test, [test_detail1, test_detail2])
186*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_equal_testinfo_lists(
187*c2e18aaaSAndroid Build Coastguard Worker        self, test_infos, expected_test_infos
188*c2e18aaaSAndroid Build Coastguard Worker    )
189*c2e18aaaSAndroid Build Coastguard Worker    for test_info in test_infos:
190*c2e18aaaSAndroid Build Coastguard Worker      if test_info == uc.MODULE_INFO:
191*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(
192*c2e18aaaSAndroid Build Coastguard Worker            test_detail1.options, test_info.data[constants.TI_MODULE_ARG]
193*c2e18aaaSAndroid Build Coastguard Worker        )
194*c2e18aaaSAndroid Build Coastguard Worker      else:
195*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(
196*c2e18aaaSAndroid Build Coastguard Worker            test_detail2.options, test_info.data[constants.TI_MODULE_ARG]
197*c2e18aaaSAndroid Build Coastguard Worker        )
198*c2e18aaaSAndroid Build Coastguard Worker
199*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(atest_utils, 'update_test_info_cache')
200*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(metrics, 'FindTestFinishEvent')
201*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(test_finder_handler, 'get_find_methods_for_test')
202*c2e18aaaSAndroid Build Coastguard Worker  def test_get_test_infos_2(
203*c2e18aaaSAndroid Build Coastguard Worker      self, mock_getfindmethods, _metrics, _mock_update_test_info
204*c2e18aaaSAndroid Build Coastguard Worker  ):
205*c2e18aaaSAndroid Build Coastguard Worker    """Test _get_test_infos method."""
206*c2e18aaaSAndroid Build Coastguard Worker    ctr = cli_t.CLITranslator()
207*c2e18aaaSAndroid Build Coastguard Worker    find_method_return_module_info2 = lambda x, y: uc.MODULE_INFOS2
208*c2e18aaaSAndroid Build Coastguard Worker    find_method_ret_mod_cls_info2 = (
209*c2e18aaaSAndroid Build Coastguard Worker        lambda x, test: uc.MODULE_INFOS2
210*c2e18aaaSAndroid Build Coastguard Worker        if test == uc.MODULE_NAME
211*c2e18aaaSAndroid Build Coastguard Worker        else uc.CLASS_INFOS2
212*c2e18aaaSAndroid Build Coastguard Worker    )
213*c2e18aaaSAndroid Build Coastguard Worker    one_test = [uc.MODULE_NAME]
214*c2e18aaaSAndroid Build Coastguard Worker    mult_test = [uc.MODULE_NAME, uc.CLASS_NAME]
215*c2e18aaaSAndroid Build Coastguard Worker    # Let's make sure we return what we expect.
216*c2e18aaaSAndroid Build Coastguard Worker    expected_test_infos = [uc.MODULE_INFO, uc.MODULE_INFO2]
217*c2e18aaaSAndroid Build Coastguard Worker    mock_getfindmethods.return_value = [
218*c2e18aaaSAndroid Build Coastguard Worker        test_finder_base.Finder(None, find_method_return_module_info2, None)
219*c2e18aaaSAndroid Build Coastguard Worker    ]
220*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_equal_testinfo_lists(
221*c2e18aaaSAndroid Build Coastguard Worker        self, ctr._get_test_infos(one_test), expected_test_infos
222*c2e18aaaSAndroid Build Coastguard Worker    )
223*c2e18aaaSAndroid Build Coastguard Worker    # Check we receive multiple test infos.
224*c2e18aaaSAndroid Build Coastguard Worker    expected_test_infos = [
225*c2e18aaaSAndroid Build Coastguard Worker        uc.MODULE_INFO,
226*c2e18aaaSAndroid Build Coastguard Worker        uc.MODULE_INFO2,
227*c2e18aaaSAndroid Build Coastguard Worker        uc.CLASS_INFO,
228*c2e18aaaSAndroid Build Coastguard Worker        uc.CLASS_INFO2,
229*c2e18aaaSAndroid Build Coastguard Worker    ]
230*c2e18aaaSAndroid Build Coastguard Worker    mock_getfindmethods.return_value = [
231*c2e18aaaSAndroid Build Coastguard Worker        test_finder_base.Finder(None, find_method_ret_mod_cls_info2, None)
232*c2e18aaaSAndroid Build Coastguard Worker    ]
233*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_equal_testinfo_lists(
234*c2e18aaaSAndroid Build Coastguard Worker        self, ctr._get_test_infos(mult_test), expected_test_infos
235*c2e18aaaSAndroid Build Coastguard Worker    )
236*c2e18aaaSAndroid Build Coastguard Worker    # Check the method works for test mapping.
237*c2e18aaaSAndroid Build Coastguard Worker    test_detail1 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST)
238*c2e18aaaSAndroid Build Coastguard Worker    test_detail2 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_OPTION)
239*c2e18aaaSAndroid Build Coastguard Worker    expected_test_infos = [
240*c2e18aaaSAndroid Build Coastguard Worker        uc.MODULE_INFO,
241*c2e18aaaSAndroid Build Coastguard Worker        uc.MODULE_INFO2,
242*c2e18aaaSAndroid Build Coastguard Worker        uc.CLASS_INFO,
243*c2e18aaaSAndroid Build Coastguard Worker        uc.CLASS_INFO2,
244*c2e18aaaSAndroid Build Coastguard Worker    ]
245*c2e18aaaSAndroid Build Coastguard Worker    mock_getfindmethods.return_value = [
246*c2e18aaaSAndroid Build Coastguard Worker        test_finder_base.Finder(None, find_method_ret_mod_cls_info2, None)
247*c2e18aaaSAndroid Build Coastguard Worker    ]
248*c2e18aaaSAndroid Build Coastguard Worker    test_infos = ctr._get_test_infos(mult_test, [test_detail1, test_detail2])
249*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_equal_testinfo_lists(
250*c2e18aaaSAndroid Build Coastguard Worker        self, test_infos, expected_test_infos
251*c2e18aaaSAndroid Build Coastguard Worker    )
252*c2e18aaaSAndroid Build Coastguard Worker    for test_info in test_infos:
253*c2e18aaaSAndroid Build Coastguard Worker      if test_info in [uc.MODULE_INFO, uc.MODULE_INFO2]:
254*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(
255*c2e18aaaSAndroid Build Coastguard Worker            test_detail1.options, test_info.data[constants.TI_MODULE_ARG]
256*c2e18aaaSAndroid Build Coastguard Worker        )
257*c2e18aaaSAndroid Build Coastguard Worker      elif test_info in [uc.CLASS_INFO, uc.CLASS_INFO2]:
258*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(
259*c2e18aaaSAndroid Build Coastguard Worker            test_detail2.options, test_info.data[constants.TI_MODULE_ARG]
260*c2e18aaaSAndroid Build Coastguard Worker        )
261*c2e18aaaSAndroid Build Coastguard Worker
262*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(module_finder.ModuleFinder, 'get_fuzzy_searching_results')
263*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(metrics, 'FindTestFinishEvent')
264*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(test_finder_handler, 'get_find_methods_for_test')
265*c2e18aaaSAndroid Build Coastguard Worker  def test_get_test_infos_with_mod_info(
266*c2e18aaaSAndroid Build Coastguard Worker      self,
267*c2e18aaaSAndroid Build Coastguard Worker      mock_getfindmethods,
268*c2e18aaaSAndroid Build Coastguard Worker      _metrics,
269*c2e18aaaSAndroid Build Coastguard Worker      mock_getfuzzyresults,
270*c2e18aaaSAndroid Build Coastguard Worker  ):
271*c2e18aaaSAndroid Build Coastguard Worker    """Test _get_test_infos method."""
272*c2e18aaaSAndroid Build Coastguard Worker    mod_info = module_info.load_from_file(
273*c2e18aaaSAndroid Build Coastguard Worker        module_file=os.path.join(uc.TEST_DATA_DIR, uc.JSON_FILE)
274*c2e18aaaSAndroid Build Coastguard Worker    )
275*c2e18aaaSAndroid Build Coastguard Worker    ctr = cli_t.CLITranslator(mod_info=mod_info)
276*c2e18aaaSAndroid Build Coastguard Worker    null_test_info = []
277*c2e18aaaSAndroid Build Coastguard Worker    mock_getfindmethods.return_value = []
278*c2e18aaaSAndroid Build Coastguard Worker    mock_getfuzzyresults.return_value = []
279*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_equal_testinfo_lists(
280*c2e18aaaSAndroid Build Coastguard Worker        self, ctr._get_test_infos('not_exist_module'), null_test_info
281*c2e18aaaSAndroid Build Coastguard Worker    )
282*c2e18aaaSAndroid Build Coastguard Worker
283*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(
284*c2e18aaaSAndroid Build Coastguard Worker      test_finder_utils, 'find_host_unit_tests', return_value=set()
285*c2e18aaaSAndroid Build Coastguard Worker  )
286*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(
287*c2e18aaaSAndroid Build Coastguard Worker      cli_t.CLITranslator,
288*c2e18aaaSAndroid Build Coastguard Worker      '_get_test_infos',
289*c2e18aaaSAndroid Build Coastguard Worker      side_effect=gettestinfos_side_effect,
290*c2e18aaaSAndroid Build Coastguard Worker  )
291*c2e18aaaSAndroid Build Coastguard Worker  def test_translate_class(self, _info, _find):
292*c2e18aaaSAndroid Build Coastguard Worker    """Test translate method for tests by class name."""
293*c2e18aaaSAndroid Build Coastguard Worker    # Check that we can find a class.
294*c2e18aaaSAndroid Build Coastguard Worker    self.args.tests = [uc.CLASS_NAME]
295*c2e18aaaSAndroid Build Coastguard Worker    self.args.host_unit_test_only = False
296*c2e18aaaSAndroid Build Coastguard Worker    test_infos = self.ctr.translate(self.args)
297*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_strict_equal(
298*c2e18aaaSAndroid Build Coastguard Worker        self, _gather_build_targets(test_infos), uc.CLASS_BUILD_TARGETS
299*c2e18aaaSAndroid Build Coastguard Worker    )
300*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_equal_testinfo_lists(
301*c2e18aaaSAndroid Build Coastguard Worker        self, test_infos, [uc.CLASS_INFO]
302*c2e18aaaSAndroid Build Coastguard Worker    )
303*c2e18aaaSAndroid Build Coastguard Worker
304*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(
305*c2e18aaaSAndroid Build Coastguard Worker      test_finder_utils, 'find_host_unit_tests', return_value=set()
306*c2e18aaaSAndroid Build Coastguard Worker  )
307*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(
308*c2e18aaaSAndroid Build Coastguard Worker      cli_t.CLITranslator,
309*c2e18aaaSAndroid Build Coastguard Worker      '_get_test_infos',
310*c2e18aaaSAndroid Build Coastguard Worker      side_effect=gettestinfos_side_effect,
311*c2e18aaaSAndroid Build Coastguard Worker  )
312*c2e18aaaSAndroid Build Coastguard Worker  def test_translate_module(self, _info, _find):
313*c2e18aaaSAndroid Build Coastguard Worker    """Test translate method for tests by module or class name."""
314*c2e18aaaSAndroid Build Coastguard Worker    # Check that we get all the build targets we expect.
315*c2e18aaaSAndroid Build Coastguard Worker    self.args.tests = [uc.MODULE_NAME, uc.CLASS_NAME]
316*c2e18aaaSAndroid Build Coastguard Worker    self.args.host_unit_test_only = False
317*c2e18aaaSAndroid Build Coastguard Worker    test_infos = self.ctr.translate(self.args)
318*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_strict_equal(
319*c2e18aaaSAndroid Build Coastguard Worker        self,
320*c2e18aaaSAndroid Build Coastguard Worker        _gather_build_targets(test_infos),
321*c2e18aaaSAndroid Build Coastguard Worker        uc.MODULE_CLASS_COMBINED_BUILD_TARGETS,
322*c2e18aaaSAndroid Build Coastguard Worker    )
323*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_equal_testinfo_lists(
324*c2e18aaaSAndroid Build Coastguard Worker        self, test_infos, [uc.MODULE_INFO, uc.CLASS_INFO]
325*c2e18aaaSAndroid Build Coastguard Worker    )
326*c2e18aaaSAndroid Build Coastguard Worker
327*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(os, 'getcwd', return_value='/src/build_top/somewhere')
328*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(test_finder_utils, 'find_host_unit_tests', return_value=[])
329*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(cli_t.CLITranslator, '_find_tests_by_test_mapping')
330*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(
331*c2e18aaaSAndroid Build Coastguard Worker      cli_t.CLITranslator,
332*c2e18aaaSAndroid Build Coastguard Worker      '_get_test_infos',
333*c2e18aaaSAndroid Build Coastguard Worker      side_effect=gettestinfos_side_effect,
334*c2e18aaaSAndroid Build Coastguard Worker  )
335*c2e18aaaSAndroid Build Coastguard Worker  def test_translate_test_mapping(
336*c2e18aaaSAndroid Build Coastguard Worker      self, _info, mock_testmapping, _find_unit_tests, _getcwd
337*c2e18aaaSAndroid Build Coastguard Worker  ):
338*c2e18aaaSAndroid Build Coastguard Worker    """Test translate method for tests in test mapping."""
339*c2e18aaaSAndroid Build Coastguard Worker    # Check that test mappings feeds into get_test_info properly.
340*c2e18aaaSAndroid Build Coastguard Worker    test_detail1 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST)
341*c2e18aaaSAndroid Build Coastguard Worker    test_detail2 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_OPTION)
342*c2e18aaaSAndroid Build Coastguard Worker    mock_testmapping.return_value = ([test_detail1, test_detail2], None)
343*c2e18aaaSAndroid Build Coastguard Worker    self.args.tests = []
344*c2e18aaaSAndroid Build Coastguard Worker    self.args.host = False
345*c2e18aaaSAndroid Build Coastguard Worker    self.args.host_unit_test_only = False
346*c2e18aaaSAndroid Build Coastguard Worker    test_infos = self.ctr.translate(self.args)
347*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_strict_equal(
348*c2e18aaaSAndroid Build Coastguard Worker        self,
349*c2e18aaaSAndroid Build Coastguard Worker        _gather_build_targets(test_infos),
350*c2e18aaaSAndroid Build Coastguard Worker        uc.MODULE_CLASS_COMBINED_BUILD_TARGETS,
351*c2e18aaaSAndroid Build Coastguard Worker    )
352*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_equal_testinfo_lists(
353*c2e18aaaSAndroid Build Coastguard Worker        self, test_infos, [uc.MODULE_INFO, uc.CLASS_INFO]
354*c2e18aaaSAndroid Build Coastguard Worker    )
355*c2e18aaaSAndroid Build Coastguard Worker
356*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(cli_t.CLITranslator, '_find_tests_by_test_mapping')
357*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(
358*c2e18aaaSAndroid Build Coastguard Worker      cli_t.CLITranslator,
359*c2e18aaaSAndroid Build Coastguard Worker      '_get_test_infos',
360*c2e18aaaSAndroid Build Coastguard Worker      side_effect=gettestinfos_side_effect,
361*c2e18aaaSAndroid Build Coastguard Worker  )
362*c2e18aaaSAndroid Build Coastguard Worker  def test_translate_test_mapping_all(self, _info, mock_testmapping):
363*c2e18aaaSAndroid Build Coastguard Worker    """Test translate method for tests in test mapping."""
364*c2e18aaaSAndroid Build Coastguard Worker    # Check that test mappings feeds into get_test_info properly.
365*c2e18aaaSAndroid Build Coastguard Worker    test_detail1 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST)
366*c2e18aaaSAndroid Build Coastguard Worker    test_detail2 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_OPTION)
367*c2e18aaaSAndroid Build Coastguard Worker    mock_testmapping.return_value = ([test_detail1, test_detail2], None)
368*c2e18aaaSAndroid Build Coastguard Worker    self.args.tests = ['src_path:all']
369*c2e18aaaSAndroid Build Coastguard Worker    self.args.test_mapping = True
370*c2e18aaaSAndroid Build Coastguard Worker    self.args.host = False
371*c2e18aaaSAndroid Build Coastguard Worker    test_infos = self.ctr.translate(self.args)
372*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_strict_equal(
373*c2e18aaaSAndroid Build Coastguard Worker        self,
374*c2e18aaaSAndroid Build Coastguard Worker        _gather_build_targets(test_infos),
375*c2e18aaaSAndroid Build Coastguard Worker        uc.MODULE_CLASS_COMBINED_BUILD_TARGETS,
376*c2e18aaaSAndroid Build Coastguard Worker    )
377*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_equal_testinfo_lists(
378*c2e18aaaSAndroid Build Coastguard Worker        self, test_infos, [uc.MODULE_INFO, uc.CLASS_INFO]
379*c2e18aaaSAndroid Build Coastguard Worker    )
380*c2e18aaaSAndroid Build Coastguard Worker
381*c2e18aaaSAndroid Build Coastguard Worker  def test_find_tests_by_test_mapping_presubmit(self):
382*c2e18aaaSAndroid Build Coastguard Worker    """Test _find_tests_by_test_mapping method to locate presubmit tests."""
383*c2e18aaaSAndroid Build Coastguard Worker    # TODO: (b/264015241) Stop mocking build variables.
384*c2e18aaaSAndroid Build Coastguard Worker    os_environ_mock = {constants.ANDROID_BUILD_TOP: uc.TEST_DATA_DIR}
385*c2e18aaaSAndroid Build Coastguard Worker    with mock.patch.dict('os.environ', os_environ_mock, clear=True):
386*c2e18aaaSAndroid Build Coastguard Worker      tests, all_tests = self.ctr._find_tests_by_test_mapping(
387*c2e18aaaSAndroid Build Coastguard Worker          path=TEST_MAPPING_DIR,
388*c2e18aaaSAndroid Build Coastguard Worker          file_name='test_mapping_sample',
389*c2e18aaaSAndroid Build Coastguard Worker          checked_files=set(),
390*c2e18aaaSAndroid Build Coastguard Worker      )
391*c2e18aaaSAndroid Build Coastguard Worker    expected = set([TEST_1, TEST_2, TEST_5, TEST_7, TEST_9])
392*c2e18aaaSAndroid Build Coastguard Worker    expected_all_tests = {
393*c2e18aaaSAndroid Build Coastguard Worker        'presubmit': expected,
394*c2e18aaaSAndroid Build Coastguard Worker        'postsubmit': set([TEST_3, TEST_6, TEST_8, TEST_10]),
395*c2e18aaaSAndroid Build Coastguard Worker        'other_group': set([TEST_4]),
396*c2e18aaaSAndroid Build Coastguard Worker    }
397*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(expected, tests)
398*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(expected_all_tests, all_tests)
399*c2e18aaaSAndroid Build Coastguard Worker
400*c2e18aaaSAndroid Build Coastguard Worker  def test_find_tests_by_test_mapping_postsubmit(self):
401*c2e18aaaSAndroid Build Coastguard Worker    """Test _find_tests_by_test_mapping method to locate postsubmit tests."""
402*c2e18aaaSAndroid Build Coastguard Worker    # TODO: (b/264015241) Stop mocking build variables.
403*c2e18aaaSAndroid Build Coastguard Worker    os_environ_mock = {constants.ANDROID_BUILD_TOP: uc.TEST_DATA_DIR}
404*c2e18aaaSAndroid Build Coastguard Worker    with mock.patch.dict('os.environ', os_environ_mock, clear=True):
405*c2e18aaaSAndroid Build Coastguard Worker      tests, all_tests = self.ctr._find_tests_by_test_mapping(
406*c2e18aaaSAndroid Build Coastguard Worker          path=TEST_MAPPING_DIR,
407*c2e18aaaSAndroid Build Coastguard Worker          test_groups=[constants.TEST_GROUP_POSTSUBMIT],
408*c2e18aaaSAndroid Build Coastguard Worker          file_name='test_mapping_sample',
409*c2e18aaaSAndroid Build Coastguard Worker          checked_files=set(),
410*c2e18aaaSAndroid Build Coastguard Worker      )
411*c2e18aaaSAndroid Build Coastguard Worker    expected_presubmit = set([TEST_1, TEST_2, TEST_5, TEST_7, TEST_9])
412*c2e18aaaSAndroid Build Coastguard Worker    expected = set([TEST_3, TEST_6, TEST_8, TEST_10])
413*c2e18aaaSAndroid Build Coastguard Worker    expected_all_tests = {
414*c2e18aaaSAndroid Build Coastguard Worker        'presubmit': expected_presubmit,
415*c2e18aaaSAndroid Build Coastguard Worker        'postsubmit': set([TEST_3, TEST_6, TEST_8, TEST_10]),
416*c2e18aaaSAndroid Build Coastguard Worker        'other_group': set([TEST_4]),
417*c2e18aaaSAndroid Build Coastguard Worker    }
418*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(expected, tests)
419*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(expected_all_tests, all_tests)
420*c2e18aaaSAndroid Build Coastguard Worker
421*c2e18aaaSAndroid Build Coastguard Worker  def test_find_tests_by_test_mapping_all_group(self):
422*c2e18aaaSAndroid Build Coastguard Worker    """Test _find_tests_by_test_mapping method to locate postsubmit tests."""
423*c2e18aaaSAndroid Build Coastguard Worker    # TODO: (b/264015241) Stop mocking build variables.
424*c2e18aaaSAndroid Build Coastguard Worker    os_environ_mock = {constants.ANDROID_BUILD_TOP: uc.TEST_DATA_DIR}
425*c2e18aaaSAndroid Build Coastguard Worker    with mock.patch.dict('os.environ', os_environ_mock, clear=True):
426*c2e18aaaSAndroid Build Coastguard Worker      tests, all_tests = self.ctr._find_tests_by_test_mapping(
427*c2e18aaaSAndroid Build Coastguard Worker          path=TEST_MAPPING_DIR,
428*c2e18aaaSAndroid Build Coastguard Worker          test_groups=[constants.TEST_GROUP_ALL],
429*c2e18aaaSAndroid Build Coastguard Worker          file_name='test_mapping_sample',
430*c2e18aaaSAndroid Build Coastguard Worker          checked_files=set(),
431*c2e18aaaSAndroid Build Coastguard Worker      )
432*c2e18aaaSAndroid Build Coastguard Worker    expected_presubmit = set([TEST_1, TEST_2, TEST_5, TEST_7, TEST_9])
433*c2e18aaaSAndroid Build Coastguard Worker    expected = set([
434*c2e18aaaSAndroid Build Coastguard Worker        TEST_1,
435*c2e18aaaSAndroid Build Coastguard Worker        TEST_2,
436*c2e18aaaSAndroid Build Coastguard Worker        TEST_3,
437*c2e18aaaSAndroid Build Coastguard Worker        TEST_4,
438*c2e18aaaSAndroid Build Coastguard Worker        TEST_5,
439*c2e18aaaSAndroid Build Coastguard Worker        TEST_6,
440*c2e18aaaSAndroid Build Coastguard Worker        TEST_7,
441*c2e18aaaSAndroid Build Coastguard Worker        TEST_8,
442*c2e18aaaSAndroid Build Coastguard Worker        TEST_9,
443*c2e18aaaSAndroid Build Coastguard Worker        TEST_10,
444*c2e18aaaSAndroid Build Coastguard Worker    ])
445*c2e18aaaSAndroid Build Coastguard Worker    expected_all_tests = {
446*c2e18aaaSAndroid Build Coastguard Worker        'presubmit': expected_presubmit,
447*c2e18aaaSAndroid Build Coastguard Worker        'postsubmit': set([TEST_3, TEST_6, TEST_8, TEST_10]),
448*c2e18aaaSAndroid Build Coastguard Worker        'other_group': set([TEST_4]),
449*c2e18aaaSAndroid Build Coastguard Worker    }
450*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(expected, tests)
451*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(expected_all_tests, all_tests)
452*c2e18aaaSAndroid Build Coastguard Worker
453*c2e18aaaSAndroid Build Coastguard Worker  def test_find_tests_by_test_mapping_include_subdir(self):
454*c2e18aaaSAndroid Build Coastguard Worker    """Test _find_tests_by_test_mapping method to include sub directory."""
455*c2e18aaaSAndroid Build Coastguard Worker    # TODO: (b/264015241) Stop mocking build variables.
456*c2e18aaaSAndroid Build Coastguard Worker    os_environ_mock = {constants.ANDROID_BUILD_TOP: uc.TEST_DATA_DIR}
457*c2e18aaaSAndroid Build Coastguard Worker    with mock.patch.dict('os.environ', os_environ_mock, clear=True):
458*c2e18aaaSAndroid Build Coastguard Worker      tests, all_tests = self.ctr._find_tests_by_test_mapping(
459*c2e18aaaSAndroid Build Coastguard Worker          path=TEST_MAPPING_TOP_DIR,
460*c2e18aaaSAndroid Build Coastguard Worker          file_name='test_mapping_sample',
461*c2e18aaaSAndroid Build Coastguard Worker          include_subdirs=True,
462*c2e18aaaSAndroid Build Coastguard Worker          checked_files=set(),
463*c2e18aaaSAndroid Build Coastguard Worker      )
464*c2e18aaaSAndroid Build Coastguard Worker    expected = set([TEST_1, TEST_2, TEST_5, TEST_7, TEST_9])
465*c2e18aaaSAndroid Build Coastguard Worker    expected_all_tests = {
466*c2e18aaaSAndroid Build Coastguard Worker        'presubmit': expected,
467*c2e18aaaSAndroid Build Coastguard Worker        'postsubmit': set([TEST_3, TEST_6, TEST_8, TEST_10]),
468*c2e18aaaSAndroid Build Coastguard Worker        'other_group': set([TEST_4]),
469*c2e18aaaSAndroid Build Coastguard Worker    }
470*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(expected, tests)
471*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(expected_all_tests, all_tests)
472*c2e18aaaSAndroid Build Coastguard Worker
473*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch('builtins.input', return_value='')
474*c2e18aaaSAndroid Build Coastguard Worker  def test_confirm_running(self, mock_input):
475*c2e18aaaSAndroid Build Coastguard Worker    """Test _confirm_running method."""
476*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue(self.ctr._confirm_running([TEST_1]))
477*c2e18aaaSAndroid Build Coastguard Worker    mock_input.return_value = 'N'
478*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(self.ctr._confirm_running([TEST_2]))
479*c2e18aaaSAndroid Build Coastguard Worker
480*c2e18aaaSAndroid Build Coastguard Worker  def test_print_fuzzy_searching_results(self):
481*c2e18aaaSAndroid Build Coastguard Worker    """Test _print_fuzzy_searching_results"""
482*c2e18aaaSAndroid Build Coastguard Worker    modules = [uc.MODULE_NAME, uc.MODULE2_NAME]
483*c2e18aaaSAndroid Build Coastguard Worker    capture_output = StringIO()
484*c2e18aaaSAndroid Build Coastguard Worker    sys.stdout = capture_output
485*c2e18aaaSAndroid Build Coastguard Worker    self.ctr._print_fuzzy_searching_results(modules)
486*c2e18aaaSAndroid Build Coastguard Worker    sys.stdout = sys.__stdout__
487*c2e18aaaSAndroid Build Coastguard Worker    output = 'Did you mean the following modules?\n{0}\n{1}\n'.format(
488*c2e18aaaSAndroid Build Coastguard Worker        uc.MODULE_NAME, uc.MODULE2_NAME
489*c2e18aaaSAndroid Build Coastguard Worker    )
490*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(capture_output.getvalue(), output)
491*c2e18aaaSAndroid Build Coastguard Worker
492*c2e18aaaSAndroid Build Coastguard Worker  def test_filter_comments(self):
493*c2e18aaaSAndroid Build Coastguard Worker    """Test filter_comments method"""
494*c2e18aaaSAndroid Build Coastguard Worker    file_with_comments = os.path.join(
495*c2e18aaaSAndroid Build Coastguard Worker        TEST_MAPPING_TOP_DIR, 'folder6', 'test_mapping_sample_with_comments'
496*c2e18aaaSAndroid Build Coastguard Worker    )
497*c2e18aaaSAndroid Build Coastguard Worker    file_with_comments_golden = os.path.join(
498*c2e18aaaSAndroid Build Coastguard Worker        TEST_MAPPING_TOP_DIR, 'folder6', 'test_mapping_sample_golden'
499*c2e18aaaSAndroid Build Coastguard Worker    )
500*c2e18aaaSAndroid Build Coastguard Worker    test_mapping_dict = json.loads(self.ctr.filter_comments(file_with_comments))
501*c2e18aaaSAndroid Build Coastguard Worker    test_mapping_dict_gloden = None
502*c2e18aaaSAndroid Build Coastguard Worker    with open(file_with_comments_golden) as json_file:
503*c2e18aaaSAndroid Build Coastguard Worker      test_mapping_dict_gloden = json.load(json_file)
504*c2e18aaaSAndroid Build Coastguard Worker
505*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(test_mapping_dict, test_mapping_dict_gloden)
506*c2e18aaaSAndroid Build Coastguard Worker
507*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(module_info.ModuleInfo, 'get_testable_modules')
508*c2e18aaaSAndroid Build Coastguard Worker  def test_extract_testable_modules_by_wildcard(self, mock_mods):
509*c2e18aaaSAndroid Build Coastguard Worker    """Test _extract_testable_modules_by_wildcard method."""
510*c2e18aaaSAndroid Build Coastguard Worker    mod_info = module_info.load_from_file(
511*c2e18aaaSAndroid Build Coastguard Worker        module_file=os.path.join(uc.TEST_DATA_DIR, uc.JSON_FILE)
512*c2e18aaaSAndroid Build Coastguard Worker    )
513*c2e18aaaSAndroid Build Coastguard Worker    ctr = cli_t.CLITranslator(mod_info=mod_info)
514*c2e18aaaSAndroid Build Coastguard Worker    mock_mods.return_value = [
515*c2e18aaaSAndroid Build Coastguard Worker        'test1',
516*c2e18aaaSAndroid Build Coastguard Worker        'test2',
517*c2e18aaaSAndroid Build Coastguard Worker        'test3',
518*c2e18aaaSAndroid Build Coastguard Worker        'test11',
519*c2e18aaaSAndroid Build Coastguard Worker        'Test22',
520*c2e18aaaSAndroid Build Coastguard Worker        'Test100',
521*c2e18aaaSAndroid Build Coastguard Worker        'aTest101',
522*c2e18aaaSAndroid Build Coastguard Worker    ]
523*c2e18aaaSAndroid Build Coastguard Worker    # test '*'
524*c2e18aaaSAndroid Build Coastguard Worker    expr1 = ['test*']
525*c2e18aaaSAndroid Build Coastguard Worker    result1 = ['test1', 'test2', 'test3', 'test11']
526*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(ctr._extract_testable_modules_by_wildcard(expr1), result1)
527*c2e18aaaSAndroid Build Coastguard Worker    # test '?'
528*c2e18aaaSAndroid Build Coastguard Worker    expr2 = ['test?']
529*c2e18aaaSAndroid Build Coastguard Worker    result2 = ['test1', 'test2', 'test3']
530*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(ctr._extract_testable_modules_by_wildcard(expr2), result2)
531*c2e18aaaSAndroid Build Coastguard Worker    # test '*' and '?'
532*c2e18aaaSAndroid Build Coastguard Worker    expr3 = ['*Test???']
533*c2e18aaaSAndroid Build Coastguard Worker    result3 = ['Test100', 'aTest101']
534*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(ctr._extract_testable_modules_by_wildcard(expr3), result3)
535*c2e18aaaSAndroid Build Coastguard Worker
536*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(os, 'getcwd', return_value='/src/build_top/somewhere')
537*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(
538*c2e18aaaSAndroid Build Coastguard Worker      test_finder_utils,
539*c2e18aaaSAndroid Build Coastguard Worker      'find_host_unit_tests',
540*c2e18aaaSAndroid Build Coastguard Worker      return_value=[uc.HOST_UNIT_TEST_NAME_1, uc.HOST_UNIT_TEST_NAME_2],
541*c2e18aaaSAndroid Build Coastguard Worker  )
542*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(cli_t.CLITranslator, '_find_tests_by_test_mapping')
543*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(
544*c2e18aaaSAndroid Build Coastguard Worker      cli_t.CLITranslator,
545*c2e18aaaSAndroid Build Coastguard Worker      '_get_test_infos',
546*c2e18aaaSAndroid Build Coastguard Worker      side_effect=gettestinfos_side_effect,
547*c2e18aaaSAndroid Build Coastguard Worker  )
548*c2e18aaaSAndroid Build Coastguard Worker  def test_translate_test_mapping_host_unit_test(
549*c2e18aaaSAndroid Build Coastguard Worker      self, _info, mock_testmapping, _find_unit_tests, _getcwd
550*c2e18aaaSAndroid Build Coastguard Worker  ):
551*c2e18aaaSAndroid Build Coastguard Worker    """Test translate method for tests belong to host unit tests."""
552*c2e18aaaSAndroid Build Coastguard Worker    # Check that test mappings feeds into get_test_info properly.
553*c2e18aaaSAndroid Build Coastguard Worker    test_detail1 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST)
554*c2e18aaaSAndroid Build Coastguard Worker    test_detail2 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_OPTION)
555*c2e18aaaSAndroid Build Coastguard Worker    mock_testmapping.return_value = ([test_detail1, test_detail2], None)
556*c2e18aaaSAndroid Build Coastguard Worker    self.args.tests = []
557*c2e18aaaSAndroid Build Coastguard Worker    self.args.host = False
558*c2e18aaaSAndroid Build Coastguard Worker    self.args.host_unit_test_only = False
559*c2e18aaaSAndroid Build Coastguard Worker    test_infos = self.ctr.translate(self.args)
560*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_equal_testinfo_lists(
561*c2e18aaaSAndroid Build Coastguard Worker        self,
562*c2e18aaaSAndroid Build Coastguard Worker        test_infos,
563*c2e18aaaSAndroid Build Coastguard Worker        [
564*c2e18aaaSAndroid Build Coastguard Worker            uc.MODULE_INFO,
565*c2e18aaaSAndroid Build Coastguard Worker            uc.CLASS_INFO,
566*c2e18aaaSAndroid Build Coastguard Worker            uc.MODULE_INFO_HOST_1,
567*c2e18aaaSAndroid Build Coastguard Worker            uc.MODULE_INFO_HOST_2,
568*c2e18aaaSAndroid Build Coastguard Worker        ],
569*c2e18aaaSAndroid Build Coastguard Worker    )
570*c2e18aaaSAndroid Build Coastguard Worker
571*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(
572*c2e18aaaSAndroid Build Coastguard Worker      test_finder_utils,
573*c2e18aaaSAndroid Build Coastguard Worker      'find_host_unit_tests',
574*c2e18aaaSAndroid Build Coastguard Worker      return_value=[uc.HOST_UNIT_TEST_NAME_1, uc.HOST_UNIT_TEST_NAME_2],
575*c2e18aaaSAndroid Build Coastguard Worker  )
576*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(cli_t.CLITranslator, '_find_tests_by_test_mapping')
577*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(
578*c2e18aaaSAndroid Build Coastguard Worker      cli_t.CLITranslator,
579*c2e18aaaSAndroid Build Coastguard Worker      '_get_test_infos',
580*c2e18aaaSAndroid Build Coastguard Worker      side_effect=gettestinfos_side_effect,
581*c2e18aaaSAndroid Build Coastguard Worker  )
582*c2e18aaaSAndroid Build Coastguard Worker  def test_translate_test_mapping_without_host_unit_test(
583*c2e18aaaSAndroid Build Coastguard Worker      self, _info, mock_testmapping, _find_unit_tests
584*c2e18aaaSAndroid Build Coastguard Worker  ):
585*c2e18aaaSAndroid Build Coastguard Worker    """Test translate method not using host unit tests if test_mapping arg ."""
586*c2e18aaaSAndroid Build Coastguard Worker    # Check that test mappings feeds into get_test_info properly.
587*c2e18aaaSAndroid Build Coastguard Worker    test_detail1 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST)
588*c2e18aaaSAndroid Build Coastguard Worker    test_detail2 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_OPTION)
589*c2e18aaaSAndroid Build Coastguard Worker    mock_testmapping.return_value = ([test_detail1, test_detail2], None)
590*c2e18aaaSAndroid Build Coastguard Worker    self.args.tests = []
591*c2e18aaaSAndroid Build Coastguard Worker    self.args.host = False
592*c2e18aaaSAndroid Build Coastguard Worker    self.args.test_mapping = True
593*c2e18aaaSAndroid Build Coastguard Worker    self.args.host_unit_test_only = False
594*c2e18aaaSAndroid Build Coastguard Worker    test_infos = self.ctr.translate(self.args)
595*c2e18aaaSAndroid Build Coastguard Worker    unittest_utils.assert_equal_testinfo_lists(
596*c2e18aaaSAndroid Build Coastguard Worker        self, test_infos, [uc.MODULE_INFO, uc.CLASS_INFO]
597*c2e18aaaSAndroid Build Coastguard Worker    )
598*c2e18aaaSAndroid Build Coastguard Worker
599*c2e18aaaSAndroid Build Coastguard Worker
600*c2e18aaaSAndroid Build Coastguard Workerclass ParseTestIdentifierTest(unittest.TestCase):
601*c2e18aaaSAndroid Build Coastguard Worker  """Test parse_test_identifier with different test names."""
602*c2e18aaaSAndroid Build Coastguard Worker
603*c2e18aaaSAndroid Build Coastguard Worker  def test_no_mainline_modules(self):
604*c2e18aaaSAndroid Build Coastguard Worker    """non-mainline module testing."""
605*c2e18aaaSAndroid Build Coastguard Worker    given = 'testName'
606*c2e18aaaSAndroid Build Coastguard Worker
607*c2e18aaaSAndroid Build Coastguard Worker    identifier = cli_t.parse_test_identifier(given)
608*c2e18aaaSAndroid Build Coastguard Worker
609*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual('testName', identifier.test_name)
610*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual([], identifier.module_names)
611*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual([], identifier.binary_names)
612*c2e18aaaSAndroid Build Coastguard Worker
613*c2e18aaaSAndroid Build Coastguard Worker  def test_single_mainline_module(self):
614*c2e18aaaSAndroid Build Coastguard Worker    """only one mainline module."""
615*c2e18aaaSAndroid Build Coastguard Worker    given = 'testName[Module1.apk]'
616*c2e18aaaSAndroid Build Coastguard Worker
617*c2e18aaaSAndroid Build Coastguard Worker    identifier = cli_t.parse_test_identifier(given)
618*c2e18aaaSAndroid Build Coastguard Worker
619*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual('testName', identifier.test_name)
620*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(['Module1'], identifier.module_names)
621*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(['Module1.apk'], identifier.binary_names)
622*c2e18aaaSAndroid Build Coastguard Worker
623*c2e18aaaSAndroid Build Coastguard Worker  def test_multiple_mainline_modules(self):
624*c2e18aaaSAndroid Build Coastguard Worker    """multiple mainline modules."""
625*c2e18aaaSAndroid Build Coastguard Worker    given = 'testName[Module1.apk+Module2.apex]'
626*c2e18aaaSAndroid Build Coastguard Worker
627*c2e18aaaSAndroid Build Coastguard Worker    identifier = cli_t.parse_test_identifier(given)
628*c2e18aaaSAndroid Build Coastguard Worker
629*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual('testName', identifier.test_name)
630*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(['Module1', 'Module2'], identifier.module_names)
631*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(['Module1.apk', 'Module2.apex'], identifier.binary_names)
632*c2e18aaaSAndroid Build Coastguard Worker
633*c2e18aaaSAndroid Build Coastguard Worker  def test_missing_closing_bracket(self):
634*c2e18aaaSAndroid Build Coastguard Worker    """test the brackets are not in pair"""
635*c2e18aaaSAndroid Build Coastguard Worker    given = 'testName[Module1.apk+Module2.apex'
636*c2e18aaaSAndroid Build Coastguard Worker
637*c2e18aaaSAndroid Build Coastguard Worker    identifier = cli_t.parse_test_identifier(given)
638*c2e18aaaSAndroid Build Coastguard Worker
639*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(given, identifier.test_name)
640*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual([], identifier.module_names)
641*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual([], identifier.binary_names)
642*c2e18aaaSAndroid Build Coastguard Worker
643*c2e18aaaSAndroid Build Coastguard Worker
644*c2e18aaaSAndroid Build Coastguard Workerclass VerifyMainlineModuleTest(fake_filesystem_unittest.TestCase):
645*c2e18aaaSAndroid Build Coastguard Worker  """test verify_mainline_modules sub-methods."""
646*c2e18aaaSAndroid Build Coastguard Worker
647*c2e18aaaSAndroid Build Coastguard Worker  def setUp(self):
648*c2e18aaaSAndroid Build Coastguard Worker    """Setup func."""
649*c2e18aaaSAndroid Build Coastguard Worker    self.setUpPyfakefs()
650*c2e18aaaSAndroid Build Coastguard Worker
651*c2e18aaaSAndroid Build Coastguard Worker  def test_verified_mainline_modules_no_brackets(self):
652*c2e18aaaSAndroid Build Coastguard Worker    """True for it's not in mainline module pattern. (no brackets)"""
653*c2e18aaaSAndroid Build Coastguard Worker    test_name = 'module0'
654*c2e18aaaSAndroid Build Coastguard Worker    mod_info = create_module_info([
655*c2e18aaaSAndroid Build Coastguard Worker        module(name=test_name),
656*c2e18aaaSAndroid Build Coastguard Worker    ])
657*c2e18aaaSAndroid Build Coastguard Worker
658*c2e18aaaSAndroid Build Coastguard Worker    test_identifier = cli_t.parse_test_identifier(test_name)
659*c2e18aaaSAndroid Build Coastguard Worker    translator = cli_t.CLITranslator(mod_info)
660*c2e18aaaSAndroid Build Coastguard Worker
661*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue(translator._verified_mainline_modules(test_identifier))
662*c2e18aaaSAndroid Build Coastguard Worker
663*c2e18aaaSAndroid Build Coastguard Worker  def test_verified_mainline_modules_not_valid_test_module(self):
664*c2e18aaaSAndroid Build Coastguard Worker    """False for test_name is not a module."""
665*c2e18aaaSAndroid Build Coastguard Worker    test_name = 'module1[foo.apk+goo.apk]'
666*c2e18aaaSAndroid Build Coastguard Worker    mod_info = create_module_info([
667*c2e18aaaSAndroid Build Coastguard Worker        module(name='module_1'),
668*c2e18aaaSAndroid Build Coastguard Worker        module(name='foo'),
669*c2e18aaaSAndroid Build Coastguard Worker        module(name='goo'),
670*c2e18aaaSAndroid Build Coastguard Worker    ])
671*c2e18aaaSAndroid Build Coastguard Worker
672*c2e18aaaSAndroid Build Coastguard Worker    test_identifier = cli_t.parse_test_identifier(test_name)
673*c2e18aaaSAndroid Build Coastguard Worker    translator = cli_t.CLITranslator(mod_info)
674*c2e18aaaSAndroid Build Coastguard Worker
675*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(translator._verified_mainline_modules(test_identifier))
676*c2e18aaaSAndroid Build Coastguard Worker
677*c2e18aaaSAndroid Build Coastguard Worker  def test_verified_mainline_modules_not_valid_mainline_module(self):
678*c2e18aaaSAndroid Build Coastguard Worker    """False for mainline_modules are not modules."""
679*c2e18aaaSAndroid Build Coastguard Worker    test_name = 'module2[foo.apk+goo.apk]'
680*c2e18aaaSAndroid Build Coastguard Worker    mod_info = create_module_info([module(name='module2')])
681*c2e18aaaSAndroid Build Coastguard Worker
682*c2e18aaaSAndroid Build Coastguard Worker    test_identifier = cli_t.parse_test_identifier(test_name)
683*c2e18aaaSAndroid Build Coastguard Worker    translator = cli_t.CLITranslator(mod_info)
684*c2e18aaaSAndroid Build Coastguard Worker
685*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(translator._verified_mainline_modules(test_identifier))
686*c2e18aaaSAndroid Build Coastguard Worker
687*c2e18aaaSAndroid Build Coastguard Worker  def test_verified_mainline_modules_no_test_mainline_modules(self):
688*c2e18aaaSAndroid Build Coastguard Worker    """False for no definition in `test_mainline_modules` attribute."""
689*c2e18aaaSAndroid Build Coastguard Worker    test_name = 'module3[foo.apk+goo.apex]'
690*c2e18aaaSAndroid Build Coastguard Worker    mod_info = create_module_info([
691*c2e18aaaSAndroid Build Coastguard Worker        module(name='module3', test_mainline_modules=[]),
692*c2e18aaaSAndroid Build Coastguard Worker        module(name='foo', installed=['out/path/foo.apk']),
693*c2e18aaaSAndroid Build Coastguard Worker        module(name='goo', installed=['out/path/goo.apex']),
694*c2e18aaaSAndroid Build Coastguard Worker    ])
695*c2e18aaaSAndroid Build Coastguard Worker
696*c2e18aaaSAndroid Build Coastguard Worker    test_identifier = cli_t.parse_test_identifier(test_name)
697*c2e18aaaSAndroid Build Coastguard Worker    translator = cli_t.CLITranslator(mod_info)
698*c2e18aaaSAndroid Build Coastguard Worker
699*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(translator._verified_mainline_modules(test_identifier))
700*c2e18aaaSAndroid Build Coastguard Worker
701*c2e18aaaSAndroid Build Coastguard Worker  def test_verified_mainline_modules_test_mainline_modules(self):
702*c2e18aaaSAndroid Build Coastguard Worker    """True for definition in `test_mainline_modules` attribute."""
703*c2e18aaaSAndroid Build Coastguard Worker    test_name = 'module4[foo.apk+goo.apex]'
704*c2e18aaaSAndroid Build Coastguard Worker    mod_info = create_module_info([
705*c2e18aaaSAndroid Build Coastguard Worker        module(name='module4', test_mainline_modules=['foo.apk+goo.apex']),
706*c2e18aaaSAndroid Build Coastguard Worker        module(name='foo', installed=['out/path/foo.apk']),
707*c2e18aaaSAndroid Build Coastguard Worker        module(name='goo', installed=['out/path/goo.apex']),
708*c2e18aaaSAndroid Build Coastguard Worker    ])
709*c2e18aaaSAndroid Build Coastguard Worker
710*c2e18aaaSAndroid Build Coastguard Worker    test_identifier = cli_t.parse_test_identifier(test_name)
711*c2e18aaaSAndroid Build Coastguard Worker    translator = cli_t.CLITranslator(mod_info)
712*c2e18aaaSAndroid Build Coastguard Worker
713*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue(translator._verified_mainline_modules(test_identifier))
714*c2e18aaaSAndroid Build Coastguard Worker
715*c2e18aaaSAndroid Build Coastguard Worker  def test_verified_mainline_modules_were_in_test_config(self):
716*c2e18aaaSAndroid Build Coastguard Worker    """True for mainline modules were defined in the test_config."""
717*c2e18aaaSAndroid Build Coastguard Worker    test_name = 'module5[foo.apk+goo.apex]'
718*c2e18aaaSAndroid Build Coastguard Worker    mainline_config = 'out/module3/AndroidTest.xml'
719*c2e18aaaSAndroid Build Coastguard Worker    self.fs.create_file(
720*c2e18aaaSAndroid Build Coastguard Worker        mainline_config,
721*c2e18aaaSAndroid Build Coastguard Worker        contents="""
722*c2e18aaaSAndroid Build Coastguard Worker            <configuration description="Mainline Module tests">
723*c2e18aaaSAndroid Build Coastguard Worker                <option name="config"
724*c2e18aaaSAndroid Build Coastguard Worker                        key="parameter" value="value_1" />
725*c2e18aaaSAndroid Build Coastguard Worker                <option name="config-descriptor:metadata"
726*c2e18aaaSAndroid Build Coastguard Worker                        key="mainline-param" value="foo.apk+goo.apex" />
727*c2e18aaaSAndroid Build Coastguard Worker            </configuration>
728*c2e18aaaSAndroid Build Coastguard Worker            """,
729*c2e18aaaSAndroid Build Coastguard Worker    )
730*c2e18aaaSAndroid Build Coastguard Worker    mod_info = create_module_info([
731*c2e18aaaSAndroid Build Coastguard Worker        module(
732*c2e18aaaSAndroid Build Coastguard Worker            name='module5',
733*c2e18aaaSAndroid Build Coastguard Worker            test_config=[mainline_config],
734*c2e18aaaSAndroid Build Coastguard Worker            test_mainline_modules=[],
735*c2e18aaaSAndroid Build Coastguard Worker            auto_test_config=[],
736*c2e18aaaSAndroid Build Coastguard Worker        ),
737*c2e18aaaSAndroid Build Coastguard Worker        module(name='foo', installed=['out/path/foo.apk']),
738*c2e18aaaSAndroid Build Coastguard Worker        module(name='goo', installed=['out/path/goo.apex']),
739*c2e18aaaSAndroid Build Coastguard Worker    ])
740*c2e18aaaSAndroid Build Coastguard Worker
741*c2e18aaaSAndroid Build Coastguard Worker    test_identifier = cli_t.parse_test_identifier(test_name)
742*c2e18aaaSAndroid Build Coastguard Worker    translator = cli_t.CLITranslator(mod_info)
743*c2e18aaaSAndroid Build Coastguard Worker
744*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue(translator._verified_mainline_modules(test_identifier))
745*c2e18aaaSAndroid Build Coastguard Worker
746*c2e18aaaSAndroid Build Coastguard Worker  def test_verified_mainline_modules_were_in_auto_config(self):
747*c2e18aaaSAndroid Build Coastguard Worker    """True for 'auto_test_config'=[True]"""
748*c2e18aaaSAndroid Build Coastguard Worker    test_name = 'module6[foo.apk+goo.apex]'
749*c2e18aaaSAndroid Build Coastguard Worker    mod_info = create_module_info([
750*c2e18aaaSAndroid Build Coastguard Worker        module(
751*c2e18aaaSAndroid Build Coastguard Worker            name='module6',
752*c2e18aaaSAndroid Build Coastguard Worker            test_config=['somewhere/AndroidTest.xml'],
753*c2e18aaaSAndroid Build Coastguard Worker            auto_test_config=[True],
754*c2e18aaaSAndroid Build Coastguard Worker        ),
755*c2e18aaaSAndroid Build Coastguard Worker        module(name='foo', installed=['out/path/foo.apk']),
756*c2e18aaaSAndroid Build Coastguard Worker        module(name='goo', installed=['out/path/goo.apex']),
757*c2e18aaaSAndroid Build Coastguard Worker    ])
758*c2e18aaaSAndroid Build Coastguard Worker
759*c2e18aaaSAndroid Build Coastguard Worker    test_identifier = cli_t.parse_test_identifier(test_name)
760*c2e18aaaSAndroid Build Coastguard Worker    translator = cli_t.CLITranslator(mod_info)
761*c2e18aaaSAndroid Build Coastguard Worker
762*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue(translator._verified_mainline_modules(test_identifier))
763*c2e18aaaSAndroid Build Coastguard Worker
764*c2e18aaaSAndroid Build Coastguard Worker  def test_verified_mainline_modules_have_no_association(self):
765*c2e18aaaSAndroid Build Coastguard Worker    """False for null auto_test_config and null `mainline-param` in the test_config."""
766*c2e18aaaSAndroid Build Coastguard Worker    test_name = 'module7[foo.apk+goo.apex]'
767*c2e18aaaSAndroid Build Coastguard Worker    config = 'out/module7/AndroidTest.xml'
768*c2e18aaaSAndroid Build Coastguard Worker    self.fs.create_file(
769*c2e18aaaSAndroid Build Coastguard Worker        config,
770*c2e18aaaSAndroid Build Coastguard Worker        contents="""
771*c2e18aaaSAndroid Build Coastguard Worker            <configuration description="Mainline Module tests">
772*c2e18aaaSAndroid Build Coastguard Worker                <option name="config"
773*c2e18aaaSAndroid Build Coastguard Worker                        key="parameter" value="value_1" />
774*c2e18aaaSAndroid Build Coastguard Worker            </configuration>
775*c2e18aaaSAndroid Build Coastguard Worker            """,
776*c2e18aaaSAndroid Build Coastguard Worker    )
777*c2e18aaaSAndroid Build Coastguard Worker    mod_info = create_module_info([
778*c2e18aaaSAndroid Build Coastguard Worker        module(name='module7', test_config=[config], auto_test_config=[]),
779*c2e18aaaSAndroid Build Coastguard Worker        module(name='foo', installed=['out/path/foo.apk']),
780*c2e18aaaSAndroid Build Coastguard Worker        module(name='goo', installed=['out/path/goo.apex']),
781*c2e18aaaSAndroid Build Coastguard Worker    ])
782*c2e18aaaSAndroid Build Coastguard Worker
783*c2e18aaaSAndroid Build Coastguard Worker    test_identifier = cli_t.parse_test_identifier(test_name)
784*c2e18aaaSAndroid Build Coastguard Worker    translator = cli_t.CLITranslator(mod_info)
785*c2e18aaaSAndroid Build Coastguard Worker
786*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(translator._verified_mainline_modules(test_identifier))
787*c2e18aaaSAndroid Build Coastguard Worker
788*c2e18aaaSAndroid Build Coastguard Worker  def test_verified_mainline_modules_were_in_auto_config(self):
789*c2e18aaaSAndroid Build Coastguard Worker    """False for the given mainline is a capex file."""
790*c2e18aaaSAndroid Build Coastguard Worker    test_name = 'module8[foo.apk+goo.apex]'
791*c2e18aaaSAndroid Build Coastguard Worker    mod_info = create_module_info([
792*c2e18aaaSAndroid Build Coastguard Worker        module(name='module8', test_mainline_modules=['foo.apk+goo.apex']),
793*c2e18aaaSAndroid Build Coastguard Worker        module(name='foo', installed=['out/path/foo.apk']),
794*c2e18aaaSAndroid Build Coastguard Worker        module(name='goo', installed=['out/path/goo.capex']),
795*c2e18aaaSAndroid Build Coastguard Worker    ])
796*c2e18aaaSAndroid Build Coastguard Worker
797*c2e18aaaSAndroid Build Coastguard Worker    test_identifier = cli_t.parse_test_identifier(test_name)
798*c2e18aaaSAndroid Build Coastguard Worker    translator = cli_t.CLITranslator(mod_info)
799*c2e18aaaSAndroid Build Coastguard Worker
800*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(translator._verified_mainline_modules(test_identifier))
801*c2e18aaaSAndroid Build Coastguard Worker
802*c2e18aaaSAndroid Build Coastguard Worker
803*c2e18aaaSAndroid Build Coastguard Workerdef create_module_info(modules=None):
804*c2e18aaaSAndroid Build Coastguard Worker  """wrapper func for creating module_info.ModuleInfo"""
805*c2e18aaaSAndroid Build Coastguard Worker  name_to_module_info = {}
806*c2e18aaaSAndroid Build Coastguard Worker  modules = modules or []
807*c2e18aaaSAndroid Build Coastguard Worker
808*c2e18aaaSAndroid Build Coastguard Worker  for m in modules:
809*c2e18aaaSAndroid Build Coastguard Worker    name_to_module_info[m['module_name']] = m
810*c2e18aaaSAndroid Build Coastguard Worker
811*c2e18aaaSAndroid Build Coastguard Worker  return module_info.load_from_dict(name_to_module_info)
812*c2e18aaaSAndroid Build Coastguard Worker
813*c2e18aaaSAndroid Build Coastguard Worker
814*c2e18aaaSAndroid Build Coastguard Workerdef module(
815*c2e18aaaSAndroid Build Coastguard Worker    name=None,
816*c2e18aaaSAndroid Build Coastguard Worker    path=None,
817*c2e18aaaSAndroid Build Coastguard Worker    installed=None,
818*c2e18aaaSAndroid Build Coastguard Worker    classes=None,
819*c2e18aaaSAndroid Build Coastguard Worker    auto_test_config=None,
820*c2e18aaaSAndroid Build Coastguard Worker    test_config=None,
821*c2e18aaaSAndroid Build Coastguard Worker    shared_libs=None,
822*c2e18aaaSAndroid Build Coastguard Worker    dependencies=None,
823*c2e18aaaSAndroid Build Coastguard Worker    runtime_dependencies=None,
824*c2e18aaaSAndroid Build Coastguard Worker    data=None,
825*c2e18aaaSAndroid Build Coastguard Worker    data_dependencies=None,
826*c2e18aaaSAndroid Build Coastguard Worker    compatibility_suites=None,
827*c2e18aaaSAndroid Build Coastguard Worker    host_dependencies=None,
828*c2e18aaaSAndroid Build Coastguard Worker    srcs=None,
829*c2e18aaaSAndroid Build Coastguard Worker    supported_variants=None,
830*c2e18aaaSAndroid Build Coastguard Worker    test_mainline_modules=None,
831*c2e18aaaSAndroid Build Coastguard Worker):
832*c2e18aaaSAndroid Build Coastguard Worker  """return a module info dict."""
833*c2e18aaaSAndroid Build Coastguard Worker  name = name or 'libhello'
834*c2e18aaaSAndroid Build Coastguard Worker
835*c2e18aaaSAndroid Build Coastguard Worker  m = {}
836*c2e18aaaSAndroid Build Coastguard Worker
837*c2e18aaaSAndroid Build Coastguard Worker  m['module_name'] = name
838*c2e18aaaSAndroid Build Coastguard Worker  m['class'] = classes or ['ETC']
839*c2e18aaaSAndroid Build Coastguard Worker  m['path'] = [path or '']
840*c2e18aaaSAndroid Build Coastguard Worker  m['installed'] = installed or []
841*c2e18aaaSAndroid Build Coastguard Worker  m['is_unit_test'] = 'false'
842*c2e18aaaSAndroid Build Coastguard Worker  m['auto_test_config'] = auto_test_config or []
843*c2e18aaaSAndroid Build Coastguard Worker  m['test_config'] = test_config or []
844*c2e18aaaSAndroid Build Coastguard Worker  m['shared_libs'] = shared_libs or []
845*c2e18aaaSAndroid Build Coastguard Worker  m['runtime_dependencies'] = runtime_dependencies or []
846*c2e18aaaSAndroid Build Coastguard Worker  m['dependencies'] = dependencies or []
847*c2e18aaaSAndroid Build Coastguard Worker  m['data'] = data or []
848*c2e18aaaSAndroid Build Coastguard Worker  m['data_dependencies'] = data_dependencies or []
849*c2e18aaaSAndroid Build Coastguard Worker  m['compatibility_suites'] = compatibility_suites or []
850*c2e18aaaSAndroid Build Coastguard Worker  m['host_dependencies'] = host_dependencies or []
851*c2e18aaaSAndroid Build Coastguard Worker  m['srcs'] = srcs or []
852*c2e18aaaSAndroid Build Coastguard Worker  m['supported_variants'] = supported_variants or []
853*c2e18aaaSAndroid Build Coastguard Worker  m['test_mainline_modules'] = test_mainline_modules or []
854*c2e18aaaSAndroid Build Coastguard Worker  return m
855*c2e18aaaSAndroid Build Coastguard Worker
856*c2e18aaaSAndroid Build Coastguard Worker
857*c2e18aaaSAndroid Build Coastguard Workerdef _gather_build_targets(test_infos):
858*c2e18aaaSAndroid Build Coastguard Worker  targets = set()
859*c2e18aaaSAndroid Build Coastguard Worker  for t_info in test_infos:
860*c2e18aaaSAndroid Build Coastguard Worker    targets |= t_info.build_targets
861*c2e18aaaSAndroid Build Coastguard Worker  return targets
862*c2e18aaaSAndroid Build Coastguard Worker
863*c2e18aaaSAndroid Build Coastguard Worker
864*c2e18aaaSAndroid Build Coastguard Workerif __name__ == '__main__':
865*c2e18aaaSAndroid Build Coastguard Worker  unittest.main()
866