0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-30 22:34:10 -05:00

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
This commit is contained in:
Marc Bernard 2024-08-22 02:49:38 -04:00 committed by GitHub
parent 3f46ff0107
commit a05a7d8a13
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 51 additions and 39 deletions

View file

@ -0,0 +1,5 @@
---
'@verdaccio/config': patch
---
fix(config): test runs on Windows

View file

@ -8,7 +8,6 @@ describe('config-path', () => {
let statSyncMock; let statSyncMock;
let mkdirSyncMock; let mkdirSyncMock;
let writeFileSyncMock; let writeFileSyncMock;
let platformMock;
let accessSyncMock; let accessSyncMock;
let fakeStats = { let fakeStats = {
isDirectory: vi.fn().mockReturnValue(true), isDirectory: vi.fn().mockReturnValue(true),
@ -20,8 +19,6 @@ describe('config-path', () => {
mkdirSyncMock = vi.spyOn(fs, 'mkdirSync'); mkdirSyncMock = vi.spyOn(fs, 'mkdirSync');
writeFileSyncMock = vi.spyOn(fs, 'writeFileSync'); writeFileSyncMock = vi.spyOn(fs, 'writeFileSync');
accessSyncMock = vi.spyOn(fs, 'accessSync'); accessSyncMock = vi.spyOn(fs, 'accessSync');
platformMock = vi.spyOn(os, 'platform');
platformMock.mockReturnValue('linux');
}); });
afterEach(() => { afterEach(() => {
@ -30,22 +27,27 @@ describe('config-path', () => {
vi.unstubAllEnvs(); vi.unstubAllEnvs();
}); });
function platformPath(path: string): string {
return path.replace(/\//g, os.platform() === 'win32' ? '\\' : '/');
}
describe('findConfigFile', () => { describe('findConfigFile', () => {
describe('using defiled location from arguments', () => { describe('using file location from arguments', () => {
test('with custom location', () => { test('with custom location', () => {
// mock // mock
statSyncMock.mockReturnValue(fakeStats); statSyncMock.mockReturnValue(fakeStats);
mkdirSyncMock.mockReturnValue(true); mkdirSyncMock.mockReturnValue(true);
writeFileSyncMock.mockReturnValue(undefined); writeFileSyncMock.mockReturnValue(undefined);
expect(findConfigFile('/home/user/custom/location/config.yaml')).toEqual( // Note: on Windows, path contains drive letter
'/home/user/custom/location/config.yaml' expect(findConfigFile('/home/user/custom/location/config.yaml')).toMatch(
platformPath('/home/user/custom/location/config.yaml')
); );
expect(writeFileSyncMock).not.toHaveBeenCalled(); expect(writeFileSyncMock).not.toHaveBeenCalled();
expect(mkdirSyncMock).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 () => { test('the env XDG_CONFIG_HOME is defined and the directory exist but config file is missing', async () => {
// mock // mock
statSyncMock.mockReturnValue(fakeStats); statSyncMock.mockReturnValue(fakeStats);
@ -54,9 +56,9 @@ describe('config-path', () => {
// node env variable // node env variable
vi.stubEnv('XDG_CONFIG_HOME', '/home/user'); 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( expect(writeFileSyncMock).toHaveBeenCalledWith(
'/home/user/verdaccio/config.yaml', platformPath('/home/user/verdaccio/config.yaml'),
expect.stringContaining('packages') expect.stringContaining('packages')
); );
}); });
@ -68,13 +70,13 @@ describe('config-path', () => {
writeFileSyncMock.mockReturnValue(undefined); writeFileSyncMock.mockReturnValue(undefined);
vi.stubEnv('XDG_CONFIG_HOME', ''); vi.stubEnv('XDG_CONFIG_HOME', '');
vi.stubEnv('HOME', '/home/user'); 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( expect(writeFileSyncMock).toHaveBeenCalledWith(
'/home/user/.config/verdaccio/config.yaml', platformPath('/home/user/.config/verdaccio/config.yaml'),
expect.stringContaining('packages') expect.stringContaining('packages')
); );
expect(mkdirSyncMock).toHaveBeenCalledWith( expect(mkdirSyncMock).toHaveBeenCalledWith(
'/home/user/.config/verdaccio', platformPath('/home/user/.config/verdaccio'),
expect.anything() expect.anything()
); );
}); });
@ -90,38 +92,43 @@ describe('config-path', () => {
// node env variable // node env variable
vi.stubEnv('XDG_CONFIG_HOME', '/home/user/fail'); 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', () => { // Does not work on Windows
vi.stubEnv('XDG_CONFIG_HOME', '/home/user'); if (os.platform() !== 'win32') {
accessSyncMock.mockImplementation(() => { test('no permissions on read default config file', () => {
throw new Error('error on write 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 () { // Note: Trying to mock Windows platform leads to different results (incorrect slashes)
findConfigFile(); if (os.platform() === 'win32') {
}).toThrow(/configuration file does not have enough permissions for reading/); 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();
});
});
}); });
}); });