2020-08-19 20:27:35 +02:00
|
|
|
import path from 'path';
|
2021-10-03 14:21:01 +02:00
|
|
|
import { Logger } from '@verdaccio/types';
|
2021-09-08 19:06:37 +02:00
|
|
|
import * as readFile from '../src/fs';
|
2020-08-19 20:27:35 +02:00
|
|
|
|
2021-03-30 14:05:58 +02:00
|
|
|
import { findPackages, _dbGenPath } from '../src/utils';
|
2020-08-19 20:27:35 +02:00
|
|
|
import { loadPrivatePackages } from '../src/pkg-utils';
|
|
|
|
import { noSuchFile } from '../src/local-fs';
|
|
|
|
|
2021-10-03 14:21:01 +02:00
|
|
|
const logger: Logger = {
|
|
|
|
error: jest.fn(),
|
|
|
|
info: jest.fn(),
|
|
|
|
debug: jest.fn(),
|
|
|
|
child: jest.fn(),
|
|
|
|
warn: jest.fn(),
|
|
|
|
http: jest.fn(),
|
|
|
|
trace: jest.fn(),
|
|
|
|
};
|
2020-08-19 20:27:35 +02:00
|
|
|
|
|
|
|
describe('Utitlies', () => {
|
|
|
|
const loadDb = (name): string => path.join(__dirname, '__fixtures__/databases', `${name}.json`);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetModules();
|
|
|
|
});
|
|
|
|
|
2021-03-30 14:05:58 +02:00
|
|
|
test('should load private packages', async () => {
|
2020-08-19 20:27:35 +02:00
|
|
|
const database = loadDb('ok');
|
2021-03-30 14:05:58 +02:00
|
|
|
const db = await loadPrivatePackages(database, logger);
|
2020-08-19 20:27:35 +02:00
|
|
|
|
|
|
|
expect(db.list).toHaveLength(15);
|
|
|
|
});
|
|
|
|
|
2021-03-30 14:05:58 +02:00
|
|
|
test('should load and empty private packages if database file is valid and empty', async () => {
|
2020-08-19 20:27:35 +02:00
|
|
|
const database = loadDb('empty');
|
2021-03-30 14:05:58 +02:00
|
|
|
const db = await loadPrivatePackages(database, logger);
|
2020-08-19 20:27:35 +02:00
|
|
|
|
|
|
|
expect(db.list).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
2021-03-30 14:05:58 +02:00
|
|
|
test('should fails on load private packages', async () => {
|
2020-08-19 20:27:35 +02:00
|
|
|
const database = loadDb('corrupted');
|
|
|
|
|
2021-03-30 14:05:58 +02:00
|
|
|
await expect(loadPrivatePackages(database, logger)).rejects.toThrow();
|
2020-08-19 20:27:35 +02:00
|
|
|
});
|
|
|
|
|
2021-03-30 14:05:58 +02:00
|
|
|
test('should handle null read values and return empty database', async () => {
|
|
|
|
const spy = jest.spyOn(readFile, 'readFilePromise');
|
|
|
|
spy.mockResolvedValue(null);
|
2020-08-19 20:27:35 +02:00
|
|
|
const database = loadDb('ok');
|
2021-03-30 14:05:58 +02:00
|
|
|
const db = await loadPrivatePackages(database, logger);
|
2020-08-19 20:27:35 +02:00
|
|
|
|
|
|
|
expect(db.list).toHaveLength(0);
|
|
|
|
|
|
|
|
spy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('find packages', () => {
|
|
|
|
test('should fails on wrong storage path', async () => {
|
|
|
|
try {
|
|
|
|
await findPackages(
|
|
|
|
'./no_such_folder_fake',
|
|
|
|
jest.fn(() => true)
|
|
|
|
);
|
2021-08-30 08:19:08 +02:00
|
|
|
} catch (e: any) {
|
2020-08-19 20:27:35 +02:00
|
|
|
expect(e.code).toEqual(noSuchFile);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fetch all packages from valid storage', async () => {
|
|
|
|
const storage = path.join(__dirname, '__fixtures__/findPackages');
|
|
|
|
const validator = jest.fn((file) => file.indexOf('.') === -1);
|
|
|
|
const pkgs = await findPackages(storage, validator);
|
|
|
|
// the result is 7 due number of packages on "findPackages" directory
|
|
|
|
expect(pkgs).toHaveLength(5);
|
|
|
|
expect(validator).toHaveBeenCalledTimes(8);
|
|
|
|
});
|
|
|
|
});
|
2021-03-30 14:05:58 +02:00
|
|
|
|
|
|
|
describe('dbGenPath', () => {
|
|
|
|
test('should generate a storage path', () => {
|
|
|
|
expect(
|
|
|
|
_dbGenPath('local.db', {
|
|
|
|
storage: './storage',
|
|
|
|
config_path: '/etc/foo/config.yaml',
|
|
|
|
})
|
|
|
|
).toMatch('local.db');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should verify with empty storage', () => {
|
|
|
|
expect(
|
|
|
|
_dbGenPath('local.db', {
|
|
|
|
storage: '',
|
|
|
|
config_path: '/etc/foo/config.yaml',
|
|
|
|
})
|
|
|
|
).toMatch('local.db');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should verify with undefined storage', () => {
|
|
|
|
expect(
|
|
|
|
_dbGenPath('local.db', {
|
|
|
|
storage: '',
|
|
|
|
config_path: '/etc/foo/config.yaml',
|
|
|
|
})
|
|
|
|
).toMatch('local.db');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should verify with config path is invalid', () => {
|
|
|
|
expect(
|
|
|
|
_dbGenPath('local.db', {
|
|
|
|
storage: './storage',
|
|
|
|
config_path: undefined,
|
|
|
|
})
|
|
|
|
).toMatch('local.db');
|
|
|
|
});
|
|
|
|
});
|
2020-08-19 20:27:35 +02:00
|
|
|
});
|