|
1 | 1 | import assert from 'node:assert'; |
2 | 2 | import { stub } from 'sinon'; |
3 | 3 | import { after, afterEach, before, beforeEach, describe, esmocha, expect, it } from 'esmocha'; |
4 | | -import helpers, { getCreateEnv as getCreateEnvironment } from './helpers.js'; |
| 4 | +import helpers, { getCreateEnv as getCreateEnvironment, result } from './helpers.js'; |
5 | 5 | import { greaterThan5 } from './generator-versions.js'; |
6 | 6 |
|
7 | | -const { commitSharedFsTask } = await esmocha.mock('../src/commit.ts', import('../src/commit.ts')); |
8 | | -const { packageManagerInstallTask } = await esmocha.mock('../src/package-manager.ts', import('../src/package-manager.ts')); |
| 7 | +const commitModule = await import('../src/commit.ts'); |
| 8 | +const { commitSharedFsTask: originalCommitSharedFsTask } = commitModule; |
| 9 | +const { commitSharedFsTask } = await esmocha.mock('../src/commit.ts', Promise.resolve(commitModule)); |
9 | 10 | const { execa } = await esmocha.mock('execa', import('execa')); |
| 11 | +const packageManagerModule = await import('../src/package-manager.ts'); |
| 12 | +const { packageManagerInstallTask: originalPackageManagerInstallTask } = packageManagerModule; |
| 13 | +const { packageManagerInstallTask } = await esmocha.mock('../src/package-manager.ts', Promise.resolve(packageManagerModule)); |
10 | 14 | const { default: BasicEnvironment } = await import('../src/environment-base.ts'); |
11 | 15 |
|
12 | 16 | for (const generatorVersion of greaterThan5) { |
@@ -216,6 +220,92 @@ for (const generatorVersion of greaterThan5) { |
216 | 220 | }); |
217 | 221 | }); |
218 | 222 |
|
| 223 | + describe('with ask customInstallTask', () => { |
| 224 | + describe('accepting to run install', () => { |
| 225 | + beforeEach(async () => { |
| 226 | + commitSharedFsTask.mockImplementation(originalCommitSharedFsTask); |
| 227 | + packageManagerInstallTask.mockImplementation(originalPackageManagerInstallTask); |
| 228 | + await helpers |
| 229 | + .run('custom-install', undefined, { createEnv: getCreateEnvironment(BasicEnvironment) }) |
| 230 | + .withOptions({ skipInstall: false }) |
| 231 | + .withAnswers({ runInstall: true }) |
| 232 | + .withGenerators([ |
| 233 | + [ |
| 234 | + class extends FeaturesGenerator { |
| 235 | + constructor(arguments_, options, features) { |
| 236 | + super(arguments_, options, { ...features, customInstallTask: 'ask' }); |
| 237 | + } |
| 238 | + |
| 239 | + packageJsonTask() { |
| 240 | + this.packageJson.set({ name: 'foo' }); |
| 241 | + } |
| 242 | + }, |
| 243 | + { namespace: 'custom-install:app' }, |
| 244 | + ], |
| 245 | + ]); |
| 246 | + }); |
| 247 | + |
| 248 | + it('should write package.json', () => { |
| 249 | + result.assertFile('package.json'); |
| 250 | + }); |
| 251 | + |
| 252 | + it('should call packageManagerInstallTask', () => { |
| 253 | + expect(packageManagerInstallTask).toHaveBeenCalledTimes(1); |
| 254 | + expect(packageManagerInstallTask).toHaveBeenCalledWith( |
| 255 | + expect.objectContaining({ |
| 256 | + customInstallTask: 'ask', |
| 257 | + }), |
| 258 | + ); |
| 259 | + }); |
| 260 | + |
| 261 | + it('should call execa', () => { |
| 262 | + expect(execa).toHaveBeenCalled(); |
| 263 | + }); |
| 264 | + }); |
| 265 | + |
| 266 | + describe('declining to run install', () => { |
| 267 | + beforeEach(async () => { |
| 268 | + commitSharedFsTask.mockImplementation(originalCommitSharedFsTask); |
| 269 | + packageManagerInstallTask.mockImplementation(originalPackageManagerInstallTask); |
| 270 | + await helpers |
| 271 | + .run('custom-install', undefined, { createEnv: getCreateEnvironment(BasicEnvironment) }) |
| 272 | + .withOptions({ skipInstall: false }) |
| 273 | + .withAnswers({ runInstall: false }) |
| 274 | + .withGenerators([ |
| 275 | + [ |
| 276 | + class extends FeaturesGenerator { |
| 277 | + constructor(arguments_, options, features) { |
| 278 | + super(arguments_, options, { ...features, customInstallTask: 'ask' }); |
| 279 | + } |
| 280 | + |
| 281 | + packageJsonTask() { |
| 282 | + this.packageJson.set({ name: 'foo' }); |
| 283 | + } |
| 284 | + }, |
| 285 | + { namespace: 'custom-install:app' }, |
| 286 | + ], |
| 287 | + ]); |
| 288 | + }); |
| 289 | + |
| 290 | + it('should write package.json', () => { |
| 291 | + result.assertFile('package.json'); |
| 292 | + }); |
| 293 | + |
| 294 | + it('should call packageManagerInstallTask', () => { |
| 295 | + expect(packageManagerInstallTask).toHaveBeenCalledTimes(1); |
| 296 | + expect(packageManagerInstallTask).toHaveBeenCalledWith( |
| 297 | + expect.objectContaining({ |
| 298 | + customInstallTask: 'ask', |
| 299 | + }), |
| 300 | + ); |
| 301 | + }); |
| 302 | + |
| 303 | + it('should not call execa', () => { |
| 304 | + expect(execa).not.toHaveBeenCalled(); |
| 305 | + }); |
| 306 | + }); |
| 307 | + }); |
| 308 | + |
219 | 309 | describe('with function customInstallTask and custom path', () => { |
220 | 310 | let runContext; |
221 | 311 | let customInstallTask; |
|
0 commit comments