Merged
修复变更范围解析器重复展开模块配置 #14174
puppyAdd on Oct 20
修复变更范围解析器重复展开模块配置 #14174
Merged
Total: 2 files changed+471-322
| @@ -9,375 +9,215 @@ | |||
| 9 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 9 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | # See LICENSE in the root of the software repository for the full text of the License. | 10 | # See LICENSE in the root of the software repository for the full text of the License. |
| 11 | # ----------------------------------------------------------------------------------------------------------- | 11 | # ----------------------------------------------------------------------------------------------------------- |
| 12 | -""" | 12 | +"""Select CI test targets from module rules and repository-relative changes.""" |
| 13 | -获取修改文件应触发的测试范围. | ||
| 14 | - | ||
| 15 | -当前仅支持对应触发的 UTest 用例进行分析, 切仅支持 ops_test 这个 UTest 目标. | ||
| 16 | -""" | ||
| 17 | 13 | ||
| 18 | import argparse | 14 | import argparse |
| 19 | import logging | 15 | import logging |
| 20 | from pathlib import Path | 16 | from pathlib import Path |
| 21 | -from typing import List, Dict, Any, Optional | ||
| 22 | 17 | ||
| 23 | import yaml | 18 | import yaml |
| 24 | 19 | ||
| 25 | 20 | ||
| 26 | class Module: | 21 | class Module: |
| 22 | + """One terminal rule, independent of its depth in the configuration tree.""" | ||
| 23 | + | ||
| 27 | def __init__(self, name): | 24 | def __init__(self, name): |
| 28 | - self.name: str = name | 25 | + self.name = name |
| 29 | - self.src_files: List[Path] = [] | 26 | + self.src_files = [] |
| 30 | - self.src_exclude_files: List[Path] = [] | 27 | + self.src_exclude_files = [] |
| 31 | - self.tests_ut_ops_test_src_files: List[Path] = [] | 28 | + self.options = [] |
| 32 | - self.tests_ut_ops_test_src_exclude_files: List[Path] = [] | 29 | + self.test_excludes = [] |
| 33 | - self.tests_ut_ops_test_options: List[str] = [] | ||
| 34 | - self.options: List[str] = [] | ||
| 35 | - self.test_excludes: List[str] = [] | ||
| 36 | 30 | ||
| 37 | - @staticmethod | 31 | + def update_classify_cfg(self, desc): |
| 38 | - def _add_str_cfg(src, dst: List[str]): | 32 | + fields = {} |
| 39 | - if isinstance(src, str): | 33 | + for key, attribute in ( |
| 40 | - src = [src] | 34 | + ("src", "src_files"), |
| 41 | - for s in src: | 35 | + ("exclude", "src_exclude_files"), |
| 42 | - if s not in dst: | 36 | + ("options", "options"), |
| 43 | - dst.append(s) | 37 | + ): |
| 38 | + values = desc.get(key, []) | ||
| 39 | + if isinstance(values, (str, Path)): | ||
| 40 | + values = [values] | ||
| 41 | + values = list(dict.fromkeys(values)) | ||
| 42 | + if key != "options": | ||
| 43 | + values = [Path(value) for value in values] | ||
| 44 | + if any(value.is_absolute() for value in values): | ||
| 45 | + logging.error("Rule %s requires relative %s paths", self.name, key) | ||
| 46 | + return False | ||
| 47 | + fields[attribute] = values | ||
| 48 | + for attribute, values in fields.items(): | ||
| 49 | + setattr(self, attribute, values) | ||
| 50 | + tests = desc.get("test", {}) | ||
| 51 | + self.test_excludes = [ | ||
| 52 | + kind | ||
| 53 | + for kind in ("examples", "ut") | ||
| 54 | + if isinstance(tests, dict) and not tests.get(kind, True) | ||
| 55 | + ] | ||
| 44 | return True | 56 | return True |
| 45 | 57 | ||
| 46 | - @staticmethod | 58 | + def get_test_options(self, f): |
| 47 | - def _add_test_excludes(test_options, dst: List[str]): | 59 | + ancestors = {f, *f.parents} |
| 48 | - if isinstance(test_options, Dict): | 60 | + included = ancestors.intersection(self.src_files) |
| 49 | - if "examples" in test_options and not test_options["examples"]: | 61 | + excluded = ancestors.intersection(self.src_exclude_files) |
| 50 | - dst.append("examples") | 62 | + return self.options.copy() if included and not excluded else [] |
| 51 | - if "ut" in test_options and not test_options["ut"]: | ||
| 52 | - dst.append("ut") | ||
| 53 | - return True | ||
| 54 | 63 | ||
| 55 | - def update_classify_cfg(self, desc: Dict[str, Any]) -> bool: | 64 | + def get_test_example_ops_test_options(self, f): |
| 56 | - if not self._update_src(desc=desc): | ||
| 57 | - return False | ||
| 58 | - if not self._update_exclude_src(desc=desc): | ||
| 59 | - return False | ||
| 60 | - if not self._update_test_excludes(desc=desc): | ||
| 61 | - return False | ||
| 62 | - if not self._update_options(desc=desc): | ||
| 63 | - return False | ||
| 64 | - return True | ||
| 65 | - | ||
| 66 | - def get_test_options(self, f: Path) -> List[str]: | ||
| 67 | - def is_excluded(e_f: Path): | ||
| 68 | - for e in self.src_exclude_files: | ||
| 69 | - try: | ||
| 70 | - e_f.relative_to(e) | ||
| 71 | - return True | ||
| 72 | - except ValueError: | ||
| 73 | - continue | ||
| 74 | - return False | ||
| 75 | - | ||
| 76 | - related_options: List[str] = [] | ||
| 77 | - for s in self.src_files: | ||
| 78 | - if is_excluded(e_f=f): | ||
| 79 | - continue | ||
| 80 | - try: | ||
| 81 | - if f.relative_to(s): | ||
| 82 | - # 当同一个修改文件需要触发多个 Options 时, 需要把这些 Options 全部添加 | ||
| 83 | - related_options.extend(self.options) | ||
| 84 | - except ValueError: | ||
| 85 | - continue | ||
| 86 | - # 关联 Options 去重 | ||
| 87 | - related_options = list(set(related_options)) | ||
| 88 | - return related_options | ||
| @@ -0,0 +1,309 @@ | |||
| 1 | +# ----------------------------------------------------------------------------------------------------------- | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +# ----------------------------------------------------------------------------------------------------------- | ||
| 10 | + | ||
| 11 | +import importlib.util | ||
| 12 | +from pathlib import Path | ||
| 13 | +import subprocess | ||
| 14 | +import sys | ||
| 15 | +import tempfile | ||
| 16 | +import textwrap | ||
| 17 | +import unittest | ||
| 18 | + | ||
| 19 | + | ||
| 20 | +SCRIPT_PATH = Path(__file__).resolve().parents[1] / "parse_changed_files.py" | ||
| 21 | +SPEC = importlib.util.spec_from_file_location("parse_changed_files", SCRIPT_PATH) | ||
| 22 | +PARSE_CHANGED_FILES = importlib.util.module_from_spec(SPEC) | ||
| 23 | +SPEC.loader.exec_module(PARSE_CHANGED_FILES) | ||
| 24 | + | ||
| 25 | + | ||
| 26 | +class ParseClassifyFileTest(unittest.TestCase): | ||
| 27 | + def setUp(self): | ||
| 28 | + class IsolatedParser(PARSE_CHANGED_FILES.Parser): | ||
| 29 | + _Modules = [] | ||
| 30 | + _ChangedPaths = [] | ||
| 31 | + _UTExcludes = [] | ||
| 32 | + _ExamplesExcludes = [] | ||
| 33 | + | ||
| 34 | + self.parser = IsolatedParser | ||
| 35 | + temp_dir = tempfile.TemporaryDirectory() | ||
| 36 | + self.addCleanup(temp_dir.cleanup) | ||
| 37 | + self.temp_path = Path(temp_dir.name) | ||
| 38 | + | ||
| 39 | + def parse_config(self, content): | ||
| 40 | + config_file = self.temp_path / "test_config.yaml" | ||
| 41 | + config_file.write_text(textwrap.dedent(content), encoding="utf-8") | ||
| 42 | + self.assertTrue(self.parser.parse_classify_file(config_file)) | ||
| 43 | + | ||
| 44 | + def test_two_level_modules_are_parsed_once_with_full_names(self): | ||
| 45 | + self.parse_config( | ||
| 46 | + """ | ||
| 47 | + attention: | ||
| 48 | + shared: | ||
| 49 | + module: true | ||
| 50 | + src: [attention/shared] | ||
| 51 | + options: [attention_shared] | ||
| 52 | + moe: | ||
| 53 | + shared: | ||
| 54 | + module: true | ||
| 55 | + src: [moe/shared] | ||
| 56 | + options: [moe_shared] | ||
| 57 | + """ | ||
| 58 | + ) | ||
| 59 | + | ||
| 60 | + self.assertEqual(len(self.parser._Modules), 2) | ||
| 61 | + self.assertEqual( | ||
| 62 | + [module.name for module in self.parser._Modules], | ||
| 63 | + ["root/attention/shared", "root/moe/shared"], | ||
| 64 | + ) | ||
| 65 | + self.assertEqual(self.parser._Modules[0].options, ["attention_shared"]) | ||
| 66 | + self.assertEqual(self.parser._Modules[1].options, ["moe_shared"]) | ||
| 67 | + | ||
| 68 | + def test_three_level_modules_are_parsed_once_with_full_names(self): | ||
| 69 | + self.parse_config( | ||
| 70 | + """ | ||
| 71 | + mc2: | ||
| 72 | + matrix: | ||
| 73 | + shared: | ||
| 74 | + module: true | ||
| 75 | + src: [mc2/matrix/shared] | ||
| 76 | + options: [matrix_shared] | ||
| 77 | + examples: | ||
| 78 | + mc2: | ||
| 79 | + shared: | ||
| 80 | + module: true | ||
| 81 | + src: [examples/mc2/shared] | ||
| 82 | + options: [example_shared] | ||
| 83 | + """ | ||
| 84 | + ) | ||
| 85 | + | ||
| 86 | + self.assertEqual(len(self.parser._Modules), 2) | ||
| 87 | + self.assertEqual( | ||
| 88 | + [module.name for module in self.parser._Modules], | ||
| 89 | + ["root/mc2/matrix/shared", "root/examples/mc2/shared"], | ||
| 90 | + ) | ||
| 91 | + | ||
| 92 | + def test_test_selection_and_source_excludes_are_preserved(self): | ||
| 93 | + self.parse_config( | ||
| 94 | + """ | ||
| 95 | + attention: | ||
| 96 | + enabled: | ||
| 97 | + module: true | ||
| 98 | + src: [attention/enabled] | ||
| 99 | + exclude: [attention/enabled/skipped] | ||
| 100 | + options: [enabled, no_ut, no_examples] | ||
| 101 | + no_ut: | ||
| 102 | + module: true | ||
| 103 | + src: [attention/no_ut] | ||
| 104 | + test: | ||
| 105 | + ut: false | ||
| 106 | + options: [no_ut] | ||
| 107 | + mc2: | ||
| 108 | + matrix: | ||
| 109 | + no_examples: | ||
| 110 | + module: true | ||
| 111 | + src: [mc2/matrix/no_examples] | ||
| 112 | + test: | ||
| 113 | + examples: false | ||
| 114 | + options: [no_examples] | ||
| 115 | + """ | ||
| 116 | + ) | ||
| 117 | + | ||
| 118 | + self.assertEqual(len(self.parser._Modules), 3) | ||
| 119 | + self.assertEqual(self.parser._UTExcludes, ["no_ut"]) | ||
| 120 | + self.assertEqual(self.parser._ExamplesExcludes, ["no_examples"]) | ||
| 121 | + enabled = self.parser._Modules[0] | ||
| 122 | + self.assertEqual( | ||
| 123 | + enabled.get_test_options(Path("attention/enabled/skipped/op.cpp")), [] | ||
| 124 | + ) | ||
| 125 | + self.assertEqual(enabled.get_test_options(Path("unrelated/op.cpp")), []) | ||
| 126 | + | ||
| 127 | + changed_file = self.temp_path / "changed_files" | ||
| 128 | + changed_file.write_text( | ||
| 129 | + "attention/enabled/op.cpp\nmc2/matrix/no_examples/op.cpp\n", | ||
| 130 | + encoding="utf-8", | ||
| 131 | + ) | ||
| 132 | + self.assertTrue(self.parser.parse_changed_file(changed_file)) | ||
| 133 | + self.assertEqual( | ||
| 134 | + set(self.parser.get_related_ut().split(";")) - {""}, | ||
| 135 | + {"enabled", "no_examples"}, | ||
| 136 | + ) | ||
| 137 | + self.assertEqual( | ||
| 138 | + set(self.parser.get_related_examples().split(";")) - {""}, | ||
| 139 | + {"enabled", "no_ut"}, | ||
| 140 | + ) | ||
| 141 | + self.assertEqual(self.parser.get_related_ut_mc2(), "no_examples;") | ||
| 142 | + self.assertEqual( | ||
| 143 | + set(self.parser.get_related_ut_exclude_mc2().split(";")) - {""}, | ||
| 144 | + {"enabled", "no_examples"}, | ||
| 145 | + ) | ||
| 146 | + | ||
| 147 | + def test_paths_use_directory_boundaries_and_support_single_strings(self): | ||
| 148 | + self.parse_config( | ||
| 149 | + """ | ||
| 150 | + op: | ||
| 151 | + module: true | ||
| 152 | + src: ops/foo | ||
| 153 | + exclude: ops/foo/private | ||
| 154 | + options: foo | ||
| 155 | + """ | ||
| 156 | + ) | ||
| 157 | + rule = self.parser._Modules[0] | ||
| 158 | + for path in ("ops/foo", "ops/foo/kernel.cpp", "ops/foo/private_other/k.cpp"): | ||
| 159 | + with self.subTest(path=path): | ||
| 160 | + self.assertEqual(rule.get_test_options(Path(path)), ["foo"]) | ||
| 161 | + for path in ("ops/foobar/k.cpp", "ops/foo/private", "ops/foo/private/k.cpp"): | ||
| 162 | + with self.subTest(path=path): | ||
| 163 | + self.assertEqual(rule.get_test_options(Path(path)), []) | ||
| 164 | + | ||
| 165 | + def test_disabled_modules_and_metadata_are_not_expanded(self): | ||
| 166 | + self.parse_config( | ||
| 167 | + """ | ||
| 168 | + metadata: [unused] | ||
| 169 | + disabled: | ||
| 170 | + module: false | ||
| 171 | + src: [disabled] | ||
| 172 | + options: [disabled] | ||
| 173 | + enabled: | ||
| 174 | + module: true | ||
| 175 | + src: [src, src] | ||
| 176 | + options: [first, second, first] | ||
| 177 | + """ | ||
| 178 | + ) | ||
| 179 | + self.assertEqual([rule.name for rule in self.parser._Modules], ["root/enabled"]) | ||
| 180 | + self.assertEqual(self.parser._Modules[0].src_files, [Path("src")]) | ||
| 181 | + self.assertEqual(self.parser._Modules[0].options, ["first", "second"]) | ||
| 182 | + | ||
| 183 | + def test_reloading_rules_replaces_modules_and_exclusions(self): | ||
| 184 | + self.parse_config("old: {module: true, options: old, test: {ut: false}}") | ||
| 185 | + self.parse_config("new: {module: true, src: new, options: new}") | ||
| 186 | + self.assertEqual([rule.name for rule in self.parser._Modules], ["root/new"]) | ||
| 187 | + self.assertEqual(self.parser._UTExcludes, []) | ||
| 188 | + self.assertEqual(self.parser._ExamplesExcludes, []) | ||
| 189 | + | ||
| 190 | + def test_changed_paths_filter_non_sources_and_replace_previous_input(self): | ||
| 191 | + changed_file = self.temp_path / "changed" | ||
| 192 | + changed_file.write_text( | ||
| 193 | + "src/k.cpp\nsrc/k.cpp\n\ndocs/a.h\nsrc/README.md\na.json\na.ini\n", | ||
| 194 | + encoding="utf-8", | ||
| 195 | + ) | ||
| 196 | + self.assertTrue(self.parser.parse_changed_file(changed_file)) | ||
| 197 | + self.assertEqual(self.parser._ChangedPaths, [Path("src/k.cpp")]) | ||
| 198 | + changed_file.write_text("src/new.h\n", encoding="utf-8") | ||
| 199 | + self.assertTrue(self.parser.parse_changed_file(changed_file)) | ||
| 200 | + self.assertEqual(self.parser._ChangedPaths, [Path("src/new.h")]) | ||
| 201 | + | ||
| 202 | + def test_invalid_rules_do_not_publish_partial_state(self): | ||
| 203 | + self.parse_config("old: {module: true, src: old, options: old}") | ||
| 204 | + config = self.temp_path / "invalid.yaml" | ||
| 205 | + for content in ( | ||
| 206 | + "first: {module: true, src: first}\nbad: {module: true, src: /absolute}", | ||
| 207 | + "bad: {module: true, exclude: /absolute}", | ||
| 208 | + "bad: [unterminated", | ||
| 209 | + ): | ||
| 210 | + with self.subTest(content=content), self.assertLogs(level="ERROR"): | ||
| 211 | + config.write_text(content, encoding="utf-8") | ||
| 212 | + self.assertFalse(self.parser.parse_classify_file(config)) | ||
| 213 | + self.assertEqual( | ||
| 214 | + [rule.name for rule in self.parser._Modules], ["root/old"] | ||
| 215 | + ) | ||
| 216 | + | ||
| 217 | + def test_invalid_changes_do_not_publish_partial_state(self): | ||
| 218 | + self.parser._ChangedPaths = [Path("old.cpp")] | ||
| 219 | + changed_file = self.temp_path / "changed" | ||
| 220 | + changed_file.write_text("new.cpp\n/absolute.cpp\n", encoding="utf-8") | ||
| 221 | + with self.assertLogs(level="ERROR"): | ||
| 222 | + self.assertFalse(self.parser.parse_changed_file(changed_file)) | ||
| 223 | + self.assertEqual(self.parser._ChangedPaths, [Path("old.cpp")]) | ||
| 224 | + | ||
| 225 | + def test_missing_files_return_false(self): | ||
| 226 | + missing = self.temp_path / "missing" | ||
| 227 | + with self.assertLogs(level="ERROR"): | ||
| 228 | + self.assertFalse(self.parser.parse_classify_file(missing)) | ||
| 229 | + self.assertFalse(self.parser.parse_changed_file(missing)) | ||
| 230 | + | ||
| 231 | + def test_all_target_is_preserved_for_each_selector(self): | ||
| 232 | + self.parse_config( | ||
| 233 | + "all: {module: true, src: ., options: all, test: {ut: false, examples: false}}" | ||
| 234 | + ) | ||
| 235 | + self.parser._ChangedPaths = [Path("mc2/a.cpp"), Path("src/b.cpp")] | ||
| 236 | + for command in ( | ||
| 237 | + "get_related_ut", | ||
| 238 | + "get_related_ut_mc2", | ||
| 239 | + "get_related_ut_exclude_mc2", | ||
| 240 | + "get_related_examples", | ||
| 241 | + ): | ||
| 242 | + with self.subTest(command=command): | ||
| 243 | + self.assertEqual(getattr(self.parser, command)(), "all") | ||
| 244 | + | ||
| 245 | + def test_empty_configuration_and_changes_select_nothing(self): | ||
| 246 | + self.parse_config("") | ||
| 247 | + changed_file = self.temp_path / "changed" | ||
| 248 | + changed_file.write_text("\n", encoding="utf-8") | ||
| 249 | + self.assertTrue(self.parser.parse_changed_file(changed_file)) | ||
| 250 | + self.assertEqual(self.parser.get_related_ut(), "") | ||
| 251 | + self.assertEqual(self.parser.get_related_examples(), "") | ||
| 252 | + | ||
| 253 | + def test_cli_selectors_and_soc_script_keep_their_output_contract(self): | ||
| 254 | + self.parse_config( | ||
| 255 | + """ | ||
| 256 | + first: {module: true, src: src, options: first} | ||
| 257 | + second: {module: true, src: mc2, options: second, test: {examples: false}} | ||
| 258 | + """ | ||
| 259 | + ) | ||
| 260 | + changed_file = self.temp_path / "changed" | ||
| 261 | + changed_file.write_text("src/a.cpp\nmc2/b.cpp\n", encoding="utf-8") | ||
| 262 | + for command, expected in ( | ||
| 263 | + ("get_related_ut", "first;second;"), | ||
| 264 | + ("get_related_ut_mc2", "second;"), | ||
| 265 | + ("get_related_ut_exclude_mc2", "first;"), | ||
| 266 | + ("get_related_examples", "first;"), | ||
| 267 | + ): | ||
| 268 | + with self.subTest(command=command): | ||
| 269 | + result = subprocess.run( | ||
| 270 | + [ | ||
| 271 | + sys.executable, | ||
| 272 | + str(SCRIPT_PATH), | ||
| 273 | + "-c", | ||
| 274 | + str(self.temp_path / "test_config.yaml"), | ||
| 275 | + "-f", | ||
| 276 | + str(changed_file), | ||
| 277 | + command, | ||
| 278 | + ], | ||
| 279 | + capture_output=True, | ||
| 280 | + text=True, | ||
| 281 | + check=True, | ||
| 282 | + ) | ||
| 283 | + self.assertEqual(result.stdout, expected + "\n") | ||
| 284 | + soc_config = self.temp_path / "soc.yaml" | ||
| 285 | + soc_config.write_text( | ||
| 286 | + "soc: {module: true, src: ., options: [ascend910b, ascend950]}", | ||
| 287 | + encoding="utf-8", | ||
| 288 | + ) | ||
| 289 | + result = subprocess.run( | ||
| 290 | + [ | ||
| 291 | + sys.executable, | ||
| 292 | + str(SCRIPT_PATH.with_name("get_soc_version.py")), | ||
| 293 | + "-c", | ||
| 294 | + str(soc_config), | ||
| 295 | + "-f", | ||
| 296 | + str(changed_file), | ||
| 297 | + "get_related_soc", | ||
| 298 | + ], | ||
| 299 | + capture_output=True, | ||
| 300 | + text=True, | ||
| 301 | + check=True, | ||
| 302 | + ) | ||
| 303 | + self.assertEqual( | ||
| 304 | + set(result.stdout.strip().split(",")), {"ascend910b", "ascend950"} | ||
| 305 | + ) | ||
| 306 | + | ||
| 307 | + | ||
| 308 | +if __name__ == "__main__": | ||
| 309 | + unittest.main() | ||