Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
mehulkar committed Mar 5, 2024
1 parent df64796 commit 4dec919
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 115 deletions.
57 changes: 57 additions & 0 deletions packages/turbo-repository/__tests__/affected-packages.test.ts
@@ -0,0 +1,57 @@
import * as path from "node:path";
import { Workspace, Package, PackageManager } from "../js/dist/index.js";

type PackageReduced = Pick<Package, "name" | "relativePath">;

interface AffectedPackagesTestParams {
files: string[];
expected: PackageReduced[];
description: string;
}

describe("affectedPackages", () => {
const tests: AffectedPackagesTestParams[] = [
{
description: "app change",
files: ["apps/app/file.txt"],
expected: [{ name: "app-a", relativePath: "apps/app" }],
},
{
description: "lib change",
files: ["packages/ui/a.txt"],
expected: [{ name: "ui", relativePath: "packages/ui" }],
},
{
description: "global change",
files: ["package.json"],
expected: [
{ name: "app-a", relativePath: "apps/app" },
{ name: "ui", relativePath: "packages/ui" },
],
},
{
description: "global change that can be ignored",
files: ["README.md"],
expected: [],
},
];

test.each(tests)(
"$description",
async (testParams: AffectedPackagesTestParams) => {
const { files, expected } = testParams;
const dir = path.resolve(__dirname, "./fixtures/monorepo");
const workspace = await Workspace.find(dir);
const reduced: PackageReduced[] = (
await workspace.affectedPackages(files)
).map((pkg) => {
return {
name: pkg.name,
relativePath: pkg.relativePath,
};
});

expect(reduced).toEqual(expected);
}
);
});
27 changes: 27 additions & 0 deletions packages/turbo-repository/__tests__/find-packages.test.ts
@@ -0,0 +1,27 @@
import * as path from "node:path";
import { Workspace, Package } from "../js/dist/index.js";

describe("findPackages", () => {
it("enumerates packages", async () => {
const workspace = await Workspace.find("./fixtures/monorepo");
const packages: Package[] = await workspace.findPackages();
expect(packages.length).not.toBe(0);
});

it("returns a package graph", async () => {
const dir = path.resolve(__dirname, "./fixtures/monorepo");
const workspace = await Workspace.find(dir);
const packages = await workspace.findPackagesAndDependents(); // TODO: rename this

expect(Object.keys(packages).length).toBe(2);

const pkg1 = packages["apps/app"];
const pkg2 = packages["packages/ui"];

expect(pkg1.dependencies).toEqual(["packages/ui"]);
expect(pkg1.dependents).toEqual([]);

expect(pkg2.dependencies).toEqual([]);
expect(pkg2.dependents).toEqual(["apps/app"]);
});
});
93 changes: 0 additions & 93 deletions packages/turbo-repository/__tests__/find.test.ts

This file was deleted.

16 changes: 16 additions & 0 deletions packages/turbo-repository/__tests__/workspace.test.ts
@@ -0,0 +1,16 @@
import * as path from "node:path";
import { Workspace, PackageManager } from "../js/dist/index.js";

describe("Workspace", () => {
it("finds a workspace", async () => {
const workspace = await Workspace.find();
const expectedRoot = path.resolve(__dirname, "../../..");
expect(workspace.absolutePath).toBe(expectedRoot);
});

it("finds a package manager", async () => {
const workspace = await Workspace.find();
const packageManager: PackageManager = workspace.packageManager;
expect(packageManager.name).toBe("pnpm");
});
});
32 changes: 10 additions & 22 deletions packages/turbo-repository/rust/src/lib.rs
Expand Up @@ -25,27 +25,11 @@ pub struct Package {
pub relative_path: String,
}

// #[napi]
// pub struct SerializablePackage {
// pub package: String,
// pub dependents: Vec<String>,
// pub dependencies: Vec<String>,
// }

// impl SerializablePackage {
// pub fn new(package: Package, dependents: Vec<Package>, dependencies:
// Vec<Package>) -> Self { Self {
// package,
// dependents,
// dependencies,
// }
// }
// }

// type PackageName = String;
type RelativePath = String;
#[napi]
struct PackageDetails {
#[derive(Debug)]
pub struct PackageDetails {
#[napi(readonly)]
pub dependencies: Vec<RelativePath>,
#[napi(readonly)]
Expand Down Expand Up @@ -129,6 +113,7 @@ impl Package {
};

pkgs.iter()
.filter(|node| !matches!(node, PackageNode::Root))
.filter_map(|node| {
let info = graph.package_info(node.as_package_name())?;
// If we don't get a package name back, we'll just skip it.
Expand Down Expand Up @@ -164,14 +149,17 @@ impl Workspace {

let workspace_path = match AbsoluteSystemPath::new(self.absolute_path.as_str()) {
Ok(path) => path,
Err(e) => return Err(Error::from_reason(e.to_string())),
Err(e) => {
print!("Error: {:?}", e);
return Err(Error::from_reason(e.to_string()));
}
};

// Create a map of package names to their dependents and dependencies
// {
// "package-name": {
// "dependents": ["dependent1", "dependent2"],
// "dependencies": ["dependency1", "dependency2"]
// "package-path": {
// "dependents": ["dependent1_path", "dependent2_path"],
// "dependencies": ["dependency1_path", "dependency2_path"]
// }
// }
let map: HashMap<RelativePath, PackageDetails> = packages
Expand Down

0 comments on commit 4dec919

Please sign in to comment.