From 71d09af32a9f837ee54d75a9c88de151db154abf Mon Sep 17 00:00:00 2001 From: Jeffry Hesse <5544326+DarthHater@users.noreply.github.com> Date: Tue, 7 Sep 2021 10:29:50 -0800 Subject: [PATCH 1/2] Make these optional items return undefined if they do not exist --- src/Config/OssIndexServerConfig.ts | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Config/OssIndexServerConfig.ts b/src/Config/OssIndexServerConfig.ts index 8304e22d..960522cc 100644 --- a/src/Config/OssIndexServerConfig.ts +++ b/src/Config/OssIndexServerConfig.ts @@ -27,16 +27,25 @@ export class OssIndexServerConfig extends Config { } } - public getUsername(): string { - return this.username; + public getUsername(): string | undefined { + if (this.username != '') { + return this.username; + } + return undefined; } - public getToken(): string { - return this.token; + public getToken(): string | undefined { + if (this.token != '') { + return this.token; + } + return undefined; } - public getCacheLocation(): string { - return this.cacheLocation; + public getCacheLocation(): string | undefined { + if (this.cacheLocation != '') { + return this.cacheLocation; + } + return undefined; } public async clearCache(): Promise { @@ -53,9 +62,13 @@ export class OssIndexServerConfig extends Config { public getConfigFromFile(saveLocation: string = this.getConfigLocation()): OssIndexServerConfig { const doc = safeLoad(readFileSync(saveLocation, 'utf8')) as OssIndexServerConfigOnDisk; - if (doc && doc.Username && doc.Token && doc.CacheLocation) { + if (doc && doc.Username) { this.username = doc.Username; + } + if (doc && doc.Token) { this.token = doc.Token; + } + if (doc && doc.CacheLocation) { this.cacheLocation = doc.CacheLocation; } From 925ac2e37b1ce31985f19f762ee8971d0c9f0fd8 Mon Sep 17 00:00:00 2001 From: Dan Rollo Date: Tue, 7 Sep 2021 16:21:13 -0400 Subject: [PATCH 2/2] test undefined when property does not exist --- src/Config/OssIndexServerConfig.spec.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Config/OssIndexServerConfig.spec.ts b/src/Config/OssIndexServerConfig.spec.ts index d3829fe8..b65c83d6 100644 --- a/src/Config/OssIndexServerConfig.spec.ts +++ b/src/Config/OssIndexServerConfig.spec.ts @@ -38,4 +38,18 @@ describe('OssIndexServerConfig', async () => { mock.restore(); sinon.restore(); }); + + it('should return undefined when property does not exist', async () => { + sinon.stub(os, 'homedir').returns('/nonsense'); + mock({ '/nonsense': {} }); + + const conf = new OssIndexServerConfig(); + + expect(conf.getUsername()).to.equal(undefined); + expect(conf.getToken()).to.equal(undefined); + expect(conf.getCacheLocation()).to.equal(undefined); + + mock.restore(); + sinon.restore(); + }); });