Skip to content

Commit

Permalink
fix: move throwOnNotFound option to ConfigFile
Browse files Browse the repository at this point in the history
  • Loading branch information
tnoonan-salesforce committed Dec 13, 2018
1 parent f11b84a commit 924352a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
12 changes: 8 additions & 4 deletions src/authInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,10 @@ export class AuthInfo extends AsyncCreatable<AuthInfo.Options> {

this.logger.debug(dataToSave);

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

Expand Down Expand Up @@ -631,7 +632,10 @@ export class AuthInfo extends AsyncCreatable<AuthInfo.Options> {
} else {
// Fetch from the persisted auth file
try {
const config: AuthInfoConfig = await AuthInfoConfig.create(AuthInfoConfig.getOptions(username));
const config: AuthInfoConfig = await AuthInfoConfig.create({
...AuthInfoConfig.getOptions(username),
throwOnNotFound: true
});
authConfig = config.toObject();
} catch (e) {
if (e.code === 'ENOENT') {
Expand Down
26 changes: 2 additions & 24 deletions src/config/authInfoConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,16 @@ import { ConfigFile } from './configFile';
* const authInfo = await AuthInfoConfig.create(AuthInfoConfig.getOptions(username));
* ```
*/
export class AuthInfoConfig extends ConfigFile<AuthInfoConfig.Options> {
export class AuthInfoConfig extends ConfigFile<ConfigFile.Options> {
/**
* Gets the config options for a given org ID.
* @param username The username for the org.
*/
public static getOptions(username: string): AuthInfoConfig.Options {
public static getOptions(username: string): ConfigFile.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;
}
}
11 changes: 9 additions & 2 deletions src/config/configFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,11 @@ export class ConfigFile<T extends ConfigFile.Options> extends BaseConfigStore<T>

/**
* Used to initialize asynchronous components.
*
* **Throws** *{@link SfdxError}{ name: 'NamedOrgNotFound' }* If the username.json file is not found when
* options.throwOnNotFound is true.
*/
protected async init(throwOnNotFound?: boolean): Promise<void> {
protected async init(): Promise<void> {
let defaultOptions = {};
try {
defaultOptions = ConfigFile.getDefaultOptions();
Expand Down Expand Up @@ -211,7 +214,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(throwOnNotFound);
await this.read(this.options.throwOnNotFound);
}
}

Expand Down Expand Up @@ -240,5 +243,9 @@ export namespace ConfigFile {
* The full file path where the config file is stored.
*/
filePath?: string;
/**
* Indicates if init should throw if the corresponding config file is not found.
*/
throwOnNotFound?: boolean;
}
}
15 changes: 6 additions & 9 deletions test/unit/authInfoConfigTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const $$ = testSetup();

describe('AuthInfoConfigTest', () => {
const username = '[email protected]';
let options: AuthInfoConfig.Options;
let options: ConfigFile.Options;
beforeEach(() => {
$$.SANDBOXES.CONFIG.restore();
stubMethod($$.SANDBOX, ConfigFile.prototype, 'write').callsFake(async () => {
Expand All @@ -28,7 +28,7 @@ describe('AuthInfoConfigTest', () => {
$$.SANDBOX.restore();
});

it ('init should throw', async () => {
it('init should throw', async () => {
options.throwOnNotFound = true;
try {
await shouldThrow(AuthInfoConfig.create(options));
Expand All @@ -37,15 +37,12 @@ describe('AuthInfoConfigTest', () => {
}
});

it ('init should throw on undefined', async () => {
try {
await shouldThrow(AuthInfoConfig.create(options));
} catch (e) {
expect(e).to.have.property('code', 'ENOENT');
}
it('init should throw on undefined', async () => {
const authInfoConfig = await AuthInfoConfig.create(options);
expect(authInfoConfig.getPath()).to.include('[email protected]');
});

it ('init shouldn\'t throw', async () => {
it("init shouldn't throw", async () => {
options.throwOnNotFound = false;
const authInfoConfig = await AuthInfoConfig.create(options);
expect(authInfoConfig.getPath()).to.include('[email protected]');
Expand Down

0 comments on commit 924352a

Please sign in to comment.