diff --git a/deno.lock b/deno.lock index 0770e57..85b9d84 100644 --- a/deno.lock +++ b/deno.lock @@ -1,16 +1,24 @@ { "version": "5", "specifiers": { + "jsr:@std/assert@^1.0.13": "1.0.14", "jsr:@std/fmt@^1.0.8": "1.0.8", "jsr:@std/internal@^1.0.10": "1.0.12", "jsr:@std/path@^1.0.6": "1.1.2", "jsr:@std/semver@^1.0.3": "1.0.6", + "jsr:@std/testing@^1.0.3": "1.0.15", "jsr:@std/yaml@^1.0.5": "1.0.10", "npm:json5@^2.2.3": "2.2.3", "npm:jsonc-parser@^3.2.1": "3.3.1", "npm:yargs@^17.7.2": "17.7.2" }, "jsr": { + "@std/assert@1.0.14": { + "integrity": "68d0d4a43b365abc927f45a9b85c639ea18a9fab96ad92281e493e4ed84abaa4", + "dependencies": [ + "jsr:@std/internal" + ] + }, "@std/fmt@1.0.8": { "integrity": "71e1fc498787e4434d213647a6e43e794af4fd393ef8f52062246e06f7e372b7" }, @@ -26,6 +34,13 @@ "@std/semver@1.0.6": { "integrity": "b7c98ae2843547cf3f7ac37f3995889e6e4cee0a97b57b57f17f62722843303c" }, + "@std/testing@1.0.15": { + "integrity": "a490169f5ccb0f3ae9c94fbc69d2cd43603f2cffb41713a85f99bbb0e3087cbc", + "dependencies": [ + "jsr:@std/assert@^1.0.13", + "jsr:@std/internal" + ] + }, "@std/yaml@1.0.10": { "integrity": "245706ea3511cc50c8c6d00339c23ea2ffa27bd2c7ea5445338f8feff31fa58e" } diff --git a/src/hooks/patch.test.ts b/src/hooks/patch.test.ts new file mode 100644 index 0000000..16a9a42 --- /dev/null +++ b/src/hooks/patch.test.ts @@ -0,0 +1,69 @@ +import { describe, it } from "testing/bdd"; +import { assertSpyCall, stub } from "testing/mock"; +import { parse } from "semver"; +import { patch } from "./patch.ts"; + +describe("patch", () => { + it("should log formatted version string for csproj", async () => { + const consoleLogStub = stub(console, "log"); + const readTextFileStub = stub( + Deno, + "readTextFile", + () => + Promise.resolve( + "\n \n 1.0.0\n \n", + ), + ); + const writeTextFileStub = stub( + Deno, + "writeTextFile", + () => Promise.resolve(), + ); + + try { + await patch("test.csproj", parse("1.2.3")!); + + // Verify console.log was called with formatted string, not [object Object] + assertSpyCall(consoleLogStub, 0, { + args: ["patching 1.2.3 in test.csproj"], + }); + } finally { + consoleLogStub.restore(); + readTextFileStub.restore(); + writeTextFileStub.restore(); + } + }); + + it("should log formatted version string for package.json", async () => { + const consoleLogStub = stub(console, "log"); + const readTextFileStub = stub( + Deno, + "readTextFile", + () => Promise.resolve('{"version": "1.0.0"}'), + ); + const writeTextFileStub = stub( + Deno, + "writeTextFile", + () => Promise.resolve(), + ); + const statStub = stub( + Deno, + "stat", + () => Promise.reject(new Deno.errors.NotFound()), + ); + + try { + await patch("package.json", parse("1.2.3")!); + + // Verify console.log was called with formatted string, not [object Object] + assertSpyCall(consoleLogStub, 0, { + args: ["patching 1.2.3 in package.json"], + }); + } finally { + consoleLogStub.restore(); + readTextFileStub.restore(); + writeTextFileStub.restore(); + statStub.restore(); + } + }); +}); diff --git a/src/hooks/replace.test.ts b/src/hooks/replace.test.ts new file mode 100644 index 0000000..1434add --- /dev/null +++ b/src/hooks/replace.test.ts @@ -0,0 +1,41 @@ +import { describe, it } from "testing/bdd"; +import { assertSpyCall, stub } from "testing/mock"; +import { parse } from "semver"; +import { replace } from "./replace.ts"; + +describe("replace", () => { + it("should log formatted version strings", async () => { + const consoleLogStub = stub(console, "log"); + const readTextFileStub = stub( + Deno, + "readTextFile", + () => Promise.resolve("version 1.0.0"), + ); + const writeTextFileStub = stub( + Deno, + "writeTextFile", + () => Promise.resolve(), + ); + + try { + await replace("test.txt", parse("1.0.0")!, parse("1.2.3")!); + + // Verify console.log was called with formatted strings, not [object Object] + assertSpyCall(consoleLogStub, 0, { + args: ["replacing 1.0.0 -> 1.2.3 in test.txt"], + }); + + // Verify file operations + assertSpyCall(readTextFileStub, 0, { + args: ["test.txt"], + }); + assertSpyCall(writeTextFileStub, 0, { + args: ["test.txt", "version 1.2.3"], + }); + } finally { + consoleLogStub.restore(); + readTextFileStub.restore(); + writeTextFileStub.restore(); + } + }); +}); diff --git a/src/hooks/replace.ts b/src/hooks/replace.ts index fb8819c..2e50e24 100644 --- a/src/hooks/replace.ts +++ b/src/hooks/replace.ts @@ -5,10 +5,10 @@ export async function replace( previous: SemVer, current: SemVer, ) { - console.log(`replacing ${previous} -> ${current} in ${file}`); - const contents = await Deno.readTextFile(file); const previousString = format(previous); const currentString = format(current); + console.log(`replacing ${previousString} -> ${currentString} in ${file}`); + const contents = await Deno.readTextFile(file); const updated = contents.replaceAll(previousString, currentString); await Deno.writeTextFile(file, updated); }