Skip to content

Commit

Permalink
test(core): add tests for mod.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Jul 29, 2024
1 parent a282ab6 commit 11b2086
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 14 deletions.
133 changes: 121 additions & 12 deletions core/mod_test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,129 @@
import { all, cmd, fs } from "@chiezo/amber";
import { afterEach, beforeEach, describe, it } from "@std/testing/bdd";
import { assertSpyCallArg } from "@std/testing/mock";
import { collect } from "./mod.ts";
import { assertEquals, assertExists } from "@std/assert";
import dedent from "dedent";

Deno.test("Molt", { ignore: true }, async () => {
cmd.stub("git");
all(cmd, fs).mock();
const MOD_TS = dedent`
import "jsr:@luca/flag@1.0.0";
`;

const deps = await collect({
config: "deno.json",
lock: "deno.lock",
});
const DENO_JSON = dedent`
{
"imports": {
"@luca/flag": "jsr:@luca/flag@^1.0.0",
}
}
`;

for (const dep of deps) {
const update = await dep.check();
if (update) {
await update.write();
await update.commit();
const DENO_LOCK = dedent`
{
"version": "3",
"packages": {
"specifiers": {
"jsr:@luca/flag@^1.0.0": "jsr:@luca/flag@1.0.0"
},
"jsr": {
"@luca/flag@1.0.0": {
"integrity": "1c76cf54839a86d0929a619c61bd65bb73d7d8a4e31788e48c720dbc46c5d546"
}
}
},
"remote": {},
"workspace": {
"dependencies": [
"jsr:@luca/flag@^1.0.0"
]
}
}
`;

describe("core", () => {
let git: cmd.Stub<"git">;

beforeEach(async () => {
git = cmd.stub("git");
all(cmd, fs).mock();
await Promise.all([
Deno.writeTextFile("mod.ts", MOD_TS),
Deno.writeTextFile("deno.json", DENO_JSON),
Deno.writeTextFile("deno.lock", DENO_LOCK),
]);
});

afterEach(() => all(cmd, fs).dispose());

it("should update dependencies in a module", async () => {
const deps = await collect({ source: ["mod.ts"] });
assertEquals(deps.length, 1);
const [dep] = deps;
const update = await dep.check();
assertExists(update);
assertEquals(update.dep.name, "@luca/flag");
await update.write();
assertEquals(await Deno.readTextFile("deno.json"), DENO_JSON);
assertEquals(await Deno.readTextFile("deno.lock"), DENO_LOCK);
assertEquals(
await Deno.readTextFile("mod.ts"),
`import "jsr:@luca/flag@1.0.1";`,
);
await update.commit();
assertSpyCallArg(git, 0, 1, {
args: [
"commit",
"-m",
"bump @luca/flag from 1.0.0 to 1.0.1",
"mod.ts",
],
lock: undefined,
});
});

it("should update dependencies in a configuration", async () => {
const deps = await collect({ config: "deno.json", lock: "deno.lock" });
assertEquals(deps.length, 1);
const [dep] = deps;
const update = await dep.check();
assertExists(update);
assertEquals(update.dep.name, "@luca/flag");
await update.write();
assertEquals(await Deno.readTextFile("mod.ts"), MOD_TS);
assertEquals(await Deno.readTextFile("deno.json"), DENO_JSON);
assertEquals(
await Deno.readTextFile("deno.lock"),
dedent`
{
"version": "3",
"packages": {
"specifiers": {
"jsr:@luca/flag@^1.0.0": "jsr:@luca/flag@1.0.1"
},
"jsr": {
"@luca/flag@1.0.1": {
"integrity": "dce7eb4159b1bdb1606fe05c2e5388dcff5ae3b0b84184b934bc623143742408"
}
}
},
"remote": {},
"workspace": {
"dependencies": [
"jsr:@luca/flag@^1.0.0"
]
}
}
` + "\n",
);
await update.commit();
assertSpyCallArg(git, 0, 1, {
args: [
"commit",
"-m",
"bump @luca/flag from 1.0.0 to 1.0.1",
"deno.json",
"deno.lock",
],
lock: "deno.lock",
});
});
});
2 changes: 1 addition & 1 deletion core/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function rewriteImportMap(
}
const str = JSON.stringify(json, null, 2);
const eol = detect(content) ?? EOL;
return format(str, eol) + eol;
return format(str, eol) + (content.at(-1) === eol ? eol : "");
}

/** Options for committing the updated dependency. */
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"cache": "deno task -q ls | xargs deno cache",
"check": "deno task -q ls | xargs deno check",
"ls": "ls ./lib/*.ts ./core/*.ts ./integration/*.ts ./cli/*.ts ./cli/src/*.ts",
"pre-commit": "deno fmt && deno lint && deno task -q check && deno task -q test:unit",
"pre-commit": "deno fmt && deno lint && deno task -q check && deno task -q test",
"run": "deno run -A --env --unstable-kv ./cli/main.ts",
"test": "deno test -A --unstable-kv --no-check",
"update:commit": "deno task -q run --commit --prefix 'chore:' --pre-commit=check,test:unit"
Expand Down

0 comments on commit 11b2086

Please sign in to comment.