@hubspot/npm-scripts 0.1.0-beta.0 → 0.1.1
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.
- package/package.json +5 -3
- package/src/constants/release.d.ts +2 -1
- package/src/constants/release.js +7 -1
- package/src/constants/repo-sync.d.ts +2 -1
- package/src/constants/repo-sync.js +2 -1
- package/src/release.js +55 -19
- package/src/repo-sync.d.ts +1 -1
- package/src/repo-sync.js +15 -15
- package/src/types.d.ts +6 -1
- package/src/utils/__tests__/filesystem.test.d.ts +1 -0
- package/src/utils/__tests__/filesystem.test.js +154 -0
- package/src/utils/__tests__/git.test.d.ts +1 -0
- package/src/utils/__tests__/git.test.js +267 -0
- package/src/utils/__tests__/logging.test.d.ts +1 -0
- package/src/utils/__tests__/logging.test.js +64 -0
- package/src/utils/__tests__/npm.test.d.ts +1 -0
- package/src/utils/__tests__/npm.test.js +266 -0
- package/src/utils/__tests__/process.test.d.ts +1 -0
- package/src/utils/__tests__/process.test.js +12 -0
- package/src/utils/__tests__/prompting.test.d.ts +1 -0
- package/src/utils/__tests__/prompting.test.js +100 -0
- package/src/utils/npm.d.ts +3 -1
- package/src/utils/npm.js +17 -8
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { getCurrentGitBranch, gitCleanup, getGitEnvWithSshKey, cloneRepo, checkoutBranch, createBranch, commitChanges, pushBranch, } from '../git.js';
|
|
3
|
+
import { exec as _exec } from 'node:child_process';
|
|
4
|
+
import fs from 'fs-extra';
|
|
5
|
+
import { logger } from '../logging.js';
|
|
6
|
+
import { input } from '../prompting.js';
|
|
7
|
+
vi.mock('util', () => ({
|
|
8
|
+
promisify: vi.fn(fn => fn),
|
|
9
|
+
}));
|
|
10
|
+
vi.mock('node:child_process', () => ({
|
|
11
|
+
exec: vi.fn(),
|
|
12
|
+
}));
|
|
13
|
+
vi.mock('fs-extra', () => ({
|
|
14
|
+
default: {
|
|
15
|
+
remove: vi.fn(),
|
|
16
|
+
},
|
|
17
|
+
remove: vi.fn(),
|
|
18
|
+
}));
|
|
19
|
+
vi.mock('../logging.js', () => ({
|
|
20
|
+
logger: {
|
|
21
|
+
log: vi.fn(),
|
|
22
|
+
error: vi.fn(),
|
|
23
|
+
},
|
|
24
|
+
}));
|
|
25
|
+
vi.mock('../process.js', () => ({
|
|
26
|
+
EXIT_CODES: {
|
|
27
|
+
SUCCESS: 0,
|
|
28
|
+
ERROR: 1,
|
|
29
|
+
},
|
|
30
|
+
}));
|
|
31
|
+
vi.mock('../prompting.js', () => ({
|
|
32
|
+
input: vi.fn(),
|
|
33
|
+
}));
|
|
34
|
+
const mockedExec = vi.mocked(_exec);
|
|
35
|
+
describe('git', () => {
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
vi.resetAllMocks();
|
|
38
|
+
});
|
|
39
|
+
describe('getCurrentGitBranch', () => {
|
|
40
|
+
it('should return the current git branch name', async () => {
|
|
41
|
+
mockedExec.mockImplementation(() => Promise.resolve({
|
|
42
|
+
stdout: ' main \n',
|
|
43
|
+
stderr: '',
|
|
44
|
+
}));
|
|
45
|
+
const result = await getCurrentGitBranch();
|
|
46
|
+
expect(mockedExec).toHaveBeenCalledWith('git rev-parse --abbrev-ref HEAD');
|
|
47
|
+
expect(result).toBe('main');
|
|
48
|
+
});
|
|
49
|
+
it('should trim whitespace from branch name', async () => {
|
|
50
|
+
mockedExec.mockImplementation(() => Promise.resolve({
|
|
51
|
+
stdout: ' feature-branch \n',
|
|
52
|
+
stderr: '',
|
|
53
|
+
}));
|
|
54
|
+
const result = await getCurrentGitBranch();
|
|
55
|
+
expect(result).toBe('feature-branch');
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
describe('gitCleanup', () => {
|
|
59
|
+
it('should reset HEAD and checkout files', async () => {
|
|
60
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
61
|
+
await gitCleanup('1.0.0', false);
|
|
62
|
+
expect(mockedExec).toHaveBeenCalledWith('git reset HEAD~');
|
|
63
|
+
expect(mockedExec).toHaveBeenCalledWith('git checkout .');
|
|
64
|
+
expect(mockedExec).not.toHaveBeenCalledWith(expect.stringContaining('git tag -d'));
|
|
65
|
+
});
|
|
66
|
+
it('should delete tag when deleteTag is true', async () => {
|
|
67
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
68
|
+
await gitCleanup('1.0.0', true);
|
|
69
|
+
expect(mockedExec).toHaveBeenCalledWith('git reset HEAD~');
|
|
70
|
+
expect(mockedExec).toHaveBeenCalledWith('git checkout .');
|
|
71
|
+
expect(mockedExec).toHaveBeenCalledWith('git tag -d v1.0.0');
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
describe('getGitEnvWithSshKey', () => {
|
|
75
|
+
it('should return process.env when sshKeyPath is not provided and user leaves blank', async () => {
|
|
76
|
+
vi.mocked(input).mockResolvedValue('');
|
|
77
|
+
const result = await getGitEnvWithSshKey();
|
|
78
|
+
expect(result).toBe(process.env);
|
|
79
|
+
expect(input).toHaveBeenCalled();
|
|
80
|
+
});
|
|
81
|
+
it('should return process.env with GIT_SSH_COMMAND when sshKeyPath is provided', async () => {
|
|
82
|
+
const result = await getGitEnvWithSshKey('/path/to/key');
|
|
83
|
+
expect(result).toEqual({
|
|
84
|
+
...process.env,
|
|
85
|
+
GIT_SSH_COMMAND: 'ssh -i /path/to/key -o IdentitiesOnly=yes',
|
|
86
|
+
});
|
|
87
|
+
expect(input).not.toHaveBeenCalled();
|
|
88
|
+
});
|
|
89
|
+
it('should prompt for sshKeyPath when not provided', async () => {
|
|
90
|
+
vi.mocked(input).mockResolvedValue('/custom/path/key');
|
|
91
|
+
const result = await getGitEnvWithSshKey();
|
|
92
|
+
expect(input).toHaveBeenCalledWith({
|
|
93
|
+
message: 'Enter the SSH key path to use (leave blank to use the default):',
|
|
94
|
+
});
|
|
95
|
+
expect(result).toEqual({
|
|
96
|
+
...process.env,
|
|
97
|
+
GIT_SSH_COMMAND: 'ssh -i /custom/path/key -o IdentitiesOnly=yes',
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
describe('cloneRepo', () => {
|
|
102
|
+
it('should clone repository with correct git URL format', async () => {
|
|
103
|
+
const repoUrl = 'https://git.mycorp.com/Org/repo-name';
|
|
104
|
+
const destinationDir = '/tmp';
|
|
105
|
+
const targetRepoPath = '/tmp/repo-name';
|
|
106
|
+
vi.mocked(fs.remove).mockResolvedValue();
|
|
107
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
108
|
+
const result = await cloneRepo(repoUrl, destinationDir);
|
|
109
|
+
expect(vi.mocked(fs.remove)).toHaveBeenCalledWith(targetRepoPath);
|
|
110
|
+
expect(mockedExec).toHaveBeenCalledWith('git clone git@git.mycorp.com:Org/repo-name.git /tmp/repo-name', { env: undefined });
|
|
111
|
+
expect(result).toBe(targetRepoPath);
|
|
112
|
+
});
|
|
113
|
+
it('should handle repo URL without protocol', async () => {
|
|
114
|
+
const repoUrl = 'git.mycorp.com/Org/repo-name';
|
|
115
|
+
const destinationDir = '/tmp';
|
|
116
|
+
vi.mocked(fs.remove).mockResolvedValue();
|
|
117
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
118
|
+
await cloneRepo(repoUrl, destinationDir);
|
|
119
|
+
expect(mockedExec).toHaveBeenCalledWith('git clone git@git.mycorp.com:Org/repo-name.git /tmp/repo-name', { env: undefined });
|
|
120
|
+
});
|
|
121
|
+
it('should use provided git environment', async () => {
|
|
122
|
+
const repoUrl = 'https://git.mycorp.com/Org/repo-name';
|
|
123
|
+
const destinationDir = '/tmp';
|
|
124
|
+
const gitEnv = { GIT_SSH_COMMAND: 'ssh -i /path/to/key' };
|
|
125
|
+
vi.mocked(fs.remove).mockResolvedValue();
|
|
126
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
127
|
+
await cloneRepo(repoUrl, destinationDir, gitEnv);
|
|
128
|
+
expect(mockedExec).toHaveBeenCalledWith('git clone git@git.mycorp.com:Org/repo-name.git /tmp/repo-name', { env: gitEnv });
|
|
129
|
+
});
|
|
130
|
+
it('should handle errors and exit with error code', async () => {
|
|
131
|
+
const repoUrl = 'https://git.mycorp.com/Org/repo-name';
|
|
132
|
+
const destinationDir = '/tmp';
|
|
133
|
+
const error = new Error('Clone failed');
|
|
134
|
+
vi.mocked(fs.remove).mockResolvedValue();
|
|
135
|
+
mockedExec.mockImplementation(() => Promise.reject(error));
|
|
136
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
137
|
+
throw new Error('process.exit called');
|
|
138
|
+
});
|
|
139
|
+
try {
|
|
140
|
+
await expect(cloneRepo(repoUrl, destinationDir)).rejects.toThrow('process.exit called');
|
|
141
|
+
expect(vi.mocked(logger.error)).toHaveBeenCalledWith('Error cloning repository:', error);
|
|
142
|
+
}
|
|
143
|
+
finally {
|
|
144
|
+
exitSpy.mockRestore();
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
describe('checkoutBranch', () => {
|
|
149
|
+
it('should checkout branch without pulling', async () => {
|
|
150
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
151
|
+
await checkoutBranch('feature-branch', false);
|
|
152
|
+
expect(mockedExec).toHaveBeenCalledWith('git checkout feature-branch');
|
|
153
|
+
expect(mockedExec).not.toHaveBeenCalledWith(expect.stringContaining('git pull'));
|
|
154
|
+
});
|
|
155
|
+
it('should checkout branch and pull when pullLatest is true', async () => {
|
|
156
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
157
|
+
await checkoutBranch('feature-branch', true);
|
|
158
|
+
expect(mockedExec).toHaveBeenCalledWith('git checkout feature-branch');
|
|
159
|
+
expect(mockedExec).toHaveBeenCalledWith('git pull origin feature-branch');
|
|
160
|
+
});
|
|
161
|
+
it('should handle errors and exit with error code', async () => {
|
|
162
|
+
const error = new Error('Checkout failed');
|
|
163
|
+
mockedExec.mockImplementation(() => Promise.reject(error));
|
|
164
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
165
|
+
throw new Error('process.exit called');
|
|
166
|
+
});
|
|
167
|
+
try {
|
|
168
|
+
await expect(checkoutBranch('feature-branch')).rejects.toThrow('process.exit called');
|
|
169
|
+
expect(vi.mocked(logger.error)).toHaveBeenCalledWith('Error checking out branch feature-branch:', error);
|
|
170
|
+
}
|
|
171
|
+
finally {
|
|
172
|
+
exitSpy.mockRestore();
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
describe('createBranch', () => {
|
|
177
|
+
it('should create and checkout new branch', async () => {
|
|
178
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
179
|
+
await createBranch('new-branch');
|
|
180
|
+
expect(mockedExec).toHaveBeenCalledWith('git checkout -b new-branch');
|
|
181
|
+
});
|
|
182
|
+
it('should handle errors and exit with error code', async () => {
|
|
183
|
+
const error = new Error('Create branch failed');
|
|
184
|
+
mockedExec.mockImplementation(() => Promise.reject(error));
|
|
185
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
186
|
+
throw new Error('process.exit called');
|
|
187
|
+
});
|
|
188
|
+
try {
|
|
189
|
+
await expect(createBranch('new-branch')).rejects.toThrow('process.exit called');
|
|
190
|
+
expect(vi.mocked(logger.error)).toHaveBeenCalledWith('Error creating branch new-branch:', error);
|
|
191
|
+
}
|
|
192
|
+
finally {
|
|
193
|
+
exitSpy.mockRestore();
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
describe('commitChanges', () => {
|
|
198
|
+
it('should commit changes when there are staged changes', async () => {
|
|
199
|
+
mockedExec
|
|
200
|
+
.mockImplementationOnce(() => Promise.resolve({ stdout: '', stderr: '' }))
|
|
201
|
+
.mockImplementationOnce(() => Promise.resolve({ stdout: 'M file.txt', stderr: '' }))
|
|
202
|
+
.mockImplementationOnce(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
203
|
+
await commitChanges('Test commit message');
|
|
204
|
+
expect(mockedExec).toHaveBeenCalledWith('git add .');
|
|
205
|
+
expect(mockedExec).toHaveBeenCalledWith('git status --porcelain');
|
|
206
|
+
expect(mockedExec).toHaveBeenCalledWith('git commit -m "Test commit message"');
|
|
207
|
+
});
|
|
208
|
+
it('should not commit when there are no changes', async () => {
|
|
209
|
+
mockedExec
|
|
210
|
+
.mockImplementationOnce(() => Promise.resolve({ stdout: '', stderr: '' }))
|
|
211
|
+
.mockImplementationOnce(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
212
|
+
await commitChanges('Test commit message');
|
|
213
|
+
expect(mockedExec).toHaveBeenCalledWith('git add .');
|
|
214
|
+
expect(mockedExec).toHaveBeenCalledWith('git status --porcelain');
|
|
215
|
+
expect(mockedExec).not.toHaveBeenCalledWith(expect.stringContaining('git commit'));
|
|
216
|
+
expect(vi.mocked(logger.log)).toHaveBeenCalledWith('No changes to commit');
|
|
217
|
+
});
|
|
218
|
+
it('should handle errors and exit with error code', async () => {
|
|
219
|
+
const error = new Error('Status check failed');
|
|
220
|
+
mockedExec
|
|
221
|
+
.mockImplementationOnce(() => Promise.resolve({ stdout: '', stderr: '' }))
|
|
222
|
+
.mockImplementationOnce(() => Promise.reject(error));
|
|
223
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
224
|
+
throw new Error('process.exit called');
|
|
225
|
+
});
|
|
226
|
+
try {
|
|
227
|
+
await expect(commitChanges('Test commit message')).rejects.toThrow('process.exit called');
|
|
228
|
+
expect(vi.mocked(logger.error)).toHaveBeenCalledWith('Error checking git status');
|
|
229
|
+
}
|
|
230
|
+
finally {
|
|
231
|
+
exitSpy.mockRestore();
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
describe('pushBranch', () => {
|
|
236
|
+
it('should push branch to origin', async () => {
|
|
237
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
238
|
+
await pushBranch('feature-branch');
|
|
239
|
+
expect(vi.mocked(logger.log)).toHaveBeenCalledWith('Pushing branch feature-branch...');
|
|
240
|
+
expect(mockedExec).toHaveBeenCalledWith('git push origin feature-branch', {
|
|
241
|
+
env: undefined,
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
it('should use provided git environment', async () => {
|
|
245
|
+
const gitEnv = { GIT_SSH_COMMAND: 'ssh -i /path/to/key' };
|
|
246
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
247
|
+
await pushBranch('feature-branch', gitEnv);
|
|
248
|
+
expect(mockedExec).toHaveBeenCalledWith('git push origin feature-branch', {
|
|
249
|
+
env: gitEnv,
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
it('should handle errors and exit with error code', async () => {
|
|
253
|
+
const error = new Error('Push failed');
|
|
254
|
+
mockedExec.mockImplementation(() => Promise.reject(error));
|
|
255
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
256
|
+
throw new Error('process.exit called');
|
|
257
|
+
});
|
|
258
|
+
try {
|
|
259
|
+
await expect(pushBranch('feature-branch')).rejects.toThrow('process.exit called');
|
|
260
|
+
expect(vi.mocked(logger.error)).toHaveBeenCalledWith('Error pushing branch feature-branch:', error);
|
|
261
|
+
}
|
|
262
|
+
finally {
|
|
263
|
+
exitSpy.mockRestore();
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { logger } from '../logging.js';
|
|
3
|
+
describe('logging', () => {
|
|
4
|
+
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
consoleLogSpy.mockClear();
|
|
7
|
+
});
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
consoleLogSpy.mockClear();
|
|
10
|
+
});
|
|
11
|
+
describe('logger.log', () => {
|
|
12
|
+
it('should call console.log with provided arguments', () => {
|
|
13
|
+
logger.log('test message', 123);
|
|
14
|
+
expect(consoleLogSpy).toHaveBeenCalledWith('test message', 123);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
describe('logger.info', () => {
|
|
18
|
+
it('should call console.log with [INFO] prefix', () => {
|
|
19
|
+
logger.info('info message');
|
|
20
|
+
expect(consoleLogSpy).toHaveBeenCalled();
|
|
21
|
+
const callArgs = consoleLogSpy.mock.calls[0];
|
|
22
|
+
expect(callArgs[0]).toContain('[INFO]');
|
|
23
|
+
expect(callArgs[1]).toBe('info message');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
describe('logger.success', () => {
|
|
27
|
+
it('should call console.log with [SUCCESS] prefix', () => {
|
|
28
|
+
logger.success('success message');
|
|
29
|
+
expect(consoleLogSpy).toHaveBeenCalled();
|
|
30
|
+
const callArgs = consoleLogSpy.mock.calls[0];
|
|
31
|
+
expect(callArgs[0]).toContain('[SUCCESS]');
|
|
32
|
+
expect(callArgs[1]).toBe('success message');
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
describe('logger.warn', () => {
|
|
36
|
+
it('should call console.log with [WARN] prefix', () => {
|
|
37
|
+
logger.warn('warning message');
|
|
38
|
+
expect(consoleLogSpy).toHaveBeenCalled();
|
|
39
|
+
const callArgs = consoleLogSpy.mock.calls[0];
|
|
40
|
+
expect(callArgs[0]).toContain('[WARN]');
|
|
41
|
+
expect(callArgs[1]).toBe('warning message');
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
describe('logger.error', () => {
|
|
45
|
+
it('should call console.log with [ERROR] prefix', () => {
|
|
46
|
+
logger.error('error message');
|
|
47
|
+
expect(consoleLogSpy).toHaveBeenCalled();
|
|
48
|
+
const callArgs = consoleLogSpy.mock.calls[0];
|
|
49
|
+
expect(callArgs[0]).toContain('[ERROR]');
|
|
50
|
+
expect(callArgs[1]).toBe('error message');
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
describe('logger.table', () => {
|
|
54
|
+
it('should call console.table with provided arguments', () => {
|
|
55
|
+
const consoleTableSpy = vi
|
|
56
|
+
.spyOn(console, 'table')
|
|
57
|
+
.mockImplementation(() => { });
|
|
58
|
+
const data = { a: 1, b: 2 };
|
|
59
|
+
logger.table(data);
|
|
60
|
+
expect(consoleTableSpy).toHaveBeenCalledWith(data);
|
|
61
|
+
consoleTableSpy.mockRestore();
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { getDistTags, publish, updateDistTag, updateNextTag, updatePreviousTag, } from '../npm.js';
|
|
3
|
+
import { exec as _exec, spawn } from 'node:child_process';
|
|
4
|
+
import { logger } from '../logging.js';
|
|
5
|
+
import { EXIT_CODES } from '../process.js';
|
|
6
|
+
import { TAG } from '../../constants/release.js';
|
|
7
|
+
vi.mock('util', () => ({
|
|
8
|
+
promisify: vi.fn(fn => fn),
|
|
9
|
+
}));
|
|
10
|
+
vi.mock('node:child_process', () => ({
|
|
11
|
+
exec: vi.fn(),
|
|
12
|
+
spawn: vi.fn(),
|
|
13
|
+
}));
|
|
14
|
+
vi.mock('../logging.js', () => ({
|
|
15
|
+
logger: {
|
|
16
|
+
log: vi.fn(),
|
|
17
|
+
success: vi.fn(),
|
|
18
|
+
},
|
|
19
|
+
}));
|
|
20
|
+
vi.mock('../process.js', () => ({
|
|
21
|
+
EXIT_CODES: {
|
|
22
|
+
SUCCESS: 0,
|
|
23
|
+
ERROR: 1,
|
|
24
|
+
},
|
|
25
|
+
}));
|
|
26
|
+
const mockedExec = vi.mocked(_exec);
|
|
27
|
+
const mockedSpawn = vi.mocked(spawn);
|
|
28
|
+
describe('npm', () => {
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
vi.resetAllMocks();
|
|
31
|
+
});
|
|
32
|
+
describe('getDistTags', () => {
|
|
33
|
+
it('should return parsed dist tags from npm view', async () => {
|
|
34
|
+
const mockDistTags = {
|
|
35
|
+
latest: '1.0.0',
|
|
36
|
+
next: '1.1.0-beta.1',
|
|
37
|
+
experimental: '1.2.0-experimental.1',
|
|
38
|
+
};
|
|
39
|
+
mockedExec.mockImplementation(() => Promise.resolve({
|
|
40
|
+
stdout: JSON.stringify(mockDistTags),
|
|
41
|
+
stderr: '',
|
|
42
|
+
}));
|
|
43
|
+
const result = await getDistTags('@hubspot/test-package');
|
|
44
|
+
expect(mockedExec).toHaveBeenCalledWith('npm view @hubspot/test-package dist-tags --json');
|
|
45
|
+
expect(result).toEqual(mockDistTags);
|
|
46
|
+
});
|
|
47
|
+
it('should handle whitespace in stdout', async () => {
|
|
48
|
+
const mockDistTags = {
|
|
49
|
+
latest: '1.0.0',
|
|
50
|
+
next: '1.1.0-beta.1',
|
|
51
|
+
};
|
|
52
|
+
mockedExec.mockImplementation(() => Promise.resolve({
|
|
53
|
+
stdout: `\n${JSON.stringify(mockDistTags)}\n`,
|
|
54
|
+
stderr: '',
|
|
55
|
+
}));
|
|
56
|
+
const result = await getDistTags('@hubspot/test-package');
|
|
57
|
+
expect(result).toEqual(mockDistTags);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
describe('publish', () => {
|
|
61
|
+
it('should publish with correct arguments', async () => {
|
|
62
|
+
const mockChildProcess = {
|
|
63
|
+
on: vi.fn((event, callback) => {
|
|
64
|
+
if (event === 'close') {
|
|
65
|
+
setTimeout(() => callback(EXIT_CODES.SUCCESS), 0);
|
|
66
|
+
}
|
|
67
|
+
return mockChildProcess;
|
|
68
|
+
}),
|
|
69
|
+
};
|
|
70
|
+
mockedSpawn.mockImplementation(() => mockChildProcess);
|
|
71
|
+
await publish(TAG.LATEST, '123456', false, false, 'https://registry.npmjs.org/');
|
|
72
|
+
expect(mockedSpawn).toHaveBeenCalledWith('npm', [
|
|
73
|
+
'publish',
|
|
74
|
+
'--tag',
|
|
75
|
+
TAG.LATEST,
|
|
76
|
+
'--registry',
|
|
77
|
+
'https://registry.npmjs.org/',
|
|
78
|
+
'--otp',
|
|
79
|
+
'123456',
|
|
80
|
+
], {
|
|
81
|
+
stdio: 'inherit',
|
|
82
|
+
cwd: './dist',
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
it('should include --dry-run flag when isDryRun is true', async () => {
|
|
86
|
+
const mockChildProcess = {
|
|
87
|
+
on: vi.fn((event, callback) => {
|
|
88
|
+
if (event === 'close') {
|
|
89
|
+
setTimeout(() => callback(EXIT_CODES.SUCCESS), 0);
|
|
90
|
+
}
|
|
91
|
+
return mockChildProcess;
|
|
92
|
+
}),
|
|
93
|
+
};
|
|
94
|
+
mockedSpawn.mockImplementation(() => mockChildProcess);
|
|
95
|
+
await publish(TAG.NEXT, '123456', true, false, 'https://registry.npmjs.org/');
|
|
96
|
+
expect(mockedSpawn).toHaveBeenCalledWith('npm', [
|
|
97
|
+
'publish',
|
|
98
|
+
'--tag',
|
|
99
|
+
TAG.NEXT,
|
|
100
|
+
'--registry',
|
|
101
|
+
'https://registry.npmjs.org/',
|
|
102
|
+
'--otp',
|
|
103
|
+
'123456',
|
|
104
|
+
'--dry-run',
|
|
105
|
+
], {
|
|
106
|
+
stdio: 'inherit',
|
|
107
|
+
cwd: './dist',
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
it('should include --ignore-scripts flag when ignoreScripts is true', async () => {
|
|
111
|
+
const mockChildProcess = {
|
|
112
|
+
on: vi.fn((event, callback) => {
|
|
113
|
+
if (event === 'close') {
|
|
114
|
+
setTimeout(() => callback(EXIT_CODES.SUCCESS), 0);
|
|
115
|
+
}
|
|
116
|
+
return mockChildProcess;
|
|
117
|
+
}),
|
|
118
|
+
};
|
|
119
|
+
mockedSpawn.mockImplementation(() => mockChildProcess);
|
|
120
|
+
await publish(TAG.LATEST, '123456', false, true, 'https://registry.npmjs.org/');
|
|
121
|
+
expect(mockedSpawn).toHaveBeenCalledWith('npm', [
|
|
122
|
+
'publish',
|
|
123
|
+
'--tag',
|
|
124
|
+
TAG.LATEST,
|
|
125
|
+
'--registry',
|
|
126
|
+
'https://registry.npmjs.org/',
|
|
127
|
+
'--otp',
|
|
128
|
+
'123456',
|
|
129
|
+
'--ignore-scripts',
|
|
130
|
+
], {
|
|
131
|
+
stdio: 'inherit',
|
|
132
|
+
cwd: './dist',
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
it('should include both --dry-run and --ignore-scripts flags when both are true', async () => {
|
|
136
|
+
const mockChildProcess = {
|
|
137
|
+
on: vi.fn((event, callback) => {
|
|
138
|
+
if (event === 'close') {
|
|
139
|
+
setTimeout(() => callback(EXIT_CODES.SUCCESS), 0);
|
|
140
|
+
}
|
|
141
|
+
return mockChildProcess;
|
|
142
|
+
}),
|
|
143
|
+
};
|
|
144
|
+
mockedSpawn.mockImplementation(() => mockChildProcess);
|
|
145
|
+
await publish(TAG.LATEST, '123456', true, true, 'https://registry.npmjs.org/');
|
|
146
|
+
expect(mockedSpawn).toHaveBeenCalledWith('npm', [
|
|
147
|
+
'publish',
|
|
148
|
+
'--tag',
|
|
149
|
+
TAG.LATEST,
|
|
150
|
+
'--registry',
|
|
151
|
+
'https://registry.npmjs.org/',
|
|
152
|
+
'--otp',
|
|
153
|
+
'123456',
|
|
154
|
+
'--dry-run',
|
|
155
|
+
'--ignore-scripts',
|
|
156
|
+
], {
|
|
157
|
+
stdio: 'inherit',
|
|
158
|
+
cwd: './dist',
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
it('should reject on non-zero exit code', async () => {
|
|
162
|
+
const mockChildProcess = {
|
|
163
|
+
on: vi.fn((event, callback) => {
|
|
164
|
+
if (event === 'close') {
|
|
165
|
+
setTimeout(() => callback(EXIT_CODES.ERROR), 0);
|
|
166
|
+
}
|
|
167
|
+
return mockChildProcess;
|
|
168
|
+
}),
|
|
169
|
+
};
|
|
170
|
+
mockedSpawn.mockImplementation(() => mockChildProcess);
|
|
171
|
+
await expect(publish(TAG.LATEST, '123456', false, false, 'https://registry.npmjs.org/')).rejects.toThrow('npm publish failed with exit code 1');
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
describe('updateDistTag', () => {
|
|
175
|
+
it('should update dist tag with correct arguments', async () => {
|
|
176
|
+
const mockChildProcess = {
|
|
177
|
+
on: vi.fn((event, callback) => {
|
|
178
|
+
if (event === 'close') {
|
|
179
|
+
setTimeout(() => callback(EXIT_CODES.SUCCESS), 0);
|
|
180
|
+
}
|
|
181
|
+
return mockChildProcess;
|
|
182
|
+
}),
|
|
183
|
+
};
|
|
184
|
+
mockedSpawn.mockImplementation(() => mockChildProcess);
|
|
185
|
+
await updateDistTag('@hubspot/test-package', '1.0.0', 'custom-tag', '123456', false, 'https://registry.npmjs.org/');
|
|
186
|
+
expect(mockedSpawn).toHaveBeenCalledWith('npm', [
|
|
187
|
+
'dist-tag',
|
|
188
|
+
'add',
|
|
189
|
+
'@hubspot/test-package@1.0.0',
|
|
190
|
+
'custom-tag',
|
|
191
|
+
'--registry',
|
|
192
|
+
'https://registry.npmjs.org/',
|
|
193
|
+
'--otp',
|
|
194
|
+
'123456',
|
|
195
|
+
], { stdio: 'inherit' });
|
|
196
|
+
expect(vi.mocked(logger.success)).toHaveBeenCalledWith('custom-tag tag updated successfully');
|
|
197
|
+
});
|
|
198
|
+
it('should skip execution and log dry run message when isDryRun is true', async () => {
|
|
199
|
+
await updateDistTag('@hubspot/test-package', '1.0.0', 'custom-tag', '123456', true, 'https://registry.npmjs.org/');
|
|
200
|
+
expect(mockedSpawn).not.toHaveBeenCalled();
|
|
201
|
+
expect(vi.mocked(logger.log)).toHaveBeenCalledWith(expect.stringContaining('Dry run: skipping run of'));
|
|
202
|
+
});
|
|
203
|
+
it('should reject on non-zero exit code', async () => {
|
|
204
|
+
const mockChildProcess = {
|
|
205
|
+
on: vi.fn((event, callback) => {
|
|
206
|
+
if (event === 'close') {
|
|
207
|
+
setTimeout(() => callback(EXIT_CODES.ERROR), 0);
|
|
208
|
+
}
|
|
209
|
+
return mockChildProcess;
|
|
210
|
+
}),
|
|
211
|
+
};
|
|
212
|
+
mockedSpawn.mockImplementation(() => mockChildProcess);
|
|
213
|
+
await expect(updateDistTag('@hubspot/test-package', '1.0.0', 'custom-tag', '123456', false, 'https://registry.npmjs.org/')).rejects.toThrow('npm dist-tag add failed with exit code 1');
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
describe('updateNextTag', () => {
|
|
217
|
+
it('should call updateDistTag with TAG.NEXT', async () => {
|
|
218
|
+
const mockChildProcess = {
|
|
219
|
+
on: vi.fn((event, callback) => {
|
|
220
|
+
if (event === 'close') {
|
|
221
|
+
setTimeout(() => callback(EXIT_CODES.SUCCESS), 0);
|
|
222
|
+
}
|
|
223
|
+
return mockChildProcess;
|
|
224
|
+
}),
|
|
225
|
+
};
|
|
226
|
+
mockedSpawn.mockImplementation(() => mockChildProcess);
|
|
227
|
+
await updateNextTag('@hubspot/test-package', '1.0.0', '123456', false, 'https://registry.npmjs.org/');
|
|
228
|
+
expect(mockedSpawn).toHaveBeenCalledWith('npm', [
|
|
229
|
+
'dist-tag',
|
|
230
|
+
'add',
|
|
231
|
+
'@hubspot/test-package@1.0.0',
|
|
232
|
+
TAG.NEXT,
|
|
233
|
+
'--registry',
|
|
234
|
+
'https://registry.npmjs.org/',
|
|
235
|
+
'--otp',
|
|
236
|
+
'123456',
|
|
237
|
+
], { stdio: 'inherit' });
|
|
238
|
+
expect(vi.mocked(logger.success)).toHaveBeenCalledWith(`${TAG.NEXT} tag updated successfully`);
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
describe('updatePreviousTag', () => {
|
|
242
|
+
it('should call updateDistTag with TAG.PREVIOUS', async () => {
|
|
243
|
+
const mockChildProcess = {
|
|
244
|
+
on: vi.fn((event, callback) => {
|
|
245
|
+
if (event === 'close') {
|
|
246
|
+
setTimeout(() => callback(EXIT_CODES.SUCCESS), 0);
|
|
247
|
+
}
|
|
248
|
+
return mockChildProcess;
|
|
249
|
+
}),
|
|
250
|
+
};
|
|
251
|
+
mockedSpawn.mockImplementation(() => mockChildProcess);
|
|
252
|
+
await updatePreviousTag('@hubspot/test-package', '8.3.6', '123456', false, 'https://registry.npmjs.org/');
|
|
253
|
+
expect(mockedSpawn).toHaveBeenCalledWith('npm', [
|
|
254
|
+
'dist-tag',
|
|
255
|
+
'add',
|
|
256
|
+
'@hubspot/test-package@8.3.6',
|
|
257
|
+
TAG.PREVIOUS,
|
|
258
|
+
'--registry',
|
|
259
|
+
'https://registry.npmjs.org/',
|
|
260
|
+
'--otp',
|
|
261
|
+
'123456',
|
|
262
|
+
], { stdio: 'inherit' });
|
|
263
|
+
expect(vi.mocked(logger.success)).toHaveBeenCalledWith(`${TAG.PREVIOUS} tag updated successfully`);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { EXIT_CODES } from '../process.js';
|
|
3
|
+
describe('process', () => {
|
|
4
|
+
describe('EXIT_CODES', () => {
|
|
5
|
+
it('should have SUCCESS equal to 0', () => {
|
|
6
|
+
expect(EXIT_CODES.SUCCESS).toBe(0);
|
|
7
|
+
});
|
|
8
|
+
it('should have ERROR equal to 1', () => {
|
|
9
|
+
expect(EXIT_CODES.ERROR).toBe(1);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|