Skip to content

Commit

Permalink
fix: allow packageAliases in sfdx-project.json
Browse files Browse the repository at this point in the history
  • Loading branch information
amphro committed Apr 2, 2019
1 parent 8f9dee4 commit 8d7b1fe
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/sfdxProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import { sfdc } from './util/sfdc';
* **See** [force:project:create](https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_ws_create_new.htm)
*/
export class SfdxProjectJson extends ConfigFile<ConfigFile.Options> {
public static BLACKLIST = ['packageAliases'];

public static getFileName() {
return SFDX_PROJECT_JSON;
}
Expand All @@ -51,13 +53,23 @@ export class SfdxProjectJson extends ConfigFile<ConfigFile.Options> {
const contents = await super.read();

// Verify that the configObject does not have upper case keys; throw if it does. Must be heads down camel case.
const upperCaseKey = sfdc.findUpperCaseKeys(this.toObject());
const upperCaseKey = sfdc.findUpperCaseKeys(this.toObject(), SfdxProjectJson.BLACKLIST);
if (upperCaseKey) {
throw SfdxError.create('@salesforce/core', 'core', 'InvalidJsonCasing', [upperCaseKey, this.getPath()]);
}
return contents;
}

public async write(newContents?: ConfigContents): Promise<ConfigContents> {
// Verify that the configObject does not have upper case keys; throw if it does. Must be heads down camel case.
const upperCaseKey = sfdc.findUpperCaseKeys(newContents, SfdxProjectJson.BLACKLIST);
if (upperCaseKey) {
throw SfdxError.create('@salesforce/core', 'core', 'InvalidJsonCasing', [upperCaseKey, this.getPath()]);
}

return super.write(newContents);
}

public getDefaultOptions(options?: ConfigFile.Options): ConfigFile.Options {
const defaultOptions: ConfigFile.Options = {
isState: false
Expand Down
4 changes: 2 additions & 2 deletions src/testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export interface ConfigStub {
* A function that controls all aspects of {@link ConfigFile.write}. For example, it won't read the contents unless
* explicitly done. Only use this if you know what you are doing. Use updateContents instead.
*/
writeFn?: () => Promise<void>;
writeFn?: (contents: AnyJson) => Promise<void>;
/**
* The contents that are used when @{link ConfigFile.read} unless retrieveContents is set. This will also contain the
* new config when @{link ConfigFile.write} is called. This will persist through config instances,
Expand Down Expand Up @@ -291,7 +291,7 @@ const _testSetup = (sinon?: any) => {
if (request === `${this.instanceUrl}/services/data`) {
return Promise.resolve([{ version: '42.0' }]);
}
return testContext.fakeConnectionRequest.call(this, request, options);
return testContext.fakeConnectionRequest.call(this, request, options as AnyJson);
});

// Always start with the default and tests beforeEach or it methods can override it.
Expand Down
6 changes: 5 additions & 1 deletion src/util/sfdc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,17 @@ export const sfdc = {
* Returns the first key within the object that has an upper case first letter.
*
* @param data The object in which to check key casing.
* @param sectionBlacklist properties in the object to exclude from the search. e.g. a blacklist of `["a"]` and data of `{ "a": { "B" : "b"}}` would ignore `B` because it is in the object value under `a`.
*/
findUpperCaseKeys: (data?: JsonMap): Optional<string> => {
findUpperCaseKeys: (data?: JsonMap, sectionBlacklist: string[] = []): Optional<string> => {
let key: Optional<string>;
findKey(data, (val: AnyJson, k: string) => {
if (k[0] === k[0].toUpperCase()) {
key = k;
} else if (isJsonMap(val)) {
if (sectionBlacklist.includes(k)) {
return key;
}
key = sfdc.findUpperCaseKeys(asJsonMap(val));
}
return key;
Expand Down
14 changes: 14 additions & 0 deletions test/unit/projectTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ describe('SfdxProject', async () => {
beforeEach(async () => {
projectPath = await $$.localPathRetriever($$.id);
});

describe('json', async () => {
it('allows uppercase packaging aliases on write', async () => {
const json = await SfdxProjectJson.create({});
await json.write({ packageAliases: { MyName: 'somePackage' } });
expect($$.getConfigStubContents('SfdxProjectJson').packageAliases['MyName']).to.equal('somePackage');
});
it('allows uppercase packaging aliases on read', async () => {
$$.setConfigStubContents('SfdxProjectJson', { contents: { packageAliases: { MyName: 'somePackage' } } });
const json = await SfdxProjectJson.create({});
expect(json.get('packageAliases')['MyName']).to.equal('somePackage');
});
});

describe('resolve', () => {
it('with working directory', async () => {
$$.SANDBOX.stub(internal, 'resolveProjectPath').callsFake(() => projectPath);
Expand Down
9 changes: 9 additions & 0 deletions test/unit/util/sfdcTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,14 @@ describe('util/sfdc', () => {
};
expect(sfdc.findUpperCaseKeys(testObj)).to.be.undefined;
});

it('should return the first nested upper case key unless blacklisted', () => {
const testObj = {
lowercase: true,
uppercase: false,
nested: { NestedUpperCase: true }
};
expect(sfdc.findUpperCaseKeys(testObj, ['nested'])).to.equal(undefined);
});
});
});

0 comments on commit 8d7b1fe

Please sign in to comment.