Releasing: Version mgmt (#8964)

Co-authored-by: gemini-cli-robot <gemini-cli-robot@google.com>
This commit is contained in:
matt korwel
2025-09-24 21:02:00 -07:00
committed by GitHub
parent 422eb78b41
commit 9ba1640c07
5 changed files with 478 additions and 38 deletions

View File

@@ -17,7 +17,7 @@ describe('getVersion', () => {
});
const mockExecSync = (command) => {
// NPM Mocks
// NPM dist-tags - source of truth
if (command.includes('npm view') && command.includes('--tag=latest'))
return '0.4.1';
if (command.includes('npm view') && command.includes('--tag=preview'))
@@ -25,14 +25,25 @@ describe('getVersion', () => {
if (command.includes('npm view') && command.includes('--tag=nightly'))
return '0.6.0-nightly.20250910.a31830a3';
// Git Tag Mocks
if (command.includes("git tag --sort=-creatordate -l 'v[0-9].[0-9].[0-9]'"))
return 'v0.4.1';
if (command.includes("git tag --sort=-creatordate -l 'v*-preview*'"))
return 'v0.5.0-preview.2';
if (command.includes("git tag --sort=-creatordate -l 'v*-nightly*'"))
// NPM versions list - for conflict validation
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([
'0.4.1',
'0.5.0-preview.2',
'0.6.0-nightly.20250910.a31830a3',
]);
// Git Tag Mocks - with semantic sorting
if (command.includes("git tag -l 'v[0-9].[0-9].[0-9]'")) return 'v0.4.1';
if (command.includes("git tag -l 'v*-preview*'")) return 'v0.5.0-preview.2';
if (command.includes("git tag -l 'v*-nightly*'"))
return 'v0.6.0-nightly.20250910.a31830a3';
// Conflict validation - Git tag checks
if (command.includes("git tag -l 'v0.5.0'")) return ''; // Version doesn't exist yet
if (command.includes("git tag -l 'v0.4.2'")) return ''; // Version doesn't exist yet
if (command.includes("git tag -l 'v0.6.0-preview.0'")) return ''; // Version doesn't exist yet
// GitHub Release Mocks
if (command.includes('gh release view "v0.4.1"')) return 'v0.4.1';
if (command.includes('gh release view "v0.5.0-preview.2"'))
@@ -150,17 +161,230 @@ describe('getVersion', () => {
});
});
describe('Semver Sorting Edge Cases', () => {
it('should handle Git tag creation date vs semantic version sorting', () => {
const mockWithSemverGitSorting = (command) => {
// NPM dist-tags are correct (source of truth)
if (command.includes('npm view') && command.includes('--tag=latest'))
return '0.5.0'; // NPM correctly has 0.5.0 as latest
if (command.includes('npm view') && command.includes('--tag=preview'))
return '0.6.0-preview.2';
if (command.includes('npm view') && command.includes('--tag=nightly'))
return '0.7.0-nightly.20250910.a31830a3';
// NPM versions list for conflict validation
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([
'0.0.77', // This was the problematic dev version
'0.4.1',
'0.5.0',
'0.6.0-preview.1',
'0.6.0-preview.2',
'0.7.0-nightly.20250910.a31830a3',
]);
// Git tags - test that semantic sorting works correctly
if (command.includes("git tag -l 'v[0-9].[0-9].[0-9]'"))
return 'v0.0.77\nv0.5.0\nv0.4.1'; // Multiple tags - should pick v0.5.0 semantically
if (command.includes("git tag -l 'v*-preview*'"))
return 'v0.6.0-preview.2';
if (command.includes("git tag -l 'v*-nightly*'"))
return 'v0.7.0-nightly.20250910.a31830a3';
// Conflict validation - new versions don't exist yet
if (command.includes("git tag -l 'v0.5.1'")) return '';
if (command.includes("git tag -l 'v0.6.0'")) return '';
// GitHub releases
if (command.includes('gh release view "v0.5.0"')) return 'v0.5.0';
if (command.includes('gh release view "v0.6.0-preview.2"'))
return 'v0.6.0-preview.2';
if (
command.includes('gh release view "v0.7.0-nightly.20250910.a31830a3"')
)
return 'v0.7.0-nightly.20250910.a31830a3';
// GitHub conflict validation - new versions don't exist
if (command.includes('gh release view "v0.5.1"'))
throw new Error('Not found');
if (command.includes('gh release view "v0.6.0"'))
throw new Error('Not found');
// Git Hash Mock
if (command.includes('git rev-parse --short HEAD')) return 'd3bf8a3d';
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithSemverGitSorting);
// Test patch calculation - should be 0.5.1 from NPM's latest=0.5.0
const patchResult = getVersion({ type: 'patch', 'patch-from': 'stable' });
expect(patchResult.releaseVersion).toBe('0.5.1');
expect(patchResult.previousReleaseTag).toBe('v0.5.0');
// Verify no rollback information is included in normal scenarios
expect(patchResult.rollbackDetected).toBeUndefined();
// Test stable calculation - should be 0.6.0 from preview
const stableResult = getVersion({ type: 'stable' });
expect(stableResult.releaseVersion).toBe('0.6.0');
expect(stableResult.previousReleaseTag).toBe('v0.5.0');
// Verify no rollback information for stable calculation either
expect(stableResult.rollbackDetected).toBeUndefined();
});
it('should fail when git tags are not semver-sorted correctly', () => {
const mockWithIncorrectGitSorting = (command) => {
// NPM correctly returns 0.5.0 as latest
if (command.includes('npm view') && command.includes('--tag=latest'))
return '0.5.0';
// But git tag sorting returns wrong semantic version
if (command.includes("git tag -l 'v[0-9].[0-9].[0-9]'"))
return 'v0.4.1'; // This should cause a discrepancy error (NPM says 0.5.0)
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithIncorrectGitSorting);
// This should throw because NPM says 0.5.0 but git tag sorting says v0.4.1
expect(() =>
getVersion({ type: 'patch', 'patch-from': 'stable' }),
).toThrow(
'Discrepancy found! NPM latest tag (0.5.0) does not match latest git latest tag (v0.4.1).',
);
});
it('should handle rollback scenarios by using highest existing version', () => {
const mockWithRollback = (command) => {
// NPM dist-tag was rolled back from 0.5.0 to 0.4.1 due to issues
if (command.includes('npm view') && command.includes('--tag=latest'))
return '0.4.1'; // Rolled back version
if (command.includes('npm view') && command.includes('--tag=preview'))
return '0.6.0-preview.2';
if (command.includes('npm view') && command.includes('--tag=nightly'))
return '0.7.0-nightly.20250910.a31830a3';
// NPM versions list shows 0.5.0 was published (but rolled back)
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([
'0.3.0',
'0.4.1', // Current dist-tag
'0.5.0', // Published but rolled back
'0.6.0-preview.1',
'0.6.0-preview.2',
'0.7.0-nightly.20250910.a31830a3',
]);
// Git tags show both versions exist
if (command.includes("git tag -l 'v[0-9].[0-9].[0-9]'"))
return 'v0.4.1\nv0.5.0'; // Both tags exist
if (command.includes("git tag -l 'v*-preview*'"))
return 'v0.6.0-preview.2';
if (command.includes("git tag -l 'v*-nightly*'"))
return 'v0.7.0-nightly.20250910.a31830a3';
// Specific git tag checks for rollback validation
if (command.includes("git tag -l 'v0.5.0'")) return 'v0.5.0';
// Conflict validation - new versions don't exist yet
if (command.includes("git tag -l 'v0.5.1'")) return '';
if (command.includes("git tag -l 'v0.6.0'")) return '';
// GitHub releases exist for both versions
if (command.includes('gh release view "v0.4.1"')) return 'v0.4.1';
if (command.includes('gh release view "v0.5.0"')) return 'v0.5.0'; // Exists but rolled back
if (command.includes('gh release view "v0.6.0-preview.2"'))
return 'v0.6.0-preview.2';
// GitHub conflict validation - new versions don't exist
if (command.includes('gh release view "v0.5.1"'))
throw new Error('Not found');
if (command.includes('gh release view "v0.6.0"'))
throw new Error('Not found');
// Git Hash Mock
if (command.includes('git rev-parse --short HEAD')) return 'd3bf8a3d';
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithRollback);
// Mock console.warn to capture rollback warning
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
// Test patch calculation - should be 0.5.1 (from rolled back 0.5.0, not current dist-tag 0.4.1)
const patchResult = getVersion({ type: 'patch', 'patch-from': 'stable' });
expect(patchResult.releaseVersion).toBe('0.5.1'); // Fix for 0.5.0, not 0.4.2
expect(patchResult.previousReleaseTag).toBe('v0.5.0'); // Uses highest existing, not dist-tag
// Verify rollback information is included in output
expect(patchResult.rollbackDetected).toBeDefined();
expect(patchResult.rollbackDetected.rollbackScenario).toBe(true);
expect(patchResult.rollbackDetected.distTagVersion).toBe('0.4.1');
expect(patchResult.rollbackDetected.highestExistingVersion).toBe('0.5.0');
expect(patchResult.rollbackDetected.baselineUsed).toBe('0.5.0');
expect(patchResult.rollbackDetected.message).toContain(
'Rollback detected: NPM tag was 0.4.1, but using 0.5.0 as baseline for next version calculation',
);
// Verify rollback was detected and warning was shown
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining(
'Rollback detected! NPM latest tag is 0.4.1, but using 0.5.0 as baseline for next version calculation',
),
);
// Test stable calculation - should be 0.6.0 from preview
const stableResult = getVersion({ type: 'stable' });
expect(stableResult.releaseVersion).toBe('0.6.0');
expect(stableResult.previousReleaseTag).toBe('v0.5.0'); // Uses rollback baseline
consoleSpy.mockRestore();
});
it('should fail rollback scenario when git tag for highest version is missing', () => {
const mockWithMissingGitTag = (command) => {
// NPM rolled back but git tag was deleted (bad practice)
if (command.includes('npm view') && command.includes('--tag=latest'))
return '0.4.1'; // Rolled back
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify(['0.4.1', '0.5.0']); // 0.5.0 exists in NPM
if (command.includes("git tag -l 'v[0-9].[0-9].[0-9]'"))
return 'v0.4.1'; // Only old tag exists
if (command.includes("git tag -l 'v0.5.0'")) return ''; // Missing!
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithMissingGitTag);
expect(() =>
getVersion({ type: 'patch', 'patch-from': 'stable' }),
).toThrow(
'Rollback scenario detected, but git tag v0.5.0 does not exist! This is required to calculate the next version.',
);
});
});
describe('Failure Path - Discrepancy Checks', () => {
it('should throw an error if the git tag does not match npm', () => {
const mockWithMismatchGitTag = (command) => {
if (command.includes("git tag --sort=-creatordate -l 'v*-preview*'"))
return 'v0.4.0-preview-99'; // Mismatch
if (command.includes("git tag -l 'v*-preview*'"))
return 'v0.4.0-preview.99'; // Mismatch with NPM's 0.5.0-preview.2
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithMismatchGitTag);
expect(() => getVersion({ type: 'stable' })).toThrow(
'Discrepancy found! NPM preview tag (0.5.0-preview.2) does not match latest git preview tag (v0.4.0-preview-99).',
'Discrepancy found! NPM preview tag (0.5.0-preview.2) does not match latest git preview tag (v0.4.0-preview.99).',
);
});