Skip to content

Commit

Permalink
Fixes for dreamforce (#142)
Browse files Browse the repository at this point in the history
* fix: remove header for 3PP

* fix: generate unit test by default

* chore: update comment

* chore: add test for plugin generator

* chore: maybe fix tests

* chore: temporarily skip test
  • Loading branch information
mdonnalley authored Sep 19, 2022
1 parent eebb099 commit 05c60cd
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 20 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@salesforce/ts-sinon": "1.3.21",
"@types/shelljs": "^0.8.10",
"@types/yeoman-generator": "^5.2.10",
"@types/yeoman-test": "^4.0.3",
"@types/yosay": "^2.0.1",
"@typescript-eslint/eslint-plugin": "^5.37.0",
"@typescript-eslint/parser": "^5.37.0",
Expand All @@ -55,7 +56,8 @@
"shx": "^0.3.3",
"sinon": "10.0.0",
"ts-node": "^10.4.0",
"typescript": "^4.8.3"
"typescript": "^4.8.3",
"yeoman-test": "^6.3.0"
},
"config": {
"commitizen": {
Expand Down Expand Up @@ -118,12 +120,12 @@
"posttest": "yarn lint && yarn test:deprecation-policy",
"prepack": "sf-prepack",
"pretest": "sf-compile-test",
"test": "sf-test",
"test": "sf-test --timeout 600000",
"test:deprecation-policy": "./bin/dev snapshot:compare",
"test:nuts": "nyc mocha \"**/*.nut.ts\" --slow 4500 --timeout 600000 --parallel",
"version": "oclif readme"
},
"publishConfig": {
"access": "public"
}
}
}
2 changes: 1 addition & 1 deletion src/commands/dev/generate/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class GenerateCommand extends SfCommand<void> {
unit: Flags.boolean({
summary: messages.getMessage('flags.unit.summary'),
allowNo: true,
default: false,
default: true,
}),
};

