diff --git a/core/mod_test.ts b/core/mod_test.ts index e1a61e24..dc977ae2 100644 --- a/core/mod_test.ts +++ b/core/mod_test.ts @@ -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", + }); + }); }); diff --git a/core/refs.ts b/core/refs.ts index c4e53437..53eb0fb9 100644 --- a/core/refs.ts +++ b/core/refs.ts @@ -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. */ diff --git a/deno.json b/deno.json index 9f180323..4ddb89c9 100644 --- a/deno.json +++ b/deno.json @@ -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"