Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make caching class return undefined if property does not exist #247

Merged
merged 2 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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