Skip to content

Commit 273deed

Browse files
committed
Move cwd ENOENT test to where it matters
Fix: #515
1 parent c6a862a commit 273deed

File tree

5 files changed

+60
-30
lines changed

5 files changed

+60
-30
lines changed

package-lock.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"@types/tap": "^15.0.7",
7070
"c8": "^7.12.0",
7171
"eslint-config-prettier": "^8.6.0",
72+
"memfs": "^3.4.13",
7273
"mkdirp": "^2.1.4",
7374
"prettier": "^2.8.3",
7475
"rimraf": "^4.1.3",

src/processor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ export class Processor {
136136
}
137137
}
138138

139+
if (t.isENOENT()) continue
140+
139141
let p: MMPattern
140142
let rest: Pattern | null
141143
let changed = false

src/walker.ts

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -405,30 +405,30 @@ export class GlobWalker<
405405

406406
async walk(): Promise<Matches<O>> {
407407
if (this.signal?.aborted) throw this.signal.reason
408-
const t = this.path.isUnknown() ? await this.path.lstat() : this.path
409-
if (t) {
410-
await new Promise((res, rej) => {
411-
this.walkCB(t, this.patterns, () => {
412-
if (this.signal?.aborted) {
413-
rej(this.signal.reason)
414-
} else {
415-
res(this.matches)
416-
}
417-
})
418-
})
408+
if (this.path.isUnknown()) {
409+
await this.path.lstat()
419410
}
411+
await new Promise((res, rej) => {
412+
this.walkCB(this.path, this.patterns, () => {
413+
if (this.signal?.aborted) {
414+
rej(this.signal.reason)
415+
} else {
416+
res(this.matches)
417+
}
418+
})
419+
})
420420
return this.matches
421421
}
422422

423423
walkSync(): Matches<O> {
424424
if (this.signal?.aborted) throw this.signal.reason
425-
const t = this.path.isUnknown() ? this.path.lstatSync() : this.path
426-
// nothing for the callback to do, because this never pauses
427-
if (t) {
428-
this.walkCBSync(t, this.patterns, () => {
429-
if (this.signal?.aborted) throw this.signal.reason
430-
})
425+
if (this.path.isUnknown()) {
426+
this.path.lstatSync()
431427
}
428+
// nothing for the callback to do, because this never pauses
429+
this.walkCBSync(this.path, this.patterns, () => {
430+
if (this.signal?.aborted) throw this.signal.reason
431+
})
432432
return this.matches
433433
}
434434
}
@@ -463,12 +463,8 @@ export class GlobStream<
463463
stream(): MatchStream<O> {
464464
const target = this.path
465465
if (target.isUnknown()) {
466-
target.lstat().then(e => {
467-
if (e) {
468-
this.walkCB(target, this.patterns, () => this.results.end())
469-
} else {
470-
this.results.end()
471-
}
466+
target.lstat().then(() => {
467+
this.walkCB(target, this.patterns, () => this.results.end())
472468
})
473469
} else {
474470
this.walkCB(target, this.patterns, () => this.results.end())
@@ -477,14 +473,10 @@ export class GlobStream<
477473
}
478474

479475
streamSync(): MatchStream<O> {
480-
const target = this.path.isUnknown()
481-
? this.path.lstatSync()
482-
: this.path
483-
if (target) {
484-
this.walkCBSync(target, this.patterns, () => this.results.end())
485-
} else {
486-
this.results.end()
476+
if (this.path.isUnknown()) {
477+
this.path.lstatSync()
487478
}
479+
this.walkCBSync(this.path, this.patterns, () => this.results.end())
488480
return this.results
489481
}
490482
}

test/memfs.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { fs as memfs, vol } from 'memfs'
2+
import t from 'tap'
3+
import { glob } from '../'
4+
t.beforeEach(() => vol.fromJSON({ '/x': 'abc' }))
5+
6+
const fs = memfs as unknown as typeof import('fs')
7+
8+
t.test('should match single file with pattern', async t => {
9+
const expandedFiles = await glob(['.', '/**/*'], { nodir: true, fs })
10+
t.strictSame(expandedFiles, ['/x'])
11+
})
12+
13+
t.test('should match single file without pattern', async t => {
14+
const expandedFiles = await glob(['.', '/x'], { nodir: true, fs })
15+
t.strictSame(expandedFiles, ['/x'])
16+
})

0 commit comments

Comments
 (0)