Skip to content
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
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@yeoman/conflicter": "^4.1.0",
"@yeoman/namespace": "^2.1.0",
"@yeoman/transform": "^2.1.1",
"@yeoman/types": "^1.11.0",
"@yeoman/types": "^1.11.1",
"arrify": "^3.0.0",
"chalk": "^5.6.2",
"commander": "^14.0.3",
Expand Down Expand Up @@ -87,7 +87,6 @@
"c8": "^11.0.0",
"eslint": "10.2.1",
"esmocha": "^5.0.0",
"fs-extra": "^11.3.4",
"jsdoc": "^4.0.5",
"prettier": "3.8.3",
"prettier-plugin-packagejson": "^3.0.2",
Expand Down
125 changes: 60 additions & 65 deletions test/generator-features.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-nocheck
import { after, afterEach, before, beforeEach, describe, esmocha, expect, it } from 'esmocha';
import helpers, { getCreateEnv as getCreateEnvironment, result } from './helpers.ts';
import type { BaseGeneratorConstructor, GeneratorFeatures, GeneratorOptions } from '@yeoman/types';
import helpers, { getCreateEnv, result } from './helpers.ts';
import { greaterThan5 } from './generator-versions.ts';
import { createHelpers } from 'yeoman-test';

const commitModule = await import('../src/commit.ts');
const { commitSharedFsTask: originalCommitSharedFsTask } = commitModule;
Expand All @@ -13,9 +13,20 @@ const { packageManagerInstallTask: originalPackageManagerInstallTask } = package
const { packageManagerInstallTask } = await esmocha.mock('../src/package-manager.ts', Promise.resolve(packageManagerModule));
const { default: BasicEnvironment } = await import('../src/environment-base.ts');

const helperWithMockedFeatures = createHelpers({
createEnv: getCreateEnv(BasicEnvironment),
});

