@@ -2,13 +2,24 @@ const t = require('tap')
2
2
const noptLib = require ( '../lib/nopt-lib.js' )
3
3
const Stream = require ( 'stream' )
4
4
5
- const nopt = ( t , argv , opts , expected ) => {
5
+ const logs = [ ]
6
+ t . afterEach ( ( ) => {
7
+ logs . length = 0
8
+ } )
9
+ process . on ( 'log' , ( ...msg ) => {
10
+ logs . push ( msg )
11
+ } )
12
+
13
+ const nopt = ( t , argv , opts , expected , expectedLogs ) => {
6
14
if ( Array . isArray ( argv ) ) {
7
15
t . strictSame ( noptLib . nopt ( argv , { typeDefs : noptLib . typeDefs , ...opts } ) , expected )
8
16
} else {
9
17
noptLib . clean ( argv , { typeDefs : noptLib . typeDefs , ...opts } )
10
18
t . match ( argv , expected )
11
19
}
20
+ if ( expectedLogs ) {
21
+ t . match ( expectedLogs , logs )
22
+ }
12
23
t . end ( )
13
24
}
14
25
@@ -125,6 +136,43 @@ t.test('false invalid handler', (t) => {
125
136
} )
126
137
} )
127
138
139
+ t . test ( 'longhand abbreviation' , ( t ) => {
140
+ nopt ( t , [ '--lon' , 'text' ] , {
141
+ types : {
142
+ long : String ,
143
+ } ,
144
+ } , {
145
+ long : 'text' ,
146
+ argv : {
147
+ remain : [ ] ,
148
+ cooked : [ '--lon' , 'text' ] ,
149
+ original : [ '--lon' , 'text' ] ,
150
+ } ,
151
+ } , [
152
+ /* eslint-disable-next-line max-len */
153
+ [ 'warn' , 'Expanding "--lon" to "--long". This will stop working in the next major version of npm.' ] ,
154
+ ] )
155
+ } )
156
+
157
+ t . test ( 'shorthand abbreviation' , ( t ) => {
158
+ nopt ( t , [ '--shor' ] , {
159
+ types : { } ,
160
+ shorthands : {
161
+ short : '--shorthand' ,
162
+ } ,
163
+ } , {
164
+ shorthand : true ,
165
+ argv : {
166
+ remain : [ ] ,
167
+ cooked : [ '--shorthand' ] ,
168
+ original : [ '--shor' ] ,
169
+ } ,
170
+ } , [
171
+ /* eslint-disable-next-line max-len */
172
+ [ 'warn' , 'Expanding "--shor" to "--short". This will stop working in the next major version of npm.' ] ,
173
+ ] )
174
+ } )
175
+
128
176
t . test ( 'shorthands that is the same' , ( t ) => {
129
177
nopt ( t , [ '--sh' ] , {
130
178
types : { } ,
@@ -142,14 +190,16 @@ t.test('shorthands that is the same', (t) => {
142
190
} )
143
191
144
192
t . test ( 'unknown multiple' , ( t ) => {
145
- nopt ( t , [ '--mult' , '--mult' , '--mult' ] , {
193
+ nopt ( t , [ '--mult' , '--mult' , '--mult' , 'extra' ] , {
146
194
types : { } ,
147
195
} , {
148
196
mult : [ true , true , true ] ,
149
197
argv : {
150
- remain : [ ] ,
151
- cooked : [ '--mult' , '--mult' , '--mult' ] ,
152
- original : [ '--mult' , '--mult' , '--mult' ] ,
198
+ remain : [ 'extra' ] ,
199
+ cooked : [ '--mult' , '--mult' , '--mult' , 'extra' ] ,
200
+ original : [ '--mult' , '--mult' , '--mult' , 'extra' ] ,
153
201
} ,
154
- } )
202
+ } , [
203
+ [ 'warn' , '"extra" is being parsed as a normal command line argument.' ] ,
204
+ ] )
155
205
} )
0 commit comments