Skip to content

Commit

Permalink
Merge pull request #14394 from mshima/skip_ci_needle_angular2
Browse files Browse the repository at this point in the history
Add regression tests for duplicated needle and fix css needle.
  • Loading branch information
mshima authored Mar 20, 2021
2 parents c4e6e19 + a5d0492 commit 5b9204d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 86 deletions.
12 changes: 8 additions & 4 deletions generators/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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';
}
Expand All @@ -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()
);

Expand Down
2 changes: 2 additions & 0 deletions generators/client/needle-api/needle-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
17 changes: 11 additions & 6 deletions generators/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down
31 changes: 10 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
81 changes: 27 additions & 54 deletions test/needle-api/needle-client-angular.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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' +
Expand All @@ -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' +
Expand Down

0 comments on commit 5b9204d

Please sign in to comment.