From c7aa0f2a409c12bf5e06e26eda80eeefdb3a8c79 Mon Sep 17 00:00:00 2001 From: lukasz Date: Mon, 30 Dec 2024 14:24:52 +0100 Subject: [PATCH] fix: changed error detection from stderr to command return code to prevent yarn warnings from crashing license-auditor process --- .../src/dependency-finder/exec-command.ts | 11 ++-- packages/core/src/dependency-finder/pnpm.ts | 2 +- .../src/dependency-finder/yarn-classic.ts | 2 +- test/test/yarn.test.ts | 51 ++++++++++--------- 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/packages/core/src/dependency-finder/exec-command.ts b/packages/core/src/dependency-finder/exec-command.ts index d0e2c0cc..61cbc2cf 100644 --- a/packages/core/src/dependency-finder/exec-command.ts +++ b/packages/core/src/dependency-finder/exec-command.ts @@ -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 { +): Promise { 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 @@ -24,7 +29,7 @@ export async function execCommand( ); } - resolve(stdout); + resolve({ stdout, stderr }); }); }); } diff --git a/packages/core/src/dependency-finder/pnpm.ts b/packages/core/src/dependency-finder/pnpm.ts index 6e6d5efa..106bb6de 100644 --- a/packages/core/src/dependency-finder/pnpm.ts +++ b/packages/core/src/dependency-finder/pnpm.ts @@ -10,7 +10,7 @@ export async function findPnpmDependencies( production?: boolean | undefined, verbose?: boolean | undefined, ): Promise { - const output = await execCommand( + const { stdout: output } = await execCommand( production ? findPnpmProdDepsCommand : findPnpmDepsCommand, projectRoot, verbose, diff --git a/packages/core/src/dependency-finder/yarn-classic.ts b/packages/core/src/dependency-finder/yarn-classic.ts index 93c9315d..2cc7656e 100644 --- a/packages/core/src/dependency-finder/yarn-classic.ts +++ b/packages/core/src/dependency-finder/yarn-classic.ts @@ -31,7 +31,7 @@ export async function findYarnClassicDependencies( production?: boolean | undefined, verbose?: boolean | undefined, ): Promise { - const output = await execCommand( + const { stdout: output } = await execCommand( production ? findYarnClassicProdDepsCommand : findYarnClassicDepsCommand, projectRoot, verbose, diff --git a/test/test/yarn.test.ts b/test/test/yarn.test.ts index e28c7280..f1c95662 100644 --- a/test/test/yarn.test.ts +++ b/test/test/yarn.test.ts @@ -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"; @@ -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", ); @@ -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", ); @@ -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", ); @@ -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", ); @@ -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", ); @@ -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", ); @@ -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", ); @@ -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", ); @@ -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");