Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix failing tests #96

Merged
merged 1 commit into from
Jul 5, 2024
Merged
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
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
Loading