ui / scripts / shadcn-drift.test.tsmain at d68d7e1
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { expect, test } from "vitest";
import { driftOf, parseView, VENDORED_FILES } from "./shadcn-drift.ts";
const repoRoot = resolve(import.meta.dirname, "..");
// `shadcn add <items> --dry-run --view src/` from shadcn@4.21.0, NO_COLOR, cut to two files: the
// box's `│ │ ` before an empty line keeps its trailing space.
const viewOutput = `- Resolving items.
┌ shadcn add skeleton, use-mobile (dry run)
│
├ src/components/ui/skeleton.tsx (overwrite) 4 lines
│ ┌──────────────────────────────────────────────
│ │ import { cn } from "cn"
│ │
│ │ export { Skeleton }
│ │
│ └──────────────────────────────────────────────
│
├ src/hooks/use-mobile.ts (skip) 2 lines
│ ┌──────────────────────────────────────────────
│ │ export const MOBILE_BREAKPOINT = 768
│ │
│ └──────────────────────────────────────────────
│
└ Run without --dry-run to apply.
`;
test("reads each file of the dry run's view by its repository path, with its exact content", () => {
expect(parseView(viewOutput, ".")).toEqual([
{
path: "src/components/ui/skeleton.tsx",
action: "overwrite",
content: 'import { cn } from "cn"\n\n export { Skeleton }\n',
},
{
path: "src/hooks/use-mobile.ts",
action: "skip",
content: "export const MOBILE_BREAKPOINT = 768\n",
},
]);
expect(() => parseView(viewOutput.replace("(skip) 2 lines", "(skip) 3 lines"), ".")).toThrow(
/printed 2 of src\/hooks\/use-mobile.ts's 3 lines/,
);
});
test("no drift when every vendored file holds upstream's bytes and globals.css needs nothing", () => {
const { files, current } = inSync();
expect(driftOf(files, current)).toEqual([]);
const css = {
path: "src/styles/globals.css",
action: "skip",
content: "@theme {}\n",
};
expect(driftOf([...files, css], current)).toEqual([]);
});
test("drift names an overwritten file, whitespace the CLI ignores, a file off the list, a file upstream stopped writing, and CSS", () => {
const { files, repository, current } = inSync();
const button = files.find((file) => file.path === "src/components/ui/button.tsx")!;
Object.assign(button, { action: "overwrite", content: "upstream's button\n" });
repository.set("src/components/ui/skeleton.tsx", "src/components/ui/skeleton.tsx\n\n");
const withoutInputGroup = files.filter((file) => !file.path.endsWith("/input-group.tsx"));
withoutInputGroup.push(
{ path: "src/components/ui/kbd.tsx", action: "create", content: "kbd\n" },
{ path: "src/styles/globals.css", action: "update", content: "@theme {}\n" },
);
expect(driftOf(withoutInputGroup, current)).toEqual([
"src/components/ui/button.tsx (overwrite)",
"src/components/ui/skeleton.tsx (whitespace)",
"src/components/ui/kbd.tsx (create, not on the vendored list)",
"src/styles/globals.css (update)",
"src/components/ui/input-group.tsx (upstream no longer writes it)",
]);
});
// The vendored files are excluded from our own tooling: both lists name the same files.
test.for([
{ list: ".oxlintrc.json ignorePatterns", read: () => json(".oxlintrc.json").ignorePatterns },
{ list: ".oxfmtrc.json ignorePatterns", read: () => json(".oxfmtrc.json").ignorePatterns },
])("$list names every vendored file", ({ read }) => {
expect(read()).toEqual(expect.arrayContaining(VENDORED_FILES));
});
function read(path: string) {
return readFileSync(resolve(repoRoot, path), "utf8");
}
function json(path: string) {
return JSON.parse(read(path)) as { ignorePatterns: string[] };
}
/** Upstream's content for every vendored file, and the repository holding exactly that. */
function inSync() {
const files = VENDORED_FILES.map((path) => ({ path, action: "skip", content: `${path}\n` }));
const repository = new Map(files.map((file) => [file.path, file.content]));
return { files, repository, current: (path: string) => repository.get(path) };
}