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
|
const t = require('tap')
const Definition = require('../../../../lib/utils/config/definition.js')
const {
typeDefs: {
semver: { type: semver },
Umask: { type: Umask },
url: { type: url },
path: { type: path },
},
} = require('@npmcli/config')
t.test('basic definition', async t => {
const def = new Definition('key', {
default: 'some default value',
type: [Number, String],
description: 'just a test thingie',
})
t.same(def, {
constructor: Definition,
key: 'key',
default: 'some default value',
defaultDescription: '"some default value"',
type: [Number, String],
hint: '<key>',
usage: '--key <key>|--key <key>',
typeDescription: 'Number or String',
description: 'just a test thingie',
})
t.matchSnapshot(def.describe(), 'human-readable description')
const deprecated = new Definition('deprecated', {
deprecated: 'do not use this',
default: 1234,
description: ' it should not be used\n ever\n\n not even once.\n\n',
type: Number,
defaultDescription: 'A number bigger than 1',
typeDescription: 'An expression of a numeric quantity using numerals',
})
t.matchSnapshot(deprecated.describe(), 'description of deprecated thing')
const nullOrUmask = new Definition('key', {
default: null,
type: [null, Umask],
description: 'asdf',
})
t.equal(nullOrUmask.typeDescription, 'null or Octal numeric string in range 0000..0777 (0..511)')
const nullDateOrBool = new Definition('key', {
default: 7,
type: [null, Date, Boolean],
description: 'asdf',
})
t.equal(nullDateOrBool.typeDescription, 'null, Date, or Boolean')
const manyPaths = new Definition('key', {
default: ['asdf'],
type: [path, Array],
description: 'asdf',
})
t.equal(manyPaths.typeDescription, 'Path (can be set multiple times)')
const pathOrUrl = new Definition('key', {
default: ['https://example.com'],
type: [path, url],
description: 'asdf',
})
t.equal(pathOrUrl.typeDescription, 'Path or URL')
const multi12 = new Definition('key', {
default: [],
type: [1, 2, Array],
description: 'asdf',
})
t.equal(multi12.typeDescription, '1 or 2 (can be set multiple times)')
const multi123 = new Definition('key', {
default: [],
type: [1, 2, 3, Array],
description: 'asdf',
})
t.equal(multi123.typeDescription, '1, 2, or 3 (can be set multiple times)')
const multi123Semver = new Definition('key', {
default: [],
type: [1, 2, 3, Array, semver],
description: 'asdf',
})
t.equal(multi123Semver.typeDescription, '1, 2, 3, or SemVer string (can be set multiple times)')
const hasUsage = new Definition('key', {
default: 'test default',
type: String,
description: 'test description',
usage: 'test usage',
})
t.equal(hasUsage.usage, 'test usage')
const hasShort = new Definition('key', {
default: 'test default',
short: 't',
type: String,
description: 'test description',
})
t.equal(hasShort.usage, '-t|--key <key>')
const hardCodedTypes = new Definition('key', {
default: 'test default',
type: ['string1', 'string2'],
description: 'test description',
})
t.equal(hardCodedTypes.usage, '--key <string1|string2>')
const hardCodedOptionalTypes = new Definition('key', {
default: 'test default',
type: [null, 'string1', 'string2'],
description: 'test description',
})
t.equal(hardCodedOptionalTypes.usage, '--key <string1|string2>')
const hasHint = new Definition('key', {
default: 'test default',
type: String,
description: 'test description',
hint: '<testparam>',
})
t.equal(hasHint.usage, '--key <testparam>')
})
t.test('missing fields', async t => {
t.throws(() => new Definition('lacks-default', {
description: 'no default',
type: String,
}), { message: 'config lacks default: lacks-default' })
t.throws(() => new Definition('lacks-type', {
description: 'no type',
default: 1234,
}), { message: 'config lacks type: lacks-type' })
t.throws(() => new Definition(null, {
description: 'falsey key',
default: 1234,
type: Number,
}), { message: 'config lacks key: null' })
t.throws(() => new Definition('extra-field', {
type: String,
default: 'extra',
extra: 'more than is wanted',
description: 'this is not ok',
}), { message: 'config defines unknown field extra: extra-field' })
})
t.test('long description', async t => {
const { stdout: { columns } } = process
t.teardown(() => process.stdout.columns = columns)
const long = new Definition('walden', {
description: `
WHEN I WROTE the following pages, or rather the bulk of them, I lived
alone, in the woods, a mile from any neighbor, in a house which I had
built myself, on the shore of Walden Pond, in Concord, Massachusetts, and
earned my living by the labor of my hands only. I lived there two years
and two months. At present I am a sojourner in civilized life again.
I should not obtrude my affairs so much on the notice of my readers if
very particular inquiries had not been made by my townsmen concerning my
mode of life, which some would call impertinent, though they do not
appear to me at all impertinent, but, considering the circumstances, very
natural and pertinent.
\`\`\`
this.is('a', {
code: 'sample',
})
with (multiple) {
blocks()
}
\`\`\`
`,
default: true,
type: Boolean,
})
process.stdout.columns = 40
t.matchSnapshot(long.describe(), 'cols=40')
process.stdout.columns = 9000
t.matchSnapshot(long.describe(), 'cols=9000')
process.stdout.columns = 0
t.matchSnapshot(long.describe(), 'cols=0')
process.stdout.columns = -1
t.matchSnapshot(long.describe(), 'cols=-1')
process.stdout.columns = NaN
t.matchSnapshot(long.describe(), 'cols=NaN')
})
|