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

Add files for Yarn 3 and Yarn 4 #305

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
test/yarn-berry.cjs
test/yarn-*.cjs
dist
coverage
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test/yarn-berry.cjs linguist-vendored
test/yarn-*.cjs linguist-vendored
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test/yarn-berry.cjs
coverage
test/yarn-*.cjs
**/pnpm-lock.yaml
**/*-output.json
**/.pnp.cjs
44 changes: 9 additions & 35 deletions lib/yarn-auditor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { YarnAudit, YarnBerryAuditReport } from "audit-types";
import { execSync } from "child_process";
import * as semver from "semver";
import { blue, red, yellow } from "./colors.js";
import { reportAudit, runProgram } from "./common.js";
import {
Expand All @@ -9,38 +7,14 @@ import {
type AuditCiFullConfig,
} from "./config.js";
import Model, { type Summary } from "./model.js";

const MINIMUM_YARN_CLASSIC_VERSION = "1.12.3";
const MINIMUM_YARN_BERRY_VERSION = "2.4.0";
/**
* Change this to the appropriate version when
* yarn audit --registry is supported:
* @see https://github.com/yarnpkg/yarn/issues/7012
*/
const MINIMUM_YARN_AUDIT_REGISTRY_VERSION = "99.99.99";

function getYarnVersion(cwd?: string) {
const version = execSync("yarn -v", { cwd }).toString().replace("\n", "");
return version;
}

function yarnSupportsClassicAudit(yarnVersion: string | semver.SemVer) {
return semver.satisfies(yarnVersion, `^${MINIMUM_YARN_CLASSIC_VERSION}`);
}

function yarnSupportsBerryAudit(yarnVersion: string | semver.SemVer) {
return semver.gte(yarnVersion, MINIMUM_YARN_BERRY_VERSION);
}

function yarnSupportsAudit(yarnVersion: string | semver.SemVer) {
return (
yarnSupportsClassicAudit(yarnVersion) || yarnSupportsBerryAudit(yarnVersion)
);
}

function yarnAuditSupportsRegistry(yarnVersion: string | semver.SemVer) {
return semver.gte(yarnVersion, MINIMUM_YARN_AUDIT_REGISTRY_VERSION);
}
import {
MINIMUM_YARN_BERRY_VERSION,
MINIMUM_YARN_CLASSIC_VERSION,
getYarnVersion,
yarnAuditSupportsRegistry,
yarnSupportsAudit,
yarnSupportsClassicAudit,
} from "./yarn-version.js";

const printJson = (data: unknown) => {
console.log(JSON.stringify(data, undefined, 2));
Expand Down Expand Up @@ -83,7 +57,7 @@ export async function auditWithFullConfig(
let missingLockFile = false;
const model = new Model(config);

const yarnVersion = getYarnVersion(directory);
const yarnVersion = getYarnVersion(yarnExec, directory);
const isYarnVersionSupported = yarnSupportsAudit(yarnVersion);
if (!isYarnVersionSupported) {
throw new Error(
Expand Down
39 changes: 39 additions & 0 deletions lib/yarn-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { execSync } from "child_process";
import semver from "semver";

export const MINIMUM_YARN_CLASSIC_VERSION = "1.12.3";
export const MINIMUM_YARN_BERRY_VERSION = "2.4.0";
/**
* Change this to the appropriate version when
* yarn audit --registry is supported:
* @see https://github.com/yarnpkg/yarn/issues/7012
*/
const MINIMUM_YARN_AUDIT_REGISTRY_VERSION = "99.99.99";

export function yarnSupportsClassicAudit(yarnVersion: string | semver.SemVer) {
return semver.satisfies(yarnVersion, `^${MINIMUM_YARN_CLASSIC_VERSION}`);
}

export function yarnSupportsBerryAudit(yarnVersion: string | semver.SemVer) {
return semver.gte(yarnVersion, MINIMUM_YARN_BERRY_VERSION);
}

export function yarnSupportsAudit(yarnVersion: string | semver.SemVer) {
return (
yarnSupportsClassicAudit(yarnVersion) || yarnSupportsBerryAudit(yarnVersion)
);
}

export function yarnAuditSupportsRegistry(yarnVersion: string | semver.SemVer) {
return semver.gte(yarnVersion, MINIMUM_YARN_AUDIT_REGISTRY_VERSION);
}

const versionMap = new Map<string, string>();
export function getYarnVersion(yarnExec = "yarn", cwd?: string) {
const key = `${yarnExec}:${cwd}`;
let version = versionMap.get(key);
if (version) return version;
version = execSync(`${yarnExec} -v`, { cwd }).toString().replace("\n", "");
versionMap.set(key, version);
return version;
}
5 changes: 5 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Yarn Berry tests

Also, the `.yarnrc.yml` file in each Yarn Berry test project re-exports the `yarn-*.cjs` file at the root of tests.
Re-exporting the file reduces duplication and version mismatching for tests.
Currently, this project is set up to use the latest version v2.4.0 (at the time of writing this, Dec 6th, 2020).
12 changes: 12 additions & 0 deletions test/yarn-1-auditor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path from "path";
import { SemVer } from "semver";
import { performAuditTests } from "./yarn-auditor.js";

const version = "1.22.19";

const yarnAbsolutePath = path.resolve(__dirname, `./yarn-${version}.cjs`);

performAuditTests({
yarnAbsolutePath,
yarnVersion: new SemVer(version),
});
1 change: 1 addition & 0 deletions test/yarn-1-config-file/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: "../yarn-1.22.19.cjs"
File renamed without changes.
1 change: 1 addition & 0 deletions test/yarn-1-critical/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: "../yarn-1.22.19.cjs"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "audit-ci-yarn-critical-vulnerability",
"name": "audit-ci-yarn-1-critical-vulnerability",
"description": "Test package.json with critical vulnerability",
"dependencies": {
"open": "0.0.5"
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions test/yarn-1-duplicate-paths/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: "../yarn-1.22.19.cjs"
1 change: 1 addition & 0 deletions test/yarn-1-high/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: "../yarn-1.22.19.cjs"
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/yarn-1-low/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: "../yarn-1.22.19.cjs"
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/yarn-1-moderate/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: "../yarn-1.22.19.cjs"
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/yarn-1-none/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: "../yarn-1.22.19.cjs"
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/yarn-1-skip-dev/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: "../yarn-1.22.19.cjs"
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/yarn-1-workspace-empty/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: "../yarn-1.22.19.cjs"
1 change: 1 addition & 0 deletions test/yarn-1-workspace/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: "../yarn-1.22.19.cjs"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"private": true,
"name": "audit-ci-yarn-workspace-empty",
"name": "audit-ci-yarn-workspace",
"description": "Test yarn workspace",
"workspaces": [
"packages/*"
Expand Down
File renamed without changes.