Skip to content

Commit 843a6a3

Browse files
author
fanwenjie
committed
chore: rush plugin for generate change logs
1 parent 069de27 commit 843a6a3

25 files changed

+905
-2
lines changed

common/_templates/node-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"@coze-infra/vitest-config": "workspace:*",
2020
"@types/node": "^22.13.13",
2121
"@vitest/coverage-v8": "^3.0.9",
22-
"tsx": "^4.19.2",
22+
"tsx": "^4.19.3",
2323
"vitest": "^3.0.9"
2424
}
2525
}

common/_templates/rush-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"@coze-infra/vitest-config": "workspace:*",
2020
"@types/node": "^22.13.13",
2121
"@vitest/coverage-v8": "^3.0.9",
22-
"tsx": "^4.19.2",
22+
"tsx": "^4.19.3",
2323
"vitest": "^3.0.9"
2424
}
2525
}

common/config/subspaces/default/pnpm-lock.yaml

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# @coze-infra/rush-change-plugin

packages/rush-plugins/change/__tests__/.gitkeep

Whitespace-only changes.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import path from 'path';
2+
import fs from 'fs/promises';
3+
4+
import { describe, it, expect, vi, beforeEach } from 'vitest';
5+
import { logger } from '@coze-infra/rush-logger';
6+
7+
import * as helper from '../helper';
8+
import * as amendCommit from '../amend-commit';
9+
import { generateChangeFiles } from '../action';
10+
import * as projectAnalyzer from '../../../utils/project-analyzer';
11+
12+
// Mock all dependencies
13+
vi.mock('fs/promises');
14+
vi.mock('path');
15+
vi.mock('@coze-infra/rush-logger');
16+
vi.mock('../helper');
17+
vi.mock('../amend-commit');
18+
vi.mock('../../../utils/project-analyzer');
19+
20+
describe('generateChangeFiles', () => {
21+
beforeEach(() => {
22+
vi.clearAllMocks();
23+
// Reset environment variables
24+
delete process.env.CI;
25+
});
26+
27+
it('should do nothing in CI environment', async () => {
28+
process.env.CI = 'true';
29+
await generateChangeFiles({ ci: false });
30+
expect(helper.analysisCommitMsg).not.toHaveBeenCalled();
31+
});
32+
33+
it('should do nothing when ci option is true', async () => {
34+
await generateChangeFiles({ ci: true });
35+
expect(helper.analysisCommitMsg).not.toHaveBeenCalled();
36+
});
37+
38+
it('should handle amend commit option', async () => {
39+
await generateChangeFiles({ amendCommit: true });
40+
expect(amendCommit.amendCommit).toHaveBeenCalled();
41+
expect(helper.analysisCommitMsg).not.toHaveBeenCalled();
42+
});
43+
44+
it('should read commit message from file when not provided', async () => {
45+
const mockCommitMsg = 'feat: test commit';
46+
const mockRushConfig = {
47+
rushJsonFolder: '/test/path',
48+
};
49+
50+
vi.mocked(projectAnalyzer.getRushConfiguration).mockReturnValue(
51+
mockRushConfig as any,
52+
);
53+
vi.mocked(fs.readFile).mockResolvedValue(mockCommitMsg);
54+
vi.mocked(helper.analysisCommitMsg).mockResolvedValue({
55+
content: 'test commit',
56+
type: 'minor',
57+
});
58+
59+
await generateChangeFiles({});
60+
61+
expect(fs.readFile).toHaveBeenCalledWith(
62+
path.resolve('/test/path', '.git/COMMIT_EDITMSG'),
63+
'utf-8',
64+
);
65+
expect(helper.analysisCommitMsg).toHaveBeenCalledWith(mockCommitMsg);
66+
expect(helper.generateAllChangesFile).toHaveBeenCalledWith(
67+
'test commit',
68+
'minor',
69+
);
70+
});
71+
72+
it('should use provided commit message', async () => {
73+
const mockCommitMsg = 'feat: direct commit';
74+
vi.mocked(helper.analysisCommitMsg).mockResolvedValue({
75+
content: 'direct commit',
76+
type: 'minor',
77+
});
78+
79+
await generateChangeFiles({ commitMsg: mockCommitMsg });
80+
81+
expect(fs.readFile).not.toHaveBeenCalled();
82+
expect(helper.analysisCommitMsg).toHaveBeenCalledWith(mockCommitMsg);
83+
expect(helper.generateAllChangesFile).toHaveBeenCalledWith(
84+
'direct commit',
85+
'minor',
86+
);
87+
});
88+
89+
it('should handle invalid commit message', async () => {
90+
vi.mocked(helper.analysisCommitMsg).mockResolvedValue({
91+
content: '',
92+
type: 'minor',
93+
});
94+
95+
await generateChangeFiles({ commitMsg: 'invalid commit' });
96+
97+
expect(logger.warning).toHaveBeenCalledWith('Invalid subject');
98+
expect(helper.generateAllChangesFile).not.toHaveBeenCalled();
99+
});
100+
101+
it('should handle errors', async () => {
102+
const error = new Error('Test error');
103+
vi.mocked(helper.analysisCommitMsg).mockRejectedValue(error);
104+
105+
await generateChangeFiles({ commitMsg: 'test commit' });
106+
107+
expect(logger.error).toHaveBeenCalledWith(
108+
`Generate changes file fail \n ${error}`,
109+
);
110+
});
111+
});
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import path from 'path';
2+
3+
import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest';
4+
5+
import { amendCommit } from '../amend-commit';
6+
import * as projectAnalyzer from '../../../utils/project-analyzer';
7+
import * as gitCommand from '../../../utils/git-command';
8+
import { exec } from '../../../utils/exec';
9+
10+
// Mock all dependencies
11+
vi.mock('path', () => ({ default: { relative: vi.fn() } }));
12+
vi.mock('../../../utils/git-command');
13+
vi.mock('../../../utils/project-analyzer');
14+
vi.mock('../../../utils/exec', () => ({
15+
exec: vi.fn(),
16+
}));
17+
18+
describe('amendCommit', () => {
19+
beforeEach(() => {
20+
vi.clearAllMocks();
21+
});
22+
23+
it('should amend commit when changes file is detected', async () => {
24+
// Mock rush configuration
25+
const mockRushConfig = {
26+
rushJsonFolder: '/root/path',
27+
changesFolder: '/root/path/common/changes',
28+
};
29+
vi.mocked(projectAnalyzer.getRushConfiguration).mockReturnValue(
30+
mockRushConfig as any,
31+
);
32+
33+
// Mock path.relative to return the changes folder path
34+
(path.relative as Mock).mockReturnValue('common/changes');
35+
36+
// Mock git changed files to include a changes file
37+
vi.mocked(gitCommand.getChangedFilesFromCached).mockResolvedValue([
38+
'common/changes/my-package.json',
39+
'some/other/file.js',
40+
]);
41+
42+
await amendCommit();
43+
44+
// Verify the correct paths were used
45+
expect(path.relative).toHaveBeenCalledWith(
46+
'/root/path',
47+
'/root/path/common/changes',
48+
);
49+
50+
// Verify git command was executed
51+
expect(exec).toHaveBeenCalledWith('git commit --amend --no-edit -n');
52+
});
53+
54+
it('should not amend commit when no changes file is detected', async () => {
55+
// Mock rush configuration
56+
const mockRushConfig = {
57+
rushJsonFolder: '/root/path',
58+
changesFolder: '/root/path/common/changes',
59+
};
60+
vi.mocked(projectAnalyzer.getRushConfiguration).mockReturnValue(
61+
mockRushConfig as any,
62+
);
63+
64+
// Mock path.relative to return the changes folder path
65+
vi.mocked(path.relative).mockReturnValue('common/changes');
66+
67+
// Mock git changed files to not include any changes file
68+
vi.mocked(gitCommand.getChangedFilesFromCached).mockResolvedValue([
69+
'src/index.ts',
70+
'package.json',
71+
]);
72+
73+
await amendCommit();
74+
75+
// Verify path.relative was still called
76+
expect(path.relative).toHaveBeenCalledWith(
77+
'/root/path',
78+
'/root/path/common/changes',
79+
);
80+
81+
// Verify git command was not executed
82+
expect(exec).not.toHaveBeenCalled();
83+
});
84+
85+
it('should handle empty changed files array', async () => {
86+
// Mock rush configuration
87+
const mockRushConfig = {
88+
rushJsonFolder: '/root/path',
89+
changesFolder: '/root/path/common/changes',
90+
};
91+
vi.mocked(projectAnalyzer.getRushConfiguration).mockReturnValue(
92+
mockRushConfig as any,
93+
);
94+
95+
// Mock path.relative
96+
vi.mocked(path.relative).mockReturnValue('common/changes');
97+
98+
// Mock git changed files to return empty array
99+
vi.mocked(gitCommand.getChangedFilesFromCached).mockResolvedValue([]);
100+
101+
await amendCommit();
102+
103+
// Verify path.relative was called
104+
expect(path.relative).toHaveBeenCalledWith(
105+
'/root/path',
106+
'/root/path/common/changes',
107+
);
108+
109+
// Verify git command was not executed
110+
expect(exec).not.toHaveBeenCalled();
111+
});
112+
});

0 commit comments

Comments
 (0)