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 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,9 +92,11 @@ 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'));
});
// 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(() => {
@ -103,25 +107,28 @@ describe('config-path', () => {
findConfigFile();
}).toThrow(/configuration file does not have enough permissions for reading/);
});
}
});
});
describe('with no env variables', () => {
// 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(() => {});
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');
vi.stubEnv('APPDATA', 'C:\\Users\\Tester\\AppData\\');
expect(findConfigFile()).toEqual('C:\\Users\\Tester\\AppData\\verdaccio\\config.yaml');
expect(writeFileSyncMock).toHaveBeenCalled();
expect(mkdirSyncMock).toHaveBeenCalled();
});
});
}
});
});