This file contains model execution instructions and is responsible for scheduling 23 independent rule skills to complete the scan.
Model execution instructions
When the user triggers this skill (such as /check-test-code-quality /path/to/code --level all), execute the following steps:
Step 1: Parse parameters
Extract the scan path (required) and option parameters from the user input. See the complete parameter definitions in skill_config.json of usage field.
Parameter mutual exclusion explanation: --rules and --level are not recommended to be used at the same time. The two overlap in functionality (both filter execution rules), and using them together can easily cause confusion. Prefer using --rules to specify specific rules, or use --level to batch select by severity level. If both are specified,--rules takes effect first, and--level then filters from it (this usage is not recommended).
--level filtering rules(definitions are in skill_config.json of rules field):
critical: R001-R004,R006,R007,R010-R012,R014,R017-R023 (17)warning: R005,R008,R009,R013,R015,R016 (6)all: all 23
Step 2: Environment preparation (file collection + independent XTS project identification)
This step is theprerequisite dependencyfor all subsequent rule scans and must be completed before rule scanning.
2a. File collection
Use the Glob tool to traverse the scan path, collect files in buckets by file type, and output statistics:
| Category | File pattern | Related rules | Description |
|---|---|---|---|
| Source code files | **/*.ets, **/*.ts, **/*.js(exclude .test suffix) |
R001-R006,R019,R020,R022,R023 | All source code, including page/module/Ability files |
| Test files | **/*.test.ets, **/*.test.ts, **/*.test.js |
R004,R008,R009,R011,R013,R015,R016,R018 | Important: Use endswith('.test.ets') etc. to identify multi-suffix files |
| Build files | **/BUILD.gn |
R010,R014 | GN build configuration |
| Configuration files | **/Test.json |
R007 | Test configuration |
| Signature files | **/*.p7b |
R012 | DER binary signature certificate |
| Ability configuration | **/syscap.json |
R017 | System capability declaration |
| Package configuration | **/oh-package.json5 |
R021 | hypium version detection |
Output example:
[文件收集]
源代码文件: 86647
测试文件: 29074
BUILD.gn: 4028
Test.json: 3386
p7b: 3223
syscap.json: 5269
oh-package.json5: 6960
2b. Identify independent XTS projects
The detection scope of some rules (R011 duplicate testsuite, R019 duplicate .key, R020 duplicate .id, R021 hypium version) isproject-level, so independent XTS project boundaries need to be identified first.
Identification logic(see references/ENGINE_IDENTITY.md):
- Find all directories containing
BUILD.gndirectory - Filter out parent directories containing child
BUILD.gn(only non-group types) - Exclude
group()type BUILD.gn itself - The remaining directories are independent XTS projects
Output example:
[独立XTS工程]
识别到 3624 个独立XTS工程
Step 3: Scan rule by rule
Preset script rules(only 1):
- R004: Test cases lack assertions (implementation in
scripts/r004_scanner.py)
R004 must use the preset script: R004 (test cases lack assertions) is the most complex L5 rule, involving recursive function call chain tracing, class method extraction, cross-file import resolution, helper function try-catch defect detection, etc.mustuse scripts/r004_scanner.py the preset script to execute the R004 scan; manual implementation by the model can easily lead to severe false positives (historical lesson: v1 had 23,152 false positives). Note: R004 currently has a false positive issue and the indirect assertion tracing algorithm needs to be optimized.
Dynamically generated rules(22 in total, dynamically generating scanning code based on each rule's rules/Rxxx/SKILL.md ):
- R001: Use of the getSync system API is prohibited
- R002: Error code assertions must be of the number type
- R003: Tautological assertions are prohibited
- R005: Component dimensions use fixed values
- R006: Differentiation based on device type is prohibited
- R007: Test.json configuration items are prohibited
- R008: Irregular test case declaration format
- R009: Nonstandard @tc.number naming
- R010: part_name/subsystem_name mismatch
- R011: Duplicate testsuite
- R012: Incorrect signature certificate APL level and app-feature configuration
- R013: Commented-out deprecated code
- R014: Nonstandard test HAP naming
- R015: Missing Level parameter
- R016: testcase naming convention
- R017: syscap.json configures multiple capabilities
- R018: Duplicate testcase
- R019: Duplicate .key
- R020: Duplicate .id
- R021: hypium version number >= 1.0.26
- R022: Use of == instead of === for errcode value assertions
- R023: Assertions after forced type conversion of errcode values are prohibited
Except for R004, the scanning code for all rules is dynamically generated and executed by the model at runtime based on the detection logic and regex patterns in rules/Rxxx/SKILL.md . The generated code should:
- Strictly follow the detection logic in the rule file
- Use the regex patterns defined in the rule file
- Pay attention to the "Known pitfalls" section in the rule file to avoid common false positives
- Return a problem list in the standard format
R010 external data dependency: R010 needs to retrieve 3 configuration files from a remote repository to build the subsystem-component mapping table. If the mapping table data cannot be retrieved due to network reasons, and the URL is unreachable, a clear warning must be issued, and it must not silently return 0 problems.
Common utility library: scripts/common.py contains utility functions shared by all rules (file collection, it()/describe() block parsing, standalone XTS project identification, subsystem mapping, report generation, etc.). When the documents under the corresponding references/ are updated,common.py the code in also needs to be updated in sync (the synchronization source is annotated at the head of each function).
Dynamically generated rule execution process:
- Read
rules/Rxxx/SKILL.mdto obtain the complete detection logic of the rule - Strictly generate scanning code according to the detection logic and regex patterns in the rule file
- Pay special attention: Read the "Known pitfalls" section in the rule file to avoid common false positives
- Execute the generated scanning code
- Each issue must contain the following fields:
{
'rule': 'R004',
'type': '测试用例缺少断言',
'severity': 'Critical',
'file': 'rel/path.test.ets',
'line': 25,
'testcase': 'testName',
'snippet': '...',
'suggestion': '...',
'subsystem': '元能力',
}
testcase field: Parse the it() block range (algorithm see references/SCAN_ALGORITHMS.md), and determine the it() block to which the issue line number belongs. For non-test files or not within an it() block, fill in -。
subsystem field: Use the references/subsystem_mapping.md mapping table, match by the longest directory prefix (algorithm see references/SCAN_ALGORITHMS.md). If no match is found, fill in -。
Step 4: Generate Report
Aggregate the results of all rules and generate:
Mandatory requirements: the scan results of the rules actually executed in this runMust all be displayed, even if the number of issues is 0, it cannot be omitted. Rules not within the scope of this scan must not appear in the report.
-
Markdown statistical report: the terminal outputs the scan results of the rules executed in this run, in the following format:
Example(
--level warning):| 规则编号 | 问题类型 | 严重级别 | 问题数量 | |---------|---------|---------|---------| | R005 | 组件尺寸使用固定值 | Warning | 120 | | R008 | 用例声明格式不规范 | Warning | 0 | | R009 | @tc.number命名不规范 | Warning | 45 | | R013 | 注释的废弃代码 | Warning | 0 | | R015 | Level参数缺省 | Warning | 88 | | R016 | testcase命名规范 | Warning | 0 |Example(
--level critical):| 规则编号 | 问题类型 | 严重级别 | 问题数量 | |---------|---------|---------|---------| | R001 | 禁止使用getSync系统接口 | Critical | 300 | | R002 | 错误码断言必须是number类型 | Critical | 0 | ...(仅展示本次执行的17个Critical规则) | R023 | 禁止errcode值类型强转后断言 | Critical | 0 |Example(
--rules R001,R003):| 规则编号 | 问题类型 | 严重级别 | 问题数量 | |---------|---------|---------|---------| | R001 | 禁止使用getSync系统接口 | Critical | 300 | | R003 | 禁止恒真断言 | Critical | 0 |Prohibited:
- omitting rules with an issue count of 0
- only displaying rules with issues
- displaying rules not within the scope of this scan
-
Excel report: UTF-8 BOM encoding, containing two sheets:
- Sheet 1 - "Code Quality Check Report": 12-column issue details (see references/REPORT_FORMAT.md)
- Sheet 2 - "Issue Scan Result Summary": list only the statistics of the rules actually executed in this run, with headers
规则编号 | 问题类型 | 严重级别 | 问题数量, all rules executed in this run must be listed (those with an issue count of 0 must also be displayed)
Step 5: Integrity Check
Confirm that all rules within the scope of this scan have been executed, and that the report statistics have no omissions. Rules not within the scope of this scan do not need to be checked.
Step 6: Automatic fix (executed only when the user specifies --fix )
When the user command contains the --fix parameter, after Step 5 is completed, perform automatic fixes on the issues found by the scan according to the following process.
Important: --fix Only the following 6 rules are supported. The remaining 17 rules (R001-R007,R009,R010,R013,R015,R017,R019-R023) do not support automatic fixes; even if the user specifies these rules, only the scan is performed and no fixes are performed.
- Filter fixable rules: fix only the issues of the following 6 rules in this scan, ignore unsupported rules
- Fix one rule at a time: for each rule, perform the fix according to the corresponding fix guide
- Verify after fixing: after fixing is completed, rescan the fixed rules to confirm that the number of issues has decreased
Rules that support automatic fixes(6 in total):
| Rule | Fix Guide | Fix Content |
|---|---|---|
| R008 | guides/R008_testcase_format/R008_FIX_GUIDE.md |
@tc.xxx colon changed to space, delete extra blank lines |
| R011 | guides/R011_testsuite_duplicate/R011_FIX_GUIDE.md |
Append the Adapt suffix to the describe name |
| R012 | guides/R012_p7b_signature/R012_FIX_GUIDE.md |
Regenerate the p7b signature certificate (requires hap-sign-tool.jar) |
| R014 | guides/R014_hap_naming/R014_HAP_NAMING_GUIDE.md |
BUILD.gn hap naming correction + Test.json synchronization |
| R016 | guides/R016_testcase_naming/R016_FIX_GUIDE.md |
Remove special characters + Adapt suffix + synchronize @tc.name |
| R018 | guides/R018_testcase_duplicate/R018_FIX_GUIDE.md |
Deduplicate testcase names + Adapt suffix + synchronize @tc.name |
Fix execution requirements:
- Before performing fixes, read the fix guide under the corresponding
guides/and strictly execute according to the fix rules in the guide - R012 fixes require the use of a signing tool (
guides/R012_p7b_signature/signature_tools/); if the tool is unavailable, prompt the user to fix manually - Fixing will directly modify source files; before fixing, the user must be prompted for confirmation or it is recommended to commit the code to a version control system
- After the fix is complete, rescan and verify the fixed rules, and output a comparison of the number of issues before and after the fix
Usage Examples:
/check-test-code-quality /path/to/code --rules R008,R016 --fix
/check-test-code-quality /path/to/code --level all --fix
Rule Overview
For the complete rule list (name, scan scope, detailed implementation path), see skill_config.json of rules and rule_descriptions fields. For the detection logic of each rule, see rules/Rxxx/SKILL.md。
High-frequency traps quick reference (must pay attention during scanning)
| Trap | Affected Rule | Key Point |
|---|---|---|
| Curly braces in strings interfere with it() block extraction | R004,R015,R016,R018,R019,R020 | Use a state machine to track whether inside/outside a string |
| Apostrophes in backtick template strings interfere | R004,R018 | Track the in_backtick state |
| p7b is DER binary; json.loads will definitely fail | R012 | Use fault-tolerant decoding + regex extraction |
| R001/R005/R006 must scan all source files | R001,R005,R006 | Cannot scan only .test files |
| R016 only checks the character set and does not restrict the naming format | R016 | Use^[a-zA-Z0-9_-]+$, not a format regex |
| R016 detects it() parameters, not @tc.name | R016 | It is strictly forbidden to use the @tc.name value as the detection source |
| R006 only detects conditional judgments | R006 | Assignments and log printing are not issues |
| A group parent BUILD.gn does not block child projects | R011,R019,R020 | Trap 10, see references/TRAPS.md |
| R010 depends on a remote mapping table; it silently returns 0 | R010 | Three configuration files must be fetched remotely to build the mapping table; when the URL is unreachable, an explicit warning is required, see Trap 11 |
For detailed trap descriptions, see references/TRAPS.md。
Detailed Documentation Index
| Document | Description |
|---|---|
| references/TRAPS.md | Detailed descriptions and fix code for 11 known scanning traps |
| references/REPORT_FORMAT.md | Excel/CSV report format specification (12-column header, fix suggestion format) |
| references/ENGINE_IDENTITY.md | Standalone XTS project identification specification (group type handling) |
| references/SCAN_ALGORITHMS.md | Specification for it()/describe() block extraction and subsystem mapping algorithm |
| references/subsystem_mapping.md | 97 subsystem path mapping table |
| references/project_level_scan.md | Shared project-level detection logic (R011/R019/R020/R021) |
| references/VERSION_HISTORY.md | Version change record |
| skill_config.json | Skill configuration center (rule definitions, parameters, output format) |
| scripts/r004_scanner.py | R004 preset scanning script (recursive assertion tracing, must be used) |
| scripts/simple_rules.py | Common scanning utility functions and regex patterns (for reference by dynamically generated code) |
| scripts/common.py | Common utility library (file collection, block parsing, project identification, report generation) |
| scripts/main.py | Scanning entry point (parameter parsing, rule scheduling, report output) |
| guides/FIX_GUIDE.md | Issue Fix Guide |