Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/__tests__/version-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ vi.mock('../utils/logger', () => ({
},
}));

const hasPathSuffix = (path: string, suffix: string) =>
path.replaceAll('\\', '/').endsWith(suffix);

describe('version-check utilities', () => {
describe('compareSemver', () => {
it('should return "lt" if a < b', () => {
Expand Down Expand Up @@ -109,9 +112,15 @@ describe('version-check utilities', () => {
vi.mocked(readFileSync).mockReset();
});

it('should treat Windows and POSIX separators the same when matching package paths', () => {
expect(hasPathSuffix('C:\\repo\\src\\package.json', 'src/package.json')).toBe(true);
expect(hasPathSuffix('/repo/src/package.json', 'src/package.json')).toBe(true);
expect(hasPathSuffix('/repo/src/package.json', 'src/other.json')).toBe(false);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

it('should resolve and return the version if package.json in production path is valid', () => {
vi.mocked(readFileSync).mockImplementation((path) => {
if (typeof path === 'string' && path.endsWith('src/package.json')) {
if (typeof path === 'string' && hasPathSuffix(path, 'src/package.json')) {
return JSON.stringify({ version: '2.3.4' });
}
throw new Error('File not found');
Expand All @@ -122,7 +131,7 @@ describe('version-check utilities', () => {

it('should resolve and return the version if production path fails but development path succeeds', () => {
vi.mocked(readFileSync).mockImplementation((path) => {
if (typeof path === 'string' && path.endsWith('package.json') && !path.endsWith('src/package.json')) {
if (typeof path === 'string' && hasPathSuffix(path, 'package.json') && !hasPathSuffix(path, 'src/package.json')) {
return JSON.stringify({ version: '1.2.3' });
}
throw new Error('File not found');
Expand All @@ -133,10 +142,10 @@ describe('version-check utilities', () => {

it('should advance to the next path if JSON is malformed', () => {
vi.mocked(readFileSync).mockImplementation((path) => {
if (typeof path === 'string' && path.endsWith('src/package.json')) {
if (typeof path === 'string' && hasPathSuffix(path, 'src/package.json')) {
return '{ malformed json';
}
if (typeof path === 'string' && path.endsWith('package.json') && !path.endsWith('src/package.json')) {
if (typeof path === 'string' && hasPathSuffix(path, 'package.json') && !hasPathSuffix(path, 'src/package.json')) {
return JSON.stringify({ version: '1.2.9' });
}
throw new Error('File not found');
Expand All @@ -147,10 +156,10 @@ describe('version-check utilities', () => {

it('should advance to next path if version is not a string or missing', () => {
vi.mocked(readFileSync).mockImplementation((path) => {
if (typeof path === 'string' && path.endsWith('src/package.json')) {
if (typeof path === 'string' && hasPathSuffix(path, 'src/package.json')) {
return JSON.stringify({ version: 12345 }); // non-string version
}
if (typeof path === 'string' && path.endsWith('package.json') && !path.endsWith('src/package.json')) {
if (typeof path === 'string' && hasPathSuffix(path, 'package.json') && !hasPathSuffix(path, 'src/package.json')) {
return JSON.stringify({ version: '1.2.8' });
}
throw new Error('File not found');
Expand Down
Loading