Skip to content

Commit

Permalink
Merge pull request #156 from forcedotcom/develop
Browse files Browse the repository at this point in the history
Release Merge
  • Loading branch information
tnoonan-salesforce authored Jul 16, 2019
2 parents 027b966 + 745291b commit 5e156c0
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/config/sandboxOrgConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { ConfigFile } from './configFile';

/**
* A config file that stores usernames for an org.
*/
export class SandboxOrgConfig extends ConfigFile<SandboxOrgConfig.Options> {
/**
* Gets the config options for a given org ID.
* @param orgId The orgId. Generally this org would have multiple users configured.
*/
public static getOptions(orgId: string): SandboxOrgConfig.Options {
return {
isGlobal: true,
isState: true,
filename: `${orgId}.sandbox.json`,
orgId
};
}

/**
* Constructor.
* **Do not directly construct instances of this class -- use {@link SandboxConfig.create} instead.**
* @param options The options for the class instance
* @ignore
*/
public constructor(options: SandboxOrgConfig.Options) {
super(options);
}
}

export namespace SandboxOrgConfig {
/**
* The config file options.
*/
export interface Options extends ConfigFile.Options {
/**
* The org id associated with this sandbox.
*/
orgId: string;
}

export enum Fields {
/**
* The username of the user who created the sandbox.
*/
PROD_ORG_USERNAME = 'prodOrgUsername'
}
}
41 changes: 41 additions & 0 deletions src/org.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { Config } from './config/config';
import { ConfigAggregator, ConfigInfo } from './config/configAggregator';
import { ConfigContents } from './config/configStore';
import { OrgUsersConfig } from './config/orgUsersConfig';
import { SandboxOrgConfig } from './config/sandboxOrgConfig';
import { Connection } from './connection';
import { Global } from './global';
import { Logger } from './logger';
Expand Down Expand Up @@ -162,6 +163,17 @@ export class Org extends AsyncCreatable<Org.Options> {
}

await aliases.write();

// Delete the sandbox org config file if it exists.
// This is an optional file so don't throw an error when the file doesn't exist.
const sandboxOrgConfig = await this.retrieveSandboxOrgConfig();
if (await sandboxOrgConfig.exists()) {
await this.manageDelete(
async () => await sandboxOrgConfig.unlink(),
sandboxOrgConfig.getPath(),
throwWhenRemoveFails
);
}
}

/**
Expand Down Expand Up @@ -397,6 +409,28 @@ export class Org extends AsyncCreatable<Org.Options> {
return this;
}

/**
* Sets the key/value pair in the sandbox config for this org. For convenience `this` object is returned.
*
*
* @param {key} The key for this value
* @param {value} The value to save
*/
public async setSandboxOrgConfigField(field: SandboxOrgConfig.Fields, value: string): Promise<Org> {
const sandboxOrgConfig = await this.retrieveSandboxOrgConfig();
sandboxOrgConfig.set(field, value);
await sandboxOrgConfig.write();
return this;
}

/**
* Returns an org config field. Returns undefined if the field is not set or invalid.
*/
public async getSandboxOrgConfigField(field: SandboxOrgConfig.Fields) {
const sandboxOrgConfig = await this.retrieveSandboxOrgConfig();
return sandboxOrgConfig.get(field);
}

/**
* Retrieves the highest api version that is supported by the target server instance. If the apiVersion configured for
* Sfdx is greater than the one returned in this call an api version mismatch occurs. In the case of the CLI that
Expand Down Expand Up @@ -489,6 +523,13 @@ export class Org extends AsyncCreatable<Org.Options> {
throw new SfdxError('Not Supported');
}

/**
* @ignore
*/
private async retrieveSandboxOrgConfig(): Promise<SandboxOrgConfig> {
return await SandboxOrgConfig.create(SandboxOrgConfig.getOptions(this.getOrgId()));
}

private manageDelete(cb: AnyFunction<Promise<void>>, dirPath: string, throwWhenRemoveFails: boolean): Promise<void> {
return cb().catch(e => {
if (throwWhenRemoveFails) {
Expand Down
48 changes: 48 additions & 0 deletions test/unit/orgTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Config } from '../../src/config/config';
import { ConfigAggregator } from '../../src/config/configAggregator';
import { ConfigFile } from '../../src/config/configFile';
import { OrgUsersConfig } from '../../src/config/orgUsersConfig';
import { SandboxOrgConfig } from '../../src/config/sandboxOrgConfig';
import { Connection } from '../../src/connection';
import { Global } from '../../src/global';
import { Org } from '../../src/org';
Expand Down Expand Up @@ -234,11 +235,19 @@ describe('Org Tests', () => {
return Promise.resolve({});
});

stubMethod($$.SANDBOX, ConfigFile.prototype, 'exists').callsFake(async () => {
return Promise.resolve(true);
});

await org.remove();

expect(deletedPaths).includes(
pathJoin(await $$.globalPathRetriever($$.id), Global.STATE_FOLDER, `${testData.orgId}.json`)
);

expect(deletedPaths).includes(
pathJoin(await $$.globalPathRetriever($$.id), Global.STATE_FOLDER, `${testData.orgId}.sandbox.json`)
);
});

it('should not fail when no scratch org has been written', async () => {
Expand Down Expand Up @@ -307,6 +316,34 @@ describe('Org Tests', () => {
alias = await Aliases.fetch('foo');
expect(alias).eq(undefined);
});

it('should not fail when no sandboxOrgConfig', async () => {
const org: Org = await Org.create({
connection: await Connection.create({
authInfo: await AuthInfo.create({ username: testData.username })
})
});

const deletedPaths: string[] = [];
stubMethod($$.SANDBOX, ConfigFile.prototype, 'unlink').callsFake(function(this: ConfigFile<ConfigFile.Options>) {
deletedPaths.push(this.getPath());
return Promise.resolve({});
});

stubMethod($$.SANDBOX, fs, 'remove').callsFake(() => {
return Promise.resolve({});
});

await org.remove();

expect(deletedPaths).includes(
pathJoin(await $$.globalPathRetriever($$.id), Global.STATE_FOLDER, `${testData.orgId}.json`)
);

expect(deletedPaths).not.includes(
pathJoin(await $$.globalPathRetriever($$.id), Global.STATE_FOLDER, `${testData.orgId}.sandbox.json`)
);
});
});

describe('with multiple scratch org users', () => {
Expand Down Expand Up @@ -670,4 +707,15 @@ describe('Org Tests', () => {
expect(org.isDevHubOrg()).to.be.true;
});
});

describe('sandbox org config', () => {
it('set field', async () => {
const org: Org = await Org.create({ aliasOrUsername: testData.username });
expect(await org.getSandboxOrgConfigField(SandboxOrgConfig.Fields.PROD_ORG_USERNAME)).to.be.undefined;

await org.setSandboxOrgConfigField(SandboxOrgConfig.Fields.PROD_ORG_USERNAME, 'user@sandbox.org');

expect(await org.getSandboxOrgConfigField(SandboxOrgConfig.Fields.PROD_ORG_USERNAME)).to.eq('user@sandbox.org');
});
});
});

0 comments on commit 5e156c0

Please sign in to comment.