Skip to content

Commit 2a4c3ea

Browse files
committed
add tests for bin
1 parent d64372e commit 2a4c3ea

File tree

2 files changed

+229
-0
lines changed

2 files changed

+229
-0
lines changed

tap-snapshots/test/bin.ts.test.cjs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/bin.ts TAP usage > -h shows usage 1`] = `
9+
Object {
10+
"args": Array [
11+
"-h",
12+
],
13+
"code": 0,
14+
"options": Object {},
15+
"signal": null,
16+
"stderr": "",
17+
"stdout": String(
18+
Usage:
19+
glob [options] [<pattern> [<pattern> ...]]
20+
21+
Glob v10.2.7
22+
23+
Expand the positional glob expression arguments into any matching file system
24+
paths found.
25+
26+
-c<command> --cmd=<command>
27+
Run the command provided, passing the glob expression
28+
matches as arguments.
29+
30+
-p<pattern> --default=<pattern>
31+
If no positional arguments are provided, glob will use
32+
this pattern
33+
34+
-A --all By default, the glob cli command will not expand any
35+
arguments that are an exact match to a file on disk.
36+
37+
This prevents double-expanding, in case the shell
38+
expands an argument whose filename is a glob
39+
expression.
40+
41+
For example, if 'app/*.ts' would match 'app/[id].ts',
42+
then on Windows powershell or cmd.exe, 'glob app/*.ts'
43+
will expand to 'app/[id].ts', as expected. However, in
44+
posix shells such as bash or zsh, the shell will first
45+
expand 'app/*.ts' to a list of filenames. Then glob
46+
will look for a file matching 'app/[id].ts' (ie,
47+
'app/i.ts' or 'app/d.ts'), which is unexpected.
48+
49+
Setting '--all' prevents this behavior, causing glob to
50+
treat ALL patterns as glob expressions to be expanded,
51+
even if they are an exact match to a file on disk.
52+
53+
When setting this option, be sure to enquote arguments
54+
so that the shell will not expand them prior to passing
55+
them to the glob command process.
56+
57+
-a --absolute Expand to absolute paths
58+
-d --dot-relative Prepend './' on relative matches
59+
-m --mark Append a / on any directories matched
60+
-x --posix Always resolve to posix style paths, using '/' as the
61+
directory separator, even on Windows. Drive letter
62+
absolute matches on Windows will be expanded to their
63+
full resolved UNC maths, eg instead of 'C:\\\\foo\\\\bar', it
64+
will expand to '//?/C:/foo/bar'.
65+
66+
-f --follow Follow symlinked directories when expanding '**'
67+
-R --realpath Call 'fs.realpath' on all of the results. In the case
68+
of an entry that cannot be resolved, the entry is
69+
omitted. This incurs a slight performance penalty, of
70+
course, because of the added system calls.
71+
72+
-s --stat Call 'fs.lstat' on all entries, whether required or not
73+
to determine if it's a valid match.
74+
75+
-b --match-base Perform a basename-only match if the pattern does not
76+
contain any slash characters. That is, '*.js' would be
77+
treated as equivalent to '**/*.js', matching js files
78+
in all directories.
79+
80+
--dot Allow patterns to match files/directories that start
81+
with '.', even if the pattern does not start with '.'
82+
83+
--nobrace Do not expand {...} patterns
84+
--nocase Perform a case-insensitive match. This defaults to
85+
'true' on macOS and Windows platforms, and false on all
86+
others.
87+
88+
Note: 'nocase' should only be explicitly set when it is
89+
known that the filesystem's case sensitivity differs
90+
from the platform default. If set 'true' on
91+
case-insensitive file systems, then the walk may return
92+
more or less results than expected.
93+
94+
--nodir Do not match directories, only files.
95+
96+
Note: to *only* match directories, append a '/' at the
97+
end of the pattern.
98+
99+
--noext Do not expand extglob patterns, such as '+(a|b)'
100+
--noglobstar Do not expand '**' against multiple path portions. Ie,
101+
treat it as a normal '*' instead.
102+
103+
--windows-path-no-escape
104+
Use '\\\\' as a path separator *only*, and *never* as an
105+
escape character. If set, all '\\\\' characters are
106+
replaced with '/' in the pattern.
107+
108+
-D<n> --max-depth=<n> Maximum depth to traverse from the current working
109+
directory
110+
111+
-C<cwd> --cwd=<cwd> Current working directory to execute/match in
112+
-r<root> --root=<root> A string path resolved against the 'cwd', which is used
113+
as the starting point for absolute patterns that start
114+
with '/' (but not drive letters or UNC paths on
115+
Windows).
116+
117+
Note that this *doesn't* necessarily limit the walk to
118+
the 'root' directory, and doesn't affect the cwd
119+
starting point for non-absolute patterns. A pattern
120+
containing '..' will still be able to traverse out of
121+
the root directory, if it is not an actual root
122+
directory on the filesystem, and any non-absolute
123+
patterns will still be matched in the 'cwd'.
124+
125+
To start absolute and non-absolute patterns in the same
126+
path, you can use '--root=' to set it to the empty
127+
string. However, be aware that on Windows systems, a
128+
pattern like 'x:/*' or '//host/share/*' will *always*
129+
start in the 'x:/' or '//host/share/' directory,
130+
regardless of the --root setting.
131+
132+
--platform=<platform> Defaults to the value of 'process.platform' if
133+
available, or 'linux' if not. Setting --platform=win32
134+
on non-Windows systems may cause strange behavior!
135+
136+
-i<ignore> --ignore=<ignore>
137+
Glob patterns to ignore Can be set multiple times
138+
-v --debug Output a huge amount of noisy debug information about
139+
patterns as they are parsed and used to match files.
140+
141+
-h --help Show this usage information
142+
143+
),
144+
}
145+
`

test/bin.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { spawn, SpawnOptions } from 'child_process'
2+
import t from 'tap'
3+
import { sep } from 'path'
4+
const bin = require.resolve('../dist/cjs/src/bin.js')
5+
6+
interface Result {
7+
args: string[]
8+
options: SpawnOptions
9+
stdout: string
10+
stderr: string
11+
code: number | null
12+
signal: NodeJS.Signals | null
13+
}
14+
const run = async (args: string[], options = {}) => {
15+
const proc = spawn(
16+
process.execPath,
17+
['--enable-source-maps', bin, ...args],
18+
options
19+
)
20+
const out: Buffer[] = []
21+
const err: Buffer[] = []
22+
proc.stdout.on('data', c => out.push(c))
23+
proc.stderr.on('data', c => err.push(c))
24+
return new Promise<Result>(res => {
25+
proc.on('close', (code, signal) => {
26+
res({
27+
args,
28+
options,
29+
stdout: Buffer.concat(out).toString(),
30+
stderr: Buffer.concat(err).toString(),
31+
code,
32+
signal,
33+
})
34+
})
35+
})
36+
}
37+
38+
t.test('usage', async t => {
39+
t.matchSnapshot(await run(['-h']), '-h shows usage')
40+
const res = await run([])
41+
t.equal(res.code, 1, 'exit with code 1 when no args')
42+
t.match(res.stderr, 'No patterns provided')
43+
t.match(res.stderr, /-h --help +Show this usage information$/m)
44+
const badp = await run(['--platform=glorb'])
45+
t.equal(badp.code, 1, 'exit with code 1 on bad platform arg')
46+
t.match(badp.stderr, 'Invalid value provided for --platform: "glorb"\n')
47+
})
48+
49+
t.test('finds matches for a pattern', async t => {
50+
const cwd = t.testdir({
51+
a: {
52+
'x.y': '',
53+
'x.a': '',
54+
b: {
55+
'z.y': '',
56+
'z.a': '',
57+
},
58+
},
59+
})
60+
const res = await run(['**/*.y'], { cwd })
61+
t.match(res.stdout, `a${sep}x.y\n`)
62+
t.match(res.stdout, `a${sep}b${sep}z.y\n`)
63+
64+
const c = `node -p "process.argv.map(s=>s.toUpperCase())"`
65+
const cmd = await run(['**/*.y', '-c', c], { cwd })
66+
t.match(cmd.stdout, `'a${sep}x.y'`.toUpperCase())
67+
t.match(cmd.stdout, `'a${sep}b${sep}z.y'`.toUpperCase())
68+
})
69+
70+
t.test('prioritizes exact match if exists, unless --all', async t => {
71+
const cwd = t.testdir({
72+
routes: {
73+
'[id].tsx': '',
74+
'i.tsx': '',
75+
'd.tsx': '',
76+
}
77+
})
78+
const res = await run(['routes/[id].tsx'], { cwd })
79+
t.equal(res.stdout, 'routes/[id].tsx\n')
80+
81+
const all = await run(['routes/[id].tsx', '--all'], { cwd })
82+
t.match(all.stdout, 'routes/i.tsx\n')
83+
t.match(all.stdout, 'routes/d.tsx\n')
84+
})

0 commit comments

Comments
 (0)