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

optimize tests #27255

Merged
merged 1 commit into from
Sep 12, 2024
Merged
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
2 changes: 1 addition & 1 deletion generators/base-core/generator-core.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, it, expect as jestExpect } from 'esmocha';
import { basicHelpers as helpers } from '../../lib/testing/index.js';
import { defaultHelpers as helpers } from '../../lib/testing/index.js';

import { createJHipsterLogger } from '../base/support/index.js';
import Base from './index.js';
Expand Down
2 changes: 1 addition & 1 deletion generators/base/blueprints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mock } from 'node:test';
import { before, describe, expect, it } from 'esmocha';
import type { RunResult } from 'yeoman-test';

import { basicHelpers as helpers } from '../../lib/testing/index.js';
import { defaultHelpers as helpers } from '../../lib/testing/index.js';
import { packageJson } from '../../lib/index.js';
import BaseGenerator from './index.js';

Expand Down
14 changes: 13 additions & 1 deletion generators/bootstrap/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ const command: JHipsterCommandDefinition = {
scope: 'storage',
},
skipPrettier: {
description: 'Skip prettier',
description: 'Skip prettier transformation',
type: Boolean,
hide: true,
scope: 'generator',
},
skipEslint: {
description: 'Skip ESlint transformation',
type: Boolean,
hide: true,
scope: 'generator',
Expand All @@ -37,6 +43,12 @@ const command: JHipsterCommandDefinition = {
hide: true,
scope: 'generator',
},
skipForks: {
description: 'Dont use forks',
type: Boolean,
hide: true,
scope: 'generator',
},
},
};

Expand Down
11 changes: 9 additions & 2 deletions generators/bootstrap/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export default class BootstrapGenerator extends BaseGenerator {

upgradeCommand?: boolean;
skipPrettier?: boolean;
skipEslint?: boolean;
skipForks?: boolean;
prettierExtensions: string[] = PRETTIER_EXTENSIONS.split(',');
prettierOptions: PrettierOptions = { plugins: [] };
refreshOnCommit = false;
Expand Down Expand Up @@ -202,15 +204,20 @@ export default class BootstrapGenerator extends BaseGenerator {
const prettierTransforms: FileTransform<MemFsEditorFile>[] = [];
if (!this.skipPrettier) {
const ignoreErrors = this.options.ignoreErrors || this.upgradeCommand;
if (!this.skipEslint) {
prettierTransforms.push(
createESLintTransform.call(this, { ignoreErrors }),
createRemoveUnusedImportsTransform.call(this, { ignoreErrors }),
);
}
prettierTransforms.push(
createESLintTransform.call(this, { ignoreErrors }),
createRemoveUnusedImportsTransform.call(this, { ignoreErrors }),
await createPrettierTransform.call(this, {
ignoreErrors,
prettierPackageJson: true,
prettierJava: !this.jhipsterConfig.skipServer,
extensions: this.prettierExtensions.join(','),
prettierOptions: this.prettierOptions,
skipForks: this.skipForks,
}),
);
}
Expand Down
19 changes: 13 additions & 6 deletions generators/bootstrap/support/prettier-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,21 @@ export class PrettierPool extends Piscina {

export const createPrettierTransform = async function (
this: CoreGenerator,
options: PrettierWorkerOptions & { ignoreErrors?: boolean; extensions?: string } = {},
options: PrettierWorkerOptions & { ignoreErrors?: boolean; extensions?: string; skipForks?: boolean } = {},
) {
const pool = new PrettierPool();

const { ignoreErrors = false, extensions = '*', ...workerOptions } = options;
const { ignoreErrors = false, extensions = '*', skipForks, ...workerOptions } = options;
const globExpression = extensions.includes(',') ? `**/*.{${extensions}}` : `**/*.${extensions}`;
const minimatch = new Minimatch(globExpression, { dot: true });

let applyPrettier;
const pool = skipForks ? undefined : new PrettierPool();
if (skipForks) {
const { default: applyPrettierWorker } = await import('./prettier-worker.js');
applyPrettier = applyPrettierWorker;
} else {
applyPrettier = data => pool!.apply(data);
}

return passthrough(
async (file: VinylMemFsEditorFile) => {
if (!minimatch.match(file.path) || !isFileStateModified(file)) {
Expand All @@ -69,7 +76,7 @@ export const createPrettierTransform = async function (
if (!file.contents) {
throw new Error(`File content doesn't exist for ${file.relative}`);
}
const { result, errorMessage } = await pool.apply({
const { result, errorMessage } = await applyPrettier({
relativeFilePath: file.relative,
filePath: file.path,
fileContents: file.contents.toString('utf8'),
Expand All @@ -86,7 +93,7 @@ export const createPrettierTransform = async function (
}
},
() => {
pool.destroy();
pool?.destroy();
},
);
};
Loading
Loading