Repository navigation
Expand file tree
/
Copy patharchitect-base-command-module_spec.ts
More file actions
228 lines (197 loc) · 6.39 KB
/
Copy patharchitect-base-command-module_spec.ts
File metadata and controls
228 lines (197 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { Target } from '@angular-devkit/architect';
import { logging } from '@angular-devkit/core';
import type { ArgumentsCamelCase, Argv } from 'yargs';
import { ArchitectBaseCommandModule } from './architect-base-command-module';
import { CommandModuleError, Options, OtherOptions } from './command-module';
import { CommandContext } from './definitions';
import { Option } from './utilities/json-schema';
class TestArchitectCommand extends ArchitectBaseCommandModule<{}> {
override readonly command = 'test';
override readonly describe = 'Test architect command';
override readonly shouldReportAnalytics = false;
builderOptions: Option[] = [];
builderName = '@angular/build:unit-test';
scheduledOptions: Record<string, unknown> | undefined;
override builder(argv: Argv): Argv {
return argv;
}
override async run(options: Options<{}> & OtherOptions): Promise<number> {
return this.runSingleTarget({ project: 'app', target: 'test' }, options);
}
protected override getArchitectHost() {
return {
getBuilderNameForTarget: async (_target: Target) => this.builderName,
} as never;
}
protected override async getOptionsForBuilder(_builderConf: string): Promise<Option[]> {
return this.builderOptions;
}
protected override getArchitect() {
return {
scheduleTarget: async (_target: Target, options: Record<string, unknown>) => {
this.scheduledOptions = options;
return {
lastOutput: Promise.resolve({ success: true }),
stop: async () => {},
};
},
} as never;
}
}
function createCommandContext(): CommandContext {
return {
currentDirectory: '/workspace',
root: '/workspace',
logger: new logging.NullLogger(),
args: {
positional: [],
options: {
help: false,
jsonHelp: false,
getYargsCompletions: false,
},
},
} as unknown as CommandContext;
}
describe('ArchitectBaseCommandModule passthrough arguments', () => {
it('forwards arguments after "--" to the x-passthrough option', async () => {
const command = new TestArchitectCommand(createCommandContext());
command.builderOptions = [
{
name: 'runnerArgs',
description: 'Runner args',
type: 'array',
passthrough: true,
},
];
const args = {
_: ['test'],
$0: 'ng',
'--': ['--update', '--bail=1'],
watch: false,
} as unknown as ArgumentsCamelCase<{}> & OtherOptions;
await command.handler(args);
expect(command.scheduledOptions).toEqual({
watch: false,
runnerArgs: ['--update', '--bail=1'],
});
});
it('concatenates explicit option values before "--" passthrough arguments', async () => {
const command = new TestArchitectCommand(createCommandContext());
command.builderOptions = [
{
name: 'runnerArgs',
description: 'Runner args',
type: 'array',
passthrough: true,
},
];
const args = {
_: ['test'],
$0: 'ng',
'runner-args': ['--shard=1/2'],
'--': ['--update'],
} as unknown as ArgumentsCamelCase<{}> & OtherOptions;
await command.handler(args);
expect(command.scheduledOptions).toEqual({
runnerArgs: ['--shard=1/2', '--update'],
});
});
it('rejects "--" arguments when the target builder has no x-passthrough option', async () => {
const command = new TestArchitectCommand(createCommandContext());
command.builderName = '@angular/build:application';
command.builderOptions = [
{
name: 'watch',
description: 'Watch',
type: 'boolean',
},
];
const args = {
_: ['build'],
$0: 'ng',
'--': ['--update'],
} as unknown as ArgumentsCamelCase<{}> & OtherOptions;
await expectAsync(command.handler(args)).toBeRejectedWithError(
CommandModuleError,
"The '@angular/build:application' builder does not accept arguments after '--'.",
);
expect(command.scheduledOptions).toBeUndefined();
});
it('rejects "--" arguments when the target builder defines multiple x-passthrough options', async () => {
const command = new TestArchitectCommand(createCommandContext());
command.builderOptions = [
{
name: 'runnerArgs',
description: 'Runner args',
type: 'array',
passthrough: true,
},
{
name: 'extraArgs',
description: 'Extra args',
type: 'array',
passthrough: true,
},
];
const args = {
_: ['test'],
$0: 'ng',
'--': ['--update'],
} as unknown as ArgumentsCamelCase<{}> & OtherOptions;
await expectAsync(command.handler(args)).toBeRejectedWithError(
CommandModuleError,
"The '@angular/build:unit-test' builder schema defines multiple options with 'x-passthrough'.",
);
expect(command.scheduledOptions).toBeUndefined();
});
it('rejects "--" arguments when the x-passthrough option is not an array', async () => {
const command = new TestArchitectCommand(createCommandContext());
command.builderOptions = [
{
name: 'runnerArg',
description: 'Runner arg',
type: 'string',
passthrough: true,
},
];
const args = {
_: ['test'],
$0: 'ng',
'--': ['--update'],
} as unknown as ArgumentsCamelCase<{}> & OtherOptions;
await expectAsync(command.handler(args)).toBeRejectedWithError(
CommandModuleError,
"The '@angular/build:unit-test' builder option 'runnerArg' has 'x-passthrough' enabled but is not an array.",
);
expect(command.scheduledOptions).toBeUndefined();
});
it('wraps a scalar existing option value before "--" passthrough arguments', async () => {
const command = new TestArchitectCommand(createCommandContext());
command.builderOptions = [
{
name: 'runnerArgs',
description: 'Runner args',
type: 'array',
passthrough: true,
},
];
const args = {
_: ['run'],
$0: 'ng',
'runner-args': '--shard=1/2',
'--': ['--update'],
} as unknown as ArgumentsCamelCase<{}> & OtherOptions;
await command.handler(args);
expect(command.scheduledOptions).toEqual({
runnerArgs: ['--shard=1/2', '--update'],
});
});
});