|
6 | 6 | * found in the LICENSE file at https://angular.dev/license
|
7 | 7 | */
|
8 | 8 |
|
9 |
| -import { json, schema } from '@angular-devkit/core'; |
| 9 | +import { schema } from '@angular-devkit/core'; |
10 | 10 | import yargs from 'yargs';
|
11 | 11 |
|
12 | 12 | import { addSchemaOptionsToCommand, parseJsonSchemaToOptions } from './json-schema';
|
13 | 13 |
|
14 |
| -const YError = (() => { |
15 |
| - try { |
16 |
| - const y = yargs().strict().fail(false).exitProcess(false).parse(['--forced-failure']); |
17 |
| - } catch (e) { |
18 |
| - if (!(e instanceof Error)) { |
19 |
| - throw new Error('Unexpected non-Error thrown'); |
20 |
| - } |
21 |
| - |
22 |
| - return e.constructor as typeof Error; |
23 |
| - } |
24 |
| - throw new Error('Expected parse to fail'); |
25 |
| -})(); |
26 |
| - |
27 |
| -interface ParseFunction { |
28 |
| - (argv: string[]): unknown; |
29 |
| -} |
30 |
| - |
31 |
| -function withParseForSchema( |
32 |
| - jsonSchema: json.JsonObject, |
33 |
| - { |
34 |
| - interactive = true, |
35 |
| - includeDefaultValues = true, |
36 |
| - }: { interactive?: boolean; includeDefaultValues?: boolean } = {}, |
37 |
| -): ParseFunction { |
38 |
| - let actualParse: ParseFunction = () => { |
39 |
| - throw new Error('Called before init'); |
40 |
| - }; |
41 |
| - const parse: ParseFunction = (args) => { |
42 |
| - return actualParse(args); |
43 |
| - }; |
44 |
| - |
45 |
| - beforeEach(async () => { |
46 |
| - const registry = new schema.CoreSchemaRegistry(); |
47 |
| - const options = await parseJsonSchemaToOptions(registry, jsonSchema, interactive); |
48 |
| - |
49 |
| - actualParse = async (args: string[]) => { |
50 |
| - // Create a fresh yargs for each call. The yargs object is stateful and |
51 |
| - // calling .parse multiple times on the same instance isn't safe. |
52 |
| - const localYargs = yargs().exitProcess(false).strict().fail(false); |
53 |
| - addSchemaOptionsToCommand(localYargs, options, includeDefaultValues); |
54 |
| - |
| 14 | +describe('parseJsonSchemaToOptions', () => { |
| 15 | + describe('without required fields in schema', () => { |
| 16 | + const parse = async (args: string[]) => { |
55 | 17 | // Yargs only exposes the parse errors as proper errors when using the
|
56 | 18 | // callback syntax. This unwraps that ugly workaround so tests can just
|
57 | 19 | // use simple .toThrow/.toEqual assertions.
|
58 | 20 | return localYargs.parseAsync(args);
|
59 | 21 | };
|
60 |
| - }); |
61 |
| - |
62 |
| - return parse; |
63 |
| -} |
64 | 22 |
|
65 |
| -describe('parseJsonSchemaToOptions', () => { |
66 |
| - describe('without required fields in schema', () => { |
67 |
| - const parse = withParseForSchema({ |
68 |
| - 'type': 'object', |
69 |
| - 'properties': { |
70 |
| - 'maxSize': { |
71 |
| - 'type': 'number', |
72 |
| - }, |
73 |
| - 'ssr': { |
74 |
| - 'type': 'string', |
75 |
| - 'enum': ['always', 'surprise-me', 'never'], |
76 |
| - }, |
77 |
| - 'arrayWithChoices': { |
78 |
| - 'type': 'array', |
79 |
| - 'items': { |
80 |
| - 'type': 'string', |
81 |
| - 'enum': ['always', 'never'], |
| 23 | + let localYargs: yargs.Argv<unknown>; |
| 24 | + beforeEach(async () => { |
| 25 | + // Create a fresh yargs for each call. The yargs object is stateful and |
| 26 | + // calling .parse multiple times on the same instance isn't safe. |
| 27 | + localYargs = yargs().exitProcess(false).strict().fail(false).wrap(1_000); |
| 28 | + const jsonSchema = { |
| 29 | + 'type': 'object', |
| 30 | + 'properties': { |
| 31 | + 'maxSize': { |
| 32 | + 'type': 'number', |
82 | 33 | },
|
83 |
| - }, |
84 |
| - 'extendable': { |
85 |
| - 'type': 'object', |
86 |
| - 'properties': {}, |
87 |
| - 'additionalProperties': { |
| 34 | + 'ssr': { |
88 | 35 | 'type': 'string',
|
| 36 | + 'enum': ['always', 'surprise-me', 'never'], |
89 | 37 | },
|
90 |
| - }, |
91 |
| - 'someDefine': { |
92 |
| - 'type': 'object', |
93 |
| - 'additionalProperties': { |
94 |
| - 'type': 'string', |
| 38 | + 'arrayWithChoices': { |
| 39 | + 'type': 'array', |
| 40 | + 'default': 'default-array', |
| 41 | + 'items': { |
| 42 | + 'type': 'string', |
| 43 | + 'enum': ['always', 'never', 'default-array'], |
| 44 | + }, |
| 45 | + }, |
| 46 | + 'extendable': { |
| 47 | + 'type': 'object', |
| 48 | + 'properties': {}, |
| 49 | + 'additionalProperties': { |
| 50 | + 'type': 'string', |
| 51 | + }, |
| 52 | + }, |
| 53 | + 'someDefine': { |
| 54 | + 'type': 'object', |
| 55 | + 'additionalProperties': { |
| 56 | + 'type': 'string', |
| 57 | + }, |
95 | 58 | },
|
96 | 59 | },
|
97 |
| - }, |
| 60 | + }; |
| 61 | + const registry = new schema.CoreSchemaRegistry(); |
| 62 | + const options = await parseJsonSchemaToOptions(registry, jsonSchema, false); |
| 63 | + addSchemaOptionsToCommand(localYargs, options, true); |
98 | 64 | });
|
99 | 65 |
|
100 | 66 | describe('type=number', () => {
|
@@ -123,6 +89,10 @@ describe('parseJsonSchemaToOptions', () => {
|
123 | 89 | /Argument: array-with-choices, Given: "yes", Choices:/,
|
124 | 90 | );
|
125 | 91 | });
|
| 92 | + |
| 93 | + it('should add default value to help', async () => { |
| 94 | + expect(await localYargs.getHelp()).toContain('[default: "default-array"]'); |
| 95 | + }); |
126 | 96 | });
|
127 | 97 |
|
128 | 98 | describe('type=string, enum', () => {
|
@@ -150,11 +120,9 @@ describe('parseJsonSchemaToOptions', () => {
|
150 | 120 |
|
151 | 121 | it('rejects invalid values for string maps', async () => {
|
152 | 122 | await expectAsync(parse(['--some-define', 'foo'])).toBeRejectedWithError(
|
153 |
| - YError, |
154 | 123 | /Invalid value for argument: some-define, Given: 'foo', Expected key=value pair/,
|
155 | 124 | );
|
156 | 125 | await expectAsync(parse(['--some-define', '42'])).toBeRejectedWithError(
|
157 |
| - YError, |
158 | 126 | /Invalid value for argument: some-define, Given: '42', Expected key=value pair/,
|
159 | 127 | );
|
160 | 128 | });
|
@@ -187,43 +155,42 @@ describe('parseJsonSchemaToOptions', () => {
|
187 | 155 |
|
188 | 156 | describe('with required positional argument', () => {
|
189 | 157 | it('marks the required argument as required', async () => {
|
190 |
| - const jsonSchema = JSON.parse(` |
191 |
| - { |
192 |
| - "$id": "FakeSchema", |
193 |
| - "title": "Fake Schema", |
194 |
| - "type": "object", |
195 |
| - "required": ["a"], |
196 |
| - "properties": { |
197 |
| - "b": { |
198 |
| - "type": "string", |
199 |
| - "description": "b.", |
200 |
| - "$default": { |
201 |
| - "$source": "argv", |
202 |
| - "index": 1 |
203 |
| - } |
| 158 | + const jsonSchema = { |
| 159 | + '$id': 'FakeSchema', |
| 160 | + 'title': 'Fake Schema', |
| 161 | + 'type': 'object', |
| 162 | + 'required': ['a'], |
| 163 | + 'properties': { |
| 164 | + 'b': { |
| 165 | + 'type': 'string', |
| 166 | + 'description': 'b.', |
| 167 | + '$default': { |
| 168 | + '$source': 'argv', |
| 169 | + 'index': 1, |
| 170 | + }, |
204 | 171 | },
|
205 |
| - "a": { |
206 |
| - "type": "string", |
207 |
| - "description": "a.", |
208 |
| - "$default": { |
209 |
| - "$source": "argv", |
210 |
| - "index": 0 |
211 |
| - } |
| 172 | + 'a': { |
| 173 | + 'type': 'string', |
| 174 | + 'description': 'a.', |
| 175 | + '$default': { |
| 176 | + '$source': 'argv', |
| 177 | + 'index': 0, |
| 178 | + }, |
212 | 179 | },
|
213 |
| - "optC": { |
214 |
| - "type": "string", |
215 |
| - "description": "optC" |
| 180 | + 'optC': { |
| 181 | + 'type': 'string', |
| 182 | + 'description': 'optC', |
216 | 183 | },
|
217 |
| - "optA": { |
218 |
| - "type": "string", |
219 |
| - "description": "optA" |
| 184 | + 'optA': { |
| 185 | + 'type': 'string', |
| 186 | + 'description': 'optA', |
| 187 | + }, |
| 188 | + 'optB': { |
| 189 | + 'type': 'string', |
| 190 | + 'description': 'optB', |
220 | 191 | },
|
221 |
| - "optB": { |
222 |
| - "type": "string", |
223 |
| - "description": "optB" |
224 |
| - } |
225 |
| - } |
226 |
| - }`) as json.JsonObject; |
| 192 | + }, |
| 193 | + }; |
227 | 194 | const registry = new schema.CoreSchemaRegistry();
|
228 | 195 | const options = await parseJsonSchemaToOptions(registry, jsonSchema, /* interactive= */ true);
|
229 | 196 |
|
|
0 commit comments