Skip to content

Commit

Permalink
fix: add initial NUTs, u-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Feb 19, 2021
1 parent 74a3cdd commit ca2076c
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 69 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dependencies": {
"@oclif/config": "^1",
"@salesforce/command": "^3.1.0",
"@salesforce/core": "^2.18.3",
"@salesforce/core": "^2.19.1",
"@salesforce/source-deploy-retrieve": "^1.1.17",
"chalk": "^4.1.0",
"tslib": "^2"
Expand All @@ -16,10 +16,10 @@
"@oclif/dev-cli": "^1",
"@oclif/plugin-command-snapshot": "^2.0.0",
"@salesforce/dev-config": "^2.1.0",
"@salesforce/cli-plugins-testkit": "^0.0.4",
"@salesforce/cli-plugins-testkit": "^0.0.8",
"@salesforce/dev-scripts": "^0.7.0",
"@salesforce/plugin-command-reference": "^1.3.0",
"@salesforce/prettier-config": "^0.0.1",
"@salesforce/prettier-config": "^0.0.2",
"@salesforce/ts-sinon": "1.3.0",
"@typescript-eslint/eslint-plugin": "^4.2.0",
"@typescript-eslint/parser": "^4.2.0",
Expand Down
6 changes: 3 additions & 3 deletions src/commands/force/source/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { flags, FlagsConfig } from '@salesforce/command';
import { Lifecycle, Messages } from '@salesforce/core';
import { SourceDeployResult } from '@salesforce/source-deploy-retrieve';
import { Duration } from '@salesforce/kit';
import { asString } from '@salesforce/ts-types';
import { asString, asArray } from '@salesforce/ts-types';
import * as chalk from 'chalk';
import { SourceCommand } from '../../../sourceCommand';

