Skip to content

Commit

Permalink
fix: issue on package not found
Browse files Browse the repository at this point in the history
  • Loading branch information
@jotadeveloper authored and sergiohgz committed Jul 31, 2019
1 parent 4d7848d commit 944e1a5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 279 deletions.
1 change: 0 additions & 1 deletion plugins/memory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"cross-env": "5.1.3",
"eslint": "4.16.0",
"eslint-config-google": "0.9.1",
"express": "4.16.3",
"eslint-plugin-flowtype": "2.42.0",
"eslint-config-prettier": "2.9.0",
"eslint-plugin-jest": "21.7.0",
Expand Down
9 changes: 5 additions & 4 deletions plugins/memory/src/memory-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { HttpError } from 'http-errors';
import type { StorageList, Package, Callback, Logger } from '@verdaccio/types';
import type { ILocalPackageManager } from '@verdaccio/local-storage';

const noSuchFile: string = 'ENOENT';
export const noSuchFile: string = 'ENOENT';
export const fileExist: string = 'EEXISTS';

const fSError = function(message: string, code: number = 404): HttpError {
Expand All @@ -22,7 +22,7 @@ const fSError = function(message: string, code: number = 404): HttpError {
};

const noPackageFoundError = function(message = 'no such package') {
const err = new Error(message);
const err: HttpError = createError(404, message);
// $FlowFixMe
err.code = noSuchFile;
return err;
Expand Down Expand Up @@ -90,11 +90,12 @@ class MemoryHandler implements ILocalPackageManager {

readPackage(name: string, cb: Function): void {
const json = this._getStorage(name);
const isJson = typeof json === 'undefined';

try {
cb(typeof json === 'undefined' ? noPackageFoundError() : null, JSON.parse(json));
cb(isJson ? noPackageFoundError() : null, JSON.parse(json));
} catch (err) {
cb(fSError('package not found', 404));
cb(noPackageFoundError());
}
}

Expand Down
46 changes: 31 additions & 15 deletions plugins/memory/test/memory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import LocalMemory from '../src/index';
import config from './partials/config';
import pkgExample from './partials/pkg';
import MemoryHandler from '../src/memory-handler';
import MemoryHandler, { noSuchFile } from '../src/memory-handler';

import type { Logger } from '@verdaccio/types';
import type { ILocalData, IPackageStorage } from '@verdaccio/local-storage';
Expand All @@ -21,29 +21,29 @@ const logger: Logger = {
describe('memory unit test .', () => {
describe('LocalMemory', () => {
test('should create an LocalMemory instance', () => {
const localMemory: ILocalData = new LocalMemory(config, logger);
const localMemory: ILocalData = new LocalMemory(config, { logger });

expect(localMemory).toBeDefined();
});

test('should create add a package', () => {
const localMemory: ILocalData = new LocalMemory(config, logger);
const localMemory: ILocalData = new LocalMemory(config, { logger });
localMemory.add('test');

expect(localMemory.get()).toHaveLength(1);
});

test('should reach max limit', () => {
config.limit = 2;
const localMemory: ILocalData = new LocalMemory(config, logger);
const localMemory: ILocalData = new LocalMemory(config, { logger });
expect(localMemory.add('test1')).toBeUndefined();
expect(localMemory.add('test2')).toBeUndefined();
expect(localMemory.add('test3')).not.toBeNull();
});

test('should remove a package', () => {
const pkgName: string = 'test';
const localMemory: ILocalData = new LocalMemory(config, logger);
const localMemory: ILocalData = new LocalMemory(config, { logger });
localMemory.add(pkgName);
localMemory.remove(pkgName);

Expand All @@ -53,13 +53,13 @@ describe('memory unit test .', () => {

describe('MemoryHandler', () => {
test('should create an MemoryHandler instance', () => {
const memoryHandler: ILocalPackageManager = new MemoryHandler('test', pkgExample, logger);
const memoryHandler: ILocalPackageManager = new MemoryHandler('test', pkgExample, { logger });

expect(memoryHandler).toBeDefined();
});

test('should save a package', done => {
const localMemory: ILocalData = new LocalMemory(config, logger);
const localMemory: ILocalData = new LocalMemory(config, { logger });
const pkgName: string = 'test';

const handler = localMemory.getPackageStorage(pkgName);
Expand All @@ -77,8 +77,24 @@ describe('memory unit test .', () => {
}
});

test('should fails on read a package', done => {
const localMemory: ILocalData = new LocalMemory(config, { logger });
const pkgName: string = 'test';

const handler = localMemory.getPackageStorage(pkgName);
expect(handler).toBeDefined();

if (handler) {
handler.readPackage(pkgName, (err, data) => {
expect(err).not.toBeNull();
expect(err.code).toBe(noSuchFile);
done();
});
}
});

test('should update a package', done => {
const localMemory: ILocalData = new LocalMemory(config, logger);
const localMemory: ILocalData = new LocalMemory(config, { logger });
const pkgName: string = 'test';

const handler = localMemory.getPackageStorage(pkgName);
Expand Down Expand Up @@ -115,7 +131,7 @@ describe('memory unit test .', () => {
});

test('should write a tarball', done => {
const localMemory: ILocalData = new LocalMemory(config, logger);
const localMemory: ILocalData = new LocalMemory(config, { logger });
const pkgName: string = 'test';
const dataTarball: string = '12345';

Expand All @@ -139,7 +155,7 @@ describe('memory unit test .', () => {
});

test('should read a tarball', done => {
const localMemory: ILocalData = new LocalMemory(config, logger);
const localMemory: ILocalData = new LocalMemory(config, { logger });
const pkgName: string = 'test.tar.gz';
const dataTarball: string = '12345';

Expand All @@ -163,7 +179,7 @@ describe('memory unit test .', () => {
});

test('should abort read a tarball', done => {
const localMemory: ILocalData = new LocalMemory(config, logger);
const localMemory: ILocalData = new LocalMemory(config, { logger });
const pkgName: string = 'test2.tar.gz';
const dataTarball: string = '12345';

Expand Down Expand Up @@ -191,7 +207,7 @@ describe('memory unit test .', () => {
});

test('should fails read a tarball not found', done => {
const localMemory: ILocalData = new LocalMemory(config, logger);
const localMemory: ILocalData = new LocalMemory(config, { logger });
const pkgName: string = 'test2.tar.gz';
const handler = localMemory.getPackageStorage(pkgName);

Expand All @@ -206,7 +222,7 @@ describe('memory unit test .', () => {
});

test('should abort while write a tarball', done => {
const localMemory: ILocalData = new LocalMemory(config, logger);
const localMemory: ILocalData = new LocalMemory(config, { logger });
const pkgName: string = 'test-abort.tar.gz';
const dataTarball: string = '12345';

Expand All @@ -228,7 +244,7 @@ describe('memory unit test .', () => {
});

test('should delete a package', done => {
const localMemory: ILocalData = new LocalMemory(config, logger);
const localMemory: ILocalData = new LocalMemory(config, { logger });
const pkgName: string = 'test2';

const handler: IPackageStorage = localMemory.getPackageStorage(pkgName);
Expand All @@ -240,7 +256,7 @@ describe('memory unit test .', () => {
expect(err).toBeNull();
handler.readPackage(pkgName, (err, data) => {
expect(err).not.toBeNull();
expect(err.message).toMatch(/package not found/);
expect(err.message).toMatch(/no such package/);
done();
});
});
Expand Down
Loading

0 comments on commit 944e1a5

Please sign in to comment.