-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
52 lines (42 loc) · 1.28 KB
/
index.test.js
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
import test from 'node:test'
import assert from 'node:assert'
import { join } from 'node:path'
import { parseArgs } from 'node:util'
import { formatHelpText, readPkg } from './index.js'
/**
* @typedef {import('./index.js').ArgscloptsParseArgsOptionsConfig} ArgscloptsParseArgsOptionsConfig
*/
test('Help text string is generated', async (_) => {
// @ts-ignore
const pkgPath = join(import.meta.dirname, 'package.json')
/** @type {ArgscloptsParseArgsOptionsConfig} */
const options = {
foo: {
type: 'boolean',
short: 'f',
help: 'A foo flag thats a boolean'
},
bar: {
type: 'string',
help: 'A bar flag thats a string'
}
}
const args = ['-f', '--bar', 'b']
const helpText = await formatHelpText({
options,
pkgPath
})
const {
values,
positionals
} = parseArgs({ args, options })
assert.deepEqual(values, { foo: true, bar: 'b' }, 'Args parse right')
assert.deepEqual(positionals, [], 'Args parse right')
const pkg = await readPkg(pkgPath)
const expect = `Usage: argsclopts [options]
Example: argsclopts
--foo, -f A foo flag thats a boolean
--bar A bar flag thats a string
argsclopts (v${pkg.version})`
assert.equal(helpText, expect, 'help text is generated correctly.')
})