refactor(cli): consolidate repetitive tests in InputPrompt using it.each (#12524)

This commit is contained in:
Jainam M
2025-11-06 23:11:50 +05:30
committed by GitHub
parent 9ba1cd0336
commit c585470a71
2 changed files with 139 additions and 202 deletions
+83 -87
View File
@@ -97,61 +97,75 @@ describe('fileUtils', () => {
});
describe('isWithinRoot', () => {
const root = path.resolve('/project/root');
const defaultRoot = path.resolve('/project/root');
it('should return true for paths directly within the root', () => {
expect(isWithinRoot(path.join(root, 'file.txt'), root)).toBe(true);
expect(isWithinRoot(path.join(root, 'subdir', 'file.txt'), root)).toBe(
true,
);
});
it('should return true for the root path itself', () => {
expect(isWithinRoot(root, root)).toBe(true);
});
it('should return false for paths outside the root', () => {
expect(
isWithinRoot(path.resolve('/project/other', 'file.txt'), root),
).toBe(false);
expect(isWithinRoot(path.resolve('/unrelated', 'file.txt'), root)).toBe(
false,
);
});
it('should return false for paths that only partially match the root prefix', () => {
expect(
isWithinRoot(
path.resolve('/project/root-but-actually-different'),
root,
),
).toBe(false);
});
it('should handle paths with trailing slashes correctly', () => {
expect(isWithinRoot(path.join(root, 'file.txt') + path.sep, root)).toBe(
true,
);
expect(isWithinRoot(root + path.sep, root)).toBe(true);
});
it('should handle different path separators (POSIX vs Windows)', () => {
const posixRoot = '/project/root';
const posixPathInside = '/project/root/file.txt';
const posixPathOutside = '/project/other/file.txt';
expect(isWithinRoot(posixPathInside, posixRoot)).toBe(true);
expect(isWithinRoot(posixPathOutside, posixRoot)).toBe(false);
});
it('should return false for a root path that is a sub-path of the path to check', () => {
const pathToCheck = path.resolve('/project/root/sub');
const rootSub = path.resolve('/project/root');
expect(isWithinRoot(pathToCheck, rootSub)).toBe(true);
const pathToCheckSuper = path.resolve('/project/root');
const rootSuper = path.resolve('/project/root/sub');
expect(isWithinRoot(pathToCheckSuper, rootSuper)).toBe(false);
});
it.each([
{
name: 'a path directly within the root',
path: path.join(defaultRoot, 'file.txt'),
expected: true,
},
{
name: 'a path in a subdirectory within the root',
path: path.join(defaultRoot, 'subdir', 'file.txt'),
expected: true,
},
{ name: 'the root path itself', path: defaultRoot, expected: true },
{
name: 'a path with a trailing slash',
path: path.join(defaultRoot, 'file.txt') + path.sep,
expected: true,
},
{
name: 'the root path with a trailing slash',
path: defaultRoot + path.sep,
expected: true,
},
{
name: 'a sub-path of the path to check',
path: path.resolve('/project/root/sub'),
root: path.resolve('/project/root'),
expected: true,
},
{
name: 'a path outside the root',
path: path.resolve('/project/other', 'file.txt'),
expected: false,
},
{
name: 'an unrelated path',
path: path.resolve('/unrelated', 'file.txt'),
expected: false,
},
{
name: 'a path that only partially matches the root prefix',
path: path.resolve('/project/root-but-actually-different'),
expected: false,
},
{
name: 'a root path that is a sub-path of the path to check',
path: path.resolve('/project/root'),
root: path.resolve('/project/root/sub'),
expected: false,
},
{
name: 'a POSIX path inside',
path: '/project/root/file.txt',
root: '/project/root',
expected: true,
},
{
name: 'a POSIX path outside',
path: '/project/other/file.txt',
root: '/project/root',
expected: false,
},
])(
'should return $expected for $name',
({ path: testPath, root, expected }) => {
expect(isWithinRoot(testPath, root || defaultRoot)).toBe(expected);
},
);
});
describe('fileExists', () => {
@@ -610,45 +624,27 @@ describe('fileUtils', () => {
expect(await detectFileType('component.tsx')).toBe('text');
});
it('should detect image type by extension (png)', async () => {
mockMimeGetType.mockReturnValueOnce('image/png');
expect(await detectFileType('file.png')).toBe('image');
});
it('should detect image type by extension (jpeg)', async () => {
mockMimeGetType.mockReturnValueOnce('image/jpeg');
expect(await detectFileType('file.jpg')).toBe('image');
});
it.each([
{ type: 'image', file: 'file.png', mime: 'image/png' },
{ type: 'image', file: 'file.jpg', mime: 'image/jpeg' },
{ type: 'pdf', file: 'file.pdf', mime: 'application/pdf' },
{ type: 'audio', file: 'song.mp3', mime: 'audio/mpeg' },
{ type: 'video', file: 'movie.mp4', mime: 'video/mp4' },
{ type: 'binary', file: 'archive.zip', mime: 'application/zip' },
{ type: 'binary', file: 'app.exe', mime: 'application/octet-stream' },
])(
'should detect $type type for $file by extension',
async ({ file, mime, type }) => {
mockMimeGetType.mockReturnValueOnce(mime);
expect(await detectFileType(file)).toBe(type);
},
);
it('should detect svg type by extension', async () => {
expect(await detectFileType('image.svg')).toBe('svg');
expect(await detectFileType('image.icon.svg')).toBe('svg');
});
it('should detect pdf type by extension', async () => {
mockMimeGetType.mockReturnValueOnce('application/pdf');
expect(await detectFileType('file.pdf')).toBe('pdf');
});
it('should detect audio type by extension', async () => {
mockMimeGetType.mockReturnValueOnce('audio/mpeg');
expect(await detectFileType('song.mp3')).toBe('audio');
});
it('should detect video type by extension', async () => {
mockMimeGetType.mockReturnValueOnce('video/mp4');
expect(await detectFileType('movie.mp4')).toBe('video');
});
it('should detect known binary extensions as binary (e.g. .zip)', async () => {
mockMimeGetType.mockReturnValueOnce('application/zip');
expect(await detectFileType('archive.zip')).toBe('binary');
});
it('should detect known binary extensions as binary (e.g. .exe)', async () => {
mockMimeGetType.mockReturnValueOnce('application/octet-stream'); // Common for .exe
expect(await detectFileType('app.exe')).toBe('binary');
});
it('should use isBinaryFile for unknown extensions and detect as binary', async () => {
mockMimeGetType.mockReturnValueOnce(false); // Unknown mime type
// Create a file that isBinaryFile will identify as binary