1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-08 23:25:51 +01:00

chore: migrate plugin local-storage

This commit is contained in:
Juan Picado 2024-08-04 20:04:12 +02:00
parent ee80d20f5c
commit a8afaa32e1
7 changed files with 136 additions and 139 deletions

@ -1,10 +0,0 @@
const config = require('../../../jest/config');
module.exports = Object.assign({}, config, {
coverageThreshold: {
global: {
// FIXME: increase to 90
lines: 51,
},
},
});

@ -58,7 +58,7 @@
},
"scripts": {
"clean": "rimraf ./build",
"test": "jest --runInBand",
"test": "vitest run",
"type-check": "tsc --noEmit -p tsconfig.build.json",
"build:types": "tsc --emitDeclarationOnly -p tsconfig.build.json",
"build:js": "babel src/ --out-dir build/ --copy-files --extensions \".ts,.tsx\" --source-maps",

@ -1,4 +1,5 @@
import { join } from 'path';
import { describe, expect, test } from 'vitest';
import { getFolders, searchOnStorage } from '../src/dir-utils';

@ -1,22 +1,23 @@
/* eslint-disable jest/no-mocks-import */
import path from 'path';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { fileUtils, pluginUtils } from '@verdaccio/core';
import { logger, setup } from '@verdaccio/logger';
import LocalDatabase, { ERROR_DB_LOCKED } from '../src/local-database';
const mockWrite = jest.fn(() => Promise.resolve());
const mockmkdir = jest.fn(() => Promise.resolve());
const mockRead = jest.fn(() => Promise.resolve());
const mockWrite = vi.fn(() => Promise.resolve());
const mockmkdir = vi.fn(() => Promise.resolve());
const mockRead = vi.fn(() => Promise.resolve());
jest.mock('../src/fs', () => ({
vi.mock('../src/fs', () => ({
mkdirPromise: () => mockRead(),
readFilePromise: () => mockmkdir(),
writeFilePromise: () => mockWrite(),
}));
setup();
setup({});
const optionsPlugin: pluginUtils.PluginOptions = {
logger,
@ -31,7 +32,6 @@ describe('Local Database', () => {
beforeEach(async () => {
tmpFolder = await fileUtils.createTempFolder('local-storage-plugin-');
const tempFolder = path.join(tmpFolder, 'verdaccio-test.yaml');
// @ts-expect-error
locaDatabase = new LocalDatabase(
// @ts-expect-error
{
@ -46,8 +46,8 @@ describe('Local Database', () => {
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
vi.resetAllMocks();
vi.clearAllMocks();
});
test('should create an instance', () => {

@ -1,10 +1,11 @@
import fs from 'fs';
import path from 'path';
import { Readable } from 'stream';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { fileUtils } from '@verdaccio/core';
import { createTempFolder } from '@verdaccio/test-helper';
import { ILocalPackageManager, Logger, Manifest } from '@verdaccio/types';
import { Logger, Manifest } from '@verdaccio/types';
import LocalDriver from '../src/local-fs';
import pkg from './__fixtures__/pkg';
@ -21,15 +22,17 @@ function checkFileExists(filepath) {
}
const logger: Logger = {
error: jest.fn(),
info: jest.fn(),
debug: jest.fn(),
child: jest.fn(),
warn: jest.fn(),
http: jest.fn(),
trace: jest.fn(),
error: vi.fn(),
info: vi.fn(),
debug: vi.fn(),
child: vi.fn(),
warn: vi.fn(),
http: vi.fn(),
trace: vi.fn(),
};
vi.setConfig({ testTimeout: 20000 });
describe('Local FS test', () => {
let tmpFolder;
beforeEach(async () => {
@ -40,7 +43,7 @@ describe('Local FS test', () => {
describe.skip('deletePackage() group', () => {
test('should delete a package', async () => {
const localFs = new LocalDriver(path.join(localTempStorage, 'createPackage'), logger);
await localFs.createPackag('createPackage', pkg as unknown as Manifest);
await localFs.createPackage('createPackage', pkg as unknown as Manifest);
// verdaccio removes the package.json instead the package name
await localFs.deletePackage('package.json');
// verify if the `package.json` does not exist anymore
@ -60,119 +63,119 @@ describe('Local FS test', () => {
});
test('should successfully remove the package', async () => {
const localFs: ILocalPackageManager = new LocalDriver(
path.join(localTempStorage, '_toDelete'),
logger
);
const localFs = new LocalDriver(path.join(localTempStorage, '_toDelete'), logger);
await expect(localFs.removePackage()).resolves.toBeUndefined();
});
test('removePackage() fails', async () => {
const localFs: ILocalPackageManager = new LocalDriver(
path.join(localTempStorage, '_toDelete_fake'),
logger
);
const localFs = new LocalDriver(path.join(localTempStorage, '_toDelete_fake'), logger);
await expect(localFs.removePackage()).rejects.toThrow(/ENOENT/);
});
});
describe('writeTarballNext', () => {
test('should write a tarball', (done) => {
const abort = new AbortController();
const tmp = createTempFolder('local-fs-write-tarball');
const localFs = new LocalDriver(tmp, logger);
const readableStream = Readable.from('foooo');
// TODO: verify file exist
localFs.writeTarball('juan-1.0.0.tgz', { signal: abort.signal }).then((stream) => {
stream.on('finish', () => {
done();
test('should write a tarball', () =>
new Promise((done) => {
const abort = new AbortController();
const tmp = createTempFolder('local-fs-write-tarball');
const localFs = new LocalDriver(tmp, logger);
const readableStream = Readable.from('foooo');
// TODO: verify file exist
localFs.writeTarball('juan-1.0.0.tgz', { signal: abort.signal }).then((stream) => {
stream.on('finish', () => {
done(true);
});
readableStream.pipe(stream);
});
readableStream.pipe(stream);
});
});
}));
});
describe('writeTarballNextNoFolder', () => {
test('should write a tarball even if folder does not exist', (done) => {
const abort = new AbortController();
const tmp = path.join(localTempStorage, 'local-fs-write-tarball-new-folder');
const localFs = new LocalDriver(tmp, logger);
const readableStream = Readable.from('foooo');
localFs.writeTarball('juan-1.0.0.tgz', { signal: abort.signal }).then((stream) => {
stream.on('finish', () => {
done();
test('should write a tarball even if folder does not exist', () =>
new Promise((done) => {
const abort = new AbortController();
const tmp = path.join(localTempStorage, 'local-fs-write-tarball-new-folder');
const localFs = new LocalDriver(tmp, logger);
const readableStream = Readable.from('foooo');
localFs.writeTarball('juan-1.0.0.tgz', { signal: abort.signal }).then((stream) => {
stream.on('finish', () => {
done(true);
});
readableStream.pipe(stream);
});
readableStream.pipe(stream);
});
});
}));
});
describe('readTarballNext', () => {
test('should read a tarball', (done) => {
const abort = new AbortController();
const localFs = new LocalDriver(
path.join(__dirname, '__fixtures__/readme-test-next'),
logger
);
localFs.readTarball('test-readme-0.0.1.tgz', { signal: abort.signal }).then((stream) => {
stream.on('data', (data) => {
expect(data.length).toEqual(352);
test('should read a tarball', () =>
new Promise((done) => {
const abort = new AbortController();
const localFs = new LocalDriver(
path.join(__dirname, '__fixtures__/readme-test-next'),
logger
);
localFs.readTarball('test-readme-0.0.1.tgz', { signal: abort.signal }).then((stream) => {
stream.on('data', (data) => {
expect(data.length).toEqual(352);
});
stream.on('end', () => {
done(true);
});
});
stream.on('end', () => {
done();
});
});
});
}));
test('should abort read a tarball', (done) => {
const abort = new AbortController();
const localFs = new LocalDriver(
path.join(__dirname, '__fixtures__/readme-test-next'),
logger
);
localFs.readTarball('test-readme-0.0.3.tgz', { signal: abort.signal }).then((stream) => {
stream.on('error', (error: any) => {
// FIXME: might be different results sometimes, need research
// expect(error.code).toEqual('ABORT_ERR');
expect(error).toBeDefined();
done();
test('should abort read a tarball', () =>
new Promise((done) => {
const abort = new AbortController();
const localFs = new LocalDriver(
path.join(__dirname, '__fixtures__/readme-test-next'),
logger
);
localFs.readTarball('test-readme-0.0.3.tgz', { signal: abort.signal }).then((stream) => {
stream.on('error', (error: any) => {
// FIXME: might be different results sometimes, need research
// expect(error.code).toEqual('ABORT_ERR');
expect(error).toBeDefined();
done(true);
});
abort.abort();
});
abort.abort();
});
});
}));
test('fails on read a tarball doex not exist', (done) => {
const abort = new AbortController();
test('fails on read a tarball doex not exist', () =>
new Promise((done) => {
const abort = new AbortController();
const localFs = new LocalDriver(
path.join(__dirname, '__fixtures__/readme-test-next'),
logger
);
localFs.readTarball('does-not-exist-0.0.0.tgz', { signal: abort.signal }).then((stream) => {
stream.on('error', (error: any) => {
expect(error.code).toEqual('ENOENT');
done();
const localFs = new LocalDriver(
path.join(__dirname, '__fixtures__/readme-test-next'),
logger
);
localFs.readTarball('does-not-exist-0.0.0.tgz', { signal: abort.signal }).then((stream) => {
stream.on('error', (error: any) => {
expect(error.code).toEqual('ENOENT');
done(true);
});
});
});
});
}));
test('should return content-length', (done) => {
const localFs = new LocalDriver(
path.join(__dirname, '__fixtures__/readme-test-next'),
logger
);
const abort = new AbortController();
localFs.readTarball('test-readme-0.0.0.tgz', { signal: abort.signal }).then((stream) => {
stream.on('data', (data) => {
expect(data.length).toEqual(352);
});
test('should return content-length', () =>
new Promise((done) => {
const localFs = new LocalDriver(
path.join(__dirname, '__fixtures__/readme-test-next'),
logger
);
const abort = new AbortController();
localFs.readTarball('test-readme-0.0.0.tgz', { signal: abort.signal }).then((stream) => {
stream.on('data', (data) => {
expect(data.length).toEqual(352);
});
stream.on('content-length', (content) => {
expect(content).toEqual(352);
done();
stream.on('content-length', (content) => {
expect(content).toEqual(352);
done(true);
});
});
});
}, 100000);
}));
});
});

@ -1,6 +1,7 @@
/* eslint-disable jest/no-mocks-import */
import fs from 'fs';
import path from 'path';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { fileUtils } from '@verdaccio/core';
import { Logger, Token } from '@verdaccio/types';
@ -8,13 +9,13 @@ import { Logger, Token } from '@verdaccio/types';
import LocalDatabase from '../src/local-database';
const logger: Logger = {
error: jest.fn(),
info: jest.fn(),
debug: jest.fn(),
child: jest.fn(),
warn: jest.fn(),
http: jest.fn(),
trace: jest.fn(),
error: vi.fn(),
info: vi.fn(),
debug: vi.fn(),
child: vi.fn(),
warn: vi.fn(),
http: vi.fn(),
trace: vi.fn(),
};
describe('Local Database', () => {
@ -23,7 +24,7 @@ describe('Local Database', () => {
beforeEach(async () => {
tmpFolder = await fileUtils.createTempFolder('local-storage-plugin-');
const tempFolder = path.join(tmpFolder, 'verdaccio-test.yaml');
const writeMock = jest.spyOn(fs, 'writeFileSync').mockImplementation();
const writeMock = vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {});
locaDatabase = new LocalDatabase( // @ts-expect-error
{
storage: 'storage',
@ -38,7 +39,7 @@ describe('Local Database', () => {
});
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});
describe('token', () => {

@ -1,4 +1,5 @@
import path from 'path';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { Logger } from '@verdaccio/types';
@ -8,20 +9,20 @@ import { loadPrivatePackages } from '../src/pkg-utils';
import { _dbGenPath, findPackages } from '../src/utils';
const logger: Logger = {
error: jest.fn(),
info: jest.fn(),
debug: jest.fn(),
child: jest.fn(),
warn: jest.fn(),
http: jest.fn(),
trace: jest.fn(),
error: vi.fn(),
info: vi.fn(),
debug: vi.fn(),
child: vi.fn(),
warn: vi.fn(),
http: vi.fn(),
trace: vi.fn(),
};
describe('Utitlies', () => {
const loadDb = (name): string => path.join(__dirname, '__fixtures__/databases', `${name}.json`);
beforeEach(() => {
jest.resetModules();
vi.resetModules();
});
test('should load private packages', async () => {
@ -45,7 +46,7 @@ describe('Utitlies', () => {
});
test('should handle null read values and return empty database', async () => {
const spy = jest.spyOn(readFile, 'readFilePromise');
const spy = vi.spyOn(readFile, 'readFilePromise');
spy.mockResolvedValue(null);
const database = loadDb('ok');
const db = await loadPrivatePackages(database, logger);
@ -60,7 +61,7 @@ describe('Utitlies', () => {
try {
await findPackages(
'./no_such_folder_fake',
jest.fn(() => true)
vi.fn(() => true)
);
} catch (e: any) {
expect(e.code).toEqual(noSuchFile);
@ -69,7 +70,7 @@ describe('Utitlies', () => {
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 validator = vi.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);
@ -82,7 +83,7 @@ describe('Utitlies', () => {
expect(
_dbGenPath('local.db', {
storage: './storage',
config_path: '/etc/foo/config.yaml',
configPath: '/etc/foo/config.yaml',
})
).toMatch('local.db');
});
@ -91,7 +92,7 @@ describe('Utitlies', () => {
expect(
_dbGenPath('local.db', {
storage: '',
config_path: '/etc/foo/config.yaml',
configPath: '/etc/foo/config.yaml',
})
).toMatch('local.db');
});
@ -100,7 +101,7 @@ describe('Utitlies', () => {
expect(
_dbGenPath('local.db', {
storage: '',
config_path: '/etc/foo/config.yaml',
configPath: '/etc/foo/config.yaml',
})
).toMatch('local.db');
});
@ -109,7 +110,8 @@ describe('Utitlies', () => {
expect(
_dbGenPath('local.db', {
storage: './storage',
config_path: undefined,
// @ts-expect-error
configPath: undefined,
})
).toMatch('local.db');
});