Skip to content

Commit

Permalink
fix: pass appropriate workspace args and improve tests (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Aug 29, 2023
1 parent 84357e0 commit d480f6f
Show file tree
Hide file tree
Showing 38 changed files with 3,350 additions and 397 deletions.
47 changes: 24 additions & 23 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,37 +81,38 @@ export function getWorkspaceArgs(
options: Awaited<ReturnType<typeof resolveOperationOptions>>,
): string[] {
if (!options.workspace) {
if (options.packageManager.name === "pnpm") {
return ["-w"];
}

if (
options.packageManager.name === "yarn" &&
options.packageManager.majorVersion === "1"
) {
return ["-W"];
}

return [];
}

switch (options.packageManager.name) {
case "npm": {
return ["-w", options.workspace];
}
const workspacePkg =
typeof options.workspace === "string" && options.workspace !== ""
? options.workspace
: undefined;

case "pnpm": {
return ["-F", options.workspace];
}
// pnpm
if (options.packageManager.name === "pnpm") {
return workspacePkg ? ["--dir", workspacePkg] : ["--workspace-root"];
}

case "bun": {
return [];
}
// npm
if (options.packageManager.name === "npm") {
return workspacePkg ? ["-w", workspacePkg] : ["--workspaces"];
}

case "yarn": {
return ["workspace", options.workspace];
if (options.packageManager.name === "yarn") {
if (
!options.packageManager.majorVersion ||
options.packageManager.majorVersion === "1"
) {
// Yarn classic
return workspacePkg ? ["--cwd", workspacePkg] : ["-W"];
} else {
// Yarn berry
return workspacePkg ? ["workspace", workspacePkg] : [];
}
}

return [];
}

export function doesDependencyExist(
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export type OperationOptions = {
silent?: boolean;
packageManager?: PackageManager;
dev?: boolean;
workspace?: string;
workspace?: boolean | string;
};
74 changes: 74 additions & 0 deletions test/_shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { fileURLToPath } from "node:url";
import { isWindows } from "std-env";
import type { PackageManagerName } from "../src";

export type Fixture = {
name: string;
dir: string;
packageManager: PackageManagerName;
majorVersion?: string;
workspace: boolean;
};

export const fixtures = (
[
{
name: "bun",
packageManager: "bun",
},
{
name: "bun-workspace",
packageManager: "bun",
},
{
name: "npm",
packageManager: "npm",
},
{
name: "npm-workspace",
packageManager: "npm",
},
{
name: "pnpm",
packageManager: "pnpm",
},
{
name: "pnpm-workspace",
packageManager: "pnpm",
},
{
name: "yarn-classic",
packageManager: "yarn",
},
{
name: "yarn-classic-workspace",
packageManager: "yarn",
},
{
name: "yarn-berry",
packageManager: "yarn",
majorVersion: "3",
},
{
name: "yarn-berry-workspace",
packageManager: "yarn",
majorVersion: "3",
},
] satisfies Partial<Fixture>[]
)
.map((fixture) => ({
...fixture,
dir: resolveFixtureDirectory(fixture.name),
workspace: fixture.name.includes("workspace"),
}))
.filter((fixture) => {
// Bun is not yet supported on Windows
if (isWindows && fixture.packageManager === "bun") {
return false;
}
return true;
});

export function resolveFixtureDirectory(name: string) {
return fileURLToPath(new URL(`fixtures/${name}`, import.meta.url));
}
62 changes: 62 additions & 0 deletions test/api-workspace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { expect, it, describe, vi } from "vitest";
import { addDependency, removeDependency } from "../src";
import { fixtures } from "./_shared";

describe("api", () => {
for (const fixture of fixtures.filter((f) => f.workspace)) {
describe(fixture.name, () => {
it("adds dependency to the workspace root", async () => {
const addDependencySpy = vi.fn(addDependency);
const executeAddDependencySpy = () =>
addDependencySpy("pathe", {
cwd: fixture.dir,
silent: !process.env.DEBUG,
workspace: true,
});
await executeAddDependencySpy();
expect(addDependencySpy).toHaveReturned();
}, 30_000);

it("removes dependency from workspace root", async () => {
const removeDependencySpy = vi.fn(removeDependency);
const executeRemoveDependencySpy = () =>
removeDependencySpy("pathe", {
cwd: fixture.dir,
silent: !process.env.DEBUG,
workspace: true,
});
await executeRemoveDependencySpy();
expect(removeDependencySpy).toHaveReturned();
}, 30_000);

const workspaceRef =
fixture.name === "yarn-classic-workspace"
? "./packages/workspace-a"
: "workspace-a";

it("adds dependency to workspace package", async () => {
const addDependencySpy = vi.fn(addDependency);
const executeAddDependencySpy = () =>
addDependencySpy("ufo", {
cwd: fixture.dir,
silent: !process.env.DEBUG,
workspace: workspaceRef,
});
await executeAddDependencySpy();
expect(addDependencySpy).toHaveReturned();
}, 30_000);

it("removes dependency from workspace package", async () => {
const removeDependencySpy = vi.fn(removeDependency);
const executeRemoveDependencySpy = () =>
removeDependencySpy("ufo", {
cwd: fixture.dir,
silent: !process.env.DEBUG,
workspace: workspaceRef,
});
await executeRemoveDependencySpy();
expect(removeDependencySpy).toHaveReturned();
}, 30_000);
});
}
});
62 changes: 62 additions & 0 deletions test/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { expect, it, describe, vi } from "vitest";
import {
installDependencies,
addDependency,
removeDependency,
ensureDependencyInstalled,
} from "../src";
import { fixtures } from "./_shared";

describe("api (workspace)", () => {
for (const fixture of fixtures.filter((f) => !f.workspace)) {
describe(fixture.name, () => {
it("installs dependencies", async () => {
const installDependenciesSpy = vi.fn(installDependencies);
const executeInstallDependenciesSpy = () =>
installDependenciesSpy({
cwd: fixture.dir,
silent: !process.env.DEBUG,
});
await executeInstallDependenciesSpy();
expect(installDependenciesSpy).toHaveReturned();
}, 30_000);

it("adds dependency", async () => {
const addDependencySpy = vi.fn(addDependency);
const executeAddDependencySpy = () =>
addDependencySpy("pathe", {
cwd: fixture.dir,
silent: !process.env.DEBUG,
});

await executeAddDependencySpy();
expect(addDependencySpy).toHaveReturned();
}, 30_000);

it("ensures dependency is installed", async () => {
const ensureDependencyInstalledSpy = vi.fn(ensureDependencyInstalled);

const executeEnsureDependencyInstalledSpy = () =>
ensureDependencyInstalledSpy("pathe", {
cwd: fixture.dir,
});

await executeEnsureDependencyInstalledSpy();
expect(ensureDependencyInstalledSpy).toHaveReturned();
});

it("removes dependency", async () => {
const removeDependencySpy = vi.fn(removeDependency);

const executeRemoveDependencySpy = () =>
removeDependencySpy("pathe", {
cwd: fixture.dir,
silent: !process.env.DEBUG,
});

await executeRemoveDependencySpy();
expect(removeDependencySpy).toHaveReturned();
}, 30_000);
});
}
});
32 changes: 32 additions & 0 deletions test/detect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect, it, describe } from "vitest";
import { detectPackageManager } from "../src";
import { fixtures } from "./_shared";

describe("detectPackageManager", () => {
for (const fixture of fixtures) {
describe(fixture.name, () => {
it("should detect with package.json", async () => {
const detected = await detectPackageManager(fixture.dir, {
ignoreLockFile: true,
});
expect(detected?.name).toBe(fixture.packageManager);
if (fixture.majorVersion) {
expect(detected?.majorVersion).toBe(fixture.majorVersion);
}
});

it.skipIf(fixture.name.includes("berry") /* TODO */)(
"should detect with lock file",
async () => {
const detected = await detectPackageManager(fixture.dir, {
ignorePackageJSON: true,
});
expect(detected?.name).toBe(fixture.packageManager);
if (fixture.majorVersion) {
expect(detected?.majorVersion).toBe(fixture.majorVersion);
}
},
);
});
}
});
11 changes: 11 additions & 0 deletions test/fixtures/bun-workspace/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "fixture-bun-workspace",
"private": true,
"workspaces": [
"packages/*"
],
"dependencies": {
"consola": "3.2.2"
},
"packageManager": "bun@0.8.0"
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{
"name": "workspace-a",
"private": true,
"version": "0.0.0",
"packageManager": "yarn@1.22.19",
"devDependencies": {
"ufo": "1.1.2"
},
"private": true,
"dependencies": {}
}
9 changes: 4 additions & 5 deletions test/fixtures/bun/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"name": "fixture-bun",
"version": "0.0.0",
"packageManager": "bun@0.8.0",
"private": true,
"workspaces": [
"packages/*"
]
"dependencies": {
"consola": "3.2.2"
},
"packageManager": "bun@0.8.0"
}
8 changes: 0 additions & 8 deletions test/fixtures/bun/packages/workspace-a/package.json

This file was deleted.

30 changes: 30 additions & 0 deletions test/fixtures/npm-workspace/package-lock.json

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

11 changes: 11 additions & 0 deletions test/fixtures/npm-workspace/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "fixture-npm-workspace",
"private": true,
"workspaces": [
"packages/*"
],
"packageManager": "npm@9.7.2",
"dependencies": {
"consola": "3.2.2"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/npm-workspace/packages/workspace-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "workspace-a",
"version": "0.0.0",
"private": true
}
Loading

0 comments on commit d480f6f

Please sign in to comment.