11cb0ef41Sopenharmony_ci#!/usr/bin/env python3 21cb0ef41Sopenharmony_ci# Copyright 2018 the V8 project authors. All rights reserved. 31cb0ef41Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be 41cb0ef41Sopenharmony_ci# found in the LICENSE file. 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciimport os 71cb0ef41Sopenharmony_ciimport sys 81cb0ef41Sopenharmony_ciimport tempfile 91cb0ef41Sopenharmony_ciimport unittest 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci# Configuring the path for the v8_presubmit module 121cb0ef41Sopenharmony_ciTOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 131cb0ef41Sopenharmony_cisys.path.append(TOOLS_ROOT) 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_cifrom v8_presubmit import FileContentsCache, CacheableSourceFileProcessor 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ciclass FakeCachedProcessor(CacheableSourceFileProcessor): 191cb0ef41Sopenharmony_ci def __init__(self, cache_file_path): 201cb0ef41Sopenharmony_ci super(FakeCachedProcessor, self).__init__( 211cb0ef41Sopenharmony_ci use_cache=True, cache_file_path=cache_file_path, file_type='.test') 221cb0ef41Sopenharmony_ci def GetProcessorWorker(self): 231cb0ef41Sopenharmony_ci return object 241cb0ef41Sopenharmony_ci def GetProcessorScript(self): 251cb0ef41Sopenharmony_ci return "echo", [] 261cb0ef41Sopenharmony_ci def DetectUnformattedFiles(_, cmd, worker, files): 271cb0ef41Sopenharmony_ci raise NotImplementedError 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciclass FileContentsCacheTest(unittest.TestCase): 301cb0ef41Sopenharmony_ci def setUp(self): 311cb0ef41Sopenharmony_ci _, self.cache_file_path = tempfile.mkstemp() 321cb0ef41Sopenharmony_ci cache = FileContentsCache(self.cache_file_path) 331cb0ef41Sopenharmony_ci cache.Load() 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci def generate_file(): 361cb0ef41Sopenharmony_ci _, file_name = tempfile.mkstemp() 371cb0ef41Sopenharmony_ci with open(file_name, "w") as f: 381cb0ef41Sopenharmony_ci f.write(file_name) 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci return file_name 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci self.target_files = [generate_file() for _ in range(2)] 431cb0ef41Sopenharmony_ci unchanged_files = cache.FilterUnchangedFiles(self.target_files) 441cb0ef41Sopenharmony_ci self.assertEqual(len(unchanged_files), 2) 451cb0ef41Sopenharmony_ci cache.Save() 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci def tearDown(self): 481cb0ef41Sopenharmony_ci for file in [self.cache_file_path] + self.target_files: 491cb0ef41Sopenharmony_ci os.remove(file) 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci def testCachesFiles(self): 521cb0ef41Sopenharmony_ci cache = FileContentsCache(self.cache_file_path) 531cb0ef41Sopenharmony_ci cache.Load() 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci changed_files = cache.FilterUnchangedFiles(self.target_files) 561cb0ef41Sopenharmony_ci self.assertListEqual(changed_files, []) 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci modified_file = self.target_files[0] 591cb0ef41Sopenharmony_ci with open(modified_file, "w") as f: 601cb0ef41Sopenharmony_ci f.write("modification") 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci changed_files = cache.FilterUnchangedFiles(self.target_files) 631cb0ef41Sopenharmony_ci self.assertListEqual(changed_files, [modified_file]) 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci def testCacheableSourceFileProcessor(self): 661cb0ef41Sopenharmony_ci class CachedProcessor(FakeCachedProcessor): 671cb0ef41Sopenharmony_ci def DetectFilesToChange(_, files): 681cb0ef41Sopenharmony_ci self.assertListEqual(files, []) 691cb0ef41Sopenharmony_ci return [] 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci cached_processor = CachedProcessor(cache_file_path=self.cache_file_path) 721cb0ef41Sopenharmony_ci cached_processor.ProcessFiles(self.target_files) 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci def testCacheableSourceFileProcessorWithModifications(self): 751cb0ef41Sopenharmony_ci modified_file = self.target_files[0] 761cb0ef41Sopenharmony_ci with open(modified_file, "w") as f: 771cb0ef41Sopenharmony_ci f.write("modification") 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci class CachedProcessor(FakeCachedProcessor): 801cb0ef41Sopenharmony_ci def DetectFilesToChange(_, files): 811cb0ef41Sopenharmony_ci self.assertListEqual(files, [modified_file]) 821cb0ef41Sopenharmony_ci return [] 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci cached_processor = CachedProcessor( 851cb0ef41Sopenharmony_ci cache_file_path=self.cache_file_path, 861cb0ef41Sopenharmony_ci ) 871cb0ef41Sopenharmony_ci cached_processor.ProcessFiles(self.target_files) 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ciif __name__ == '__main__': 911cb0ef41Sopenharmony_ci unittest.main() 92