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

fix: changed error detection from stderr to command return code to prevent yarn warnings from crashing license-auditor process #150

Open
wants to merge 1 commit into
base: updatedSnapshotTestsAndAddedArborist
Choose a base branch
from
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
11 changes: 8 additions & 3 deletions packages/core/src/dependency-finder/exec-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import { exec } from "node:child_process";

import { ExecCommandException } from "../exceptions/index.js";

type ExecCommandResult = {
stdout: string;
stderr: string;
};

export async function execCommand(
command: string,
cwd: string,
verbose?: boolean | undefined,
): Promise<string> {
): Promise<ExecCommandResult> {
return new Promise((resolve, reject) => {
exec(command, { cwd }, (error, stdout, stderr) => {
if (error || (stderr && !stderr.includes("Debugger attached"))) {
if (error) {
reject(
new ExecCommandException(
error?.stack && verbose
Expand All @@ -24,7 +29,7 @@ export async function execCommand(
);
}

resolve(stdout);
resolve({ stdout, stderr });
});
});
}
2 changes: 1 addition & 1 deletion packages/core/src/dependency-finder/pnpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function findPnpmDependencies(
production?: boolean | undefined,
verbose?: boolean | undefined,
): Promise<DependenciesResult> {
const output = await execCommand(
const { stdout: output } = await execCommand(
production ? findPnpmProdDepsCommand : findPnpmDepsCommand,
projectRoot,
verbose,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/dependency-finder/yarn-classic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function findYarnClassicDependencies(
production?: boolean | undefined,
verbose?: boolean | undefined,
): Promise<DependenciesResult> {
const output = await execCommand(
const { stdout: output } = await execCommand(
production ? findYarnClassicProdDepsCommand : findYarnClassicDepsCommand,
projectRoot,
verbose,
Expand Down
51 changes: 26 additions & 25 deletions test/test/yarn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from "node:fs/promises";
import * as path from "node:path";
import type { JsonResults } from "@license-auditor/data";
import { describe, expect } from "vitest";
import { yarnFixture } from "../fixtures";
import { yarnFixture, yarnWithInvalidGithubDepFixture } from "../fixtures";
import { addToPackageJson } from "../utils/add-to-package-json";
import { getCliPath } from "../utils/get-cli-path";
import { readJsonFile } from "../utils/read-json-file";
Expand All @@ -24,13 +24,13 @@ describe("yarn", () => {
cwd: testDirectory,
});

expect(errorCode).toBe(0);
expect(output).toContain("160 licenses are compliant");

const jsonOutput: JsonResults = await readJsonFile(
path.join(testDirectory, "license-auditor.results.json"),
);

expect(errorCode).toBe(0);
expect(output).toContain("160 licenses are compliant");

const addedPackage = jsonOutput.whitelist.find(
(result) => result.packageName === "test-dep@1.0.0",
);
Expand All @@ -53,13 +53,13 @@ describe("yarn", () => {
cwd: testDirectory,
});

expect(errorCode).toBe(0);
expect(output).toContain("160 licenses are compliant");

const jsonOutput: JsonResults = await readJsonFile(
path.join(testDirectory, "license-auditor.results.json"),
);

expect(errorCode).toBe(0);
expect(output).toContain("160 licenses are compliant");

const addedPackage = jsonOutput.whitelist.find(
(result) => result.packageName === "test-dep@1.0.0",
);
Expand All @@ -82,13 +82,13 @@ describe("yarn", () => {
cwd: testDirectory,
});

expect(errorCode).toBe(0);
expect(output).toContain("160 licenses are compliant");

const jsonOutput: JsonResults = await readJsonFile(
path.join(testDirectory, "license-auditor.results.json"),
);

expect(errorCode).toBe(0);
expect(output).toContain("160 licenses are compliant");

const addedPackage = jsonOutput.whitelist.find(
(result) => result.packageName === "test-dep@1.0.0",
);
Expand All @@ -111,13 +111,13 @@ describe("yarn", () => {
cwd: testDirectory,
});

expect(errorCode).toBe(0);
expect(output).toContain("160 licenses are compliant");

const jsonOutput: JsonResults = await readJsonFile(
path.join(testDirectory, "license-auditor.results.json"),
);

expect(errorCode).toBe(0);
expect(output).toContain("160 licenses are compliant");

const addedPackage = jsonOutput.whitelist.find(
(result) => result.packageName === "test-dep@1.0.0",
);
Expand Down Expand Up @@ -147,13 +147,13 @@ describe("yarn", () => {
cwd: testDirectory,
});

expect(errorCode).toBe(0);
expect(output).toContain("160 licenses are compliant");

const jsonOutput: JsonResults = await readJsonFile(
path.join(testDirectory, "license-auditor.results.json"),
);

expect(errorCode).toBe(0);
expect(output).toContain("160 licenses are compliant");

const addedPackage = jsonOutput.whitelist.find(
(result) => result.packageName === "test-dep@1.0.0",
);
Expand Down Expand Up @@ -189,13 +189,13 @@ describe("yarn", () => {
cwd: testDirectory,
});

expect(errorCode).toBe(0);
expect(output).toContain("160 licenses are compliant");

const jsonOutput: JsonResults = await readJsonFile(
path.join(testDirectory, "license-auditor.results.json"),
);

expect(errorCode).toBe(0);
expect(output).toContain("160 licenses are compliant");

const addedPackage = jsonOutput.whitelist.find(
(result) => result.packageName === "test-dep@1.0.0",
);
Expand All @@ -220,13 +220,13 @@ describe("yarn", () => {
cwd: testDirectory,
});

expect(errorCode).toBe(0);
expect(output).toContain("67 licenses are compliant");

const jsonOutput: JsonResults = await readJsonFile(
path.join(testDirectory, "license-auditor.results.json"),
);

expect(errorCode).toBe(0);
expect(output).toContain("67 licenses are compliant");

const addedPackage = jsonOutput.whitelist.find(
(result) => result.packageName === "test-dep@1.0.0",
);
Expand All @@ -253,13 +253,13 @@ describe("yarn", () => {
cwd: testDirectory,
});

expect(errorCode).toBe(0);
expect(output).toContain("66 licenses are compliant");

const jsonOutput: JsonResults = await readJsonFile(
path.join(testDirectory, "license-auditor.results.json"),
);

expect(errorCode).toBe(0);
expect(output).toContain("66 licenses are compliant");

const addedPackage = jsonOutput.whitelist.find(
(result) => result.packageName === "test-dep@1.0.0",
);
Expand Down Expand Up @@ -292,6 +292,7 @@ describe("yarn", () => {
cwd: testDirectory,
});

expect(errorCode).toBe(1);
expect(output).toContain("Invalid configuration file at");
expect(output).toContain("Expected array, received string");
expect(output).toContain("Expected array, received number");
Expand Down
Loading