Fix userStartupWarnings to be windows compatible. (#4868)

This commit is contained in:
Tommaso Sciortino 2025-07-25 12:05:29 -07:00 committed by GitHub
parent f0e3e6ee8a
commit 7ddbf97634
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 33 additions and 77 deletions

View File

@ -10,94 +10,53 @@ import * as os from 'os';
import fs from 'fs/promises'; import fs from 'fs/promises';
import path from 'path'; import path from 'path';
vi.mock('os', () => ({ // Mock os.homedir to control the home directory in tests
default: { homedir: vi.fn() }, vi.mock('os', async (importOriginal) => {
homedir: vi.fn(), const actualOs = await importOriginal<typeof os>();
})); return {
...actualOs,
vi.mock('fs/promises', () => ({ homedir: vi.fn(),
default: { realpath: vi.fn() }, };
})); });
describe('getUserStartupWarnings', () => { describe('getUserStartupWarnings', () => {
const homeDir = '/home/user'; let testRootDir: string;
let homeDir: string;
beforeEach(() => { beforeEach(async () => {
testRootDir = await fs.mkdtemp(path.join(os.tmpdir(), 'warnings-test-'));
homeDir = path.join(testRootDir, 'home');
await fs.mkdir(homeDir, { recursive: true });
vi.mocked(os.homedir).mockReturnValue(homeDir); vi.mocked(os.homedir).mockReturnValue(homeDir);
vi.mocked(fs.realpath).mockImplementation(async (path) => path.toString());
}); });
afterEach(() => { afterEach(async () => {
await fs.rm(testRootDir, { recursive: true, force: true });
vi.clearAllMocks(); vi.clearAllMocks();
}); });
describe('home directory check', () => { describe('home directory check', () => {
it('should return a warning when running in home directory', async () => { it('should return a warning when running in home directory', async () => {
vi.mocked(fs.realpath)
.mockResolvedValueOnce(homeDir)
.mockResolvedValueOnce(homeDir);
const warnings = await getUserStartupWarnings(homeDir); const warnings = await getUserStartupWarnings(homeDir);
expect(warnings).toContainEqual( expect(warnings).toContainEqual(
expect.stringContaining('home directory'), expect.stringContaining('home directory'),
); );
}); });
it('should not return a warning when running in a project directory', async () => { it('should not return a warning when running in a project directory', async () => {
vi.mocked(fs.realpath) const projectDir = path.join(testRootDir, 'project');
.mockResolvedValueOnce('/some/project/path') await fs.mkdir(projectDir);
.mockResolvedValueOnce(homeDir); const warnings = await getUserStartupWarnings(projectDir);
const warnings = await getUserStartupWarnings('/some/project/path');
expect(warnings).not.toContainEqual( expect(warnings).not.toContainEqual(
expect.stringContaining('home directory'), expect.stringContaining('home directory'),
); );
}); });
it('should handle errors when checking directory', async () => {
vi.mocked(fs.realpath)
.mockRejectedValueOnce(new Error('FS error'))
.mockResolvedValueOnce(homeDir);
const warnings = await getUserStartupWarnings('/error/path');
expect(warnings).toContainEqual(
expect.stringContaining('Could not verify'),
);
});
}); });
// // Example of how to add a new check:
// describe('node version check', () => {
// // Tests for node version check would go here
// // This shows how easy it is to add new test sections
// });
describe('root directory check', () => { describe('root directory check', () => {
it('should return a warning when running in root directory on Unix', async () => { it('should return a warning when running in a root directory', async () => {
vi.mocked(fs.realpath) const rootDir = path.parse(testRootDir).root;
.mockResolvedValueOnce('/') const warnings = await getUserStartupWarnings(rootDir);
.mockResolvedValueOnce(homeDir);
const warnings = await getUserStartupWarnings('/');
expect(warnings).toContainEqual(
expect.stringContaining('root directory'),
);
expect(warnings).toContainEqual(
expect.stringContaining('folder structure will be used'),
);
});
it('should return a warning when running in root directory on Windows', async () => {
vi.mocked(fs.realpath)
.mockResolvedValueOnce('C:\\')
.mockResolvedValueOnce(homeDir);
vi.spyOn(path, 'dirname').mockImplementation(path.win32.dirname);
const warnings = await getUserStartupWarnings('C:\\');
expect(warnings).toContainEqual( expect(warnings).toContainEqual(
expect.stringContaining('root directory'), expect.stringContaining('root directory'),
); );
@ -107,25 +66,22 @@ describe('getUserStartupWarnings', () => {
}); });
it('should not return a warning when running in a non-root directory', async () => { it('should not return a warning when running in a non-root directory', async () => {
vi.mocked(fs.realpath) const projectDir = path.join(testRootDir, 'project');
.mockResolvedValueOnce('/some/project/path') await fs.mkdir(projectDir);
.mockResolvedValueOnce(homeDir); const warnings = await getUserStartupWarnings(projectDir);
const warnings = await getUserStartupWarnings('/some/project/path');
expect(warnings).not.toContainEqual( expect(warnings).not.toContainEqual(
expect.stringContaining('root directory'), expect.stringContaining('root directory'),
); );
}); });
});
it('should handle errors when checking root directory', async () => { describe('error handling', () => {
vi.mocked(fs.realpath) it('should handle errors when checking directory', async () => {
.mockRejectedValueOnce(new Error('FS error')) const nonExistentPath = path.join(testRootDir, 'non-existent');
.mockResolvedValueOnce(homeDir); const warnings = await getUserStartupWarnings(nonExistentPath);
const expectedWarning =
const warnings = await getUserStartupWarnings('/'); 'Could not verify the current directory due to a file system error.';
expect(warnings).toContainEqual( expect(warnings).toEqual([expectedWarning, expectedWarning]);
expect.stringContaining('Could not verify'),
);
}); });
}); });
}); });