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
20from test_finders import test_finder_base
21from test_finders import test_info
22
23class CacheFinder(test_finder_base.TestFinderBase):
24    """Cache Finder class."""
25    NAME = 'CACHE'
26
27    def __init__(self, **kwargs):
28        super(CacheFinder, self).__init__()
29
30    def _is_latest_testinfos(self, test_infos):
31        """Check whether test_infos are up-to-date.
32
33        Args:
34            test_infos: A list of TestInfo.
35
36        Returns:
37            True if all keys in test_infos and TestInfo object are equal.
38            Otherwise, False.
39        """
40        sorted_base_ti = sorted(
41            vars(test_info.TestInfo(None, None, None)).keys())
42        for cached_test_info in test_infos:
43            sorted_cache_ti = sorted(vars(cached_test_info).keys())
44            if not sorted_cache_ti == sorted_base_ti:
45                return False
46        return True
47
48    def find_test_by_cache(self, test_reference):
49        """Find the matched test_infos in saved caches.
50
51        Args:
52            test_reference: A string of the path to the test's file or dir.
53
54        Returns:
55            A list of TestInfo namedtuple if cache found and is in latest
56            TestInfo format, else None.
57        """
58        test_infos = atest_utils.load_test_info_cache(test_reference)
59        if test_infos and self._is_latest_testinfos(test_infos):
60            return test_infos
61        return None
62