Skip to content

Commit 69f7319

Browse files
committed
feat: add test suggestion disable flag
1. Added `--no-test-suggestions` flag to disable test suggestions in commit messages 2. Modified prompt templates to exclude test suggestion section when disabled 3. Updated README to reflect the new flag feat: 添加禁用测试建议的标志 1. 添加了 `--no-test-suggestions` 标志,用于在提交消息中禁用测试建议 2. 修改了提示模板,以便在禁用时排除测试建议部分 3. 更新了 README 以反映新的标志
1 parent c2cff77 commit 69f7319

3 files changed

Lines changed: 183 additions & 17 deletions

File tree

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
- 自动检测中文内容
4343
- 智能中英互译
4444
- 保持格式规范
45+
- 📋 测试建议
46+
- 基于代码变更智能生成黑盒测试建议
47+
- 关注测试重点和覆盖范围
48+
- 可通过参数禁用
4549

4650
## 📦 安装
4751

@@ -189,7 +193,7 @@ git-commit-helper translate /path/to/existing/file # 文件路径
189193
| ai list | 列出所有服务 | `git-commit-helper ai list` |
190194
| ai test | 测试指定服务 | `git-commit-helper ai test [-t "测试文本"]` |
191195
| translate | 翻译内容 | `git-commit-helper translate [-f 文件] [-t 文本]` |
192-
| commit | 生成提交信息 | `git-commit-helper commit [-t 类型] [-m 描述] [-a] [--no-review/--only-chinese/--only-english]` |
196+
| commit | 生成提交信息 | `git-commit-helper commit [-t 类型] [-m 描述] [-a] [--no-review/--no-test-suggestions/--only-chinese/--only-english]` |
193197
| ai-review | 管理 AI 代码审查 | `git-commit-helper ai-review [--enable/--disable/--status]` |
194198

195199
### 提交类型
@@ -234,6 +238,7 @@ git-commit-helper commit [选项]
234238
-m, --message <MSG> 提供对改动的描述 (可选)
235239
-a, --all 自动添加所有已修改但未暂存的文件
236240
--no-review 禁用当前提交的代码审查功能
241+
--no-test-suggestions 禁用当前提交的测试建议功能
237242
--only-chinese 仅保留中文提交信息
238243
--only-english 仅保留英文提交信息
239244
```
@@ -270,6 +275,12 @@ git-commit-helper commit --type feat --message "添加新功能" --only-chinese
270275

271276
# 单次提交使用英文
272277
git-commit-helper commit --type feat --message "Add new functions" --only-english
278+
279+
# 禁用测试建议
280+
git-commit-helper commit --no-test-suggestions
281+
282+
# 同时禁用代码审查和测试建议
283+
git-commit-helper commit --no-review --no-test-suggestions
273284
```
274285

275286
### AI 代码审查功能
@@ -279,6 +290,44 @@ git-commit-helper commit --type feat --message "Add new functions" --only-englis
279290
1. 本地提交审查:在每次提交代码时自动执行
280291
2. 远程代码审查:支持审查 GitHub 和 Gerrit 上的改动
281292

293+
### 测试建议功能
294+
295+
工具在生成提交信息时会自动包含测试建议部分,帮助开发者明确测试重点:
296+
297+
1. **智能测试建议生成**
298+
- 基于代码变更自动分析测试需求
299+
- 专注于黑盒测试方法和策略
300+
- 提供具体的测试场景和边界条件
301+
302+
2. **测试范围覆盖**
303+
- 功能性测试建议(正常流程、异常处理)
304+
- 边界值测试建议(输入验证、数据范围)
305+
- 安全性测试建议(权限验证、数据保护)
306+
- 性能测试建议(响应时间、负载处理)
307+
308+
3. **灵活控制**
309+
- 默认启用测试建议生成
310+
- 可通过 `--no-test-suggestions` 参数禁用
311+
- 支持中英双语测试建议
312+
313+
示例生成的测试建议:
314+
```text
315+
feat: 添加用户认证模块
316+
317+
1. 实现基于 JWT 的认证系统
318+
2. 添加用户登录和注册端点
319+
3. 包含使用 bcrypt 的密码哈希处理
320+
4. 设置令牌刷新机制
321+
322+
测试建议:
323+
- 测试用户注册功能,包括有效和无效输入
324+
- 验证登录功能,测试正确和错误的凭据
325+
- 测试 JWT 令牌生成和验证流程
326+
- 验证密码安全性和哈希处理
327+
- 测试令牌刷新机制和过期处理
328+
- 验证受保护端点的访问控制
329+
```
330+
282331
远程代码审查功能包含:
283332
1. 提交信息翻译
284333
- 显示原始提交标题和内容

