@@ -11,90 +11,56 @@ import yargs from 'yargs';
1111
1212import { addSchemaOptionsToCommand , parseJsonSchemaToOptions } from './json-schema' ;
1313
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 [ ] ) => {
5517 // Yargs only exposes the parse errors as proper errors when using the
5618 // callback syntax. This unwraps that ugly workaround so tests can just
5719 // use simple .toThrow/.toEqual assertions.
5820 return localYargs . parseAsync ( args ) ;
5921 } ;
60- } ) ;
61-
62- return parse ;
63- }
6422
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' ,
8233 } ,
83- } ,
84- 'extendable' : {
85- 'type' : 'object' ,
86- 'properties' : { } ,
87- 'additionalProperties' : {
34+ 'ssr' : {
8835 'type' : 'string' ,
36+ 'enum' : [ 'always' , 'surprise-me' , 'never' ] ,
8937 } ,
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+ } ,
9558 } ,
9659 } ,
97- } ,
60+ } ;
61+ const registry = new schema . CoreSchemaRegistry ( ) ;
62+ const options = await parseJsonSchemaToOptions ( registry , jsonSchema , false ) ;
63+ addSchemaOptionsToCommand ( localYargs , options , true ) ;
9864 } ) ;
9965
10066 describe ( 'type=number' , ( ) => {
@@ -123,6 +89,10 @@ describe('parseJsonSchemaToOptions', () => {
12389 / A r g u m e n t : a r r a y - w i t h - c h o i c e s , G i v e n : " y e s " , C h o i c e s : / ,
12490 ) ;
12591 } ) ;
92+
93+ it ( 'should add default value to help' , async ( ) => {
94+ expect ( await localYargs . getHelp ( ) ) . toContain ( '[default: "default-array"]' ) ;
95+ } ) ;
12696 } ) ;
12797
12898 describe ( 'type=string, enum' , ( ) => {
@@ -150,11 +120,9 @@ describe('parseJsonSchemaToOptions', () => {
150120
151121 it ( 'rejects invalid values for string maps' , async ( ) => {
152122 await expectAsync ( parse ( [ '--some-define' , 'foo' ] ) ) . toBeRejectedWithError (
153- YError ,
154123 / I n v a l i d v a l u e f o r a r g u m e n t : s o m e - d e f i n e , G i v e n : ' f o o ' , E x p e c t e d k e y = v a l u e p a i r / ,
155124 ) ;
156125 await expectAsync ( parse ( [ '--some-define' , '42' ] ) ) . toBeRejectedWithError (
157- YError ,
158126 / I n v a l i d v a l u e f o r a r g u m e n t : s o m e - d e f i n e , G i v e n : ' 4 2 ' , E x p e c t e d k e y = v a l u e p a i r / ,
159127 ) ;
160128 } ) ;
@@ -187,43 +155,42 @@ describe('parseJsonSchemaToOptions', () => {
187155
188156 describe ( 'with required positional argument' , ( ) => {
189157 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+ } ,
204171 } ,
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+ } ,
212179 } ,
213- " optC" : {
214- " type": " string" ,
215- " description": " optC"
180+ ' optC' : {
181+ ' type' : ' string' ,
182+ ' description' : ' optC' ,
216183 } ,
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' ,
220191 } ,
221- "optB": {
222- "type": "string",
223- "description": "optB"
224- }
225- }
226- }` ) as json . JsonObject ;
192+ } ,
193+ } ;
227194 const registry = new schema . CoreSchemaRegistry ( ) ;
228195 const options = await parseJsonSchemaToOptions ( registry , jsonSchema , /* interactive= */ true ) ;
229196
0 commit comments