From 5a20da27966a1732f831b63afb2c6fc63a345213 Mon Sep 17 00:00:00 2001 From: Arunprasad Rajkumar Date: Tue, 9 Feb 2021 14:27:11 +0530 Subject: [PATCH] fix: handle empty cases for all manifests (#184) Fixes fabric8-analytics/fabric8-analytics-vscode-extension#493 --- src/collector/package.json.ts | 2 +- test/collector/go.mod.test.ts | 7 +++++++ test/collector/package.json.test.ts | 5 +++++ test/collector/requirements.txt.test.ts | 5 +++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/collector/package.json.ts b/src/collector/package.json.ts index d436938b..87640c34 100644 --- a/src/collector/package.json.ts +++ b/src/collector/package.json.ts @@ -7,7 +7,7 @@ export class DependencyCollector implements IDependencyCollector { constructor(public classes: Array = ["dependencies"]) {} async collect(contents: string): Promise> { - const ast = jsonAst(contents); + const ast = jsonAst(contents || '{}'); return ast.children. filter(c => this.classes.includes(c.key.value)). flatMap(c => c.value.children). diff --git a/test/collector/go.mod.test.ts b/test/collector/go.mod.test.ts index ee2f371b..ec51ac26 100644 --- a/test/collector/go.mod.test.ts +++ b/test/collector/go.mod.test.ts @@ -72,6 +72,13 @@ github.com/stretchr/testify`); }); }); + it('tests empty go.mod', async () => { + fake(getGoLangImportsCmd(), `github.com/alecthomas/units +github.com/stretchr/testify`); + const deps = await collector.collect(``); + expect(deps).is.eql([]); + }); + it('tests empty lines in go.mod', async () => { fake(getGoLangImportsCmd(), `github.com/alecthomas/units github.com/stretchr/testify`); diff --git a/test/collector/package.json.test.ts b/test/collector/package.json.test.ts index ab674366..104a109b 100644 --- a/test/collector/package.json.test.ts +++ b/test/collector/package.json.test.ts @@ -4,6 +4,11 @@ import { DependencyCollector } from '../../src/collector/package.json'; describe('npm package.json parser test', () => { const collector = new DependencyCollector(); + it('tests empty package.json file content', async () => { + const deps = await collector.collect(``); + expect(deps.length).equal(0); + }); + it('tests empty package.json', async () => { const deps = await collector.collect(` {} diff --git a/test/collector/requirements.txt.test.ts b/test/collector/requirements.txt.test.ts index 228fa32a..61705653 100644 --- a/test/collector/requirements.txt.test.ts +++ b/test/collector/requirements.txt.test.ts @@ -45,6 +45,11 @@ describe('PyPi requirements.txt parser test', () => { }]); }); + it('tests empty requirements.txt', async () => { + const deps = await collector.collect(``); + expect(deps).is.eql([]); + }); + it('tests empty lines', async () => { const deps = await collector.collect(`