src/commit.rs

Lines changed: 128 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,115 @@ const ENGLISH_PROMPT_TEMPLATE: &str = r#"Please analyze the git diff content and
1515
1. First line: type: message (under 50 characters)
1616
2. Empty line after the title
1717
3. Detailed explanation in English (what was changed and why)
18+
4. Empty line after explanation
19+
5. Testing Suggestions section with black-box testing recommendations
20+
6. Type must be one of: feat/fix/docs/style/refactor/test/chore
21+
7. Focus on both WHAT changed and WHY it was necessary
22+
8. Include any important technical details or context
23+
9. DO NOT include any Chinese content
24+
10. DO NOT wrap the response in any markdown or code block markers
25+
26+
Example response format:
27+
feat: add user authentication module
28+
29+
1. Implement JWT-based authentication system
30+
2. Add user login and registration endpoints
31+
3. Include password hashing with bcrypt
32+
4. Set up token refresh mechanism
33+
34+
Testing Suggestions:
35+
- Test user registration with valid and invalid inputs
36+
- Verify login functionality with correct and incorrect credentials
37+
- Test JWT token generation and validation
38+
- Verify password security and hashing
39+
- Test token refresh mechanism and expiration handling
40+
- Verify access control for protected endpoints
41+
42+
Please respond with ONLY the commit message following this format,
43+
DO NOT end commit titles with any punctuation."#;
44+
45+
const CHINESE_PROMPT_TEMPLATE: &str = r#"请分析以下 git diff 内容,并按照以下格式生成提交信息:
46+
1. 第一行为标题:type: message(不超过50个字符)
47+
2. 标题下方空一行
48+
3. 详细的中文说明(解释做了什么改动以及为什么需要这些改动)
49+
4. 说明下方空一行
50+
5. 测试建议部分,提供黑盒测试的重点和范围
51+
6. type 必须是以下之一:feat/fix/docs/style/refactor/test/chore
52+
7. 关注点:变更内容(做了什么)和变更原因(为什么)
53+
8. 包含重要的技术细节或上下文
54+
9. 不要使用任何 markdown 或代码块标记
55+
10. 标题结尾不要使用标点符号
56+
57+
示例格式:
58+
feat: 添加用户认证模块
59+
60+
1. 实现基于 JWT 的认证系统
61+
2. 添加用户登录和注册端点
62+
3. 包含使用 bcrypt 的密码哈希处理
63+
4. 设置令牌刷新机制
64+
65+
测试建议:
66+
- 测试用户注册功能,包括有效和无效输入
67+
- 验证登录功能,测试正确和错误的凭据
68+
- 测试 JWT 令牌生成和验证流程
69+
- 验证密码安全性和哈希处理
70+
- 测试令牌刷新机制和过期处理
71+
- 验证受保护端点的访问控制"#;
72+
73+
const BILINGUAL_PROMPT_TEMPLATE: &str = r#"Please analyze the git diff content and generate a detailed bilingual commit message with:
74+
1. First line in English: type: message (under 50 characters)
75+
2. Empty line after the title
76+
3. Detailed explanation in English (what was changed and why)
77+
4. Empty line after English explanation
78+
5. Testing Suggestions section in English with black-box testing recommendations
79+
6. Empty line after English testing suggestions
80+
7. Chinese title and explanation (translate the English content)
81+
8. Empty line after Chinese explanation
82+
9. Chinese Testing Suggestions section (translate the English testing suggestions)
83+
10. Type must be one of: feat/fix/docs/style/refactor/test/chore
84+
11. Focus on both WHAT changed and WHY it was necessary
85+
12. Include any important technical details or context
86+
13. DO NOT wrap the response in any markdown or code block markers
87+
88+
Example response format:
89+
feat: add user authentication module
90+
91+
1. Implement JWT-based authentication system
92+
2. Add user login and registration endpoints
93+
3. Include password hashing with bcrypt
94+
4. Set up token refresh mechanism
95+
96+
Testing Suggestions:
97+
- Test user registration with valid and invalid inputs
98+
- Verify login functionality with correct and incorrect credentials
99+
- Test JWT token generation and validation
100+
- Verify password security and hashing
101+
- Test token refresh mechanism and expiration handling
102+
- Verify access control for protected endpoints
103+
104+
feat: 添加用户认证模块
105+
106+
1. 实现基于 JWT 的认证系统
107+
2. 添加用户登录和注册端点
108+
3. 包含使用 bcrypt 的密码哈希处理
109+
4. 设置令牌刷新机制
110+
111+
测试建议:
112+
- 测试用户注册功能,包括有效和无效输入
113+
- 验证登录功能,测试正确和错误的凭据
114+
- 测试 JWT 令牌生成和验证流程
115+
- 验证密码安全性和哈希处理
116+
- 测试令牌刷新机制和过期处理
117+
- 验证受保护端点的访问控制
118+
119+
Please respond with ONLY the commit message following this format,
120+
DO NOT end commit titles with any punctuation."#;
121+
122+
// 无测试建议版本的提示词模板
123+
const ENGLISH_PROMPT_TEMPLATE_NO_TEST: &str = r#"Please analyze the git diff content and generate a commit message in English only:
124+
1. First line: type: message (under 50 characters)
125+
2. Empty line after the title
126+
3. Detailed explanation in English (what was changed and why)
18127
4. Type must be one of: feat/fix/docs/style/refactor/test/chore
19128
5. Focus on both WHAT changed and WHY it was necessary
20129
6. Include any important technical details or context
@@ -32,7 +141,7 @@ feat: add user authentication module
32141
Please respond with ONLY the commit message following this format,
33142
DO NOT end commit titles with any punctuation."#;
34143

