1# Copyright 2019, The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""
16Cache Finder class.
17"""
18
19import atest_utils
20
21from test_finders import test_finder_base
22from test_finders import test_info
23
24class CacheFinder(test_finder_base.TestFinderBase):
25    """Cache Finder class."""
26    NAME = 'CACHE'
27
28    def __init__(self, **kwargs):
29        super(CacheFinder, self).__init__()
30
31    def _is_latest_testinfos(self, test_infos):
32        """Check whether test_infos are up-to-date.
33
34        Args:
35            test_infos: A list of TestInfo.
36
37        Returns:
38            True if all keys in test_infos and TestInfo object are equal.
39            Otherwise, False.
40        """
41        sorted_base_ti = sorted(
42            vars(test_info.TestInfo(None, None, None)).keys())
43        for cached_test_info in test_infos:
44            sorted_cache_ti = sorted(vars(cached_test_info).keys())
45            if not sorted_cache_ti == sorted_base_ti:
46                return False
47        return True
48
49    def find_test_by_cache(self, test_reference):
50        """Find the matched test_infos in saved caches.
51
52        Args:
53            test_reference: A string of the path to the test's file or dir.
54
55        Returns:
56            A list of TestInfo namedtuple if cache found and is in latest
57            TestInfo format, else None.
58        """
59        test_infos = atest_utils.load_test_info_cache(test_reference)
60        if test_infos and self._is_latest_testinfos(test_infos):
61            return test_infos
62        return None
63