1#!/usr/bin/env python 2# 3# Copyright 2019, 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 cache_finder.""" 18 19import unittest 20import os 21import mock 22 23# pylint: disable=import-error 24import atest_utils 25import unittest_constants as uc 26from test_finders import cache_finder 27 28 29#pylint: disable=protected-access 30class CacheFinderUnittests(unittest.TestCase): 31 """Unit tests for cache_finder.py""" 32 def setUp(self): 33 """Set up stuff for testing.""" 34 self.cache_finder = cache_finder.CacheFinder() 35 36 @mock.patch.object(atest_utils, 'get_test_info_cache_path') 37 def test_find_test_by_cache(self, mock_get_cache_path): 38 """Test find_test_by_cache method.""" 39 uncached_test = 'mytest1' 40 cached_test = 'hello_world_test' 41 uncached_test2 = 'mytest2' 42 test_cache_root = os.path.join(uc.TEST_DATA_DIR, 'cache_root') 43 # Hit matched cache file but no original_finder in it, 44 # should return None. 45 mock_get_cache_path.return_value = os.path.join( 46 test_cache_root, 47 'cd66f9f5ad63b42d0d77a9334de6bb73.cache') 48 self.assertIsNone(self.cache_finder.find_test_by_cache(uncached_test)) 49 # Hit matched cache file and original_finder is in it, 50 # should return cached test infos. 51 mock_get_cache_path.return_value = os.path.join( 52 test_cache_root, 53 '78ea54ef315f5613f7c11dd1a87f10c7.cache') 54 self.assertIsNotNone(self.cache_finder.find_test_by_cache(cached_test)) 55 # Does not hit matched cache file, should return cached test infos. 56 mock_get_cache_path.return_value = os.path.join( 57 test_cache_root, 58 '39488b7ac83c56d5a7d285519fe3e3fd.cache') 59 self.assertIsNone(self.cache_finder.find_test_by_cache(uncached_test2)) 60 61if __name__ == '__main__': 62 unittest.main() 63