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
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';
import { getReleaseVersion } from '../get-release-version';
// Mock child_process so we can spy on execSync
vi.mock('child_process', () => ({
execSync: vi.fn(),
}));
describe('getReleaseVersion', async () => {
// Dynamically import execSync after mocking
const { execSync } = await import('child_process');
const originalEnv = { ...process.env };
beforeEach(() => {
vi.resetAllMocks();
process.env = { ...originalEnv };
// Mock date to be consistent
vi.setSystemTime(new Date('2025-08-20T00:00:00.000Z'));
// Provide a default mock for execSync to avoid toString() on undefined
vi.mocked(execSync).mockReturnValue('');
});
afterEach(() => {
process.env = originalEnv;
vi.useRealTimers();
});
it('should generate a nightly version and get previous tag', () => {
process.env.IS_NIGHTLY = 'true';
vi.mocked(execSync).mockImplementation((command) => {
if (command.includes('git tag')) {
return 'v0.1.0\nv0.0.1';
}
if (command.includes('git rev-parse')) {
return 'abcdef';
}
if (command.includes('gh release list')) {
return 'v0.3.0-nightly.20250819.abcdef';
}
return '';
});
const result = getReleaseVersion();
expect(result).toEqual({
releaseTag: 'v0.3.0-nightly.20250820.abcdef',
releaseVersion: '0.3.0-nightly.20250820.abcdef',
npmTag: 'nightly',
previousReleaseTag: 'v0.3.0-nightly.20250819.abcdef',
});
});
it('should generate a preview version and get previous tag', () => {
process.env.IS_PREVIEW = 'true';
vi.mocked(execSync).mockImplementation((command) => {
if (command.includes('git tag')) {
return 'v0.1.0\nv0.0.1';
}
if (command.includes('gh release list')) {
return 'v0.1.0'; // Previous stable release
}
return '';
});
const result = getReleaseVersion();
expect(result).toEqual({
releaseTag: 'v0.2.0-preview',
releaseVersion: '0.2.0-preview',
npmTag: 'preview',
previousReleaseTag: 'v0.1.0',
});
});
it('should use the manual version and get previous tag', () => {
process.env.MANUAL_VERSION = 'v0.1.1';
vi.mocked(execSync).mockImplementation((command) => {
if (command.includes('gh release list')) {
return 'v0.1.0'; // Previous stable release
}
return '';
});
const result = getReleaseVersion();
expect(result).toEqual({
releaseTag: 'v0.1.1',
releaseVersion: '0.1.1',
npmTag: 'latest',
previousReleaseTag: 'v0.1.0',
});
});
it('should prepend v to manual version if missing', () => {
process.env.MANUAL_VERSION = '1.2.3';
const { releaseTag } = getReleaseVersion();
expect(releaseTag).toBe('v1.2.3');
});
it('should handle pre-release versions correctly', () => {
process.env.MANUAL_VERSION = 'v1.2.3-beta.1';
const { releaseTag, releaseVersion, npmTag } = getReleaseVersion();
expect(releaseTag).toBe('v1.2.3-beta.1');
expect(releaseVersion).toBe('1.2.3-beta.1');
expect(npmTag).toBe('beta');
});
it('should throw an error for invalid version format', () => {
process.env.MANUAL_VERSION = '1.2';
expect(() => getReleaseVersion()).toThrow(
'Error: Version must be in the format vX.Y.Z or vX.Y.Z-prerelease',
);
});
it('should throw an error if no version is provided for non-nightly/preview release', () => {
expect(() => getReleaseVersion()).toThrow(
'Error: No version specified and this is not a nightly or preview release.',
);
});
it('should throw an error for versions with build metadata', () => {
process.env.MANUAL_VERSION = 'v1.2.3+build456';
expect(() => getReleaseVersion()).toThrow(
'Error: Versions with build metadata (+) are not supported for releases.',
);
});
it('should correctly calculate the next version from a patch release', () => {
process.env.IS_PREVIEW = 'true';
vi.mocked(execSync).mockImplementation((command) => {
if (command.includes('git tag')) {
return 'v1.1.3\nv1.1.2\nv1.1.1\nv1.1.0\nv1.0.0';
}
if (command.includes('gh release list')) {
return 'v1.1.3';
}
return '';
});
const result = getReleaseVersion();
expect(result.releaseTag).toBe('v1.2.0-preview');
});
});
|