From a05a7d8a13d94cd31d77c1693033c725021da773 Mon Sep 17 00:00:00 2001 From: Marc Bernard <59966492+mbtools@users.noreply.github.com> Date: Thu, 22 Aug 2024 02:49:38 -0400 Subject: [PATCH] fix(config): test runs on Windows (#4786) * fix(config): test runs on Windows * Typo * limit APPDATA test to win32 * limit APPDATA test to win32 * limit APPDATA test to win32 * Limit APPDATA test to Windows (w/ mocking) * More realistic APPDATA value --- .changeset/swift-rabbits-vanish.md | 5 ++ packages/config/test/config.path.spec.ts | 85 +++++++++++++----------- 2 files changed, 51 insertions(+), 39 deletions(-) create mode 100644 .changeset/swift-rabbits-vanish.md diff --git a/.changeset/swift-rabbits-vanish.md b/.changeset/swift-rabbits-vanish.md new file mode 100644 index 000000000..58c1e8337 --- /dev/null +++ b/.changeset/swift-rabbits-vanish.md @@ -0,0 +1,5 @@ +--- +'@verdaccio/config': patch +--- + +fix(config): test runs on Windows diff --git a/packages/config/test/config.path.spec.ts b/packages/config/test/config.path.spec.ts index 9e83bea34..7f19076eb 100644 --- a/packages/config/test/config.path.spec.ts +++ b/packages/config/test/config.path.spec.ts @@ -8,7 +8,6 @@ describe('config-path', () => { let statSyncMock; let mkdirSyncMock; let writeFileSyncMock; - let platformMock; let accessSyncMock; let fakeStats = { isDirectory: vi.fn().mockReturnValue(true), @@ -20,8 +19,6 @@ describe('config-path', () => { mkdirSyncMock = vi.spyOn(fs, 'mkdirSync'); writeFileSyncMock = vi.spyOn(fs, 'writeFileSync'); accessSyncMock = vi.spyOn(fs, 'accessSync'); - platformMock = vi.spyOn(os, 'platform'); - platformMock.mockReturnValue('linux'); }); afterEach(() => { @@ -30,22 +27,27 @@ describe('config-path', () => { vi.unstubAllEnvs(); }); + function platformPath(path: string): string { + return path.replace(/\//g, os.platform() === 'win32' ? '\\' : '/'); + } + describe('findConfigFile', () => { - describe('using defiled location from arguments', () => { + describe('using file location from arguments', () => { test('with custom location', () => { // mock statSyncMock.mockReturnValue(fakeStats); mkdirSyncMock.mockReturnValue(true); writeFileSyncMock.mockReturnValue(undefined); - expect(findConfigFile('/home/user/custom/location/config.yaml')).toEqual( - '/home/user/custom/location/config.yaml' + // Note: on Windows, path contains drive letter + expect(findConfigFile('/home/user/custom/location/config.yaml')).toMatch( + platformPath('/home/user/custom/location/config.yaml') ); expect(writeFileSyncMock).not.toHaveBeenCalled(); expect(mkdirSyncMock).not.toHaveBeenCalled(); }); }); - describe('whith env variables', () => { + describe('with env variables', () => { test('the env XDG_CONFIG_HOME is defined and the directory exist but config file is missing', async () => { // mock statSyncMock.mockReturnValue(fakeStats); @@ -54,9 +56,9 @@ describe('config-path', () => { // node env variable vi.stubEnv('XDG_CONFIG_HOME', '/home/user'); - expect(findConfigFile()).toEqual('/home/user/verdaccio/config.yaml'); + expect(findConfigFile()).toEqual(platformPath('/home/user/verdaccio/config.yaml')); expect(writeFileSyncMock).toHaveBeenCalledWith( - '/home/user/verdaccio/config.yaml', + platformPath('/home/user/verdaccio/config.yaml'), expect.stringContaining('packages') ); }); @@ -68,13 +70,13 @@ describe('config-path', () => { writeFileSyncMock.mockReturnValue(undefined); vi.stubEnv('XDG_CONFIG_HOME', ''); vi.stubEnv('HOME', '/home/user'); - expect(findConfigFile()).toEqual('/home/user/.config/verdaccio/config.yaml'); + expect(findConfigFile()).toEqual(platformPath('/home/user/.config/verdaccio/config.yaml')); expect(writeFileSyncMock).toHaveBeenCalledWith( - '/home/user/.config/verdaccio/config.yaml', + platformPath('/home/user/.config/verdaccio/config.yaml'), expect.stringContaining('packages') ); expect(mkdirSyncMock).toHaveBeenCalledWith( - '/home/user/.config/verdaccio', + platformPath('/home/user/.config/verdaccio'), expect.anything() ); }); @@ -90,38 +92,43 @@ describe('config-path', () => { // node env variable vi.stubEnv('XDG_CONFIG_HOME', '/home/user/fail'); - expect(findConfigFile()).toMatch('packages/config/verdaccio/config.yaml'); + expect(findConfigFile()).toMatch(platformPath('packages/config/verdaccio/config.yaml')); }); - test('no permissions on read default config file', () => { - vi.stubEnv('XDG_CONFIG_HOME', '/home/user'); - accessSyncMock.mockImplementation(() => { - throw new Error('error on write file'); + // Does not work on Windows + if (os.platform() !== 'win32') { + test('no permissions on read default config file', () => { + vi.stubEnv('XDG_CONFIG_HOME', '/home/user'); + accessSyncMock.mockImplementation(() => { + throw new Error('error on write file'); + }); + + expect(function () { + findConfigFile(); + }).toThrow(/configuration file does not have enough permissions for reading/); }); + } + }); + }); - expect(function () { - findConfigFile(); - }).toThrow(/configuration file does not have enough permissions for reading/); + // Note: Trying to mock Windows platform leads to different results (incorrect slashes) + if (os.platform() === 'win32') { + describe('with Windows env variables', () => { + test('with relative location', () => { + // mock + statSyncMock.mockReturnValue(fakeStats); + mkdirSyncMock.mockReturnValue(true); + writeFileSyncMock.mockReturnValue(undefined); + accessSyncMock.mockImplementation(() => {}); + // delete process.env.XDG_CONFIG_HOME; + vi.stubEnv('XDG_CONFIG_HOME', ''); + vi.stubEnv('HOME', ''); + vi.stubEnv('APPDATA', 'C:\\Users\\Tester\\AppData\\'); + expect(findConfigFile()).toEqual('C:\\Users\\Tester\\AppData\\verdaccio\\config.yaml'); + expect(writeFileSyncMock).toHaveBeenCalled(); + expect(mkdirSyncMock).toHaveBeenCalled(); }); }); - }); - - describe('with no env variables', () => { - test('with relative location', () => { - // mock - statSyncMock.mockReturnValue(fakeStats); - mkdirSyncMock.mockReturnValue(true); - writeFileSyncMock.mockReturnValue(undefined); - accessSyncMock.mockImplementation(() => {}); - platformMock.mockReturnValue('win32'); - // delete process.env.XDG_CONFIG_HOME; - vi.stubEnv('XDG_CONFIG_HOME', ''); - vi.stubEnv('HOME', ''); - vi.stubEnv('APPDATA', '/app/data/'); - expect(findConfigFile()).toMatch('/app/data/verdaccio/config.yaml'); - expect(writeFileSyncMock).toHaveBeenCalled(); - expect(mkdirSyncMock).toHaveBeenCalled(); - }); - }); + } }); });