@@ -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 } ) ;
0 commit comments