for (const generatorVersion of greaterThan5) {
const { default: Generator } = await import(generatorVersion);
class FeaturesGenerator extends Generator {}
const FeaturesGenerator = Generator as BaseGeneratorConstructor;

const createGeneratorClassWithFeatures = (customFeatures: GeneratorFeatures): typeof Generator =>
class extends FeaturesGenerator {
constructor(arguments_?: string[], options?: GeneratorOptions, features?: GeneratorFeatures) {
super(arguments_, options, { ...features, ...customFeatures });
}
};

describe(`environment (generator-features) using ${generatorVersion}`, () => {
afterEach(() => {
Expand All @@ -28,8 +39,8 @@ for (const generatorVersion of greaterThan5) {
describe('customCommitTask feature', () => {
describe('without customInstallTask', () => {
beforeEach(async () => {
await helpers
.run('custom-commit', undefined, { createEnv: getCreateEnvironment(BasicEnvironment) })
await helperWithMockedFeatures
.run('custom-commit')
.withOptions({ skipInstall: true })
.withGenerators([[helpers.createMockedGenerator(Generator), { namespace: 'custom-commit:app' }]]);
});
Expand All @@ -40,24 +51,16 @@ for (const generatorVersion of greaterThan5) {
});

describe('with true customCommitTask', () => {
let runContext;
before(async () => {
runContext = helpers
.create('custom-commit', undefined, { createEnv: getCreateEnvironment(BasicEnvironment) })
await helperWithMockedFeatures
.run('custom-commit')
.withOptions({ skipInstall: true })
.withGenerators([
[
helpers.createMockedGenerator(
class extends FeaturesGenerator {
constructor(arguments_, options) {
super(arguments_, options, { customCommitTask: true });
}
},
),
helpers.createMockedGenerator(createGeneratorClassWithFeatures({ customCommitTask: true })),
{ namespace: 'custom-commit:app' },
],
]);
await runContext.run();
});

it('should not call commitSharedFs', () => {
Expand All @@ -66,33 +69,33 @@ for (const generatorVersion of greaterThan5) {
});

describe('with function customCommitTask', () => {
let runContext;
let customCommitTask;
let commitSharedFsMock: ReturnType<typeof esmocha.fn>;
let customCommitTask: ReturnType<typeof esmocha.fn>;
beforeEach(async () => {
customCommitTask = esmocha.fn();
runContext = helpers
.create('custom-commit')
await helpers
.run('custom-commit')
.withOptions({ skipInstall: true })
.withGenerators([
[
helpers.createMockedGenerator(
class extends FeaturesGenerator {
constructor(arguments_, options) {
super(arguments_, options, { customCommitTask });
constructor(arguments_?: string[], options?: GeneratorOptions, features?: GeneratorFeatures) {
super(arguments_, options, { ...features, customCommitTask });
}
},
),
{ namespace: 'custom-commit:app' },
],
])
.withEnvironment(environment => {
environment.commitSharedFs = esmocha.fn().mockReturnValue(Promise.resolve());
.withEnvironment((environment: any) => {
commitSharedFsMock = esmocha.fn().mockReturnValue(Promise.resolve());
environment.commitSharedFs = commitSharedFsMock;
});
await runContext.run();
});

it('should not call commitSharedFs', () => {
expect(runContext.env.commitSharedFs).not.toHaveBeenCalled();
expect(commitSharedFsMock).not.toHaveBeenCalled();
});

it('should call customCommitTask', () => {
Expand All @@ -103,22 +106,20 @@ for (const generatorVersion of greaterThan5) {

describe('customInstallTask feature', () => {
describe('without customInstallTask', () => {
let runContext;
beforeEach(async () => {
runContext = helpers
.create('custom-install', undefined, { createEnv: getCreateEnvironment(BasicEnvironment) })
await helperWithMockedFeatures
.run('custom-install')
.withOptions({ skipInstall: false })
.withGenerators([
[
class extends FeaturesGenerator {
class extends Generator {
packageJsonTask() {
this.packageJson.set({ name: 'foo' });
}
},
{ namespace: 'custom-install:app' },
],
]);
await runContext.run();
});

it('should call packageManagerInstallTask', () => {
Expand All @@ -132,14 +133,13 @@ for (const generatorVersion of greaterThan5) {
});

describe('v4 compatibility', () => {
let runContext;
beforeEach(async () => {
runContext = helpers
.create('custom-install', undefined, { createEnv: getCreateEnvironment(BasicEnvironment) })
await helperWithMockedFeatures
.run('custom-install')
.withOptions({ skipInstall: false })
.withGenerators([
[
class extends FeaturesGenerator {
class extends Generator {
packageJsonTask() {
this.env.compatibilityMode = 'v4';
this.packageJson.set({ name: 'foo' });
Expand All @@ -148,7 +148,6 @@ for (const generatorVersion of greaterThan5) {
{ namespace: 'custom-install:app' },
],
]);
await runContext.run();
});

it('should not call packageManagerInstallTask', () => {
Expand All @@ -157,16 +156,15 @@ for (const generatorVersion of greaterThan5) {
});

describe('with true customInstallTask', () => {
let runContext;
before(async () => {
runContext = helpers
.create('custom-install', undefined, { createEnv: getCreateEnvironment(BasicEnvironment) })
await helperWithMockedFeatures
.run('custom-install')
.withOptions({ skipInstall: false })
.withGenerators([
[
class extends FeaturesGenerator {
constructor(arguments_, options) {
super(arguments_, options, { customInstallTask: true });
class extends Generator {
constructor(arguments_?: string[], options?: GeneratorOptions, features?: GeneratorFeatures) {
super(arguments_, options, { ...features, customInstallTask: true });
}

packageJsonTask() {
Expand All @@ -176,7 +174,6 @@ for (const generatorVersion of greaterThan5) {
{ namespace: 'custom-install:app' },
],
]);
await runContext.run();
});

it('should not call execa', () => {
Expand All @@ -185,17 +182,17 @@ for (const generatorVersion of greaterThan5) {
});

describe('with function customInstallTask', () => {
let customInstallTask;
let customInstallTask: ReturnType<typeof esmocha.fn>;
beforeEach(async () => {
customInstallTask = esmocha.fn();
await helpers
.run('custom-install')
.withOptions({ skipInstall: false })
.withGenerators([
[
class extends FeaturesGenerator {
constructor(arguments_, options) {
super(arguments_, options, { customInstallTask });
class extends Generator {
constructor(arguments_?: string[], options?: GeneratorOptions, features?: GeneratorFeatures) {
super(arguments_, options, { ...features, customInstallTask });
}

packageJsonTask() {
Expand Down Expand Up @@ -225,14 +222,14 @@ for (const generatorVersion of greaterThan5) {
beforeEach(async () => {
commitSharedFsTask.mockImplementation(originalCommitSharedFsTask);
packageManagerInstallTask.mockImplementation(originalPackageManagerInstallTask);
await helpers
.run('custom-install', undefined, { createEnv: getCreateEnvironment(BasicEnvironment) })
await helperWithMockedFeatures
.run('custom-install')
.withOptions({ skipInstall: false })
.withAnswers({ runInstall: true })
.withGenerators([
[
class extends FeaturesGenerator {
constructor(arguments_, options, features) {
class extends Generator {
constructor(arguments_?: string[], options?: GeneratorOptions, features?: GeneratorFeatures) {
super(arguments_, options, { ...features, customInstallTask: 'ask' });
}

Expand Down Expand Up @@ -267,14 +264,14 @@ for (const generatorVersion of greaterThan5) {
beforeEach(async () => {
commitSharedFsTask.mockImplementation(originalCommitSharedFsTask);
packageManagerInstallTask.mockImplementation(originalPackageManagerInstallTask);
await helpers
.run('custom-install', undefined, { createEnv: getCreateEnvironment(BasicEnvironment) })
await helperWithMockedFeatures
.run('custom-install')
.withOptions({ skipInstall: false })
.withAnswers({ runInstall: false })
.withGenerators([
[
class extends FeaturesGenerator {
constructor(arguments_, options, features) {
class extends Generator {
constructor(arguments_?: string[], options?: GeneratorOptions, features?: GeneratorFeatures) {
super(arguments_, options, { ...features, customInstallTask: 'ask' });
}

Expand Down Expand Up @@ -307,20 +304,19 @@ for (const generatorVersion of greaterThan5) {
});

describe('with function customInstallTask and custom path', () => {
let runContext;
let customInstallTask;
let installTask;
let customInstallTask: ReturnType<typeof esmocha.fn>;
let installTask: (pm: any, defaultTask: (pm: any) => unknown) => unknown;
beforeEach(async () => {
customInstallTask = esmocha.fn();
installTask = (pm, defaultTask) => defaultTask(pm);
runContext = helpers
.create('custom-install', undefined, { createEnv: getCreateEnvironment(BasicEnvironment) })
await helperWithMockedFeatures
.run('custom-install')
.withOptions({ skipInstall: false })
.withGenerators([
[
class extends FeaturesGenerator {
constructor(arguments_, options) {
super(arguments_, options, { customInstallTask });
class extends Generator {
constructor(arguments_?: string[], options?: GeneratorOptions, features?: GeneratorFeatures) {
super(arguments_, options, { ...features, customInstallTask });
this.destinationRoot(this.destinationPath('foo'));
this.env.watchForPackageManagerInstall({
cwd: this.destinationPath(),
Expand All @@ -335,7 +331,6 @@ for (const generatorVersion of greaterThan5) {
{ namespace: 'custom-install:app' },
],
]);
await runContext.run();
});

it('should not call customInstallTask', () => {
Expand Down
7 changes: 4 additions & 3 deletions test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-nocheck
import { createHelpers } from 'yeoman-test';
import Environment from '../src/index.ts';
import BaseEnvironment from '../src/environment-base.ts';
import FullEnvironment from '../src/index.ts';

type EnvironmentConstructor = typeof Environment;
type EnvironmentConstructor = typeof BaseEnvironment;
type CreateEnvArg = string[] | Record<string, unknown> | undefined;

export const getCreateEnv =
Expand All @@ -14,7 +15,7 @@ export const getCreateEnv =
: new EnvironmentCtor(arguments_, ...(others as ConstructorParameters<EnvironmentConstructor>));

export default createHelpers({
createEnv: getCreateEnv(Environment),
createEnv: getCreateEnv(FullEnvironment),
});

export { result } from 'yeoman-test';
Loading
Loading