35-
const CHINESE_PROMPT_TEMPLATE: &str = r#"请分析以下 git diff 内容,并按照以下格式生成提交信息:
144+
const CHINESE_PROMPT_TEMPLATE_NO_TEST: &str = r#"请分析以下 git diff 内容,并按照以下格式生成提交信息:
36145
1. 第一行为标题:type: message(不超过50个字符)
37146
2. 标题下方空一行
38147
3. 详细的中文说明(解释做了什么改动以及为什么需要这些改动)
@@ -50,7 +159,7 @@ feat: 添加用户认证模块
50159
3. 包含使用 bcrypt 的密码哈希处理
51160
4. 设置令牌刷新机制"#;
52161

53-
const BILINGUAL_PROMPT_TEMPLATE: &str = r#"Please analyze the git diff content and generate a detailed bilingual commit message with:
162+
const BILINGUAL_PROMPT_TEMPLATE_NO_TEST: &str = r#"Please analyze the git diff content and generate a detailed bilingual commit message with:
54163
1. First line in English: type: message (under 50 characters)
55164
2. Empty line after the title
56165
3. Detailed explanation in English (what was changed and why)
@@ -90,19 +199,22 @@ impl LanguageMode {
90199
}
91200
}
92201

93-
fn template(&self) -> &'static str {
94-
match self {
95-
Self::EnglishOnly => ENGLISH_PROMPT_TEMPLATE,
96-
Self::ChineseOnly => CHINESE_PROMPT_TEMPLATE,
97-
Self::Bilingual => BILINGUAL_PROMPT_TEMPLATE,
202+
fn template(&self, include_test_suggestions: bool) -> &'static str {
203+
match (self, include_test_suggestions) {
204+
(Self::EnglishOnly, true) => ENGLISH_PROMPT_TEMPLATE,
205+
(Self::EnglishOnly, false) => ENGLISH_PROMPT_TEMPLATE_NO_TEST,
206+
(Self::ChineseOnly, true) => CHINESE_PROMPT_TEMPLATE,
207+
(Self::ChineseOnly, false) => CHINESE_PROMPT_TEMPLATE_NO_TEST,
208+
(Self::Bilingual, true) => BILINGUAL_PROMPT_TEMPLATE,
209+
(Self::Bilingual, false) => BILINGUAL_PROMPT_TEMPLATE_NO_TEST,
98210
}
99211
}
100212
}
101213

