Skip to content

Commit

Permalink
Move cwd ENOENT test to where it matters
Browse files Browse the repository at this point in the history
Fix: #515
  • Loading branch information
isaacs committed Mar 21, 2023
1 parent c6a862a commit 273deed
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 30 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -69,6 +69,7 @@
"@types/tap": "^15.0.7",
"c8": "^7.12.0",
"eslint-config-prettier": "^8.6.0",
"memfs": "^3.4.13",
"mkdirp": "^2.1.4",
"prettier": "^2.8.3",
"rimraf": "^4.1.3",
Expand Down
2 changes: 2 additions & 0 deletions src/processor.ts
Expand Up @@ -136,6 +136,8 @@ export class Processor {
}
}

if (t.isENOENT()) continue

let p: MMPattern
let rest: Pattern | null
let changed = false
Expand Down
52 changes: 22 additions & 30 deletions src/walker.ts
Expand Up @@ -405,30 +405,30 @@ export class GlobWalker<

async walk(): Promise<Matches<O>> {
if (this.signal?.aborted) throw this.signal.reason
const t = this.path.isUnknown() ? await this.path.lstat() : this.path
if (t) {
await new Promise((res, rej) => {
this.walkCB(t, this.patterns, () => {
if (this.signal?.aborted) {
rej(this.signal.reason)
} else {
res(this.matches)
}
})
})
if (this.path.isUnknown()) {
await this.path.lstat()
}
await new Promise((res, rej) => {
this.walkCB(this.path, this.patterns, () => {
if (this.signal?.aborted) {
rej(this.signal.reason)
} else {
res(this.matches)
}
})
})
return this.matches
}

walkSync(): Matches<O> {
if (this.signal?.aborted) throw this.signal.reason
const t = this.path.isUnknown() ? this.path.lstatSync() : this.path
// nothing for the callback to do, because this never pauses
if (t) {
this.walkCBSync(t, this.patterns, () => {
if (this.signal?.aborted) throw this.signal.reason
})
if (this.path.isUnknown()) {
this.path.lstatSync()
}
// nothing for the callback to do, because this never pauses
this.walkCBSync(this.path, this.patterns, () => {
if (this.signal?.aborted) throw this.signal.reason
})
return this.matches
}
}
Expand Down Expand Up @@ -463,12 +463,8 @@ export class GlobStream<
stream(): MatchStream<O> {
const target = this.path
if (target.isUnknown()) {
target.lstat().then(e => {
if (e) {
this.walkCB(target, this.patterns, () => this.results.end())
} else {
this.results.end()
}
target.lstat().then(() => {
this.walkCB(target, this.patterns, () => this.results.end())
})
} else {
this.walkCB(target, this.patterns, () => this.results.end())
Expand All @@ -477,14 +473,10 @@ export class GlobStream<
}

streamSync(): MatchStream<O> {
const target = this.path.isUnknown()
? this.path.lstatSync()
: this.path
if (target) {
this.walkCBSync(target, this.patterns, () => this.results.end())
} else {
this.results.end()
if (this.path.isUnknown()) {
this.path.lstatSync()
}
this.walkCBSync(this.path, this.patterns, () => this.results.end())
return this.results
}
}
16 changes: 16 additions & 0 deletions test/memfs.ts
@@ -0,0 +1,16 @@
import { fs as memfs, vol } from 'memfs'
import t from 'tap'
import { glob } from '../'
t.beforeEach(() => vol.fromJSON({ '/x': 'abc' }))

const fs = memfs as unknown as typeof import('fs')

t.test('should match single file with pattern', async t => {
const expandedFiles = await glob(['.', '/**/*'], { nodir: true, fs })
t.strictSame(expandedFiles, ['/x'])
})

t.test('should match single file without pattern', async t => {
const expandedFiles = await glob(['.', '/x'], { nodir: true, fs })
t.strictSame(expandedFiles, ['/x'])
})

0 comments on commit 273deed

Please sign in to comment.