From 0fa7d65229daf89fce5f4b187b4bf23c00e23269 Mon Sep 17 00:00:00 2001 From: "zhangzhicheng.zzc" Date: Thu, 13 Apr 2023 16:10:07 +0800 Subject: [PATCH] fix update ast not remove origin information Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/12319197 --- modelscope/utils/ast_utils.py | 1 + tests/utils/test_ast.py | 23 ++++++++++------------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/modelscope/utils/ast_utils.py b/modelscope/utils/ast_utils.py index 76f15e56..374ada20 100644 --- a/modelscope/utils/ast_utils.py +++ b/modelscope/utils/ast_utils.py @@ -613,6 +613,7 @@ def _update_index(index, files_mtime): if file not in removed_files and \ (origin_files_mtime[file] != files_mtime[file]): updated_files.append(file) + removed_files.extend(updated_files) updated_files.extend(new_files) # remove deleted index diff --git a/tests/utils/test_ast.py b/tests/utils/test_ast.py index 5d92737d..78917ccd 100644 --- a/tests/utils/test_ast.py +++ b/tests/utils/test_ast.py @@ -144,8 +144,6 @@ class AstScaningTest(unittest.TestCase): index_from_prebuilt = load_from_prebuilt(file_path) self.assertEqual(index, index_from_prebuilt) - @unittest.skip( - 'skipped the method for not cpu time on this case not stable') def test_update_load_index_method(self): file_number = 20 file_list = [] @@ -157,22 +155,27 @@ class AstScaningTest(unittest.TestCase): index_file = 'ast_indexer_1' - start = time.time() index = load_index( file_list=file_list, indexer_file_dir=self.tmp_dir, indexer_file=index_file) - duration_1 = time.time() - start self.assertEqual(len(index[FILES_MTIME_KEY]), file_number) # no changing case, time should be less than original - start = time.time() index = load_index( file_list=file_list, indexer_file_dir=self.tmp_dir, indexer_file=index_file) - duration_2 = time.time() - start - self.assertGreater(duration_1, duration_2) + self.assertEqual(len(index[FILES_MTIME_KEY]), file_number) + + # modify case + with open(file_list[0], 'a', encoding='utf-8') as f: + f.write('\n\nimport typing') + file_list.append(filename) + index = load_index( + file_list=file_list, + indexer_file_dir=self.tmp_dir, + indexer_file=index_file) self.assertEqual(len(index[FILES_MTIME_KEY]), file_number) # adding new file, time should be less than original @@ -181,24 +184,18 @@ class AstScaningTest(unittest.TestCase): f.write('import os') file_list.append(test_file_new_2) - start = time.time() index = load_index( file_list=file_list, indexer_file_dir=self.tmp_dir, indexer_file=index_file) - duration_3 = time.time() - start - self.assertGreater(duration_1, duration_3) self.assertEqual(len(index[FILES_MTIME_KEY]), file_number + 1) # deleting one file, time should be less than original file_list.pop() - start = time.time() index = load_index( file_list=file_list, indexer_file_dir=self.tmp_dir, indexer_file=index_file) - duration_4 = time.time() - start - self.assertGreater(duration_1, duration_4) self.assertEqual(len(index[FILES_MTIME_KEY]), file_number)