test-coverage-analyzer 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,30 @@
1
+ """
2
+ Test Coverage Analyzer - 多语言测试文件查找和覆盖率分析工具
3
+ """
4
+
5
+ __version__ = "0.1.0"
6
+ __author__ = "xisang"
7
+ __email__ = "xisang@gmail.com"
8
+
9
+ from .core import (
10
+ TestFileMatch,
11
+ CoverageResult,
12
+ TestFileFinder,
13
+ CodeAnalyzer,
14
+ CoverageAnalyzer,
15
+ save_results_to_txt,
16
+ save_results_to_json
17
+ )
18
+
19
+ from .cli import main
20
+
21
+ __all__ = [
22
+ "TestFileMatch",
23
+ "CoverageResult",
24
+ "TestFileFinder",
25
+ "CodeAnalyzer",
26
+ "CoverageAnalyzer",
27
+ "save_results_to_txt",
28
+ "save_results_to_json",
29
+ "main"
30
+ ]
@@ -0,0 +1,109 @@
1
+ """命令行接口"""
2
+
3
+ import sys
4
+ import os
5
+ from pathlib import Path
6
+ from .core import (
7
+ TestFileFinder,
8
+ CoverageAnalyzer,
9
+ save_results_to_txt,
10
+ save_results_to_json
11
+ )
12
+
13
+ def main():
14
+ """主函数,提供命令行接口"""
15
+ # 解析命令行参数
16
+ if len(sys.argv) < 2 or len(sys.argv) > 4:
17
+ print("用法: test-coverage-analyzer <源代码路径> [输出目录] [文件名前缀]")
18
+ print("示例:")
19
+ print(" test-coverage-analyzer ./my_project")
20
+ print(" test-coverage-analyzer ./my_project ./reports")
21
+ print(" test-coverage-analyzer ./my_project ./reports my_result")
22
+ sys.exit(1)
23
+
24
+ # 获取参数
25
+ source_path = sys.argv[1]
26
+
27
+ # 设置默认值
28
+ output_dir = "./"
29
+ file_prefix = "scanner_result"
30
+
31
+ # 解析可选参数
32
+ if len(sys.argv) >= 3:
33
+ output_dir = sys.argv[2] # 第二个参数始终是输出目录
34
+ if len(sys.argv) == 4:
35
+ file_prefix = sys.argv[3] # 第三个参数是文件名前缀
36
+
37
+ # 验证源路径
38
+ if not os.path.exists(source_path):
39
+ print(f"错误: 源路径 '{source_path}' 不存在")
40
+ sys.exit(1)
41
+
42
+ # 创建输出目录(如果不存在)
43
+ try:
44
+ Path(output_dir).mkdir(parents=True, exist_ok=True)
45
+ except Exception as e:
46
+ print(f"错误: 无法创建输出目录 '{output_dir}': {e}")
47
+ sys.exit(1)
48
+
49
+ # 构建完整的文件路径
50
+ txt_path = os.path.join(output_dir, f"{file_prefix}.txt")
51
+ json_path = os.path.join(output_dir, f"{file_prefix}.json")
52
+
53
+ # 执行分析
54
+ finder = TestFileFinder()
55
+ analyzer = CoverageAnalyzer()
56
+
57
+ try:
58
+ print(f"正在分析路径: {source_path}")
59
+ print("查找源文件和测试文件配对...")
60
+
61
+ matches, unmatched = finder.find_test_files(source_path)
62
+
63
+ print(f"找到 {len(matches)} 个配对,{len(unmatched)} 个未匹配文件")
64
+ print("分析覆盖率和代码复杂度...")
65
+
66
+ # 分析覆盖率
67
+ coverage_results = []
68
+ for match in matches:
69
+ coverage_result = analyzer.analyze_coverage(
70
+ match.source_file,
71
+ match.test_file,
72
+ match.language
73
+ )
74
+ coverage_results.append(coverage_result)
75
+
76
+ # 分析未匹配文件的复杂度
77
+ unmatched_results = []
78
+ for source_file, lang in unmatched:
79
+ unmatched_result = analyzer.analyze_unmatched_file(source_file, lang)
80
+ unmatched_results.append(unmatched_result)
81
+
82
+ # 保存结果
83
+ save_results_to_txt(matches, unmatched, coverage_results, unmatched_results, txt_path)
84
+ save_results_to_json(matches, unmatched, coverage_results, unmatched_results, json_path)
85
+
86
+ print(f"结果已保存到:")
87
+ print(f" TXT文件: {txt_path}")
88
+ print(f" JSON文件: {json_path}")
89
+ print(f"\n=== 分析结果摘要 ===")
90
+ print(f"源文件总数: {len(matches) + len(unmatched)}")
91
+ print(f"已匹配测试文件: {len(matches)}")
92
+ print(f"未匹配测试文件: {len(unmatched)}")
93
+
94
+ if coverage_results:
95
+ avg_coverage = sum(cr.coverage_percentage for cr in coverage_results) / len(coverage_results)
96
+ print(f"平均覆盖率: {avg_coverage:.2f}%")
97
+
98
+ if unmatched_results:
99
+ avg_cyclomatic = sum(ur.cyclomatic_complexity for ur in unmatched_results) / len(unmatched_results)
100
+ print(f"未匹配文件平均圈复杂度: {avg_cyclomatic:.2f}")
101
+
102
+ except Exception as e:
103
+ print(f"错误: {e}")
104
+ import traceback
105
+ traceback.print_exc()
106
+ sys.exit(1)
107
+
108
+ if __name__ == "__main__":
109
+ main()