From 23bcf6fb86e514f0d8f02b8c6b6a96f9d4d015a0 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Fri, 25 Aug 2017 22:06:37 +0200 Subject: [PATCH] test: Increase coverage for local-data storage --- package.json | 1 + test/unit/local-data.spec.js | 60 +++++++++++++++++++++++++++++++----- yarn.lock | 20 +++++++++++- 3 files changed, 72 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 5d4341a95..e545532a3 100644 --- a/package.json +++ b/package.json @@ -78,6 +78,7 @@ "file-loader": "0.11.2", "flow-runtime": "0.13.0", "friendly-errors-webpack-plugin": "1.6.1", + "fs-extra": "4.0.1", "github-markdown-css": "2.8.0", "html-webpack-plugin": "2.29.0", "in-publish": "2.0.0", diff --git a/test/unit/local-data.spec.js b/test/unit/local-data.spec.js index 0627889f6..9346a62c5 100644 --- a/test/unit/local-data.spec.js +++ b/test/unit/local-data.spec.js @@ -4,34 +4,78 @@ const assert = require('assert'); const LocalData = require('../../src/lib/storage/local/local-data'); const path = require('path'); const _ = require('lodash'); +const fs = require('fs-extra'); describe('Local Database', function() { + const buildCorruptedPath = () => path.join(__dirname, './partials/storage/verdaccio-corrupted.db.json'); + const buildValidDbPath = () => path.join(__dirname, './partials/storage/verdaccio.db.json'); describe('reading database', () => { - const buildCorruptedPath = () => path.join(__dirname, './partials/storage/verdaccio-corrupted.db.json'); - it('should return empty database on read corrupted database', () => { - const config = new LocalData(buildCorruptedPath()); - assert(_.isEmpty(config.data.list)); + const dataLocal = new LocalData(buildCorruptedPath()); + + assert(_.isEmpty(dataLocal.data.list)); }); it('should return a database on read valid database', () => { - const config = new LocalData(path.join(__dirname, './partials/storage/verdaccio.db.json')); - assert(_.isEmpty(config.data.list) === false); + const dataLocal = new LocalData(buildValidDbPath()); + + assert(_.isEmpty(dataLocal.data.list) === false); }); it('should fails on sync a corrupted database', () => { - const config = new LocalData(buildCorruptedPath()); - const error = config.sync(); + const dataLocal = new LocalData(buildCorruptedPath()); + const error = dataLocal.sync(); assert(_.isError(error)); assert(error.message.match(/locked/)); + assert(dataLocal.locked); }); }); + describe('add/remove packages to database', () => { + it('should add a new package to local database', () => { + const dataLocal = new LocalData(buildCorruptedPath()); + assert(_.isEmpty(dataLocal.data.list)); + dataLocal.add('package1'); + assert(!_.isEmpty(dataLocal.data.list)); + }); + + it('should remove a new package to local database', () => { + const dataLocal = new LocalData(buildCorruptedPath()); + const pkgName = 'package1'; + + assert(_.isEmpty(dataLocal.data.list)); + dataLocal.add(pkgName); + dataLocal.remove(pkgName); + assert(_.isEmpty(dataLocal.data.list)); + }); + }); + + describe('sync packages to database', () => { + beforeEach(function() { + this.newDb = path.join(__dirname, './test-storage/verdaccio.temp.db.json'); + fs.copySync(buildValidDbPath(), this.newDb); + }); + + it('should check sync packages', function() { + const localData1 = new LocalData(this.newDb); + + localData1.add('package1'); + + const localData2 = new LocalData(this.newDb); + + assert(_.isEmpty(localData2.data.list) === false); + assert(localData2.data.list.length === 2); + + }); + + + }); + }); diff --git a/yarn.lock b/yarn.lock index 6116f1627..e42bcef1b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2908,6 +2908,14 @@ fs-access@^1.0.0: dependencies: null-check "^1.0.0" +fs-extra@4.0.1: + version "4.0.1" + resolved "http://localhost:5555/fs-extra/-/fs-extra-4.0.1.tgz#7fc0c6c8957f983f57f306a24e5b9ddd8d0dd880" + dependencies: + graceful-fs "^4.1.2" + jsonfile "^3.0.0" + universalify "^0.1.0" + fs-readdir-recursive@^1.0.0: version "1.0.0" resolved "https://registry.npmjs.org/fs-readdir-recursive/-/fs-readdir-recursive-1.0.0.tgz#8cd1745c8b4f8a29c8caec392476921ba195f560" @@ -3136,7 +3144,7 @@ globule@^1.0.0: lodash "~4.17.4" minimatch "~3.0.2" -graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4: +graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4, graceful-fs@^4.1.6: version "4.1.11" resolved "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658" @@ -3849,6 +3857,12 @@ json5@^0.5.0, json5@^0.5.1: version "0.5.1" resolved "https://registry.npmjs.org/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821" +jsonfile@^3.0.0: + version "3.0.1" + resolved "http://localhost:5555/jsonfile/-/jsonfile-3.0.1.tgz#a5ecc6f65f53f662c4415c7675a0331d0992ec66" + optionalDependencies: + graceful-fs "^4.1.6" + jsonfilter@^1.1.2: version "1.1.2" resolved "https://registry.npmjs.org/jsonfilter/-/jsonfilter-1.1.2.tgz#21ef7cedc75193813c75932e96a98be205ba5a11" @@ -6654,6 +6668,10 @@ uniqs@^2.0.0: version "2.0.0" resolved "https://registry.npmjs.org/uniqs/-/uniqs-2.0.0.tgz#ffede4b36b25290696e6e165d4a59edb998e6b02" +universalify@^0.1.0: + version "0.1.1" + resolved "http://localhost:5555/universalify/-/universalify-0.1.1.tgz#fa71badd4437af4c148841e3b3b165f9e9e590b7" + unix-crypt-td-js@^1.0.0: version "1.0.0" resolved "https://registry.npmjs.org/unix-crypt-td-js/-/unix-crypt-td-js-1.0.0.tgz#1c0824150481bc7a01d49e98f1ec668d82412f3b"