2020-08-19 20:27:35 +02:00
|
|
|
import fs from 'fs';
|
2021-10-29 17:33:05 +02:00
|
|
|
import path from 'path';
|
2021-09-08 19:06:37 +02:00
|
|
|
import { dirSync } from 'tmp-promise';
|
2020-08-19 20:27:35 +02:00
|
|
|
|
2021-10-03 14:21:01 +02:00
|
|
|
import { ILocalPackageManager, Logger, Package } from '@verdaccio/types';
|
2020-08-19 20:27:35 +02:00
|
|
|
|
2021-10-29 17:33:05 +02:00
|
|
|
import LocalDriver, { fSError, fileExist, noSuchFile, resourceNotAvailable } from '../src/local-fs';
|
2020-08-19 20:27:35 +02:00
|
|
|
import pkg from './__fixtures__/pkg';
|
|
|
|
|
|
|
|
let localTempStorage: string;
|
|
|
|
const pkgFileName = 'package.json';
|
|
|
|
|
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('Local FS test', () => {
|
2021-09-08 19:06:37 +02:00
|
|
|
let tmpFolder;
|
|
|
|
beforeAll(() => {
|
|
|
|
tmpFolder = dirSync({ unsafeCleanup: true });
|
|
|
|
localTempStorage = path.join(tmpFolder.name, './_storage');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
// tmpFolder.removeCallback();
|
|
|
|
});
|
|
|
|
|
2020-08-19 20:27:35 +02:00
|
|
|
describe('savePackage() group', () => {
|
|
|
|
test('savePackage()', (done) => {
|
|
|
|
const data = {};
|
|
|
|
const localFs = new LocalDriver(path.join(localTempStorage, 'first-package'), logger);
|
|
|
|
|
|
|
|
localFs.savePackage('pkg.1.0.0.tar.gz', data as Package, (err) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('readPackage() group', () => {
|
|
|
|
test('readPackage() success', (done) => {
|
2020-09-17 06:48:16 +02:00
|
|
|
const localFs: ILocalPackageManager = new LocalDriver(
|
|
|
|
path.join(__dirname, '__fixtures__/readme-test'),
|
|
|
|
logger
|
|
|
|
);
|
2020-08-19 20:27:35 +02:00
|
|
|
|
|
|
|
localFs.readPackage(pkgFileName, (err) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('readPackage() fails', (done) => {
|
2020-09-17 06:48:16 +02:00
|
|
|
const localFs: ILocalPackageManager = new LocalDriver(
|
|
|
|
path.join(__dirname, '__fixtures__/readme-testt'),
|
|
|
|
logger
|
|
|
|
);
|
2020-08-19 20:27:35 +02:00
|
|
|
|
|
|
|
localFs.readPackage(pkgFileName, (err) => {
|
|
|
|
expect(err).toBeTruthy();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('readPackage() fails corrupt', (done) => {
|
2020-09-17 06:48:16 +02:00
|
|
|
const localFs: ILocalPackageManager = new LocalDriver(
|
|
|
|
path.join(__dirname, '__fixtures__/readme-test-corrupt'),
|
|
|
|
logger
|
|
|
|
);
|
2020-08-19 20:27:35 +02:00
|
|
|
|
|
|
|
localFs.readPackage('corrupt.js', (err) => {
|
|
|
|
expect(err).toBeTruthy();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('createPackage() group', () => {
|
|
|
|
test('createPackage()', (done) => {
|
|
|
|
const localFs = new LocalDriver(path.join(localTempStorage, 'createPackage'), logger);
|
|
|
|
|
|
|
|
localFs.createPackage(path.join(localTempStorage, 'package5'), pkg, (err) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('createPackage() fails by fileExist', (done) => {
|
|
|
|
const localFs = new LocalDriver(path.join(localTempStorage, 'createPackage'), logger);
|
|
|
|
|
|
|
|
localFs.createPackage(path.join(localTempStorage, 'package5'), pkg, (err) => {
|
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(err.code).toBe(fileExist);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('deletePackage() group', () => {
|
2021-09-08 19:06:37 +02:00
|
|
|
test('deletePackage()', async () => {
|
2020-08-19 20:27:35 +02:00
|
|
|
const localFs = new LocalDriver(path.join(localTempStorage, 'createPackage'), logger);
|
|
|
|
|
|
|
|
// verdaccio removes the package.json instead the package name
|
2021-09-08 19:06:37 +02:00
|
|
|
await localFs.deletePackage('package.json');
|
2020-08-19 20:27:35 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removePackage() group', () => {
|
|
|
|
beforeEach(() => {
|
2021-05-02 09:52:12 +02:00
|
|
|
fs.mkdirSync(path.join(localTempStorage, '_toDelete'), { recursive: true });
|
2020-08-19 20:27:35 +02:00
|
|
|
});
|
|
|
|
|
2021-09-08 19:06:37 +02:00
|
|
|
test('should successfully remove the package', async () => {
|
2020-09-17 06:48:16 +02:00
|
|
|
const localFs: ILocalPackageManager = new LocalDriver(
|
|
|
|
path.join(localTempStorage, '_toDelete'),
|
|
|
|
logger
|
|
|
|
);
|
2021-09-08 19:06:37 +02:00
|
|
|
|
|
|
|
await expect(localFs.removePackage()).resolves.toBeUndefined();
|
2020-08-19 20:27:35 +02:00
|
|
|
});
|
|
|
|
|
2021-09-08 19:06:37 +02:00
|
|
|
test('removePackage() fails', async () => {
|
2020-09-17 06:48:16 +02:00
|
|
|
const localFs: ILocalPackageManager = new LocalDriver(
|
|
|
|
path.join(localTempStorage, '_toDelete_fake'),
|
|
|
|
logger
|
|
|
|
);
|
2021-09-08 19:06:37 +02:00
|
|
|
await expect(localFs.removePackage()).rejects.toThrow(/ENOENT/);
|
2020-08-19 20:27:35 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-09-08 19:06:37 +02:00
|
|
|
describe('readTarball', () => {
|
|
|
|
test('should read tarball successfully', (done) => {
|
2020-09-17 06:48:16 +02:00
|
|
|
const localFs: ILocalPackageManager = new LocalDriver(
|
|
|
|
path.join(__dirname, '__fixtures__/readme-test'),
|
|
|
|
logger
|
|
|
|
);
|
2020-08-19 20:27:35 +02:00
|
|
|
const readTarballStream = localFs.readTarball('test-readme-0.0.0.tgz');
|
|
|
|
|
|
|
|
readTarballStream.on('error', function (err) {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
readTarballStream.on('content-length', function (content) {
|
|
|
|
expect(content).toBe(352);
|
|
|
|
});
|
|
|
|
|
|
|
|
readTarballStream.on('end', function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
readTarballStream.on('data', function (data) {
|
|
|
|
expect(data).toBeDefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('readTarball() fails', (done) => {
|
2020-09-17 06:48:16 +02:00
|
|
|
const localFs: ILocalPackageManager = new LocalDriver(
|
|
|
|
path.join(__dirname, '__fixtures__/readme-test'),
|
|
|
|
logger
|
|
|
|
);
|
2020-08-19 20:27:35 +02:00
|
|
|
const readTarballStream = localFs.readTarball('file-does-not-exist-0.0.0.tgz');
|
|
|
|
|
|
|
|
readTarballStream.on('error', function (err) {
|
|
|
|
expect(err).toBeTruthy();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('updatePackage() group', () => {
|
|
|
|
const updateHandler = jest.fn((name, cb) => {
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
const onWrite = jest.fn((name, data, cb) => {
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
const transform = jest.fn();
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
jest.resetModules();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('updatePackage() success', (done) => {
|
|
|
|
jest.doMock('@verdaccio/file-locking', () => {
|
|
|
|
return {
|
|
|
|
readFile: (name, _options, cb): any => cb(null, { name }),
|
|
|
|
unlockFile: (_something, cb): any => cb(null),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const LocalDriver = require('../src/local-fs').default;
|
2020-09-17 06:48:16 +02:00
|
|
|
const localFs: ILocalPackageManager = new LocalDriver(
|
|
|
|
path.join(__dirname, '__fixtures__/update-package'),
|
|
|
|
logger
|
|
|
|
);
|
2020-08-19 20:27:35 +02:00
|
|
|
|
|
|
|
localFs.updatePackage('updatePackage', updateHandler, onWrite, transform, () => {
|
|
|
|
expect(transform).toHaveBeenCalledTimes(1);
|
|
|
|
expect(updateHandler).toHaveBeenCalledTimes(1);
|
|
|
|
expect(onWrite).toHaveBeenCalledTimes(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('updatePackage() failures handler', () => {
|
|
|
|
test('updatePackage() whether locking fails', (done) => {
|
|
|
|
jest.doMock('@verdaccio/file-locking', () => {
|
|
|
|
return {
|
|
|
|
readFile: (name, _options, cb): any => cb(Error('whateverError'), { name }),
|
|
|
|
unlockFile: (_something, cb): any => cb(null),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
require('../src/local-fs').default;
|
2020-09-17 06:48:16 +02:00
|
|
|
const localFs: ILocalPackageManager = new LocalDriver(
|
|
|
|
path.join(__dirname, '__fixtures__/update-package'),
|
|
|
|
logger
|
|
|
|
);
|
2020-08-19 20:27:35 +02:00
|
|
|
|
|
|
|
localFs.updatePackage('updatePackage', updateHandler, onWrite, transform, (err) => {
|
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(transform).toHaveBeenCalledTimes(0);
|
|
|
|
expect(updateHandler).toHaveBeenCalledTimes(0);
|
|
|
|
expect(onWrite).toHaveBeenCalledTimes(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('updatePackage() unlock a missing package', (done) => {
|
|
|
|
jest.doMock('@verdaccio/file-locking', () => {
|
|
|
|
return {
|
|
|
|
readFile: (name, _options, cb): any => cb(fSError(noSuchFile, 404), { name }),
|
|
|
|
unlockFile: (_something, cb): any => cb(null),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
const LocalDriver = require('../src/local-fs').default;
|
2020-09-17 06:48:16 +02:00
|
|
|
const localFs: ILocalPackageManager = new LocalDriver(
|
|
|
|
path.join(__dirname, '__fixtures__/update-package'),
|
|
|
|
logger
|
|
|
|
);
|
2020-08-19 20:27:35 +02:00
|
|
|
|
|
|
|
localFs.updatePackage('updatePackage', updateHandler, onWrite, transform, (err) => {
|
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(transform).toHaveBeenCalledTimes(0);
|
|
|
|
expect(updateHandler).toHaveBeenCalledTimes(0);
|
|
|
|
expect(onWrite).toHaveBeenCalledTimes(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('updatePackage() unlock a resource non available', (done) => {
|
|
|
|
jest.doMock('@verdaccio/file-locking', () => {
|
|
|
|
return {
|
|
|
|
readFile: (name, _options, cb): any => cb(fSError(resourceNotAvailable, 503), { name }),
|
|
|
|
unlockFile: (_something, cb): any => cb(null),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
const LocalDriver = require('../src/local-fs').default;
|
2020-09-17 06:48:16 +02:00
|
|
|
const localFs: ILocalPackageManager = new LocalDriver(
|
|
|
|
path.join(__dirname, '__fixtures__/update-package'),
|
|
|
|
logger
|
|
|
|
);
|
2020-08-19 20:27:35 +02:00
|
|
|
|
|
|
|
localFs.updatePackage('updatePackage', updateHandler, onWrite, transform, (err) => {
|
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(transform).toHaveBeenCalledTimes(0);
|
|
|
|
expect(updateHandler).toHaveBeenCalledTimes(0);
|
|
|
|
expect(onWrite).toHaveBeenCalledTimes(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('updatePackage() if updateHandler fails', (done) => {
|
|
|
|
jest.doMock('@verdaccio/file-locking', () => {
|
|
|
|
return {
|
|
|
|
readFile: (name, _options, cb): any => cb(null, { name }),
|
|
|
|
unlockFile: (_something, cb): any => cb(null),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const LocalDriver = require('../src/local-fs').default;
|
2020-09-17 06:48:16 +02:00
|
|
|
const localFs: ILocalPackageManager = new LocalDriver(
|
|
|
|
path.join(__dirname, '__fixtures__/update-package'),
|
|
|
|
logger
|
|
|
|
);
|
2020-08-19 20:27:35 +02:00
|
|
|
const updateHandler = jest.fn((_name, cb) => {
|
|
|
|
cb(fSError('something wrong', 500));
|
|
|
|
});
|
|
|
|
|
|
|
|
localFs.updatePackage('updatePackage', updateHandler, onWrite, transform, (err) => {
|
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(transform).toHaveBeenCalledTimes(0);
|
|
|
|
expect(updateHandler).toHaveBeenCalledTimes(1);
|
|
|
|
expect(onWrite).toHaveBeenCalledTimes(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|