Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"README.md",
"LICENSE"
],
"limit": "129.25 kB",
"limit": "129.60 kB",
"brotli": false,
"gzip": false
},
Expand All @@ -33,7 +33,7 @@
"build/globals.js",
"build/deno.js"
],
"limit": "847.80 kB",
"limit": "848.15 kB",
"brotli": false,
"gzip": false
},
Expand Down Expand Up @@ -66,7 +66,7 @@
"README.md",
"LICENSE"
],
"limit": "909.40 kB",
"limit": "909.75 kB",
"brotli": false,
"gzip": false
}
Expand Down
10 changes: 10 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ export class ProcessPromise extends Promise<ProcessOutput> {
throw new Fail(`No quote function is defined: ${Fail.DOCS_URL}/quotes`)
if ($.pieces.some((p) => p == null))
throw new Fail(`Malformed command at ${$.from}`)
if ($.cwd) {
try {
const stat = fs.statSync($.cwd)
if (!stat.isDirectory())
throw new Fail(`The cwd option is not a directory: ${$.cwd}`)
} catch (err) {
if (err instanceof Fail) throw err
throw new Fail(`The cwd directory does not exist: ${$.cwd}`)
}
}

$.cmd = buildCmd(
$.quote!,
Expand Down
19 changes: 16 additions & 3 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,28 @@ describe('core', () => {
assert.match(err[inspect.custom](), /Command not found/)
})

test('error event is handled', async () => {
test('error is thrown for non-existent cwd', async () => {
await within(async () => {
$.cwd = 'wtf'
try {
await $`pwd`
assert.unreachable('should have thrown')
} catch (err) {
assert.ok(err instanceof ProcessOutput)
assert.match(err.message, /No such file or directory/)
assert.ok(err instanceof Fail)
assert.match(err.message, /The cwd directory does not exist: wtf/)
}
})
})

test('error is thrown when cwd is a file', async () => {
await within(async () => {
$.cwd = './README.md'
try {
await $`pwd`
assert.unreachable('should have thrown')
} catch (err) {
assert.ok(err instanceof Fail)
assert.match(err.message, /The cwd option is not a directory/)
}
})
})
Expand Down