From a5d0492ddccf8af35a2984a4751c46655ea1196a Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Sat, 20 Mar 2021 08:31:36 -0300 Subject: [PATCH] Add regression tests for duplicated needle and fix css needle. --- generators/bootstrap/index.js | 12 ++- generators/client/needle-api/needle-client.js | 2 + generators/utils.js | 17 ++-- package-lock.json | 31 +++---- package.json | 1 - test/needle-api/needle-client-angular.spec.js | 81 +++++++------------ 6 files changed, 58 insertions(+), 86 deletions(-) diff --git a/generators/bootstrap/index.js b/generators/bootstrap/index.js index ac22a2b9eee..d40929f7d4f 100644 --- a/generators/bootstrap/index.js +++ b/generators/bootstrap/index.js @@ -19,8 +19,12 @@ const filter = require('gulp-filter'); const _ = require('lodash'); const path = require('path'); -const { pipe } = require('pipeline-pipe'); -const { createConflicterStatusTransform, createYoRcTransform, createYoResolveTransform } = require('yeoman-environment/lib/util/transform'); +const { + createEachFileTransform, + createConflicterStatusTransform, + createYoRcTransform, + createYoResolveTransform, +} = require('yeoman-environment/lib/util/transform'); const BaseGenerator = require('../generator-base'); const { defaultConfig } = require('../generator-defaults'); @@ -144,7 +148,7 @@ module.exports = class extends BaseGenerator { const transformStreams = [ createYoResolveTransform(this.env.conflicter), createYoRcTransform(), - pipe(file => { + createEachFileTransform(file => { if (path.extname(file.path) === '.json' && path.basename(path.dirname(file.path)) === '.jhipster') { file.conflicter = 'force'; } @@ -167,7 +171,7 @@ module.exports = class extends BaseGenerator { } transformStreams.push( - pipe(file => this.env.conflicter.checkForCollision(file), { ordered: false, maxParallel: 10 }), + createEachFileTransform(file => this.env.conflicter.checkForCollision(file), { ordered: false, maxParallel: 10 }), createConflicterStatusTransform() ); diff --git a/generators/client/needle-api/needle-client.js b/generators/client/needle-api/needle-client.js index 866431a03a2..e8fcf62533e 100644 --- a/generators/client/needle-api/needle-client.js +++ b/generators/client/needle-api/needle-client.js @@ -33,6 +33,8 @@ module.exports = class extends needleBase { addStyle(style, comment, filePath, needle) { const styleBlock = this._mergeStyleAndComment(style, comment); const rewriteFileModel = this.generateFileModel(filePath, needle, styleBlock); + rewriteFileModel.regexp = `\n${style}\n`; + rewriteFileModel.prettierAware = true; this.addBlockContentToFile(rewriteFileModel, 'Style not added to JHipster app.\n'); } diff --git a/generators/utils.js b/generators/utils.js index fe2511fae9c..66c13ba4848 100644 --- a/generators/utils.js +++ b/generators/utils.js @@ -147,15 +147,20 @@ function convertToPrettierExpressions(str) { */ function rewrite(args) { // check if splicable is already in the body text - let re = args.regexp; - if (re) { - re = re.test ? re : new RegExp(re); + let re; + if (args.regexp) { + re = args.regexp; + if (!re.test) { + re = escapeRegExp(re); + } } else { - let content = args.splicable.map(line => `\\s*${escapeRegExp(normalizeLineEndings(line))}`).join('\n'); + re = args.splicable.map(line => `\\s*${escapeRegExp(normalizeLineEndings(line))}`).join('\n'); + } + if (!re.test) { if (args.prettierAware) { - content = convertToPrettierExpressions(content); + re = convertToPrettierExpressions(re); } - re = new RegExp(content); + re = new RegExp(re); } if (re.test(normalizeLineEndings(args.haystack))) { diff --git a/package-lock.json b/package-lock.json index fb3a590793a..cc915960a9b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,10 +28,9 @@ "ora": "5.3.0", "os-locale": "5.0.0", "parse-gitignore": "1.0.1", - "pipeline-pipe": "0.1.5", "pluralize": "8.0.0", "prettier": "2.2.1", - "prettier-plugin-java": "1.0.1", + "prettier-plugin-java": "1.0.2", "prettier-plugin-packagejson": "2.2.10", "progress": "2.0.3", "randexp": "0.5.3", @@ -4362,12 +4361,12 @@ } }, "node_modules/java-parser": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/java-parser/-/java-parser-1.0.0.tgz", - "integrity": "sha512-z+1shDR4S1uC/v44uITWV87kP+PtaBT7dnZgmwZcAUhnA0wldJSlA6KhGHU8IXJHaLKtjgYPbSxvmWVaMbtM6g==", + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/java-parser/-/java-parser-1.0.2.tgz", + "integrity": "sha512-lBXc+F62ds2W83eH5MwGnzuWdb6kgGBV0x0R7w0B4JKGDrJzolMUEhRMzzzlIX68HvRU7XtfPon22YaB+dVg+A==", "dependencies": { "chevrotain": "6.5.0", - "lodash": "4.17.20" + "lodash": "4.17.21" } }, "node_modules/java-parser/node_modules/chevrotain": { @@ -4378,11 +4377,6 @@ "regexp-to-ast": "0.4.0" } }, - "node_modules/java-parser/node_modules/lodash": { - "version": "4.17.20", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", - "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==" - }, "node_modules/java-parser/node_modules/regexp-to-ast": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/regexp-to-ast/-/regexp-to-ast-0.4.0.tgz", @@ -6238,20 +6232,15 @@ } }, "node_modules/prettier-plugin-java": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/prettier-plugin-java/-/prettier-plugin-java-1.0.1.tgz", - "integrity": "sha512-QaNLrBra0pzSba29cq7N5JPqHPlEM+3FRp17ViL66fKcStu+5mDOS+YV99mbkpHTrsMAuChW3rZ85ZDs4Smupw==", + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/prettier-plugin-java/-/prettier-plugin-java-1.0.2.tgz", + "integrity": "sha512-YgcN1WGZlrH0E+bHdqtIYtfDp6k2PHBnIaGjzdff/7t/NyDWAA6ypAmnD7YQVG2OuoIaXYkC37HN7cz68lLWLg==", "dependencies": { - "java-parser": "1.0.0", - "lodash": "4.17.20", + "java-parser": "1.0.2", + "lodash": "4.17.21", "prettier": "2.2.1" } }, - "node_modules/prettier-plugin-java/node_modules/lodash": { - "version": "4.17.20", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", - "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==" - }, "node_modules/prettier-plugin-packagejson": { "version": "2.2.10", "resolved": "https://registry.npmjs.org/prettier-plugin-packagejson/-/prettier-plugin-packagejson-2.2.10.tgz", diff --git a/package.json b/package.json index bda93881243..19ae5f58a7e 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,6 @@ "ora": "5.3.0", "os-locale": "5.0.0", "parse-gitignore": "1.0.1", - "pipeline-pipe": "0.1.5", "pluralize": "8.0.0", "prettier": "2.2.1", "prettier-plugin-java": "1.0.2", diff --git a/test/needle-api/needle-client-angular.spec.js b/test/needle-api/needle-client-angular.spec.js index e441448a6d4..0953cc7c1e5 100644 --- a/test/needle-api/needle-client-angular.spec.js +++ b/test/needle-api/needle-client-angular.spec.js @@ -11,36 +11,16 @@ const CLIENT_MAIN_SRC_DIR = constants.CLIENT_MAIN_SRC_DIR; const mockBlueprintSubGen = class extends ClientGenerator { constructor(args, opts) { super(args, { fromBlueprint: true, ...opts }); // fromBlueprint variable is important - const jhContext = (this.jhipsterContext = this.options.jhipsterContext); - if (!jhContext) { - this.error("This is a JHipster blueprint and should be used only like 'jhipster --blueprints myblueprint')}"); - } + this.sbsBlueprint = true; } - get initializing() { - return super._initializing(); - } - - get prompting() { - return super._prompting(); - } - - get configuring() { - return super._configuring(); - } - - get default() { - return super._default(); - } - - get writing() { - const phaseFromJHipster = super._writing(); - const customPhaseSteps = { + get postWriting() { + return { addCssStylesProperty() { - this.addMainSCSSStyle('@import style_without_comment'); - this.addMainSCSSStyle('@import style', 'my comment'); - this.addVendorSCSSStyle('@import style', 'my comment'); - this.addVendorSCSSStyle('@import style_without_comment'); + this.addMainSCSSStyle('@import style_without_comment;'); + this.addMainSCSSStyle('@import style;', 'my comment'); + this.addVendorSCSSStyle('@import style;', 'my comment'); + this.addVendorSCSSStyle('@import style_without_comment;'); }, addToMenuStep() { this.addElementToMenu('routerName1', 'iconName1', true, ANGULAR); @@ -65,52 +45,45 @@ const mockBlueprintSubGen = class extends ClientGenerator { this.addAdminRoute('entity-audit', './entity-audit/entity-audit.module', 'EntityAuditModule', 'entityAudit.home.title'); }, }; - return { ...phaseFromJHipster, ...customPhaseSteps }; - } - - get install() { - return super._install(); - } - - get end() { - return super._end(); } }; describe('needle API Angular: JHipster client generator with blueprint', () => { - before(done => { - helpers - .run(path.join(__dirname, '../../generators/client')) + let runContext; + let runResult; + + before(async () => { + runContext = helpers.create(path.join(__dirname, '../../generators/client')); + runResult = await runContext .withOptions({ fromCli: true, - build: 'maven', - auth: 'jwt', - db: 'mysql', + defaults: true, skipInstall: true, blueprint: 'myblueprint', skipChecks: true, }) .withGenerators([[mockBlueprintSubGen, 'jhipster-myblueprint:client']]) - .withPrompts({ - baseName: 'jhipster', - clientFramework: ANGULAR, - enableTranslation: true, - nativeLanguage: 'en', - languages: ['fr'], - }) - .on('end', done); + .run(); + }); + + it('should bail on any file change adding same needles again', async () => { + await runResult + .create('jhipster-myblueprint:client') + .withGenerators([[mockBlueprintSubGen, 'jhipster-myblueprint:client']]) + .withOptions({ fromCli: true, force: false, bail: true, skipChecks: true, skipInstall: true }) + .run(); }); it('vendor.scss contains the specific change (without comment) added by needle api', () => { - assert.fileContent(`${CLIENT_MAIN_SRC_DIR}content/scss/vendor.scss`, /@import style_without_comment/); + assert.fileContent(`${CLIENT_MAIN_SRC_DIR}content/scss/vendor.scss`, /\n@import style_without_comment;\n/); }); it('global.scss contains the specific change (without comment) added by needle api', () => { - assert.fileContent(`${CLIENT_MAIN_SRC_DIR}content/scss/global.scss`, /@import style_without_comment/); + assert.fileContent(`${CLIENT_MAIN_SRC_DIR}content/scss/global.scss`, /\n@import style_without_comment;\n/); }); it('vendor.scss contains the specific change added by needle api', () => { - assert.fileContent(`${CLIENT_MAIN_SRC_DIR}content/scss/vendor.scss`, /@import style/); + assert.fileContent(`${CLIENT_MAIN_SRC_DIR}content/scss/vendor.scss`, /\n@import style;\n/); assert.fileContent( `${CLIENT_MAIN_SRC_DIR}content/scss/vendor.scss`, '* ==========================================================================\n' + @@ -120,7 +93,7 @@ describe('needle API Angular: JHipster client generator with blueprint', () => { }); it('global.scss contains the specific change added by needle api', () => { - assert.fileContent(`${CLIENT_MAIN_SRC_DIR}content/scss/global.scss`, /@import style/); + assert.fileContent(`${CLIENT_MAIN_SRC_DIR}content/scss/global.scss`, /\n@import style;\n/); assert.fileContent( `${CLIENT_MAIN_SRC_DIR}content/scss/global.scss`, '* ==========================================================================\n' +