Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Jul 29, 2024
1 parent a2cc6c1 commit c08845b
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions test/finder_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ describe("Finder", () => {
const finder = new Finder({paths: ["res"]});

it("should return the path of the `executable.cmd` file on Windows", async () => {
const executables = await generatorToArray(finder.find("executable"));
const executables = await Array.fromAsync(finder.find("executable"));
equal(executables.length, Finder.isWindows ? 1 : 0);
if (Finder.isWindows) ok(executables[0].endsWith("\\res\\executable.cmd"));
});

it("should return the path of the `executable.sh` file on POSIX", async () => {
const executables = await generatorToArray(finder.find("executable.sh"));
const executables = await Array.fromAsync(finder.find("executable.sh"));
equal(executables.length, Finder.isWindows ? 0 : 1);
if (!Finder.isWindows) ok(executables[0].endsWith("/res/executable.sh"));
});

it("should return an empty array if the searched command is not executable or not found", async () => {
let executables = await generatorToArray(finder.find("not_executable.sh"));
let executables = await Array.fromAsync(finder.find("not_executable.sh"));
equal(executables.length, 0);
executables = await generatorToArray(finder.find("foo"));
executables = await Array.fromAsync(finder.find("foo"));
equal(executables.length, 0);
});
});
Expand All @@ -63,14 +63,3 @@ describe("Finder", () => {
equal(await finder.isExecutable("res/executable.cmd"), Finder.isWindows));
});
});

/**
* Converts the specified generator to an array.
* @param {AsyncGenerator<string>} generator A generator.
* @returns {Promise<string[]>} The array corresponding to the specified generator.
*/
async function generatorToArray(generator) {
const items = [];
for await (const item of generator) items.push(item);
return items;
}

0 comments on commit c08845b

Please sign in to comment.