0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

test(storage): add better test for changePackage

This commit is contained in:
Juan Picado @jotadeveloper 2018-07-29 10:28:28 +02:00
parent d1b46df423
commit 25c62f09ea
No known key found for this signature in database
GPG key ID: 18AC54485952D158
4 changed files with 88 additions and 20 deletions

View file

@ -315,35 +315,35 @@ class LocalStorage implements IStorage {
* Update the package metadata, tags and attachments (tarballs). * Update the package metadata, tags and attachments (tarballs).
* Note: Currently supports unpublishing only. * Note: Currently supports unpublishing only.
* @param {*} name * @param {*} name
* @param {*} pkg * @param {*} incomingPkg
* @param {*} revision * @param {*} revision
* @param {*} callback * @param {*} callback
* @return {Function} * @return {Function}
*/ */
changePackage(name: string, changePackage(name: string,
pkg: Package, incomingPkg: Package,
revision?: string, callback: Callback) { revision?: string, callback: Callback) {
if (!isObject(pkg.versions) || !isObject(pkg[DIST_TAGS])) { if (!isObject(incomingPkg.versions) || !isObject(incomingPkg[DIST_TAGS])) {
return callback( ErrorCode.getBadData()); return callback( ErrorCode.getBadData());
} }
this._updatePackage(name, (jsonData, cb) => { this._updatePackage(name, (localData, cb) => {
for (let ver in jsonData.versions) { for (let version in localData.versions) {
if (_.isNil(pkg.versions[ver])) { if (_.isNil(incomingPkg.versions[version])) {
this.logger.info( {name: name, version: ver}, 'unpublishing @{name}@@{version}'); this.logger.info( {name: name, version: version}, 'unpublishing @{name}@@{version}');
delete jsonData.versions[ver]; delete localData.versions[version];
delete jsonData.time[ver]; delete localData.time[version];
for (let file in jsonData._attachments) { for (let file in localData._attachments) {
if (jsonData._attachments[file].version === ver) { if (localData._attachments[file].version === version) {
delete jsonData._attachments[file].version; delete localData._attachments[file].version;
} }
} }
} }
} }
jsonData[DIST_TAGS] = pkg[DIST_TAGS]; localData[DIST_TAGS] = incomingPkg[DIST_TAGS];
cb(); cb();
}, function(err) { }, function(err) {
if (err) { if (err) {

30
test/lib/utils-test.js Normal file
View file

@ -0,0 +1,30 @@
// @flow
import type { Version } from "@verdaccio/types";
export function generateNewVersion(pkgName: string, version: string): Version {
// $FlowFixMe
return {
"name": pkgName,
"version": version,
"description": "",
"main": "index.js",
"dependencies": {
"test": "^1.4.1"
},
"author": "",
"license": "ISC",
"readme": "ERROR: No README data found!",
"_id": `${pkgName}@${version}`,
"_npmVersion": "5.5.1",
"_nodeVersion": "9.3.0",
"_npmUser": {
},
"dist": {
"integrity": "sha512-zVEqt1JUCOPsash9q4wMkJEDPD+QCx95TRhQII+JnoS31uBUKoZxhzvvUJCcLVy2CQG4QdwXARU7dYWPnrwhGg==",
"shasum": "b7088c30970489637f8b4e6795e8cf2b699d7569",
"tarball": `http:\/\/localhost:4873\/${pkgName}\/-\/npm_test-${version}.tgz`
}
}
}

View file

@ -8,6 +8,9 @@ import AppConfig from '../../../src/lib/config';
import configExample from '../partials/config/index'; import configExample from '../partials/config/index';
import Logger, {setup} from '../../../src/lib/logger'; import Logger, {setup} from '../../../src/lib/logger';
import {readFile} from '../../functional/lib/test.utils'; import {readFile} from '../../functional/lib/test.utils';
import {generatePackageTemplate} from '../../../src/lib/storage-utils';
import {generateNewVersion} from '../../lib/utils-test';
const readMetadata = (fileName: string = 'metadata') => readFile(`../../unit/partials/${fileName}`); const readMetadata = (fileName: string = 'metadata') => readFile(`../../unit/partials/${fileName}`);
@ -22,6 +25,29 @@ describe('LocalStorage', () => {
const pkgNameScoped = `@scope/${pkgName}-scope`; const pkgNameScoped = `@scope/${pkgName}-scope`;
const tarballName: string = `${pkgName}-add-tarball-1.0.4.tgz`; const tarballName: string = `${pkgName}-add-tarball-1.0.4.tgz`;
const tarballName2: string = `${pkgName}-add-tarball-1.0.5.tgz`; const tarballName2: string = `${pkgName}-add-tarball-1.0.5.tgz`;
const addNewVersion = (pkgName: string, version: string) => {
return new Promise((resolve) => {
storage.addVersion(pkgName, version, generateNewVersion(pkgName, version), '', (err, data) => {
resolve(data);
});
});
};
const addPackageToStore = (pkgName, metadata) => {
return new Promise((resolve, reject) => {
// $FlowFixMe
const pkgStoragePath = storage._getLocalStorage(pkgName);
rimRaf(pkgStoragePath.path, (err) => {
expect(err).toBeNull();
storage.addPackage(pkgName, metadata, async (err, data) => {
if (err) {
reject(err);
}
resolve(data);
});
});
});
};
beforeAll(function () { beforeAll(function () {
const config: Config = new AppConfig(configExample); const config: Config = new AppConfig(configExample);
@ -123,13 +149,25 @@ describe('LocalStorage', () => {
}); });
describe('LocalStorage::changePackage', () => { describe('LocalStorage::changePackage', () => {
test('should unpublish a version', (done) => { const pkgName: string = 'change-package';
const metadata = JSON.parse(readMetadata('metadata-unpublish'));
test('should unpublish a version', async done => {
await addPackageToStore(pkgName, generatePackageTemplate(pkgName));
await addNewVersion(pkgName, '1.0.1');
await addNewVersion(pkgName, '1.0.2');
await addNewVersion(pkgName, '1.0.3');
const metadata = JSON.parse(readMetadata('changePackage/metadata-change'));
const rev: string = metadata['_rev']; const rev: string = metadata['_rev'];
storage.changePackage(pkgName, metadata, rev, (err) => { storage.changePackage(pkgName, metadata, rev, (err) => {
expect(err).toBeUndefined(); expect(err).toBeUndefined();
done(); storage.getPackageMetadata(pkgName, (err, data ) => {
expect(err).toBeNull();
expect(data.versions['1.0.1']).toBeDefined();
expect(data.versions['1.0.2']).toBeUndefined();
expect(data.versions['1.0.3']).toBeUndefined();
done();
});
}); });
}); });
}); });

View file

@ -1,8 +1,8 @@
{ {
"name": "npm_test", "name": "change-package",
"versions": { "versions": {
"1.0.1": { "1.0.1": {
"name": "npm_test", "name": "change-package",
"version": "1.0.1", "version": "1.0.1",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
@ -20,7 +20,7 @@
], ],
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"_id": "npm_test@1.0.0", "_id": "change-package@1.0.0",
"_npmVersion": "5.5.1", "_npmVersion": "5.5.1",
"_nodeVersion": "9.3.0", "_nodeVersion": "9.3.0",
"_npmUser": { "_npmUser": {
@ -29,7 +29,7 @@
"dist": { "dist": {
"integrity": "sha512-tfzM1OFjWwg2d2Wke\/DV6icjeTZUVOZYLkbf8wmONRSAgMovL\/F+zyI24OhTtWyOXd1Kbj2YUMBvLpmpAjv8zg==", "integrity": "sha512-tfzM1OFjWwg2d2Wke\/DV6icjeTZUVOZYLkbf8wmONRSAgMovL\/F+zyI24OhTtWyOXd1Kbj2YUMBvLpmpAjv8zg==",
"shasum": "3e4e6bd5097b295e520b947c9be3259a9509a673", "shasum": "3e4e6bd5097b295e520b947c9be3259a9509a673",
"tarball": "http:\/\/localhost:4873\/npm_test\/-\/npm_test-1.0.1.tgz" "tarball": "http:\/\/localhost:4873\/change-package\/-\/change-package-1.0.1.tgz"
} }
} }
}, },