Skip to content

Commit

Permalink
join string pattern sections properly
Browse files Browse the repository at this point in the history
Fix: #547
  • Loading branch information
isaacs committed Aug 30, 2023
1 parent 46ff653 commit 61cc5a2
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ t () {
# warm up the fs cache so we don't get a spurious slow first result
bash -c 'for i in **; do :; done'

cd "$wd/bench-working-dir/fixture"

for p in "${patterns[@]}"; do
echo
Expand Down
2 changes: 2 additions & 0 deletions patterns.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
patterns=(
'{0000,0,1111,1}/{0000,0,1111,1}/{0000,0,1111,1}/**'

'**'
'**/..'

Expand Down
5 changes: 4 additions & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { foregroundChild } from 'foreground-child'
import { existsSync } from 'fs'
import { jack } from 'jackspeak'
import { join } from 'path'
import { version } from '../package.json'
import { globStream } from './index.js'

Expand Down Expand Up @@ -234,7 +235,9 @@ try {
const patterns = values.all
? positionals
: positionals.filter(p => !existsSync(p))
const matches = values.all ? [] : positionals.filter(p => existsSync(p))
const matches = values.all
? []
: positionals.filter(p => existsSync(p)).map(p => join(p))
const stream = globStream(patterns, {
absolute: values.absolute,
cwd: values.cwd,
Expand Down
13 changes: 4 additions & 9 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ export class Processor {
(rest = pattern.rest())
) {
const c = t.resolve(p)
// we can be reasonably sure that .. is a readable dir
if (c.isUnknown() && p !== '..') break
t = c
pattern = rest
changed = true
Expand All @@ -163,13 +161,10 @@ export class Processor {
// more strings for an unknown entry,
// or a pattern starting with magic, mounted on t.
if (typeof p === 'string') {
// must be final entry
if (!rest) {
const ifDir = p === '..' || p === '' || p === '.'
this.matches.add(t.resolve(p), absolute, ifDir)
} else {
this.subwalks.add(t, pattern)
}
// must not be final entry, otherwise we would have
// concatenated it earlier.
const ifDir = p === '..' || p === '' || p === '.'
this.matches.add(t.resolve(p), absolute, ifDir)
continue
} else if (p === GLOBSTAR) {
// if no rest, match and subwalk pattern
Expand Down
14 changes: 10 additions & 4 deletions test/bin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { spawn, SpawnOptions } from 'child_process'
import t from 'tap'
import { sep } from 'path'
import t from 'tap'
import { version } from '../package.json'
const bin = require.resolve('../dist/cjs/src/bin.js')

Expand Down Expand Up @@ -66,8 +66,14 @@ t.test('finds matches for a pattern', async t => {

const c = `node -p "process.argv.map(s=>s.toUpperCase())"`
const cmd = await run(['**/*.y', '-c', c], { cwd })
t.match(cmd.stdout, `'a${sep}x.y'`.toUpperCase())
t.match(cmd.stdout, `'a${sep}b${sep}z.y'`.toUpperCase())
t.match(cmd.stdout, `'a${sep.replace(/\\/g, '\\\\')}x.y'`.toUpperCase())
t.match(
cmd.stdout,
`'a${sep.replace(/\\/g, '\\\\')}b${sep.replace(
/\\/g,
'\\\\'
)}z.y'`.toUpperCase()
)
})

t.test('prioritizes exact match if exists, unless --all', async t => {
Expand All @@ -76,7 +82,7 @@ t.test('prioritizes exact match if exists, unless --all', async t => {
'[id].tsx': '',
'i.tsx': '',
'd.tsx': '',
}
},
})
const res = await run(['routes/[id].tsx'], { cwd })
t.equal(res.stdout, `routes${sep}[id].tsx\n`)
Expand Down
24 changes: 24 additions & 0 deletions test/progra-tilde.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// https://github.com/isaacs/node-glob/issues/547
import t from 'tap'

import { globSync } from '../dist/cjs/src/index.js'

if (process.platform !== 'win32') {
t.pass('no need to test this except on windows')
process.exit(0)
}

const dir = t.testdir({
'program files': {
a: '',
b: '',
c: '',
},
})

t.strictSame(
globSync('progra~1\\*', { cwd: dir, windowsPathsNoEscape: true }).sort(
(a, b) => a.localeCompare(b, 'en')
),
['a', 'b', 'c']
)

0 comments on commit 61cc5a2

Please sign in to comment.