mtkit 1.16.1__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.
mtkit/conf.py ADDED
@@ -0,0 +1,63 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Project-level config center: load .mtkit.json (convention config) from the project root.
4
+
5
+ convention: when the config file is missing, all defaults apply with zero impact; CLI args take priority over the config file.
6
+
7
+ .mtkit.json example:
8
+ {
9
+ "style": "history", # gen default style (zen/history/auto)
10
+ "output_dir": "用例输出", # default output dir (relative to project root; empty = same dir as the design doc)
11
+ "gate": true, # quality gate switch before rendering
12
+ "db": "db.json", # default database config path
13
+ "patterns_path": ".mtkit-work/patterns.json", # experience library path
14
+ "auto_improve": true # allow AI to self-iterate the tool after authorization (default false)
15
+ }
16
+ """
17
+ import json
18
+ import os
19
+
20
+ CONFIG_NAME = ".mtkit.json"
21
+
22
+ DEFAULTS = {
23
+ "style": "auto",
24
+ "output_dir": "",
25
+ "gate": True,
26
+ "db": "",
27
+ "patterns_path": ".mtkit-work/patterns.json",
28
+ "auto_improve": False,
29
+ }
30
+
31
+
32
+ def _find_config(start_dir=None):
33
+ """Search upward from start_dir for .mtkit.json, return the path or None."""
34
+ current = os.path.abspath(start_dir or os.getcwd())
35
+ while True:
36
+ candidate = os.path.join(current, CONFIG_NAME)
37
+ if os.path.isfile(candidate):
38
+ return candidate
39
+ parent = os.path.dirname(current)
40
+ if parent == current:
41
+ return None
42
+ current = parent
43
+
44
+
45
+ def load_config(start_dir=None):
46
+ """加载项目级配置(不存在则返回默认值)。
47
+
48
+ :return: dict(合并默认值后的配置)
49
+ """
50
+ config = dict(DEFAULTS)
51
+ path = _find_config(start_dir)
52
+ if not path:
53
+ return config
54
+ try:
55
+ with open(path, "r", encoding="utf-8-sig") as fp:
56
+ data = json.load(fp)
57
+ except (ValueError, OSError):
58
+ return config
59
+ if isinstance(data, dict):
60
+ for key, value in data.items():
61
+ if key in DEFAULTS and value not in (None, ""):
62
+ config[key] = value
63
+ return config
mtkit/coverage.py ADDED
@@ -0,0 +1,175 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Coverage diff module: check case tree coverage of doc features / code interfaces.
4
+
5
+ usage:
6
+ 1. design doc features -> case tree: output the uncovered feature list (missing-test reminder)
7
+ 2. code interface list -> case tree: output the uncovered interface list (missing-test reminder)
8
+
9
+ matching rules (text containment match):
10
+ - interface: the full url or its tail segment (e.g. /applypay) appearing in the case tree text counts as covered
11
+ - feature: extracted url/permission key fragments appearing in the case tree text count as covered
12
+ """
13
+ import re
14
+
15
+ # url tail extraction: /a/b/c -> c
16
+ _URL_TAIL_PATTERN = re.compile(r"/([^/\s]+)\s*$")
17
+ # matchable url fragment inside a feature
18
+ _FEATURE_URL_PATTERN = re.compile(r"url\s*[::]\s*(\S+)", re.IGNORECASE)
19
+ # matchable permission fragment inside a feature
20
+ _FEATURE_PERM_PATTERN = re.compile(r"权限点\s*[::]\s*([A-Za-z0-9_.]+)", re.IGNORECASE)
21
+
22
+
23
+ def flatten_text(tree):
24
+ """Flatten the case tree into full text (titles joined by line), for containment matching.
25
+
26
+ :param tree: case tree
27
+ :return: full text
28
+ """
29
+ lines = []
30
+
31
+ def walk(nodes):
32
+ for node in nodes:
33
+ lines.append(node.get("t") or "")
34
+ walk(node.get("c") or [])
35
+
36
+ walk(tree)
37
+ return "\n".join(lines)
38
+
39
+
40
+ def compare_interfaces(facts, tree):
41
+ """Interface list vs case tree coverage diff.
42
+
43
+ :param facts: code_facts facts (with interfaces)
44
+ :param tree: case tree
45
+ :return: {"covered": [...], "missing": [...]}
46
+ """
47
+ text = flatten_text(tree)
48
+ covered = []
49
+ missing = []
50
+ for item in facts.get("interfaces") or []:
51
+ url = item.get("url") or ""
52
+ tail = _URL_TAIL_PATTERN.search(url)
53
+ tail_text = tail.group(1) if tail else ""
54
+ if (url and url in text) or (tail_text and tail_text in text):
55
+ covered.append(item)
56
+ else:
57
+ missing.append(item)
58
+ return {"covered": covered, "missing": missing}
59
+
60
+
61
+ def compare_features(facts, tree):
62
+ """文档功能点 vs 用例树覆盖对比。
63
+
64
+ :param facts: docx 事实(含 features)
65
+ :param tree: 用例树
66
+ :return: {"covered": [...], "missing": [...]}
67
+ """
68
+ text = flatten_text(tree)
69
+ covered = []
70
+ missing = []
71
+ for feature in facts.get("features") or []:
72
+ keys = []
73
+ url_match = _FEATURE_URL_PATTERN.search(feature)
74
+ if url_match:
75
+ keys.append(url_match.group(1))
76
+ tail = _URL_TAIL_PATTERN.search(url_match.group(1))
77
+ if tail:
78
+ keys.append(tail.group(1))
79
+ perm_match = _FEATURE_PERM_PATTERN.search(feature)
80
+ if perm_match:
81
+ keys.append(perm_match.group(1))
82
+ keys = [key for key in keys if key]
83
+ if keys and any(key in text for key in keys):
84
+ covered.append(feature)
85
+ else:
86
+ missing.append(feature)
87
+ return {"covered": covered, "missing": missing}
88
+
89
+
90
+ def collect_case_titles(tree):
91
+ """Collect case titles from a case tree (builder CASE_TITLE_PATTERN aware)."""
92
+ from .builder import CASE_TITLE_PATTERN
93
+
94
+ titles = []
95
+
96
+ def walk(nodes):
97
+ for node in nodes:
98
+ title = node.get("t") or ""
99
+ if CASE_TITLE_PATTERN.match(title):
100
+ titles.append(title)
101
+ walk(node.get("c") or [])
102
+
103
+ walk(tree)
104
+ return titles
105
+
106
+
107
+ def coverage_map(facts, tree):
108
+ """Map interfaces/features to covering case titles (which case covers what).
109
+
110
+ :param facts: code_facts (interfaces) or doc facts (features)
111
+ :param tree: case tree
112
+ :return: {"interface": {key: [case titles]}, "feature": {key: [case titles]}}
113
+ """
114
+ case_titles = collect_case_titles(tree)
115
+ interface_map = {}
116
+ for item in facts.get("interfaces") or []:
117
+ url = item.get("url") or ""
118
+ tail = _URL_TAIL_PATTERN.search(url)
119
+ tail_text = tail.group(1) if tail else ""
120
+ hits = [t for t in case_titles if (url and url in t) or (tail_text and tail_text in t)]
121
+ interface_map[url or (item.get("name") or "?")] = hits
122
+ feature_map = {}
123
+ for feature in facts.get("features") or []:
124
+ url_match = _FEATURE_URL_PATTERN.search(feature)
125
+ key = url_match.group(1) if url_match else feature.replace("\n", " ")[:60]
126
+ perm_match = _FEATURE_PERM_PATTERN.search(feature)
127
+ keys = [key]
128
+ if perm_match:
129
+ keys.append(perm_match.group(1))
130
+ hits = [t for t in case_titles if any(k and k in t for k in keys)]
131
+ feature_map[key] = hits
132
+ return {"interface": interface_map, "feature": feature_map}
133
+
134
+
135
+ def build_report(result, covered=None):
136
+ """对比结果转文本报告。
137
+
138
+ :param result: {"interfaces": {...}, "features": {...}}
139
+ :param covered: coverage_map result (optional, appends covered details)
140
+ :return: 报告文本
141
+ """
142
+ lines = ["===== 覆盖度对比报告 ====="]
143
+ interfaces = result.get("interfaces")
144
+ if interfaces:
145
+ missing = interfaces["missing"]
146
+ lines.append("接口覆盖: %d/%d" % (len(interfaces["covered"]), len(interfaces["covered"]) + len(missing)))
147
+ if missing:
148
+ lines.append("未覆盖接口:")
149
+ for item in missing:
150
+ lines.append(
151
+ " - %s %s (%s, 权限点:%s)" % (
152
+ item.get("method") or "?", item.get("url") or "?",
153
+ item.get("name") or "?", item.get("permission") or "-",
154
+ )
155
+ )
156
+ features = result.get("features")
157
+ if features:
158
+ missing = features["missing"]
159
+ lines.append("功能点覆盖: %d/%d" % (len(features["covered"]), len(features["covered"]) + len(missing)))
160
+ if missing:
161
+ lines.append("未覆盖功能点:")
162
+ for item in missing[:100]:
163
+ lines.append(" - " + item.replace("\n", " ")[:120])
164
+ if covered:
165
+ # covered details: which case covers which interface/feature
166
+ covered_items = [
167
+ ("接口", covered.get("interface") or {}),
168
+ ("功能点", covered.get("feature") or {}),
169
+ ]
170
+ for label, mapping in covered_items:
171
+ if mapping:
172
+ lines.append("已覆盖%s明细:" % label)
173
+ for key, titles in mapping.items():
174
+ lines.append(" - %s -> %s" % (key, "、".join(titles) if titles else "(无)"))
175
+ return "\n".join(lines)