diff --git a/packages/cli/src/utils/userStartupWarnings.test.ts b/packages/cli/src/utils/userStartupWarnings.test.ts index 7c0e1264..6d9b8989 100644 --- a/packages/cli/src/utils/userStartupWarnings.test.ts +++ b/packages/cli/src/utils/userStartupWarnings.test.ts @@ -10,94 +10,53 @@ import * as os from 'os'; import fs from 'fs/promises'; import path from 'path'; -vi.mock('os', () => ({ - default: { homedir: vi.fn() }, - homedir: vi.fn(), -})); - -vi.mock('fs/promises', () => ({ - default: { realpath: vi.fn() }, -})); +// Mock os.homedir to control the home directory in tests +vi.mock('os', async (importOriginal) => { + const actualOs = await importOriginal(); + return { + ...actualOs, + homedir: vi.fn(), + }; +}); 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(fs.realpath).mockImplementation(async (path) => path.toString()); }); - afterEach(() => { + afterEach(async () => { + await fs.rm(testRootDir, { recursive: true, force: true }); vi.clearAllMocks(); }); describe('home directory check', () => { it('should return a warning when running in home directory', async () => { - vi.mocked(fs.realpath) - .mockResolvedValueOnce(homeDir) - .mockResolvedValueOnce(homeDir); - const warnings = await getUserStartupWarnings(homeDir); - expect(warnings).toContainEqual( expect.stringContaining('home directory'), ); }); it('should not return a warning when running in a project directory', async () => { - vi.mocked(fs.realpath) - .mockResolvedValueOnce('/some/project/path') - .mockResolvedValueOnce(homeDir); - - const warnings = await getUserStartupWarnings('/some/project/path'); + const projectDir = path.join(testRootDir, 'project'); + await fs.mkdir(projectDir); + const warnings = await getUserStartupWarnings(projectDir); expect(warnings).not.toContainEqual( 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', () => { - it('should return a warning when running in root directory on Unix', async () => { - vi.mocked(fs.realpath) - .mockResolvedValueOnce('/') - .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:\\'); - + it('should return a warning when running in a root directory', async () => { + const rootDir = path.parse(testRootDir).root; + const warnings = await getUserStartupWarnings(rootDir); expect(warnings).toContainEqual( expect.stringContaining('root directory'), ); @@ -107,25 +66,22 @@ describe('getUserStartupWarnings', () => { }); it('should not return a warning when running in a non-root directory', async () => { - vi.mocked(fs.realpath) - .mockResolvedValueOnce('/some/project/path') - .mockResolvedValueOnce(homeDir); - - const warnings = await getUserStartupWarnings('/some/project/path'); + const projectDir = path.join(testRootDir, 'project'); + await fs.mkdir(projectDir); + const warnings = await getUserStartupWarnings(projectDir); expect(warnings).not.toContainEqual( expect.stringContaining('root directory'), ); }); + }); - it('should handle errors when checking root directory', async () => { - vi.mocked(fs.realpath) - .mockRejectedValueOnce(new Error('FS error')) - .mockResolvedValueOnce(homeDir); - - const warnings = await getUserStartupWarnings('/'); - expect(warnings).toContainEqual( - expect.stringContaining('Could not verify'), - ); + describe('error handling', () => { + it('should handle errors when checking directory', async () => { + const nonExistentPath = path.join(testRootDir, 'non-existent'); + const warnings = await getUserStartupWarnings(nonExistentPath); + const expectedWarning = + 'Could not verify the current directory due to a file system error.'; + expect(warnings).toEqual([expectedWarning, expectedWarning]); }); }); });