@zjex/git-workflow 0.2.23 → 0.2.24

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,33 @@
1
+ name: Test
2
+
3
+ on:
4
+ push:
5
+ branches: [main, dev]
6
+ pull_request:
7
+ branches: [main, dev]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+
13
+ strategy:
14
+ matrix:
15
+ node-version: [18.x, 20.x]
16
+
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+
20
+ - name: Setup Node.js ${{ matrix.node-version }}
21
+ uses: actions/setup-node@v4
22
+ with:
23
+ node-version: ${{ matrix.node-version }}
24
+ cache: "npm"
25
+
26
+ - name: Install dependencies
27
+ run: npm ci
28
+
29
+ - name: Run tests
30
+ run: npm test
31
+
32
+ - name: Build
33
+ run: npm run build
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env sh
2
+ . "$(dirname -- "$0")/_/husky.sh"
3
+
4
+ # 运行测试
5
+ npm test
package/README.md CHANGED
@@ -6,6 +6,7 @@
6
6
  <a href="https://github.com/iamzjt-front-end/git-workflow"><img src="https://img.shields.io/github/stars/iamzjt-front-end/git-workflow?style=flat&colorA=18181B&colorB=F59E0B" alt="github stars"></a>
7
7
  <a href="https://github.com/iamzjt-front-end/git-workflow/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/@zjex/git-workflow?style=flat&colorA=18181B&colorB=10B981" alt="license"></a>
8
8
  <a href="https://nodejs.org"><img src="https://img.shields.io/badge/node-%3E%3D18-339933?style=flat&logo=node.js&logoColor=white&colorA=18181B" alt="node version"></a>
9
+ <a href="https://github.com/iamzjt-front-end/git-workflow/actions"><img src="https://img.shields.io/badge/tests-100%20passed-success?style=flat&colorA=18181B" alt="tests"></a>
9
10
  <a href="https://github.com/iamzjt-front-end/git-workflow/issues"><img src="https://img.shields.io/github/issues/iamzjt-front-end/git-workflow?style=flat&colorA=18181B&colorB=EC4899" alt="issues"></a>
10
11
  </p>
11
12
 
@@ -1150,11 +1151,65 @@ npm run dev
1150
1151
  # 构建
1151
1152
  npm run build
1152
1153
 
1154
+ # 运行测试
1155
+ npm test
1156
+
1157
+ # 监听模式(开发时使用)
1158
+ npm run test:watch
1159
+
1160
+ # 可视化测试界面
1161
+ npm run test:ui
1162
+
1163
+ # 生成测试覆盖率报告
1164
+ npm run test:coverage
1165
+
1153
1166
  # 本地测试
1154
1167
  npm link
1155
1168
  gw --version
1156
1169
  ```
1157
1170
 
1171
+ ### 测试
1172
+
1173
+ 本项目使用 [Vitest](https://vitest.dev/) 作为测试框架,确保每次变更不会影响现有功能。
1174
+
1175
+ **测试覆盖:**
1176
+
1177
+ - ✅ Tag 功能(前缀提取、分组、显示逻辑)
1178
+ - ✅ Commit 功能(提交类型、消息格式、emoji)
1179
+ - ✅ 工具函数
1180
+
1181
+ **运行测试:**
1182
+
1183
+ ```bash
1184
+ # 单次运行所有测试
1185
+ npm test
1186
+
1187
+ # 监听模式(开发时推荐)
1188
+ npm run test:watch
1189
+
1190
+ # 可视化界面
1191
+ npm run test:ui
1192
+
1193
+ # 生成覆盖率报告
1194
+ npm run test:coverage
1195
+ ```
1196
+
1197
+ **添加新测试:**
1198
+
1199
+ 在 `tests/` 目录创建对应的测试文件:
1200
+
1201
+ ```typescript
1202
+ import { describe, it, expect } from "vitest";
1203
+
1204
+ describe("新功能", () => {
1205
+ it("应该正确工作", () => {
1206
+ expect(result).toBe(expected);
1207
+ });
1208
+ });
1209
+ ```
1210
+
1211
+ 详细测试指南请查看 [tests/README.md](./tests/README.md)。
1212
+
1158
1213
  ## 📦 发布
1159
1214
 
1160
1215
  ```bash
