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(core, tests): remove --depth=0 from yarn command, add yarn tests #130

Merged
merged 2 commits into from
Dec 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
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"packageManager": "npm@10.8.2",
"workspaces": ["packages/*", "tooling/*", "test"],
"bin": {
"@brainhubeu/license-auditor": "packages/cli/dist/cli.js"
"license-auditor": "packages/cli/dist/cli.js"
},
"config": {
"commitizen": {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
"@total-typescript/ts-reset": "0.6.1",
"detect-package-manager": "3.0.2",
"fast-glob": "3.3.2",
"lodash.flattendeep": "4.4.0",
"spdx-expression-parse": "4.0.0",
"zod": "3.23.8"
},
"devDependencies": {
"@license-auditor/typescript-config": "*",
"tsup": "8.3.5",
"@types/lodash.flattendeep": "4.4.9",
"@types/spdx-expression-parse": "3.0.5",
"@vitest/coverage-v8": "2.1.5",
"tsup": "8.3.5",
"tsx": "4.19.1",
"typescript": "5.6.2",
"vitest": "2.1.5"
Expand Down
23 changes: 11 additions & 12 deletions packages/core/src/dependency-finder/yarn-classic.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import fs from "node:fs/promises";
import path from "node:path";
import type { DependenciesResult } from "@license-auditor/data";
import flattenDeep from "lodash.flattendeep";
import { z } from "zod";
import { FindDependenciesException } from "../exceptions/find-dependecies.exception.js";
import { execCommand } from "./exec-command.js";

const YarnDependencySchema = z.object({
name: z.string(),
children: z.array(z.any()).length(0),
children: z.array(z.any()),
hint: z.string().nullable(),
color: z.string().nullable(),
depth: z.literal(0),
});

const YarnListOutputSchema = z.object({
Expand All @@ -23,9 +23,8 @@ const YarnListOutputSchema = z.object({

type YarnDependency = z.infer<typeof YarnDependencySchema>;

export const findYarnClassicDepsCommand = "yarn list --depth=0 --json -R";
export const findYarnClassicProdDepsCommand =
"yarn list --depth=0 --json -R --prod";
export const findYarnClassicDepsCommand = "yarn list --json -R";
export const findYarnClassicProdDepsCommand = "yarn list --json -R --prod";

export async function findYarnClassicDependencies(
projectRoot: string,
Expand All @@ -40,17 +39,17 @@ export async function findYarnClassicDependencies(
const dependenciesList = JSON.parse(output);

const validationResult = YarnListOutputSchema.safeParse(dependenciesList);

if (!validationResult.success) {
throw new FindDependenciesException(
"Invalid yarn list --depth=0 --json -R output",
{
originalError: validationResult.error,
},
);
throw new FindDependenciesException("Invalid yarn list -R output", {
originalError: validationResult.error,
});
}

const dependencies = flattenDeep(validationResult.data.data.trees);

const dependencyPaths = await extractDependencyPaths(
validationResult.data.data.trees,
dependencies,
projectRoot,
);

Expand Down
17 changes: 17 additions & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,20 @@ export const pnpmFixture = test.extend<TestContext>({
await fs.rm(testDirectory, { recursive: true });
},
});

export const yarnFixture = test.extend<TestContext>({
// biome-ignore lint/correctness/noEmptyPattern: destructuring pattern is required in fixture
testDirectory: async ({}, use) => {
const testDirectory = path.resolve(
TEST_TEMP_DIRECTORY,
`testProject-${Math.random().toString(36).substring(2)}`,
);
await fs.cp(path.resolve(TEST_PROJECTS_DIRECTORY, "yarn"), testDirectory, {
recursive: true,
});

await use(testDirectory);

await fs.rm(testDirectory, { recursive: true });
},
});
15 changes: 15 additions & 0 deletions test/test/yarn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from "vitest";
import { yarnFixture } from "../fixtures";
import { getCliPath } from "../utils/get-cli-path";
import { runCliCommand } from "../utils/run-cli-command";

yarnFixture("yarn", async ({ testDirectory }) => {
const { output, errorCode } = await runCliCommand({
command: "npx",
args: [getCliPath(), "--production"],
cwd: testDirectory,
});

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