102214
// 统一的提示词构建函数
103-
fn build_prompt(mode: LanguageMode, user_message: Option<&str>) -> String {
104-
let mut prompt = String::from(mode.template());
105-
215+
fn build_prompt(mode: LanguageMode, user_message: Option<&str>, include_test_suggestions: bool) -> String {
216+
let mut prompt = String::from(mode.template(include_test_suggestions));
217+
106218
if let Some(msg) = user_message {
107219
match mode {
108220
LanguageMode::ChineseOnly => {
@@ -122,7 +234,7 @@ fn build_prompt(mode: LanguageMode, user_message: Option<&str>) -> String {
122234
}
123235
}
124236
}
125-
237+
126238
prompt
127239
}
128240

@@ -229,6 +341,7 @@ pub async fn generate_commit_message(
229341
no_translate: bool,
230342
mut only_chinese: bool,
231343
mut only_english: bool,
344+
no_test_suggestions: bool,
232345
) -> anyhow::Result<()> {
233346
// 加载配置,如果指定了参数则使用参数值,否则使用配置中的默认值
234347
if let Ok(config) = config::Config::load() {
@@ -237,7 +350,7 @@ pub async fn generate_commit_message(
237350
only_english = config.only_english;
238351
}
239352
}
240-
353+
241354
// 处理语言选项冲突:only_english 优先级最高
242355
if only_english {
243356
only_chinese = false;
@@ -280,9 +393,10 @@ pub async fn generate_commit_message(
280393
// 设置环境变量标记跳过后续的代码审查
281394
std::env::set_var("GIT_COMMIT_HELPER_SKIP_REVIEW", "1");
282395

283-
// 确定语言模式并构建提示词
396+
// 确定语言模式并构建提示词,考虑是否包含测试建议
284397
let language_mode = LanguageMode::determine(only_chinese, only_english);
285-
let prompt = build_prompt(language_mode, message.as_deref());
398+
let include_test_suggestions = !no_test_suggestions;
399+
let prompt = build_prompt(language_mode, message.as_deref(), include_test_suggestions);
286400

287401
debug!("生成的提示信息:\n{}", prompt);
288402

src/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ enum Commands {
9292
/// 仅保留英文提交信息
9393
#[arg(long = "only-english")]
9494
only_english: bool,
95+
/// 禁用测试建议
96+
#[arg(long)]
97+
no_test_suggestions: bool,
9598
},
9699
/// 管理 AI 代码审查功能
97100
#[command(name = "ai-review")]
@@ -404,8 +407,8 @@ async fn main() -> Result<()> {
404407
Err(e) => Err(e)
405408
}
406409
}
407-
Some(Commands::Commit { r#type, message, all, no_translate, only_chinese, only_english }) => {
408-
commit::generate_commit_message(r#type, message, all, cli.no_review, no_translate, only_chinese, only_english).await
410+
Some(Commands::Commit { r#type, message, all, no_translate, only_chinese, only_english, no_test_suggestions }) => {
411+
commit::generate_commit_message(r#type, message, all, cli.no_review, no_translate, only_chinese, only_english, no_test_suggestions).await
409412
}
410413
Some(Commands::AIReview { enable, disable, status }) => {
411414
let mut config = config::Config::load()?;

0 commit comments

Comments
 (0)