package/TESTING.md ADDED
@@ -0,0 +1,436 @@
1
+ # 测试体系说明
2
+
3
+ ## 为什么需要测试?
4
+
5
+ 在开发 CLI 工具时,测试能够:
6
+
7
+ 1. **防止回归** - 确保新功能不会破坏现有功能
8
+ 2. **提高信心** - 重构代码时更有底气
9
+ 3. **文档作用** - 测试用例本身就是最好的使用示例
10
+ 4. **快速反馈** - 比手动测试快得多
11
+
12
+ ## 测试框架选择
13
+
14
+ 我们选择 **Vitest** 的原因:
15
+
16
+ - ✅ 快速 - 基于 Vite,启动和运行都很快
17
+ - ✅ 兼容 Jest - API 与 Jest 几乎完全兼容
18
+ - ✅ ESM 原生支持 - 无需额外配置
19
+ - ✅ TypeScript 支持 - 开箱即用
20
+ - ✅ UI 界面 - 提供可视化测试界面
21
+ - ✅ 覆盖率报告 - 内置覆盖率工具
22
+
23
+ ## 测试结构
24
+
25
+ ```
26
+ tests/
27
+ ├── utils.test.ts # 工具函数测试
28
+ ├── tag.test.ts # Tag 功能测试
29
+ ├── commit.test.ts # Commit 功能测试
30
+ └── README.md # 测试指南
31
+ ```
32
+
33
+ ## 当前测试覆盖
34
+
35
+ ### Tag 功能 ✅
36
+
37
+ - [x] 前缀提取(v, g, release-, 无前缀)
38
+ - [x] Tag 分组逻辑
39
+ - [x] 显示逻辑(最多 5 个)
40
+ - [x] 列宽计算
41
+ - [x] 省略号显示
42
+
43
+ ### Commit 功能 ✅
44
+
45
+ - [x] 提交类型定义
46
+ - [x] 提交消息格式
47
+ - [x] Refactor 对齐处理
48
+ - [x] Emoji 使用
49
+
50
+ ### Utils 功能 ✅
51
+
52
+ - [x] 字符串操作
53
+ - [x] 基本工具函数
54
+
55
+ ## 运行测试
56
+
57
+ ### 基本命令
58
+
59
+ ```bash
60
+ # 运行所有测试(单次)
61
+ npm test
62
+
63
+ # 监听模式(开发时使用)
64
+ npm run test:watch
65
+
66
+ # 可视化界面
67
+ npm run test:ui
68
+
69
+ # 生成覆盖率报告
70
+ npm run test:coverage
71
+ ```
72
+
73
+ ### 运行特定测试
74
+
75
+ ```bash
76
+ # 只运行 tag 相关测试
77
+ npx vitest tests/tag.test.ts
78
+
79
+ # 只运行匹配的测试
80
+ npx vitest -t "前缀提取"
81
+
82
+ # 运行并查看详细输出
83
+ npx vitest --reporter=verbose
84
+ ```
85
+
86
+ ## 编写测试
87
+
88
+ ### 基本示例
89
+
90
+ ```typescript
91
+ import { describe, it, expect } from "vitest";
92
+
93
+ describe("功能模块", () => {
94
+ it("应该做某事", () => {
95
+ const result = someFunction();
96
+ expect(result).toBe(expectedValue);
97
+ });
98
+ });
99
+ ```
100
+
101
+ ### 测试分组
102
+
103
+ ```typescript
104
+ describe("Tag 功能", () => {
105
+ describe("前缀提取", () => {
106
+ it("应该提取 v 前缀", () => {
107
+ // 测试代码
108
+ });
109
+
110
+ it("应该处理无前缀", () => {
111
+ // 测试代码
112
+ });
113
+ });
114
+
115
+ describe("Tag 分组", () => {
116
+ it("应该按前缀分组", () => {
117
+ // 测试代码
118
+ });
119
+ });
120
+ });
121
+ ```
122
+
123
+ ### Mock 外部依赖
124
+
125
+ ```typescript
126
+ import { vi } from "vitest";
127
+ import { execSync } from "child_process";
128
+
129
+ // Mock 整个模块
130
+ vi.mock("child_process");
131
+
132
+ it("应该调用 git 命令", () => {
133
+ const mockExecSync = vi.mocked(execSync);
134
+ mockExecSync.mockReturnValue("v0.1.0\nv0.2.0");
135
+
136
+ // 测试代码...
137
+
138
+ expect(mockExecSync).toHaveBeenCalledWith("git tag -l");
139
+ });
140
+ ```
141
+
142
+ ### 测试异步代码
143
+
144
+ ```typescript
145
+ it("应该异步获取数据", async () => {
146
+ const result = await fetchData();
147
+ expect(result).toBeDefined();
148
+ });
149
+ ```
150
+
151
+ ## CI/CD 集成
152
+
153
+ ### GitHub Actions
154
+
155
+ 已配置 `.github/workflows/test.yml`:
156
+
157
+ ```yaml
158
+ name: Test
159
+
160
+ on:
161
+ push:
162
+ branches: [main, dev]
163
+ pull_request:
164
+ branches: [main, dev]
165
+
166
+ jobs:
167
+ test:
168
+ runs-on: ubuntu-latest
169
+ strategy:
170
+ matrix:
171
+ node-version: [18.x, 20.x]
172
+ steps:
173
+ - uses: actions/checkout@v4
174
+ - name: Setup Node.js
175
+ uses: actions/setup-node@v4
176
+ with:
177
+ node-version: ${{ matrix.node-version }}
178
+ - name: Install dependencies
179
+ run: npm ci
180
+ - name: Run tests
181
+ run: npm test
182
+ - name: Build
183
+ run: npm run build
184
+ ```
185
+
186
+ ### Pre-commit Hook
187
+
188
+ 已配置 `.husky/pre-commit`,每次提交前自动运行测试:
189
+
190
+ ```bash
191
+ #!/usr/bin/env sh
192
+ . "$(dirname -- "$0")/_/husky.sh"
193
+
194
+ # 运行测试
195
+ npm test
196
+ ```
197
+
198
+ 如果测试失败,提交会被阻止。
199
+
200
+ ## 测试最佳实践
201
+
202
+ ### 1. 测试应该独立
203
+
204
+ 每个测试不应该依赖其他测试的结果:
205
+
206
+ ```typescript
207
+ // ❌ 不好 - 依赖全局状态
208
+ let result;
209
+ it("测试 1", () => {
210
+ result = doSomething();
211
+ });
212
+ it("测试 2", () => {
213
+ expect(result).toBe(expected); // 依赖测试 1
214
+ });
215
+
216
+ // ✅ 好 - 每个测试独立
217
+ it("测试 1", () => {
218
+ const result = doSomething();
219
+ expect(result).toBe(expected);
220
+ });
221
+ it("测试 2", () => {
222
+ const result = doSomething();
223
+ expect(result).toBe(expected);
224
+ });
225
+ ```
226
+
227
+ ### 2. 使用描述性的测试名称
228
+
229
+ ```typescript
230
+ // ❌ 不好
231
+ it("测试 1", () => {});
232
+
233
+ // ✅ 好
234
+ it("应该正确提取 v 前缀", () => {});
235
+ it("无前缀时应该返回 (无前缀)", () => {});
236
+ ```
237
+
238
+ ### 3. 测试边界情况
239
+
240
+ ```typescript
241
+ describe("Tag 显示", () => {
242
+ it("应该显示少于 5 个的所有 tag", () => {
243
+ // 测试 1-4 个 tag
244
+ });
245
+
246
+ it("应该只显示最后 5 个 tag", () => {
247
+ // 测试正好 5 个 tag
248
+ });
249
+
250
+ it("超过 5 个时应该显示省略号", () => {
251
+ // 测试 6+ 个 tag
252
+ });
253
+
254
+ it("应该处理空数组", () => {
255
+ // 测试 0 个 tag
256
+ });
257
+ });
258
+ ```
259
+
260
+ ### 4. 使用 beforeEach/afterEach
261
+
262
+ ```typescript
263
+ import { beforeEach, afterEach } from "vitest";
264
+
265
+ describe("测试套件", () => {
266
+ beforeEach(() => {
267
+ // 每个测试前执行
268
+ vi.clearAllMocks();
269
+ });
270
+
271
+ afterEach(() => {
272
+ // 每个测试后执行
273
+ vi.restoreAllMocks();
274
+ });
275
+
276
+ it("测试 1", () => {});
277
+ it("测试 2", () => {});
278
+ });
279
+ ```
280
+
281
+ ### 5. 测试覆盖率目标
282
+
283
+ - **核心功能** - 100% 覆盖
284
+ - **工具函数** - 90%+ 覆盖
285
+ - **UI 交互** - 关键路径覆盖
286
+
287
+ ## 添加新功能时的测试流程
288
+
289
+ 1. **先写测试(TDD)**
290
+
291
+ ```typescript
292
+ // 1. 写测试
293
+ it("应该支持新功能", () => {
294
+ const result = newFeature();
295
+ expect(result).toBe(expected);
296
+ });
297
+
298
+ // 2. 运行测试(应该失败)
299
+ // npm test
300
+
301
+ // 3. 实现功能
302
+ function newFeature() {
303
+ // 实现代码
304
+ }
305
+
306
+ // 4. 运行测试(应该通过)
307
+ // npm test
308
+ ```
309
+
310
+ 2. **或先实现后测试**
311
+
312
+ ```typescript
313
+ // 1. 实现功能
314
+ function newFeature() {
315
+ // 实现代码
316
+ }
317
+
318
+ // 2. 写测试
319
+ it("应该支持新功能", () => {
320
+ const result = newFeature();
321
+ expect(result).toBe(expected);
322
+ });
323
+
324
+ // 3. 运行测试
325
+ // npm test
326
+ ```
327
+
328
+ 3. **提交前检查**
329
+ ```bash
330
+ # 运行所有测试
331
+ npm test
332
+ # 检查覆盖率
333
+ npm run test:coverage
334
+ # 构建验证
335
+ npm run build
336
+ ```
337
+
338
+ ## 调试测试
339
+
340
+ ### 使用 console.log
341
+
342
+ ```typescript
343
+ it("调试测试", () => {
344
+ const result = someFunction();
345
+ console.log("result:", result); // 会在测试输出中显示
346
+ expect(result).toBe(expected);
347
+ });
348
+ ```
349
+
350
+ ### 使用 test.only
351
+
352
+ ```typescript
353
+ // 只运行这一个测试
354
+ it.only("调试这个测试", () => {
355
+ // 测试代码
356
+ });
357
+ ```
358
+
359
+ ### 使用 UI 界面
360
+
361
+ ```bash
362
+ npm run test:ui
363
+ ```
364
+
365
+ 在浏览器中查看测试结果,可以:
366
+
367
+ - 查看测试树
368
+ - 查看失败详情
369
+ - 重新运行单个测试
370
+ - 查看覆盖率
371
+
372
+ ## 常见问题
373
+
374
+ ### Q: 测试运行很慢怎么办?
375
+
376
+ A: 使用监听模式,只运行改动的测试:
377
+
378
+ ```bash
379
+ npm run test:watch
380
+ ```
381
+
382
+ ### Q: 如何跳过某个测试?
383
+
384
+ A: 使用 `it.skip`:
385
+
386
+ ```typescript
387
+ it.skip("暂时跳过这个测试", () => {
388
+ // 测试代码
389
+ });
390
+ ```
391
+
392
+ ### Q: 如何测试 CLI 交互?
393
+
394
+ A: Mock inquirer 的 prompt:
395
+
396
+ ```typescript
397
+ import { vi } from "vitest";
398
+ import { select } from "@inquirer/prompts";
399
+
400
+ vi.mock("@inquirer/prompts");
401
+
402
+ it("应该选择正确的选项", async () => {
403
+ vi.mocked(select).mockResolvedValue("option1");
404
+ // 测试代码
405
+ });
406
+ ```
407
+
408
+ ### Q: 如何测试 git 命令?
409
+
410
+ A: Mock child_process:
411
+
412
+ ```typescript
413
+ import { vi } from "vitest";
414
+ import { execSync } from "child_process";
415
+
416
+ vi.mock("child_process");
417
+
418
+ it("应该执行 git 命令", () => {
419
+ vi.mocked(execSync).mockReturnValue("success");
420
+ // 测试代码
421
+ });
422
+ ```
423
+
424
+ ## 未来计划
425
+
426
+ - [ ] 增加集成测试
427
+ - [ ] 增加 E2E 测试
428
+ - [ ] 提高测试覆盖率到 90%+
429
+ - [ ] 添加性能测试
430
+ - [ ] 添加快照测试
431
+
432
+ ## 参考资料
433
+
434
+ - [Vitest 官方文档](https://vitest.dev/)
435
+ - [Testing Best Practices](https://github.com/goldbergyoni/javascript-testing-best-practices)
436
+ - [Jest Cheat Sheet](https://github.com/sapegin/jest-cheat-sheet)
package/dist/index.js CHANGED
@@ -674,7 +674,7 @@ async function listTags(prefix) {
674
674
  }
675
675
  const grouped = /* @__PURE__ */ new Map();
676
676
  tags.forEach((tag) => {
677
- const prefix2 = tag.replace(/[0-9].*/, "") || "\u65E0\u524D\u7F00";
677
+ const prefix2 = tag.replace(/[0-9].*/, "") || "(\u65E0\u524D\u7F00)";
678
678
  if (!grouped.has(prefix2)) {
679
679
  grouped.set(prefix2, []);
680
680
  }
@@ -2496,7 +2496,7 @@ process.on("SIGTERM", () => {
2496
2496
  console.log("");
2497
2497
  process.exit(0);
2498
2498
  });
2499
- var version = true ? "0.2.23" : "0.0.0-dev";
2499
+ var version = true ? "0.2.24" : "0.0.0-dev";
2500
2500
  async function mainMenu() {
2501
2501
  console.log(
2502
2502
  colors.green(`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zjex/git-workflow",
3
- "version": "0.2.23",
3
+ "version": "0.2.24",
4
4
  "description": "🚀 极简的 Git 工作流 CLI 工具,让分支管理和版本发布变得轻松愉快",
5
5
  "type": "module",
6
6
  "bin": {
@@ -13,6 +13,10 @@
13
13
  "scripts": {
14
14
  "build": "tsup",
15
15
  "dev": "tsx src/index.ts",
16
+ "test": "vitest --run",
17
+ "test:watch": "vitest",
18
+ "test:ui": "vitest --ui",
19
+ "test:coverage": "vitest --coverage",
16
20
  "changelog": "changelogen -o CHANGELOG.md",
17
21
  "version": "node scripts/version.js",
18
22
  "release": "./scripts/release.sh",
@@ -48,10 +52,13 @@
48
52
  "devDependencies": {
49
53
  "@types/node": "^25.0.3",
50
54
  "@types/semver": "^7.7.1",
55
+ "@vitest/coverage-v8": "^4.0.16",
56
+ "@vitest/ui": "^4.0.16",
51
57
  "changelogen": "^0.6.2",
52
58
  "husky": "^9.1.7",
53
59
  "tsup": "^8.5.1",
54
60
  "tsx": "^4.21.0",
55
- "typescript": "^5.9.3"
61
+ "typescript": "^5.9.3",
62
+ "vitest": "^4.0.16"
56
63
  }
57
64
  }
@@ -43,7 +43,7 @@ export async function listTags(prefix?: string): Promise<void> {
43
43
  const grouped = new Map<string, string[]>();
44
44
  tags.forEach((tag) => {
45
45
  // 提取前缀:去掉数字及之后的部分,如 "v0.1.0" -> "v"
46
- const prefix = tag.replace(/[0-9].*/, "") || "无前缀";
46
+ const prefix = tag.replace(/[0-9].*/, "") || "(无前缀)";
47
47
  if (!grouped.has(prefix)) {
48
48
  grouped.set(prefix, []);
49
49
  }