Expand Down Expand Up @@ -97,9 +97,9 @@ export class deploy extends SourceCommand {
const hookEmitter = Lifecycle.getInstance();

const cs = await this.createComponentSet({
sourcepath: this.flags.sourcepath as string[],
sourcepath: asArray<string>(this.flags.sourcepath),
manifest: asString(this.flags.manifest),
metadata: this.flags.metadata as string[],
metadata: asArray<string>(this.flags.metadata),
});

await hookEmitter.emit('predeploy', { packageXmlPath: cs.getPackageXml() });
Expand Down
8 changes: 4 additions & 4 deletions src/commands/force/source/retrieve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { flags, FlagsConfig } from '@salesforce/command';
import { Lifecycle, Messages, SfdxError, SfdxProjectJson } from '@salesforce/core';
import { SourceRetrieveResult } from '@salesforce/source-deploy-retrieve';
import { Duration } from '@salesforce/kit';
import { asString } from '@salesforce/ts-types';
import { asArray, asString } from '@salesforce/ts-types';
import { blue, yellow } from 'chalk';
import { SourceCommand } from '../../../sourceCommand';

Expand Down Expand Up @@ -60,10 +60,10 @@ export class retrieve extends SourceCommand {

const cs = await this.createComponentSet({
// safe to cast from the flags as an array of strings
packagenames: this.flags.packagenames as string[],
sourcepath: this.flags.sourcepath as string[],
packagenames: asArray<string>(this.flags.packagenames),
sourcepath: asArray<string>(this.flags.sourcepath),
manifest: asString(this.flags.manifest),
metadata: this.flags.metadata as string[],
metadata: asArray<string>(this.flags.metadata),
});

// emit pre retrieve event
Expand Down
7 changes: 1 addition & 6 deletions src/sourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ export abstract class SourceCommand extends SfdxCommand {
if (options.sourcepath) {
options.sourcepath.forEach((filepath) => {
if (fs.fileExistsSync(filepath)) {
this.logger.debug(`Creating ComponentSet from sourcepath ${path.resolve(filepath)}`);
setAggregator.push(...ComponentSet.fromSource(path.resolve(filepath)));
} else {
throw SfdxError.create('@salesforce/plugin-source', 'sourceCommand', 'SourcePathInvalid', [filepath]);
throw new SfdxError(`The sourcepath "${filepath}" is not a valid source file path.`);
}
});
}
Expand All @@ -48,8 +47,6 @@ export abstract class SourceCommand extends SfdxCommand {
}

if (options.manifest) {
this.logger.debug(`Creating ComponentSet from manifest ${path.resolve(options.manifest)}`);

setAggregator.push(
...(await ComponentSet.fromManifestFile(options.manifest, {
// to create a link to the actual source component we need to have it resolve through all packages
Expand All @@ -68,8 +65,6 @@ export abstract class SourceCommand extends SfdxCommand {
// either -m ApexClass or -m ApexClass:MyApexClass
fullName: splitEntry.length === 1 ? '*' : splitEntry[1],
};
this.logger.debug(`Creating ComponentSet from metadata member ${metadata.type}:${metadata.fullName}`);

const cs = new ComponentSet([metadata]);
// we need to search the entire project for the matching metadata component
// no better way than to have it search than process.cwd()
Expand Down
81 changes: 81 additions & 0 deletions test/commands/source/deploy.nut.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { TestSession, execCmd } from '@salesforce/cli-plugins-testkit';

let session: TestSession;

describe('source:deploy NUTs', () => {
before(() => {
session = TestSession.create({
project: {
gitClone: 'https://github.com/amphro/simple-mpd-project.git',
},
// create org and push source to get something to deploy
setupCommands: [
'sfdx force:org:create -d 1 -s -f config/project-scratch-def.json',
'sfdx force:source:push',
'sfdx force:source:convert --packagename force-app --outputdir FORCE-APP',
'sfdx force:source:convert --packagename my-app --outputdir MY-APP',
],
});
});

it('deploys via sourcepath', () => {
execCmd('force:source:deploy --sourcepath "force-app" --json', { ensureExitCode: 0 });
// TODO: once json is fixed add json validation
// const output =
// expect(output.jsonOutput)
// .to.have.property('result')
// .with.keys(['username', 'accessToken', 'id', 'orgId', 'profileName', 'loginUrl', 'instanceUrl']);
// const result = (output.jsonOutput as Record<string, unknown>).result as Record<string, string>;
// expect(result.orgId).to.have.length(18);
// expect(result.id).to.have.length(18);
// expect(result.accessToken.startsWith(result.orgId.substr(0, 15))).to.be.true;
});

it('deploys via sourcepath with multiple', () => {
execCmd('force:source:deploy --sourcepath "force-app, my-app" --json', { ensureExitCode: 0 });
});

// it('deploys via package name with spaces', () => {
// execCmd('force:source:deploy --packagenames "my app" --json', { ensureExitCode: 0 });
// });
//
// it('deploys via package names', () => {
// execCmd('force:source:deploy --packagenames "my app, force-app" --json', { ensureExitCode: 0 });
// });

it('deploys via metadata', () => {
execCmd('force:source:deploy --metadata ApexClass --json', { ensureExitCode: 0 });
});

it('deploys via metadata', () => {
execCmd('force:source:deploy --metadata ApexClass:MyTest --json', { ensureExitCode: 0 });
});

it('deploys via metadata mixed param types', () => {
execCmd('force:source:deploy --metadata ApexClass:MyTest,CustomField --json', { ensureExitCode: 0 });
});

it('deploys via manifest', () => {
execCmd('force:source:deploy --manifest "FORCE-APP/package.xml" --json', { ensureExitCode: 0 });
});

it('deploys via multiple manifests', () => {
execCmd('force:source:deploy --manifest "FORCE-APP/package.xml,MY-APP/package.xml" --json', {
ensureExitCode: 0,
});
});

it('deploys via undefined manifest, should fail', () => {
execCmd('force:source:deploy --manifest "doesnotexist.xml --json', { ensureExitCode: 1 });
});

after(async () => {
await session.clean();
});
});
45 changes: 22 additions & 23 deletions test/commands/source/retrieve.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { execCmd } from '@salesforce/cli-plugins-testkit';
// import { TestSession, execCmd } from '@salesforce/cli-plugins-testkit';
import { TestSession, execCmd } from '@salesforce/cli-plugins-testkit';

// let session: TestSession;
let session: TestSession;

describe('source:retrieve NUTs', () => {
before(() => {
// session = TestSession.create({
// project: {
// gitClone: 'https://github.com/trailheadapps/ebikes-lwc.git',
// },
// // create org and push source to get something to retrieve
// setupCommands: [
// 'sfdx force:org:create -d 1 -s -f config/project-scratch-def.json',
// 'sfdx force:source:push',
// 'sfdx force:source:convert --packagename force-app --outputdir FORCE-APP',
// 'sfdx force:source:convert --packagename my-app --outputdir MY-APP',
// ],
// });
session = TestSession.create({
project: {
gitClone: 'https://github.com/amphro/simple-mpd-project.git',
},
// create org and push source to get something to retrieve
setupCommands: [
'sfdx force:org:create -d 1 -s -f config/project-scratch-def.json',
'sfdx force:source:push',
'sfdx force:source:convert --packagename force-app --outputdir FORCE-APP',
'sfdx force:source:convert --packagename my-app --outputdir MY-APP',
],
});
});

it('retrieves via sourcepath', () => {
Expand All @@ -42,13 +41,13 @@ describe('source:retrieve NUTs', () => {
execCmd('force:source:retrieve --sourcepath "force-app, my-app" --json', { ensureExitCode: 0 });
});

it('retrieves via package name with spaces', () => {
execCmd('force:source:retrieve --packagenames "my app" --json', { ensureExitCode: 0 });
});

it('retrieves via package names', () => {
execCmd('force:source:retrieve --packagenames "my app, force-app" --json', { ensureExitCode: 0 });
});
// it('retrieves via package name with spaces', () => {
// execCmd('force:source:retrieve --packagenames "my app" --json', { ensureExitCode: 0 });
// });
//
// it('retrieves via package names', () => {
// execCmd('force:source:retrieve --packagenames "my app, force-app" --json', { ensureExitCode: 0 });
// });

it('retrieves via metadata', () => {
execCmd('force:source:retrieve --metadata ApexClass --json', { ensureExitCode: 0 });
Expand Down Expand Up @@ -77,6 +76,6 @@ describe('source:retrieve NUTs', () => {
});

after(async () => {
// await session.clean();
await session.clean();
});
});
98 changes: 97 additions & 1 deletion test/commands/source/sourceCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,100 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

describe('sourceCommand tests', () => {});
import { ComponentSet } from '@salesforce/source-deploy-retrieve';
import { asArray, asString } from '@salesforce/ts-types';
import { stubMethod } from '@salesforce/ts-sinon';
import { expect, $$ } from '@salesforce/command/lib/test';
import { fs } from '@salesforce/core';
import { SinonStub } from 'sinon';
import { FlagOptions, SourceCommand } from '../../../src/sourceCommand';

class sourceCommandTest extends SourceCommand {
public async run() {}
public async callCreateCopmonentSet(options: FlagOptions): Promise<ComponentSet> {
return await this.createComponentSet(options);
}
}

describe('sourceCommand tests', () => {
describe('createComponentSet tests', () => {
const command = new sourceCommandTest([''], null);
let fromSource: SinonStub;
let fromManifest: SinonStub;

beforeEach(() => {
stubMethod($$.SANDBOX, fs, 'fileExistsSync').returns(true);
fromSource = stubMethod($$.SANDBOX, ComponentSet, 'fromSource').returns([
{ name: 'MyTest', type: { id: 'apexclass', name: 'ApexClass' }, xml: '', parent: undefined, content: '' },
]);
fromManifest = stubMethod($$.SANDBOX, ComponentSet, 'fromManifestFile').resolves([
{ name: 'MyTest', type: { id: 'apexclass', name: 'ApexClass' }, xml: '', parent: undefined, content: '' },
]);
});

it('will create appropriate ComponentSet from path', async () => {
try {
await command.callCreateCopmonentSet({
sourcepath: asArray<string>(['force-app']),
manifest: asString(''),
metadata: asArray<string>([]),
});
} catch (e) {
// we can't stub everything needed to create a ComponentSet, so expect the constructor to throw
// but we'll spy on everything and make sure it looks correct
// we need lots of NUTs
}
expect(fromSource.callCount).to.equal(1);
});

it('will create appropriate ComponentSet from multiple paths', async () => {
try {
await command.callCreateCopmonentSet({
sourcepath: asArray<string>(['force-app', 'my-app']),
manifest: asString(''),
metadata: asArray<string>([]),
});
} catch (e) {
// we can't stub everything needed to create a ComponentSet, so expect the constructor to throw
// but we'll spy on everything and make sure it looks correct
// we need lots of NUTs
}
expect(fromSource.callCount).to.equal(2);
});

it('will create appropriate ComponentSet from packagenames', async () => {
// TODO: Flush out once we can retrieve via packagenames
});

it('will create appropriate ComponentSet from multiple packagenames', async () => {
// TODO: Flush out once we can retrieve via packagenames
});

it('will create appropriate ComponentSet from metadata (ApexClass)', async () => {
// not sure how to stub ComponentSet constructor
});

it('will create appropriate ComponentSet from metadata (ApexClass:MyClass)', async () => {
// not sure how to stub ComponentSet constructor
});

it('will create appropriate ComponentSet from metadata (ApexClass:MyClass,CustomObject,CustomField:MyField', async () => {
// not sure how to stub ComponentSet constructor
});

it('will create appropriate ComponentSet from manifest', async () => {
try {
await command.callCreateCopmonentSet({
sourcepath: asArray<string>([]),
manifest: asString('manifest.xml'),
metadata: asArray<string>(['']),
});
} catch (e) {
// we can't stub everything needed to create a ComponentSet, so expect the constructor to throw
// but we'll spy on everything and make sure it looks correct
// we need lots of NUTs
}
expect(fromManifest.callCount).to.equal(1);
});
});
});
Loading

0 comments on commit ca2076c

Please sign in to comment.