Skip to content

Commit

Permalink
fix: Make caching class return undefined if property does not exist (#…
Browse files Browse the repository at this point in the history
…247)

* Make these optional items return undefined if they do not exist
  • Loading branch information
DarthHater authored Sep 7, 2021
1 parent 9f8b646 commit 8e3b3ad
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
14 changes: 14 additions & 0 deletions src/Config/OssIndexServerConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
27 changes: 20 additions & 7 deletions src/Config/OssIndexServerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
Expand All @@ -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;
}

Expand Down

0 comments on commit 8e3b3ad

Please sign in to comment.