From 6ee61b964f36298278951b4b714841162616d7cc Mon Sep 17 00:00:00 2001 From: Loonride Date: Sun, 23 Feb 2020 01:13:26 -0600 Subject: [PATCH] Fix loader-generator and plugin-generator tests (#1250) * tests(loader-generator): fix loader generator tests and update package locks * tests(generator): updated loader and plugin generators * tests(generator): update generator tests and paths * fix(cli): remove bad js files and add lost files * chore(cli): delete other js files * chore(template): fix package json template style * chore(template): fix lint for return type of function --- .../__tests__/loader-generator.test.ts | 22 +++++++++++-------- .../__tests__/plugin-generator.test.ts | 22 +++++++++++-------- packages/generators/src/addon-generator.ts | 17 +++++++------- packages/generators/src/loader-generator.ts | 2 +- packages/generators/src/plugin-generator.ts | 2 +- .../templates/addon-package.json.js | 8 +++++++ 6 files changed, 44 insertions(+), 29 deletions(-) create mode 100644 packages/generators/templates/addon-package.json.js diff --git a/packages/generators/__tests__/loader-generator.test.ts b/packages/generators/__tests__/loader-generator.test.ts index 70dfdb8c354..f4cc0d8439c 100644 --- a/packages/generators/__tests__/loader-generator.test.ts +++ b/packages/generators/__tests__/loader-generator.test.ts @@ -1,31 +1,35 @@ import { join } from 'path'; import { run } from 'yeoman-test'; -import assert from 'yeoman-assert'; +import * as assert from 'yeoman-assert'; import { makeLoaderName } from '../src/loader-generator'; describe('loader generator', () => { - it.skip('generates a default loader', async () => { - const outputDir = await run(join(__dirname, '../loader-generator')); - const loaderDir = `${outputDir}/my-loader`; + it('generates a default loader', async () => { + const loaderName = 'my-test-loader'; + const outputDir = await run(join(__dirname, '../src/loader-generator')).withPrompts({ + name: loaderName, + }); + const loaderDir = join(outputDir, loaderName); const srcFiles = ['cjs.js', 'index.js']; const testFiles = ['functional.test.js', 'test-utils.js', 'unit.test.js', 'fixtures/simple-file.js']; const exampleFiles = ['webpack.config.js', 'src/index.js', 'src/lazy-module.js', 'src/static-esm-module.js']; // Check that files in all folders are scaffolded. Checking them separately so we know which directory has the problem // assert for src files - assert.file([...srcFiles.map(file => `${loaderDir}/src/${file}`)]); + assert.file(srcFiles.map(file => join(loaderDir, 'src', file))); // assert for test files - assert.file([...testFiles.map(file => `${loaderDir}/test/${file}`)]); + assert.file(testFiles.map(file => join(loaderDir, 'test', file))); // assert for example files - assert.file([...exampleFiles.map(file => `${loaderDir}/examples/simple/${file}`)]); + assert.file(exampleFiles.map(file => join(loaderDir, 'examples/simple', file))); // Check the contents of the webpack config and loader file assert.fileContent([ - [`${loaderDir}/examples/simple/webpack.config.js`, /resolveLoader: {/], - [`${loaderDir}/src/index.js`, /export default function loader(source) {/], + [join(loaderDir, 'examples/simple/webpack.config.js'), /resolveLoader: {/], + [join(loaderDir, 'src/index.js'), /export default function loader\(source\) {/], + [join(loaderDir, 'package.json'), new RegExp(loaderName)], ]); // higher timeout so travis has enough time to execute diff --git a/packages/generators/__tests__/plugin-generator.test.ts b/packages/generators/__tests__/plugin-generator.test.ts index e0cbc533efb..58c538b0f70 100644 --- a/packages/generators/__tests__/plugin-generator.test.ts +++ b/packages/generators/__tests__/plugin-generator.test.ts @@ -1,31 +1,35 @@ import { join } from 'path'; import { run } from 'yeoman-test'; -import assert from 'yeoman-assert'; +import * as assert from 'yeoman-assert'; import { generatePluginName } from '../src/utils'; describe('plugin generator', () => { - it.skip('generates a default plugin', async () => { - const outputDir = await run(join(__dirname, '../plugin-generator')); - const pluginDir = `${outputDir}/my-webpack-plugin`; + it('generates a default plugin', async () => { + const pluginName = 'my-test-plugin'; + const outputDir = await run(join(__dirname, '../src/plugin-generator')).withPrompts({ + name: pluginName, + }); + const pluginDir = join(outputDir, pluginName); const srcFiles = ['cjs.js', 'index.js']; const testFiles = ['functional.test.js', 'test-utils.js']; const exampleFiles = ['webpack.config.js', 'src/index.js', 'src/lazy-module.js', 'src/static-esm-module.js']; // Check that files in all folders are scaffolded. Checking them separately so we know which directory has the problem // assert for src files - assert.file([...srcFiles.map(file => `${pluginDir}/src/${file}`)]); + assert.file(srcFiles.map(file => join(pluginDir, 'src', file))); // assert for test files - assert.file([...testFiles.map(file => `${pluginDir}/test/${file}`)]); + assert.file(testFiles.map(file => join(pluginDir, 'test', file))); // assert for example files - assert.file([...exampleFiles.map(file => `${pluginDir}/examples/simple/${file}`)]); + assert.file(exampleFiles.map(file => join(pluginDir, 'examples/simple', file))); // Check the contents of the webpack config and loader file assert.fileContent([ - [`${pluginDir}/examples/simple/webpack.config.js`, /new MyWebpackPlugin()/], - [`${pluginDir}/src/index.js`, /MyWebpackPlugin.prototype.apply = function(compiler) {/], + [join(pluginDir, 'examples/simple/webpack.config.js'), /new MyTestPlugin\(\)/], + [join(pluginDir, 'src/index.js'), /MyTestPlugin\.prototype\.apply = function\(compiler\) {/], + [join(pluginDir, 'package.json'), new RegExp(pluginName)], ]); // higher timeout so travis has enough time to execute diff --git a/packages/generators/src/addon-generator.ts b/packages/generators/src/addon-generator.ts index b9ef59e69c3..f875ee58275 100644 --- a/packages/generators/src/addon-generator.ts +++ b/packages/generators/src/addon-generator.ts @@ -57,24 +57,23 @@ const addonGenerator = ( } } - public writing(): void { - this.copy = generatorCopy(this, templateDir); + public writing(): void { + const packageJsonTemplatePath = "../templates/addon-package.json.js"; + this.fs.extendJSON(this.destinationPath("package.json"), require(packageJsonTemplatePath)(this.props.name)); + + this.copy = generatorCopy(this, templateDir); this.copyTpl = generatorCopyTpl(this, templateDir, templateFn(this)); copyFiles.forEach(this.copy); copyTemplateFiles.forEach(this.copyTpl); - } + } - public install(): void { + public install(): void { this.npmInstall(['webpack-defaults', 'bluebird'], { 'save-dev': true, }); } - - public end(): void { - this.spawnCommand('npm', ['run', 'defaults']); - } - }; + }; }; export default addonGenerator; diff --git a/packages/generators/src/loader-generator.ts b/packages/generators/src/loader-generator.ts index f594a93e14f..63111166295 100644 --- a/packages/generators/src/loader-generator.ts +++ b/packages/generators/src/loader-generator.ts @@ -37,7 +37,7 @@ const LoaderGenerator = addonGenerator( validate: (str: string): boolean => str.length > 0 } ], - path.resolve(__dirname, "..", "generate-loader"), + path.resolve(__dirname, "../../generate-loader/templates"), [ "src/cjs.js.tpl", "test/test-utils.js.tpl", diff --git a/packages/generators/src/plugin-generator.ts b/packages/generators/src/plugin-generator.ts index 36147b64d51..119dc069020 100644 --- a/packages/generators/src/plugin-generator.ts +++ b/packages/generators/src/plugin-generator.ts @@ -21,7 +21,7 @@ const PluginGenerator = addonGenerator( validate: (str: string): boolean => str.length > 0 } ], - path.resolve(__dirname, "..", "generate-plugin"), + path.resolve(__dirname, "../../generate-plugin/templates"), [ "src/cjs.js.tpl", "test/test-utils.js.tpl", diff --git a/packages/generators/templates/addon-package.json.js b/packages/generators/templates/addon-package.json.js new file mode 100644 index 00000000000..1a925b41020 --- /dev/null +++ b/packages/generators/templates/addon-package.json.js @@ -0,0 +1,8 @@ +// eslint-disable-next-line @typescript-eslint/explicit-function-return-type +module.exports = name => { + return { + version: "1.0.0", + description: "webpack loader", + name + }; +};