Skip to content

Commit

Permalink
chore: fix failing tests (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored Jul 5, 2024
1 parent b507e9c commit 5212ce7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/pkg_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function toPackageArgs(pkgs: JsrPackage[]): string[] {

async function isYarnBerry(cwd: string) {
// this command works for both yarn classic and berry
const version = await exec("yarn", ["--version"], cwd, undefined, true);
const output = await exec("yarn", ["--version"], cwd, undefined, true);
const version = output.stdout;
if (!version) {
logDebug("Unable to detect yarn version, assuming classic");
return false;
Expand Down
22 changes: 16 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,19 @@ export class ExecError extends Error {
}
}

export interface ExecOutput {
combined: string;
stdout: string;
stderr: string;
}

export async function exec(
cmd: string,
args: string[],
cwd: string,
env?: Record<string, string | undefined>,
captureOutput?: boolean,
) {
): Promise<ExecOutput> {
const cp = spawn(
cmd,
args.map((arg) => process.platform === "win32" ? `"${arg}"` : `'${arg}'`),
Expand All @@ -223,20 +229,24 @@ export async function exec(
},
);

let output = "";
let combined = "";
let stdout = "";
let stderr = "";

if (captureOutput) {
cp.stdout?.on("data", (data) => {
output += data;
combined += data;
stdout += data;
});
cp.stderr?.on("data", (data) => {
output += data;
combined += data;
stderr += data;
});
}

return new Promise<string>((resolve, reject) => {
return new Promise<ExecOutput>((resolve, reject) => {
cp.on("exit", (code) => {
if (code === 0) resolve(output);
if (code === 0) resolve({ combined, stdout, stderr });
else reject(new ExecError(code ?? 1));
});
});
Expand Down
6 changes: 3 additions & 3 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe("install", () => {

assert.match(
pkgJson.dependencies["@std/encoding"],
/^npm:@jsr\/std__encoding@\^\d+\.\d+\.\d+.*$/,
/^npm:@jsr\/std__encoding@\d+\.\d+\.\d+.*$/,
);

const npmRc = await readTextFile(path.join(dir, ".npmrc"));
Expand Down Expand Up @@ -770,7 +770,7 @@ describe("show", () => {
undefined,
true,
);
const txt = kl.stripColors(output);
const txt = kl.stripColors(output.combined);
assert.ok(txt.includes("latest:"));
assert.ok(txt.includes("npm tarball:"));
});
Expand All @@ -796,7 +796,7 @@ describe("show", () => {
undefined,
true,
);
const txt = kl.stripColors(output);
const txt = kl.stripColors(output.combined);
assert.ok(txt.includes("latest: -"));
assert.ok(txt.includes("npm tarball:"));
});
Expand Down

0 comments on commit 5212ce7

Please sign in to comment.