Expand Down
6 changes: 6 additions & 0 deletions src/generators/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export default class Command extends Generator {
public declare options: CommandGeneratorOptions;
public pjson!: PackageJson;

private internalPlugin = false;

public constructor(args: string | string[], opts: CommandGeneratorOptions) {
super(args, opts);
}
Expand All @@ -77,6 +79,7 @@ export default class Command extends Generator {
const commands = commandSnapshot.map((c) => c.command.replace(/:/g, '.'));
const newTopics = addTopics(this.options.name, commands);
this.pjson.oclif.topics = { ...this.pjson.oclif.topics, ...newTopics };
this.internalPlugin = true;
} else {
const newTopics = addTopics(this.options.name);
this.pjson.oclif.topics = { ...this.pjson.oclif.topics, ...newTopics };
Expand Down Expand Up @@ -123,6 +126,7 @@ export default class Command extends Generator {
returnType: `${className}Result`,
commandPath,
year: new Date().getFullYear(),
copyright: this.internalPlugin,
pluginName: this.pjson.name,
messageFile: this.getMessageFileName(),
};
Expand All @@ -144,6 +148,7 @@ export default class Command extends Generator {
const opts = {
cmd: this.options.name.replace(/:/g, ' '),
year: new Date().getFullYear(),
copyright: this.internalPlugin,
pluginName: this.pjson.name,
messageFile: this.getMessageFileName(),
};
Expand All @@ -159,6 +164,7 @@ export default class Command extends Generator {
...this.options,
name: this.options.name.replace(/:/g, ' '),
year: new Date().getFullYear(),
copyright: this.internalPlugin,
});
}
}
17 changes: 17 additions & 0 deletions src/generators/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ export default class Plugin extends Generator {
fs.rmSync(this.destinationPath('./.git2gus'), { recursive: true });
fs.rmSync(this.destinationPath('./.github'), { recursive: true });
fs.rmSync(this.destinationPath('./command-snapshot.json'));
fs.rmSync(this.destinationPath('./CODE_OF_CONDUCT.md'));
fs.rmSync(this.destinationPath('./SECURITY.md'));

// Remove /schemas from the published files.
final.files = final.files.filter((f) => f !== '/schemas');
Expand All @@ -193,6 +195,20 @@ export default class Plugin extends Generator {
delete nycConfig.extends;

this.fs.writeJSON(this.destinationPath('.nycrc'), nycConfig);

// Remove the eslint-config-salesforce-internal from eslint config.
replace.sync({
files: `${this.env.cwd}/.eslintrc.js`,
from: /'eslint-config-salesforce-license',\s/g,
to: '',
});

// Remove the copyright header from the generated files.
replace.sync({
files: `${this.env.cwd}/**/*`,
from: /\/\*\n\s\*\sCopyright([\S\s]*?)\s\*\/\n\n/g,
to: '',
});
}

this.fs.delete(this.destinationPath('./.circleci/external.config.yml'));
Expand All @@ -208,6 +224,7 @@ export default class Plugin extends Generator {

public end(): void {
exec('git init', { cwd: this.env.cwd });
exec('yarn', { cwd: this.env.cwd });
exec('yarn build', { cwd: this.env.cwd });
// Run yarn install in case dev-scripts detected changes during yarn build.
exec('yarn install', { cwd: this.env.cwd });
Expand Down
2 changes: 2 additions & 0 deletions templates/src/command.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<% if(copyright){ %>
/*
* Copyright (c) <%- year %>, 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 { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';

Expand Down
14 changes: 8 additions & 6 deletions templates/test/command.nut.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/*
* Copyright (c) <%- year %>, 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
*/
<% if(copyright){ %>
/*
* Copyright (c) <%- year %>, 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 path from 'path';
import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
import { env } from '@salesforce/kit';
Expand Down
14 changes: 8 additions & 6 deletions templates/test/command.test.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/*
* Copyright (c) <%- year %>, 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
*/
<% if(copyright){ %>
/*
* Copyright (c) <%- year %>, 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 { expect, test } from '@oclif/test';

describe('<%- name %>', () => {
Expand Down
115 changes: 115 additions & 0 deletions test/commands/dev/generate/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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 path from 'path';
import helpers = require('yeoman-test');
import { expect } from 'chai';
import { readJson } from '../../../../src/util';
import { PackageJson } from '../../../../src/types';

describe('dev generate plugin', () => {
// This test fails because the generator fails to remove the copyright headers on windows.
// Once we move to a separate 3PP template, this will no longer be a problem.
(process.platform !== 'win32' ? it : it.skip)('should generate a 3PP plugin', async () => {
const runResult = await helpers
.run(path.join(__dirname, '..', '..', '..', '..', 'src', 'generators', 'plugin.ts'))
.withPrompts({
internal: false,
name: 'my-plugin',
description: 'my plugin description',
author: 'my name',
codeCoverage: '50%',
});

runResult.assertFile(path.join(runResult.cwd, 'my-plugin', 'package.json'));
runResult.assertFile(path.join(runResult.cwd, 'my-plugin', 'src', 'commands', 'hello', 'world.ts'));
runResult.assertNoFile(path.join(runResult.cwd, 'my-plugin', 'CODE_OF_CONDUCT.md'));
runResult.assertNoFile(path.join(runResult.cwd, 'my-plugin', 'command-snapshot.json'));
runResult.assertNoFile(path.join(runResult.cwd, 'my-plugin', 'schemas', 'hello-world.json'));
runResult.assertNoFile(path.join(runResult.cwd, 'my-plugin', '.git2gus', 'config.json'));

const packageJson = readJson<PackageJson>(path.join(runResult.cwd, 'my-plugin', 'package.json'));
expect(packageJson.name).to.equal('my-plugin');
expect(packageJson.author).to.equal('my name');
expect(packageJson.description).to.equal('my plugin description');

const scripts = Object.keys(packageJson.scripts);
const keys = Object.keys(packageJson);

expect(scripts).to.not.include('test:json-schema');
expect(scripts).to.not.include('test:deprecation-policy');
expect(scripts).to.not.include('test:command-reference');
expect(packageJson.scripts.posttest).to.equal('yarn lint');
expect(keys).to.not.include('homepage');
expect(keys).to.not.include('repository');
expect(keys).to.not.include('bugs');

runResult.assertNoFileContent(
path.join(runResult.cwd, 'my-plugin', 'src', 'commands', 'hello', 'world.ts'),
/Copyright/g
);

runResult.assertNoFileContent(
path.join(runResult.cwd, 'my-plugin', '.eslintrc.js'),
/eslint-config-salesforce-license/g
);
});

it('should generate a 2PP plugin', async () => {
const runResult = await helpers
.run(path.join(__dirname, '..', '..', '..', '..', 'src', 'generators', 'plugin.ts'))
.withPrompts({
internal: true,
name: 'plugin-test',
description: 'my plugin description',
hooks: ['sf:env:list', 'sf:env:display', 'sf:deploy', 'sf:logout'],
});

runResult.assertFile(path.join(runResult.cwd, 'plugin-test', 'package.json'));
runResult.assertFile(path.join(runResult.cwd, 'plugin-test', 'src', 'commands', 'hello', 'world.ts'));
runResult.assertFile(path.join(runResult.cwd, 'plugin-test', 'CODE_OF_CONDUCT.md'));
runResult.assertFile(path.join(runResult.cwd, 'plugin-test', 'command-snapshot.json'));
runResult.assertFile(path.join(runResult.cwd, 'plugin-test', 'schemas', 'hello-world.json'));
runResult.assertFile(path.join(runResult.cwd, 'plugin-test', '.git2gus', 'config.json'));

runResult.assertFile(path.join(runResult.cwd, 'plugin-test', 'src', 'hooks', 'envList.ts'));
runResult.assertFile(path.join(runResult.cwd, 'plugin-test', 'src', 'hooks', 'envDisplay.ts'));
runResult.assertFile(path.join(runResult.cwd, 'plugin-test', 'src', 'hooks', 'deploy.ts'));
runResult.assertFile(path.join(runResult.cwd, 'plugin-test', 'src', 'hooks', 'logout.ts'));

const packageJson = readJson<PackageJson>(path.join(runResult.cwd, 'plugin-test', 'package.json'));
expect(packageJson.name).to.equal('@salesforce/plugin-test');
expect(packageJson.author).to.equal('Salesforce');
expect(packageJson.description).to.equal('my plugin description');
expect(packageJson.oclif.hooks).to.deep.equal({
'sf:env:list': './lib/hooks/envList',
'sf:env:display': './lib/hooks/envDisplay',
'sf:deploy': './lib/hooks/deploy',
'sf:logout': './lib/hooks/logout',
});

const scripts = Object.keys(packageJson.scripts);
const keys = Object.keys(packageJson);

expect(scripts).to.include('test:json-schema');
expect(scripts).to.include('test:deprecation-policy');
expect(scripts).to.include('test:command-reference');
expect(keys).to.include('homepage');
expect(keys).to.include('repository');
expect(keys).to.include('bugs');

runResult.assertFileContent(
path.join(runResult.cwd, 'plugin-test', 'src', 'commands', 'hello', 'world.ts'),
/Copyright/g
);

runResult.assertFileContent(
path.join(runResult.cwd, 'plugin-test', '.eslintrc.js'),
/eslint-config-salesforce-license/g
);
});
});
46 changes: 42 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@
dependencies:
"@sinonjs/commons" "^1.7.0"

"@sinonjs/fake-timers@^7.1.2":
"@sinonjs/fake-timers@^7.0.4", "@sinonjs/fake-timers@^7.1.2":
version "7.1.2"
resolved "https://registry.yarnpkg.com/@sinonjs/fake-timers/-/fake-timers-7.1.2.tgz#2524eae70c4910edccf99b2f4e6efc5894aff7b5"
integrity sha512-iQADsW4LBMISqZ6Ci1dupJL9pprqwcVFTcOsEmQOEhW+KLCVn/Y4Jrvg2k19fIHCp+iFprriYPTdRcQR8NbUPg==
Expand Down Expand Up @@ -1264,7 +1264,7 @@
lodash.get "^4.4.2"
type-detect "^4.0.8"

"@sinonjs/samsam@^6.0.2":
"@sinonjs/samsam@^6.0.1", "@sinonjs/samsam@^6.0.2":
version "6.1.1"
resolved "https://registry.yarnpkg.com/@sinonjs/samsam/-/samsam-6.1.1.tgz#627f7f4cbdb56e6419fa2c1a3e4751ce4f6a00b1"
integrity sha512-cZ7rKJTLiE7u7Wi/v9Hc2fs3Ucc3jrWeMgPHbbTCeVAB2S0wOBbYlkJVeNSL04i7fdhT8wIbDq1zhC/PXTD2SA==
Expand Down Expand Up @@ -1580,6 +1580,16 @@
"@types/yeoman-environment" "*"
rxjs "^6.4.0"

"@types/yeoman-test@^4.0.3":
version "4.0.3"
resolved "https://registry.yarnpkg.com/@types/yeoman-test/-/yeoman-test-4.0.3.tgz#58959764bec68844b9eecd76e9be589c75eff15b"
integrity sha512-CrR+wmWCVAmwU7/Px8WFlCFOi0wy/PoZUovkw4G0hiFNvQiGQn2I6xV4iHY9ebXlJ8w1u7sL9fnL9YkvB4Go0Q==
dependencies:
"@types/mem-fs" "*"
"@types/mem-fs-editor" "*"
"@types/yeoman-environment" "*"
"@types/yeoman-generator" "*"

"@types/yosay@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@types/yosay/-/yosay-2.0.1.tgz#320ff6ea4ba5d464a7c8ba1011d64fbf5562a1b0"
Expand Down Expand Up @@ -5558,7 +5568,7 @@ marked@^4.0.16:
resolved "https://registry.yarnpkg.com/marked/-/marked-4.0.18.tgz#cd0ac54b2e5610cfb90e8fd46ccaa8292c9ed569"
integrity sha512-wbLDJ7Zh0sqA0Vdg6aqlbT+yPxqLblpAZh1mK2+AO2twQkPywvvqQNfEPVwSSRjZ7dZcdeVBIAgiO7MMp3Dszw==

"mem-fs-editor@^8.1.2 || ^9.0.0":
"mem-fs-editor@^8.1.2 || ^9.0.0", mem-fs-editor@^9.0.0:
version "9.5.0"
resolved "https://registry.yarnpkg.com/mem-fs-editor/-/mem-fs-editor-9.5.0.tgz#9368724bd37f76eebfcf24d71fc6624b01588969"
integrity sha512-7p+bBDqsSisO20YIZf2ntYvST27fFJINn7CKE21XdPUQDcLV62b/yB5sTOooQeEoiZ3rldZQ+4RfONgL/gbRoA==
Expand Down Expand Up @@ -5959,7 +5969,7 @@ nise@^4.1.0:
just-extend "^4.0.2"
path-to-regexp "^1.7.0"

nise@^5.1.0:
nise@^5.0.1, nise@^5.1.0:
version "5.1.1"
resolved "https://registry.yarnpkg.com/nise/-/nise-5.1.1.tgz#ac4237e0d785ecfcb83e20f389185975da5c31f3"
integrity sha512-yr5kW2THW1AkxVmCnKEh4nbYkJdB3I7LUkiUgOvEkOp414mc2UMaHMA7pjq1nYowhdoJZGwEKGaQVbxfpWj10A==
Expand Down Expand Up @@ -7285,6 +7295,18 @@ [email protected]:
nise "^4.1.0"
supports-color "^7.1.0"

sinon@^10.0.0:
version "10.0.1"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-10.0.1.tgz#0d1a13ecb86f658d15984f84273e57745b1f4c57"
integrity sha512-1rf86mvW4Mt7JitEIgmNaLXaWnrWd/UrVKZZlL+kbeOujXVf9fmC4kQEQ/YeHoiIA23PLNngYWK+dngIx/AumA==
dependencies:
"@sinonjs/commons" "^1.8.1"
"@sinonjs/fake-timers" "^7.0.4"
"@sinonjs/samsam" "^6.0.1"
diff "^4.0.2"
nise "^5.0.1"
supports-color "^7.1.0"

sinon@^11.1.2:
version "11.1.2"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-11.1.2.tgz#9e78850c747241d5c59d1614d8f9cbe8840e8674"
Expand Down Expand Up @@ -7722,6 +7744,11 @@ tar@^6.0.2, tar@^6.1.0, tar@^6.1.11, tar@^6.1.2:
mkdirp "^1.0.3"
yallist "^4.0.0"

temp-dir@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/temp-dir/-/temp-dir-2.0.0.tgz#bde92b05bdfeb1516e804c9c00ad45177f31321e"
integrity sha512-aoBAniQmmwtcKp/7BzsH8Cxzv8OL736p7v1ihGb5e9DJ9kTwGWHrQrVB5+lfVDzfGrdRzXch+ig7LHaY1JTOrg==

test-exclude@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-6.0.0.tgz#04a8698661d805ea6fa293b6cb9e63ac044ef15e"
Expand Down Expand Up @@ -8530,6 +8557,17 @@ yeoman-generator@^5.6.1, yeoman-generator@^5.7.0:
sort-keys "^4.2.0"
text-table "^0.2.0"

yeoman-test@^6.3.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/yeoman-test/-/yeoman-test-6.3.0.tgz#c0b44562507dc40aa624719c8a24b2c49c2a196d"
integrity sha512-FpaLV5AiVFlYe4pizAB/QLWc+5BSyttk/TcFQfi/r8g/rz290uqEkV4waN3rHEvP/DCmoMNSJykKTZNWL2y07g==
dependencies:
inquirer "^8.0.0"
lodash "^4.17.21"
mem-fs-editor "^9.0.0"
sinon "^10.0.0"
temp-dir "^2.0.0"

[email protected]:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
Expand Down

0 comments on commit 05c60cd

Please sign in to comment.