Skip to content

Commit

Permalink
fix: ensure AuthInfoConfig can throw when config file not found
Browse files Browse the repository at this point in the history
  • Loading branch information
tnoonan-salesforce committed Dec 13, 2018
1 parent 2524d35 commit f11b84a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/authInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,9 @@ export class AuthInfo extends AsyncCreatable<AuthInfo.Options> {

this.logger.debug(dataToSave);

const config = await AuthInfoConfig.create(AuthInfoConfig.getOptions(username));
const options = AuthInfoConfig.getOptions(username);
options.throwOnNotFound = false;
const config = await AuthInfoConfig.create(options);
config.setContentsFromObject(dataToSave);
await config.write();

Expand Down
26 changes: 24 additions & 2 deletions src/config/authInfoConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,38 @@ import { ConfigFile } from './configFile';
* const authInfo = await AuthInfoConfig.create(AuthInfoConfig.getOptions(username));
* ```
*/
export class AuthInfoConfig extends ConfigFile<ConfigFile.Options> {
export class AuthInfoConfig extends ConfigFile<AuthInfoConfig.Options> {
/**
* Gets the config options for a given org ID.
* @param username The username for the org.
*/
public static getOptions(username: string): ConfigFile.Options {
public static getOptions(username: string): AuthInfoConfig.Options {
return {
isGlobal: true, // Only allow global auth files
isState: true,
filename: `${username}.json`
};
}

/**
* Init method.
*
* **Throws** *{@link SfdxError}{ name: 'NamedOrgNotFound' }* If the username.json file is not found when
* options.throwOnNotFound is true or undefined.
*/
public async init(): Promise<void> {
return super.init(this.options.throwOnNotFound === undefined ? true : this.options.throwOnNotFound);
}
}

export namespace AuthInfoConfig {
/**
* Options for the AuthInfoConfig.
*/
export interface Options extends ConfigFile.Options {
/**
* Indicates if init should throw if the corresponding config file is not found.
*/
throwOnNotFound?: boolean;
}
}
5 changes: 2 additions & 3 deletions src/config/configFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class ConfigFile<T extends ConfigFile.Options> extends BaseConfigStore<T>
/**
* Used to initialize asynchronous components.
*/
protected async init(): Promise<void> {
protected async init(throwOnNotFound?: boolean): Promise<void> {
let defaultOptions = {};
try {
defaultOptions = ConfigFile.getDefaultOptions();
Expand Down Expand Up @@ -211,8 +211,7 @@ export class ConfigFile<T extends ConfigFile.Options> extends BaseConfigStore<T>
}

this.path = pathJoin(configRootFolder, this.options.filePath ? this.options.filePath : '', this.options.filename);

await this.read();
await this.read(throwOnNotFound);
}
}

Expand Down
53 changes: 53 additions & 0 deletions test/unit/authInfoConfigTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { expect } from 'chai';

import { set } from '@salesforce/kit';
import { stubMethod } from '@salesforce/ts-sinon';

import { AuthInfoConfig } from '../../src/config/authInfoConfig';
import { ConfigFile } from '../../src/config/configFile';
import { SfdxError } from '../../src/sfdxError';
import { shouldThrow, testSetup } from '../../src/testSetup';

// Setup the test environment.
const $$ = testSetup();

describe('AuthInfoConfigTest', () => {
const username = 'foo@example.com';
let options: AuthInfoConfig.Options;
beforeEach(() => {
$$.SANDBOXES.CONFIG.restore();
stubMethod($$.SANDBOX, ConfigFile.prototype, 'write').callsFake(async () => {
const error = new SfdxError('Test error', 'testError');
set(error, 'code', 'ENOENT');
return Promise.reject(error);
});
options = AuthInfoConfig.getOptions(username);
});

afterEach(() => {
$$.SANDBOX.restore();
});

it ('init should throw', async () => {
options.throwOnNotFound = true;
try {
await shouldThrow(AuthInfoConfig.create(options));
} catch (e) {
expect(e).to.have.property('code', 'ENOENT');
}
});

it ('init should throw on undefined', async () => {
try {
await shouldThrow(AuthInfoConfig.create(options));
} catch (e) {
expect(e).to.have.property('code', 'ENOENT');
}
});

it ('init shouldn\'t throw', async () => {
options.throwOnNotFound = false;
const authInfoConfig = await AuthInfoConfig.create(options);
expect(authInfoConfig.getPath()).to.include('foo@example.com');
});
});

0 comments on commit f11b84a

Please sign in to comment.