Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: address feedback from yeoman update PR #476 #486

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"version": "54.6.0",
"npmClient": "yarn",
"packages": [
"packages/*"
]
"packages": ["packages/*"]
}
2 changes: 1 addition & 1 deletion packages/plugin-templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@salesforce/templates": "54.6.0",
"tslib": "^1",
"yeoman-environment": "3.9.1",
"yeoman-generator": "^5.6.1"
"yeoman-generator": "5.6.1"
},
"devDependencies": {
"@oclif/dev-cli": "^1",
Expand Down
2 changes: 1 addition & 1 deletion packages/templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"tar": "^6.1.10",
"tslib": "^1",
"yeoman-environment": "3.9.1",
"yeoman-generator": "^5.6.1"
"yeoman-generator": "5.6.1"
},
"devDependencies": {
"@types/chai": "^4",
Expand Down
34 changes: 13 additions & 21 deletions packages/templates/src/service/templateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,19 @@ export class TemplateService {

this.adapter.log.clear();

return new Promise((resolve, reject) => {
this.env
.run(generatorNamespace, templateOptions)
.then(() => {
const outputDir = path.resolve(this.cwd, templateOptions.outputdir!);
const created = this.adapter.log.getCleanOutput();
const rawOutput = nls.localize('RawOutput', [
outputDir,
this.adapter.log.getOutput()
]);
const result = {
outputDir,
created,
rawOutput
};
resolve(result);
})
.catch(err => {
reject(err);
});
});
await this.env.run(generatorNamespace, templateOptions);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same functionality with await/async

const outputDir = path.resolve(this.cwd, templateOptions.outputdir!);
const created = this.adapter.log.getCleanOutput();
const rawOutput = nls.localize('RawOutput', [
outputDir,
this.adapter.log.getOutput()
]);
const result = {
outputDir,
created,
rawOutput
};
return result;
}

/**
Expand Down
127 changes: 127 additions & 0 deletions packages/templates/test/generators/projectGenerator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright (c) 2022, 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 * as fs from 'fs';
import { expect } from 'chai';
import ProjectGenerator from '../../src/generators/projectGenerator';
import * as YeomanEnvironment from 'yeoman-environment';
import { CreateUtil } from '../../src/utils';
import { assert, restore, SinonStub, stub } from 'sinon';
import { ProjectOptions } from '../../src';

describe('ProjectGenerator Unit Tests', () => {
const testEnv = YeomanEnvironment.createEnv();

let mockProjectOptions: ProjectOptions & { env: any };
let checkInputsStub: SinonStub;
let mkdirSyncStub: SinonStub;
let existsSyncStub: SinonStub;

beforeEach(() => {
mockProjectOptions = {
projectname: 'testProject',
defaultpackagedir: 'some/other/dir',
ns: 'imunique',
template: 'standard',
manifest: false,
loginurl: 'https://never.gonna.log.you.in.com',
env: testEnv
};
checkInputsStub = stub(CreateUtil, 'checkInputs');
mkdirSyncStub = stub(fs, 'mkdirSync').returns(undefined);
existsSyncStub = stub(fs, 'existsSync').returns(false);
});

afterEach(() => {
restore();
});

it('Should set the customInstallTask feature to false for the ProjectGenerator', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

went to add just this test, but then much us the file was uncovered so I went ahead and added some simple tests to cover the rest of the functionality.

const generator = new ProjectGenerator([], mockProjectOptions as any);
expect((generator as any).features.customInstallTask).to.equal(false);
});

it('Should attempt to validate template.', () => {
const generator = new ProjectGenerator([], mockProjectOptions as any);
checkInputsStub.reset();
generator.validateOptions();
assert.calledOnce(checkInputsStub);
assert.calledWith(checkInputsStub, 'standard');
});

describe('writing()', () => {
it('Should attempt to write template files with standard template.', () => {
const generator = new ProjectGenerator([], mockProjectOptions as any);
const copyTplStub = stub(generator.fs, 'copyTpl');
copyTplStub.returns();
generator.writing();
assert.callCount(copyTplStub, 18);
assert.callCount(existsSyncStub, 36);
assert.callCount(mkdirSyncStub, 18);
});

it('Should attempt to write template files with empty template.', () => {
mockProjectOptions.template = 'empty';
const generator = new ProjectGenerator([], mockProjectOptions as any);
const copyTplStub = stub(generator.fs, 'copyTpl');
copyTplStub.returns();
generator.writing();
assert.callCount(copyTplStub, 4);
assert.callCount(existsSyncStub, 12);
assert.callCount(mkdirSyncStub, 8);
});

it('Should attempt to write template files with analytics template.', () => {
mockProjectOptions.template = 'analytics';
const generator = new ProjectGenerator([], mockProjectOptions as any);
const copyTplStub = stub(generator.fs, 'copyTpl');
copyTplStub.returns();
generator.writing();
assert.callCount(copyTplStub, 16);
assert.callCount(existsSyncStub, 27);
assert.callCount(mkdirSyncStub, 11);
});
});

describe('writing() with manifest', () => {
beforeEach(() => {
mockProjectOptions.manifest = true;
});

it('Should attempt to write template files with standard template with manifest.', () => {
const generator = new ProjectGenerator([], mockProjectOptions as any);
const copyTplStub = stub(generator.fs, 'copyTpl');
copyTplStub.returns();
generator.writing();
assert.callCount(copyTplStub, 19);
assert.callCount(existsSyncStub, 37);
assert.callCount(mkdirSyncStub, 18);
});

it('Should attempt to write template files with empty template with manifest.', () => {
mockProjectOptions.template = 'empty';

const generator = new ProjectGenerator([], mockProjectOptions as any);
const copyTplStub = stub(generator.fs, 'copyTpl');
copyTplStub.returns();
generator.writing();
assert.callCount(copyTplStub, 5);
assert.callCount(existsSyncStub, 13);
assert.callCount(mkdirSyncStub, 8);
});

it('Should attempt to write template files with analytics template with manifest.', () => {
mockProjectOptions.template = 'analytics';
const generator = new ProjectGenerator([], mockProjectOptions as any);
const copyTplStub = stub(generator.fs, 'copyTpl');
copyTplStub.returns();
generator.writing();
assert.callCount(copyTplStub, 17);
assert.callCount(existsSyncStub, 28);
assert.callCount(mkdirSyncStub, 11);
});
});
});
9 changes: 8 additions & 1 deletion packages/templates/test/generators/sfdxGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import { assert, match, stub } from 'sinon';
import { TemplateOptions } from '../../';
import { SfdxGenerator } from '../../src/generators/sfdxGenerator';
import * as YeomanEnvironment from 'yeoman-environment';
import { expect } from 'chai';

describe('SfdxGenerator', () => {
describe('SfdxGenerator Unit Tests', () => {
interface MyTemplateOptions extends TemplateOptions {
// env and resolved are for testing (similar to how yeoman environment instantiates the generators)
env: object;
resolved: string;
}
class MyGenerator extends SfdxGenerator<MyTemplateOptions> {
public features!: Record<string, any>;
public validateOptions() {}
public writing() {
this.doWriting(this.options);
Expand Down Expand Up @@ -51,4 +53,9 @@ describe('SfdxGenerator', () => {
new MyGenerator([], mockMyGeneratorOptions);
assert.calledOnce(validateOptionsStub);
});

it('Should set the customInstallTask feature to true for the SfdxGenerator', () => {
const generator = new MyGenerator([], mockMyGeneratorOptions);
expect(generator.features.customInstallTask).to.equal(true);
});
});