-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathchangeFixup.test.ts
More file actions
172 lines (142 loc) · 5.43 KB
/
changeFixup.test.ts
File metadata and controls
172 lines (142 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { afterEach, beforeAll, beforeEach, describe, expect, it, jest } from '@jest/globals';
import { change } from '../../commands/change';
import { RepositoryFactory } from '../../__fixtures__/repositoryFactory';
import { generateChangeFiles } from '../../__fixtures__/changeFiles';
import { getDefaultOptions } from '../../options/getDefaultOptions';
import type { ChangeFileInfo } from '../../types/ChangeInfo';
import type { Repository } from '../../__fixtures__/repository';
import type { BeachballOptions } from '../../types/BeachballOptions';
import fs from 'fs-extra';
import path from 'path';
// Mock the promptForChange module to avoid interactive prompts
jest.mock('../../changefile/promptForChange', () => ({
promptForChange: jest.fn(),
}));
import { promptForChange } from '../../changefile/promptForChange';
const mockPromptForChange = promptForChange as jest.MockedFunction<typeof promptForChange>;
describe('change command with fixup mode', () => {
let repositoryFactory: RepositoryFactory;
let repository: Repository;
function getOptions(options?: Partial<BeachballOptions>): BeachballOptions {
return {
...getDefaultOptions(),
path: repository.rootPath,
...options,
};
}
beforeAll(() => {
repositoryFactory = new RepositoryFactory('single');
});
beforeEach(() => {
repository = repositoryFactory.cloneRepository();
mockPromptForChange.mockReset();
});
afterEach(() => {
jest.restoreAllMocks();
repository?.cleanUp();
});
it('creates a normal change file when fixup is false', async () => {
const changes: ChangeFileInfo[] = [
{
type: 'patch',
comment: 'Test change',
packageName: 'foo',
email: 'test@example.com',
dependentChangeType: 'patch',
},
];
mockPromptForChange.mockResolvedValue(changes);
const options = getOptions({
fixup: false,
package: ['foo'],
commit: false, // Disable commit for easier testing
});
await change(options);
// Verify a change file was created
const changeDir = path.join(repository.rootPath, 'change');
const changeFiles = fs.readdirSync(changeDir).filter(f => f.endsWith('.json'));
expect(changeFiles).toHaveLength(1);
// Verify the content
const changeFilePath = path.join(changeDir, changeFiles[0]);
const changeFileContent = fs.readJSONSync(changeFilePath);
expect(changeFileContent.comment).toBe('Test change');
expect(changeFileContent.type).toBe('patch');
});
it('updates existing change file and creates fixup commit when fixup is true', async () => {
// Create an initial change file
const initialChanges: ChangeFileInfo[] = [
{
type: 'patch',
comment: 'Initial change',
packageName: 'foo',
email: 'test@example.com',
dependentChangeType: 'patch',
},
];
const options = getOptions({ commit: true }); // Enable commit to make sure change file is committed
generateChangeFiles(initialChanges, options);
// Now simulate a fixup operation
const fixupChanges: ChangeFileInfo[] = [
{
type: 'minor',
comment: 'Additional change',
packageName: 'foo',
email: 'test@example.com',
dependentChangeType: 'minor',
},
];
mockPromptForChange.mockResolvedValue(fixupChanges);
const fixupOptions = getOptions({
fixup: true,
package: ['foo'],
commit: false, // We'll test the commit separately
});
await change(fixupOptions);
// Verify only one change file exists (the updated one)
const changeDir = path.join(repository.rootPath, 'change');
const changeFiles = fs.readdirSync(changeDir).filter(f => f.endsWith('.json'));
expect(changeFiles).toHaveLength(1);
// Verify the content was merged
const changeFilePath = path.join(changeDir, changeFiles[0]);
const changeFileContent = fs.readJSONSync(changeFilePath);
expect(changeFileContent.comment).toBe('Initial change\n\nAdditional change');
expect(changeFileContent.type).toBe('minor'); // Higher priority type
});
it('handles case when no existing change files exist in fixup mode', async () => {
const changes: ChangeFileInfo[] = [
{
type: 'patch',
comment: 'Test change',
packageName: 'foo',
email: 'test@example.com',
dependentChangeType: 'patch',
},
];
mockPromptForChange.mockResolvedValue(changes);
const options = getOptions({
fixup: true,
package: ['foo'],
commit: false,
});
// Should not throw an error, but should fall back to creating a normal change file
await change(options);
// Verify a change file was created (fallback to normal behavior)
const changeDir = path.join(repository.rootPath, 'change');
const changeFiles = fs.existsSync(changeDir) ? fs.readdirSync(changeDir).filter(f => f.endsWith('.json')) : [];
expect(changeFiles).toHaveLength(1);
// Verify the content is correct
const changeFilePath = path.join(changeDir, changeFiles[0]);
const changeFileContent = fs.readJSONSync(changeFilePath);
expect(changeFileContent.comment).toBe('Test change');
expect(changeFileContent.type).toBe('patch');
});
it('handles empty changes gracefully', async () => {
mockPromptForChange.mockResolvedValue(undefined);
const options = getOptions({
fixup: true,
package: ['foo'],
});
// Should not throw an error
await change(options);
});
});