From 95686be47d5feabd3e35a7ce1ec291a82ebfbff1 Mon Sep 17 00:00:00 2001 From: BuffayBu Date: Tue, 29 Jan 2019 11:54:47 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20don=E2=80=99t=20packages=20that=20ha?= =?UTF-8?q?ve=20no=20uplinks=20after=20reading=20(#1204)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/storage.js | 4 +++- test/unit/api/store.spec.js | 28 ++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/lib/storage.js b/src/lib/storage.js index e13ac4290..98ae440f9 100644 --- a/src/lib/storage.js +++ b/src/lib/storage.js @@ -487,7 +487,9 @@ class Storage implements IStorageHandler { , null , upLinksErrors ); } - + if (upLinks.length === 0) { + return callback(null, packageInfo); + } self.localStorage.updateVersions(name, packageInfo, function(err, packageJsonLocal: Package) { if (err) { return callback(err); diff --git a/test/unit/api/store.spec.js b/test/unit/api/store.spec.js index 02c884e0f..b4dee0bca 100644 --- a/test/unit/api/store.spec.js +++ b/test/unit/api/store.spec.js @@ -2,6 +2,8 @@ import _ from 'lodash'; import path from 'path'; +import fs from 'fs'; +import rimraf from 'rimraf'; // $FlowFixMe import configExample from '../partials/config/index'; import AppConfig from '../../../src/lib/config'; @@ -16,12 +18,12 @@ import {DOMAIN_SERVERS} from '../../functional/config.functional'; setup(configExample.logs); +const storagePath = path.join(__dirname, '../partials/store/test-storage-store.spec'); const mockServerPort: number = 55548; const generateStorage = async function(port = mockServerPort, configDefault = configExample) { const storageConfig = _.clone(configDefault); - const storage = path.join(__dirname, '../partials/store/test-storage-store.spec'); storageConfig.self_path = __dirname; - storageConfig.storage = storage; + storageConfig.storage = storagePath; storageConfig.uplinks = { npmjs: { url: `http://${DOMAIN_SERVERS}:${port}` @@ -37,8 +39,11 @@ const generateStorage = async function(port = mockServerPort, configDefault = co describe('StorageTest', () => { let mockRegistry; - beforeAll(async () => { - mockRegistry = await mockServer(mockServerPort).init(); + beforeAll(done => { + rimraf(storagePath, async () => { + mockRegistry = await mockServer(mockServerPort).init(); + done() + }) }); afterAll(function(done) { @@ -90,5 +95,20 @@ describe('StorageTest', () => { done(); }); }); + + test('should not touch if the package exists and has no uplinks', async (done) => { + const storage: IStorageHandler = await generateStorage(); + const metadataPath = path.join(storagePath, 'npm_test/package.json') + fs.mkdirSync(path.join(storagePath, 'npm_test')) + fs.copyFileSync(path.join(__dirname, '../partials/metadata'), metadataPath) + const metadata = JSON.parse(fs.readFileSync(metadataPath).toString()) + const prevStat = fs.statSync(metadataPath) + storage._syncUplinksMetadata('npm_test', metadata, {}, (err) => { + expect(err).toBeFalsy() + const nextStat = fs.statSync(metadataPath) + expect(nextStat).toEqual(prevStat) + done() + }) + }) }); }); From 35f0b539d96e8e193b8ba7562fd5b280e55155ff Mon Sep 17 00:00:00 2001 From: BuffayBu Date: Tue, 29 Jan 2019 14:03:04 +0800 Subject: [PATCH 2/4] test: use node v6 compatible fs api --- test/unit/api/store.spec.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/api/store.spec.js b/test/unit/api/store.spec.js index b4dee0bca..0eed92228 100644 --- a/test/unit/api/store.spec.js +++ b/test/unit/api/store.spec.js @@ -98,9 +98,10 @@ describe('StorageTest', () => { test('should not touch if the package exists and has no uplinks', async (done) => { const storage: IStorageHandler = await generateStorage(); + const metadataSource = path.join(__dirname, '../partials/metadata') const metadataPath = path.join(storagePath, 'npm_test/package.json') fs.mkdirSync(path.join(storagePath, 'npm_test')) - fs.copyFileSync(path.join(__dirname, '../partials/metadata'), metadataPath) + fs.writeFileSync(metadataPath, fs.readFileSync(metadataSource)); const metadata = JSON.parse(fs.readFileSync(metadataPath).toString()) const prevStat = fs.statSync(metadataPath) storage._syncUplinksMetadata('npm_test', metadata, {}, (err) => { From 933034f3c92542db77de5b5cfceaf156cd03409e Mon Sep 17 00:00:00 2001 From: BuffayBu Date: Wed, 30 Jan 2019 11:19:54 +0800 Subject: [PATCH 3/4] test(storage): replace the hacky test --- test/unit/api/store.spec.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/unit/api/store.spec.js b/test/unit/api/store.spec.js index 0eed92228..ee93fc40b 100644 --- a/test/unit/api/store.spec.js +++ b/test/unit/api/store.spec.js @@ -103,11 +103,11 @@ describe('StorageTest', () => { fs.mkdirSync(path.join(storagePath, 'npm_test')) fs.writeFileSync(metadataPath, fs.readFileSync(metadataSource)); const metadata = JSON.parse(fs.readFileSync(metadataPath).toString()) - const prevStat = fs.statSync(metadataPath) + // $FlowFixMe + storage.localStorage.updateVersions = jest.fn(storage.localStorage.updateVersions) storage._syncUplinksMetadata('npm_test', metadata, {}, (err) => { - expect(err).toBeFalsy() - const nextStat = fs.statSync(metadataPath) - expect(nextStat).toEqual(prevStat) + expect(err).toBeNull() + expect(storage.localStorage.updateVersions).not.toHaveBeenCalled() done() }) }) From 09ac72990c259305eef2658829e1d01a65e4ac0b Mon Sep 17 00:00:00 2001 From: BuffayBu Date: Wed, 30 Jan 2019 11:21:36 +0800 Subject: [PATCH 4/4] test(storage): add semicolons --- test/unit/api/store.spec.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/unit/api/store.spec.js b/test/unit/api/store.spec.js index ee93fc40b..9ea50ff48 100644 --- a/test/unit/api/store.spec.js +++ b/test/unit/api/store.spec.js @@ -98,18 +98,18 @@ describe('StorageTest', () => { test('should not touch if the package exists and has no uplinks', async (done) => { const storage: IStorageHandler = await generateStorage(); - const metadataSource = path.join(__dirname, '../partials/metadata') - const metadataPath = path.join(storagePath, 'npm_test/package.json') - fs.mkdirSync(path.join(storagePath, 'npm_test')) + const metadataSource = path.join(__dirname, '../partials/metadata'); + const metadataPath = path.join(storagePath, 'npm_test/package.json'); + fs.mkdirSync(path.join(storagePath, 'npm_test')); fs.writeFileSync(metadataPath, fs.readFileSync(metadataSource)); - const metadata = JSON.parse(fs.readFileSync(metadataPath).toString()) + const metadata = JSON.parse(fs.readFileSync(metadataPath).toString()); // $FlowFixMe - storage.localStorage.updateVersions = jest.fn(storage.localStorage.updateVersions) + storage.localStorage.updateVersions = jest.fn(storage.localStorage.updateVersions); storage._syncUplinksMetadata('npm_test', metadata, {}, (err) => { - expect(err).toBeNull() - expect(storage.localStorage.updateVersions).not.toHaveBeenCalled() - done() - }) - }) + expect(err).toBeNull(); + expect(storage.localStorage.updateVersions).not.toHaveBeenCalled(); + done(); + }); + }); }); });