|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { logging, workspaces } from '@angular-devkit/core'; |
10 | | -import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
11 | | -import { tmpdir } from 'node:os'; |
12 | | -import { join } from 'node:path'; |
13 | | -import { PackageManager } from '../package-managers'; |
| 9 | +import { JsonObject, workspaces } from '@angular-devkit/core'; |
14 | 10 | import { AngularWorkspace } from '../utilities/config'; |
15 | | -import { CommandContext } from './definitions'; |
16 | | -import { SchematicsCommandModule } from './schematics-command-module'; |
| 11 | +import { getMissingProjectMessage } from './schematics-command-module'; |
17 | 12 |
|
18 | | -class TestCommandModule extends SchematicsCommandModule { |
19 | | - command = 'test'; |
20 | | - describe = false as const; |
| 13 | +describe('getMissingProjectMessage', () => { |
| 14 | + const projectOption = { type: 'string', $default: { $source: 'projectName' } }; |
| 15 | + const schemaJson: JsonObject = { |
| 16 | + type: 'object', |
| 17 | + properties: { name: { type: 'string' }, project: projectOption }, |
| 18 | + required: ['name', 'project'], |
| 19 | + }; |
21 | 20 |
|
22 | | - run(): Promise<number> { |
23 | | - return this.runSchematic({ |
24 | | - collectionName: './collection.json', |
25 | | - schematicName: 'test', |
26 | | - schematicOptions: {}, |
27 | | - executionOptions: { interactive: false, defaults: false, force: false, dryRun: true }, |
28 | | - }); |
29 | | - } |
30 | | -} |
31 | | - |
32 | | -describe('SchematicsCommandModule', () => { |
33 | | - let root: string; |
34 | | - |
35 | | - beforeAll(() => { |
36 | | - root = mkdtempSync(join(tmpdir(), 'angular-cli-schematics-command-')); |
37 | | - writeFileSync( |
38 | | - join(root, 'collection.json'), |
39 | | - JSON.stringify({ |
40 | | - schematics: { test: { description: 'Test', factory: './index', schema: './schema.json' } }, |
41 | | - }), |
42 | | - ); |
43 | | - writeFileSync( |
44 | | - join(root, 'schema.json'), |
45 | | - JSON.stringify({ |
46 | | - type: 'object', |
47 | | - properties: { project: { type: 'string', $default: { $source: 'projectName' } } }, |
48 | | - required: ['project'], |
49 | | - }), |
50 | | - ); |
51 | | - writeFileSync(join(root, 'index.js'), 'exports.default = () => (tree) => tree;'); |
52 | | - }); |
53 | | - |
54 | | - afterAll(() => { |
55 | | - rmSync(root, { recursive: true, force: true }); |
56 | | - }); |
57 | | - |
58 | | - async function runWithProjects(projectNames: string[]): Promise<[number, string]> { |
| 21 | + function createWorkspace(...projectNames: string[]): AngularWorkspace { |
59 | 22 | const projects = new workspaces.ProjectDefinitionCollection(); |
60 | 23 | for (const name of projectNames) { |
61 | 24 | projects.add({ name, root: `projects/${name}` }); |
62 | 25 | } |
63 | 26 |
|
64 | | - const logger = new logging.Logger('test'); |
65 | | - const messages: string[] = []; |
66 | | - logger.subscribe(({ message }) => messages.push(message)); |
| 27 | + return new AngularWorkspace({ extensions: {}, projects }, '/angular.json'); |
| 28 | + } |
67 | 29 |
|
68 | | - const command = new TestCommandModule({ |
69 | | - root, |
70 | | - logger, |
71 | | - workspace: new AngularWorkspace({ extensions: {}, projects }, join(root, 'angular.json')), |
72 | | - packageManager: { name: 'npm' } as PackageManager, |
73 | | - } as CommandContext); |
| 30 | + it('should list the available projects when the project cannot be determined', () => { |
| 31 | + expect(getMissingProjectMessage(createWorkspace('second', 'first'), schemaJson, {})).toBe( |
| 32 | + 'Cannot determine project for command.\n' + |
| 33 | + 'This is a multi-project workspace and the current working directory is not inside a ' + |
| 34 | + 'single project. Use the "--project" option to specify a project or change the current ' + |
| 35 | + 'working directory to a project directory.\n\n' + |
| 36 | + 'Available projects are:\n- first\n- second', |
| 37 | + ); |
| 38 | + }); |
74 | 39 |
|
75 | | - const exitCode = await command.run(); |
| 40 | + it('should explain how to create a project when the workspace has none', () => { |
| 41 | + expect(getMissingProjectMessage(createWorkspace(), schemaJson, {})).toBe( |
| 42 | + 'Cannot determine project for command.\n' + |
| 43 | + 'This workspace does not contain any projects. ' + |
| 44 | + 'Run "ng generate application" or "ng generate library" to create one.', |
| 45 | + ); |
| 46 | + }); |
76 | 47 |
|
77 | | - return [exitCode, messages.join('\n')]; |
78 | | - } |
| 48 | + it('should use the name of the option that relies on the smart default', () => { |
| 49 | + const message = getMissingProjectMessage( |
| 50 | + createWorkspace('first', 'second'), |
| 51 | + { properties: { targetProject: projectOption }, required: ['targetProject'] }, |
| 52 | + {}, |
| 53 | + ); |
79 | 54 |
|
80 | | - it('should explain how to pick a project when it cannot be determined from the cwd', async () => { |
81 | | - const [exitCode, output] = await runWithProjects(['second-app', 'first-app']); |
| 55 | + expect(message).toContain('Use the "--target-project" option'); |
| 56 | + }); |
82 | 57 |
|
83 | | - expect(exitCode).toBe(1); |
84 | | - expect(output).toContain('Cannot determine project for command.'); |
85 | | - expect(output).toContain('Use the "--project" option to specify a project'); |
86 | | - expect(output).toContain('Available projects are:\n- first-app\n- second-app'); |
87 | | - expect(output).not.toContain(`must have required property 'project'`); |
| 58 | + it('should not return a message when the option has a value', () => { |
| 59 | + expect( |
| 60 | + getMissingProjectMessage(createWorkspace('first', 'second'), schemaJson, { |
| 61 | + project: 'first', |
| 62 | + }), |
| 63 | + ).toBeUndefined(); |
88 | 64 | }); |
89 | 65 |
|
90 | | - it('should explain that the workspace has no projects', async () => { |
91 | | - const [exitCode, output] = await runWithProjects([]); |
| 66 | + it('should not return a message when the option is not required', () => { |
| 67 | + expect( |
| 68 | + getMissingProjectMessage( |
| 69 | + createWorkspace('first', 'second'), |
| 70 | + { ...schemaJson, required: ['name'] }, |
| 71 | + {}, |
| 72 | + ), |
| 73 | + ).toBeUndefined(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should not return a message when the option does not use the smart default', () => { |
| 77 | + expect( |
| 78 | + getMissingProjectMessage( |
| 79 | + createWorkspace('first', 'second'), |
| 80 | + { ...schemaJson, properties: { project: { type: 'string' } } }, |
| 81 | + {}, |
| 82 | + ), |
| 83 | + ).toBeUndefined(); |
| 84 | + }); |
92 | 85 |
|
93 | | - expect(exitCode).toBe(1); |
94 | | - expect(output).toContain('This workspace does not contain any projects.'); |
95 | | - expect(output).not.toContain('Available projects are:'); |
| 86 | + it('should not return a message when the option can be prompted for', () => { |
| 87 | + expect( |
| 88 | + getMissingProjectMessage( |
| 89 | + createWorkspace('first', 'second'), |
| 90 | + { |
| 91 | + ...schemaJson, |
| 92 | + properties: { project: { ...projectOption, 'x-prompt': 'Which project?' } }, |
| 93 | + }, |
| 94 | + {}, |
| 95 | + ), |
| 96 | + ).toBeUndefined(); |
96 | 97 | }); |
97 | 98 | }); |
0 commit comments