import { assertEquals } from "@std/assert"; import { BACKGROUND_CONTEXT as context } from "@earendil-works/chord/context"; import { projectFor } from "@berry/api/projects.ts"; import { onPath, tempDir } from "@berry/util"; import { join } from "node:path"; import code from "../extensions/code/mod.ts"; import { type Environments, localOnly } from "./environment.ts"; import { setup } from "./fixture.ts"; import { Projects } from "./projects.ts"; Deno.test({ name: "workspace inherits the source directory's project on its machine without changing the default", ignore: !onPath("jj"), async fn() { await using root = await tempDir(); const repo = join(root.path, "repo"); const config = join(root.path, "config.toml"); await Deno.writeTextFile(config, '[user]\nname = "Test"\nemail = "test@example.com"\n'); const variables = { ...Deno.env.toObject(), JJ_CONFIG: config, BERRY_WORKSPACES_DIR: join(root.path, "workspaces"), }; const jj = async (...args: string[]) => { const result = await new Deno.Command("jj", { args: ["--no-pager", "--color=never", ...args], cwd: root.path, env: variables, stdout: "piped", stderr: "piped", }).output(); assertEquals(result.success, true, new TextDecoder().decode(result.stderr)); }; await jj("git", "init", repo); await Deno.writeTextFile(join(repo, "baseline"), "base\n"); await jj("-R", repo, "new"); const src = join(repo, "src"); const outside = join(root.path, "outside"); await Deno.mkdir(src); await Deno.mkdir(outside); const environments: Environments = { async open(name, cwd, during) { const env = await localOnly.open("local", cwd, during); Object.defineProperty(env, "variables", { value: variables }); return Object.assign(env, { name }); }, close: () => Promise.resolve(), }; await using harness = await setup(environments); const { berry } = harness; await berry.install(code); const local = await berry.saveProject(undefined, { name: "Local", roots: [{ environment: "local", cwd: repo }], defaultRoot: 0, }); const broad = await berry.saveProject(undefined, { name: "Broad", roots: [{ environment: "other", cwd: root.path }], defaultRoot: 0, }); const project = await berry.saveProject(undefined, { name: "Repo", roots: [{ environment: "other", cwd: repo }], defaultRoot: 0, }); const before = (await berry.harness.snapshot(Projects, context))!; const tid = await berry.create({ extensions: { code: {}, environment: { environment: "other", cwd: src } }, }); const target = await berry.command(tid, "workspace", "") as string; const after = (await berry.harness.snapshot(Projects, context))!; assertEquals(after[project], { ...before[project], roots: [...before[project].roots, { environment: "other", cwd: target, platform: before[project].roots[0].platform, }], }); assertEquals(after[local], before[local]); assertEquals(after[broad], before[broad]); const binding = await berry.state(tid, "environment") as { environment: string; cwd: string }; assertEquals(binding.cwd, target); assertEquals(projectFor(after, binding), project); assertEquals(await berry.command(tid, "workspace", ""), target); await berry.command(tid, "cd", src); assertEquals(await berry.command(tid, "workspace", ""), target); await berry.command(tid, "cd", outside); assertEquals(await berry.harness.snapshot(Projects, context), after); const unassigned = await berry.create({ extensions: { code: {}, environment: { cwd: src } } }); await berry.deleteProject(local); const withoutLocal = (await berry.harness.snapshot(Projects, context))!; await berry.command(unassigned, "workspace", ""); assertEquals(await berry.harness.snapshot(Projects, context), withoutLocal); }, });