Skip to content

Commit

Permalink
Merge pull request #14 from henrymercer/henrymercer/no-throw-if-file-…
Browse files Browse the repository at this point in the history
…on-path

 Don't throw if a file is on the search path
  • Loading branch information
chrisgavin authored Dec 19, 2024
2 parents a1f991d + c7e224e commit c373973
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from "ava";
import { safeWhich, isWindows } from "./index";
import * as os from "os";
import * as path from "path";

const originalEnv = process.env;
Expand Down Expand Up @@ -71,6 +72,11 @@ else {
t.deepEqual(await safeWhich("program"), path.join(testResources, "path", "program"));
});

test("does not throw if a non-directory file is on the search path", async (t) => {
process.env.PATH = path.join(testResources, "path/program") + path.delimiter + path.join(testResources, "path");
t.deepEqual(await safeWhich("program"), path.join(testResources, "path", "program"));
});

test("program is not found if not executable", async (t) => {
process.env.PATH = path.join(testResources, "path");
await t.throwsAsync(safeWhich("non-executable-file"));
Expand Down
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ export async function safeWhich(program: string): Promise<string> {
return completePath;
}
catch (err) {
if (err.code !== "ENOENT") {
throw err;
if (typeof err === "object" && err !== null && 'code' in err) {
if (err.code === "ENOTDIR") {
console.warn(`While resolving program ${program}, skipping ${searchPath} ` +
"because it is not a directory.");
break;
}
if (err.code === "ENOENT") {
continue;
}
}
throw err;
}
}
}
Expand Down

0 comments on commit c373973

Please sign in to comment.