From 559060fbfa553e2b55700b7a273ffade0a849353 Mon Sep 17 00:00:00 2001 From: hasundue Date: Mon, 30 Oct 2023 09:32:35 +0900 Subject: [PATCH] test: fix snapshot tests --- deno.lock | 1 + lib/__snapshots__/git_test.ts.snap | 36 +-- lib/__snapshots__/update_test.ts.snap | 425 ++++++++++++-------------- lib/file_test.ts | 6 +- lib/git_test.ts | 166 ++++------ lib/std/collections.ts | 1 + lib/testing.ts | 62 ++-- lib/update_test.ts | 32 +- 8 files changed, 331 insertions(+), 398 deletions(-) diff --git a/deno.lock b/deno.lock index 8727c008..5a8fe9c0 100644 --- a/deno.lock +++ b/deno.lock @@ -94,6 +94,7 @@ "https://deno.land/std@0.204.0/assert/assertion_error.ts": "4d0bde9b374dfbcbe8ac23f54f567b77024fb67dbb1906a852d67fe050d42f56", "https://deno.land/std@0.204.0/assert/equal.ts": "9f1a46d5993966d2596c44e5858eec821859b45f783a5ee2f7a695dfc12d8ece", "https://deno.land/std@0.204.0/collections/distinct.ts": "01595bd8e608a5e2de8a8631055587e6f01d68861e463eef34f8e295713500b8", + "https://deno.land/std@0.204.0/collections/filter_keys.ts": "a29cfe8730ddb54e9e071ea45e8a82e166c7629d18675652def70c1bf80e2ef6", "https://deno.land/std@0.204.0/collections/max_by.ts": "9d5940986aac51b2e4feaebef9cd8bf6e1eceeee5edcf3303e334b737f99520d", "https://deno.land/std@0.204.0/fmt/colors.ts": "c51c4642678eb690dcf5ffee5918b675bf01a33fba82acf303701ae1a4f8c8d9", "https://deno.land/std@0.204.0/fs/_util.ts": "fbf57dcdc9f7bc8128d60301eece608246971a7836a3bb1e78da75314f08b978", diff --git a/lib/__snapshots__/git_test.ts.snap b/lib/__snapshots__/git_test.ts.snap index 4f23f932..62064bb0 100644 --- a/lib/__snapshots__/git_test.ts.snap +++ b/lib/__snapshots__/git_test.ts.snap @@ -1,40 +1,40 @@ export const snapshot = {}; snapshot[`commitAll() > no grouping 1`] = ` -FileSystemFake(2) [Map] { - "file:///home/shun/molt/test/fixtures/direct-import/lib.ts" => 'import { VERSION } from "https://deno.land/std@0.204.0/version.ts"; -export const noop = () => {}; -', - "file:///home/shun/molt/test/fixtures/direct-import/mod.ts" => 'import { VERSION } from "https://deno.land/std@0.204.0/version.ts"; +[ + 'import { VERSION } from "https://deno.land/std@0.204.0/version.ts"; import { createGraph } from "https://deno.land/x/deno_graph@0.59.1/mod.ts"; import { emojify } from "npm:node-emoji@2.1.0"; import { noop } from "./lib.ts"; ', -} + 'import { VERSION } from "https://deno.land/std@0.204.0/version.ts"; +export const noop = () => {}; +', +] `; snapshot[`commitAll() > group by dependency name 1`] = ` -FileSystemFake(2) [Map] { - "file:///home/shun/molt/test/fixtures/direct-import/lib.ts" => 'import { VERSION } from "https://deno.land/std@0.204.0/version.ts"; -export const noop = () => {}; -', - "file:///home/shun/molt/test/fixtures/direct-import/mod.ts" => 'import { VERSION } from "https://deno.land/std@0.200.0/version.ts"; +[ + 'import { VERSION } from "https://deno.land/std@0.200.0/version.ts"; import { createGraph } from "https://deno.land/x/deno_graph@0.50.0/mod.ts"; import { emojify } from "npm:node-emoji@2.1.0"; import { noop } from "./lib.ts"; ', -} + 'import { VERSION } from "https://deno.land/std@0.204.0/version.ts"; +export const noop = () => {}; +', +] `; snapshot[`commitAll() > group by module (file) name 1`] = ` -FileSystemFake(2) [Map] { - "file:///home/shun/molt/test/fixtures/direct-import/lib.ts" => 'import { VERSION } from "https://deno.land/std@0.204.0/version.ts"; -export const noop = () => {}; -', - "file:///home/shun/molt/test/fixtures/direct-import/mod.ts" => 'import { VERSION } from "https://deno.land/std@0.204.0/version.ts"; +[ + 'import { VERSION } from "https://deno.land/std@0.204.0/version.ts"; import { createGraph } from "https://deno.land/x/deno_graph@0.59.1/mod.ts"; import { emojify } from "npm:node-emoji@2.1.0"; import { noop } from "./lib.ts"; ', -} + 'import { VERSION } from "https://deno.land/std@0.204.0/version.ts"; +export const noop = () => {}; +', +] `; diff --git a/lib/__snapshots__/update_test.ts.snap b/lib/__snapshots__/update_test.ts.snap index 4971ef78..2c0ebff9 100644 --- a/lib/__snapshots__/update_test.ts.snap +++ b/lib/__snapshots__/update_test.ts.snap @@ -6,15 +6,12 @@ snapshot[`_create > https://deno.land/std 1`] = ` span: undefined, specifier: "https://deno.land/std@0.1.0/version.ts", }, - map: undefined, name: "deno.land/std", path: "/version.ts", - referrer: "file:///home/shun/molt/test/fixtures/direct-import/mod.ts", specifier: { from: "https://deno.land/std@0.1.0/version.ts", to: "https://deno.land/std@0.204.0/version.ts", }, - type: undefined, version: { from: "0.1.0", to: "0.204.0", @@ -28,15 +25,12 @@ snapshot[`_create > https://deno.land/std - unversioned 1`] = ` span: undefined, specifier: "https://deno.land/std/version.ts", }, - map: undefined, name: "deno.land/std", path: "/version.ts", - referrer: "file:///home/shun/molt/test/fixtures/direct-import/mod.ts", specifier: { from: "https://deno.land/std/version.ts", to: "https://deno.land/std@0.204.0/version.ts", }, - type: undefined, version: { from: undefined, to: "0.204.0", @@ -50,15 +44,12 @@ snapshot[`_create > https://deno.land/x/deno_graph 1`] = ` span: undefined, specifier: "https://deno.land/x/deno_graph@0.1.0/mod.ts", }, - map: undefined, name: "deno.land/x/deno_graph", path: "/mod.ts", - referrer: "file:///home/shun/molt/test/fixtures/direct-import/mod.ts", specifier: { from: "https://deno.land/x/deno_graph@0.1.0/mod.ts", to: "https://deno.land/x/deno_graph@0.59.1/mod.ts", }, - type: undefined, version: { from: "0.1.0", to: "0.59.1", @@ -72,15 +63,12 @@ snapshot[`_create > npm:node-emoji 1`] = ` span: undefined, specifier: "npm:node-emoji@1.0.0", }, - map: undefined, name: "node-emoji", path: "", - referrer: "file:///home/shun/molt/test/fixtures/direct-import/mod.ts", specifier: { from: "npm:node-emoji@1.0.0", to: "npm:node-emoji@2.1.0", }, - type: undefined, version: { from: "1.0.0", to: "2.1.0", @@ -94,15 +82,12 @@ snapshot[`_create > npm:node-emoji - unversioned 1`] = ` span: undefined, specifier: "npm:node-emoji", }, - map: undefined, name: "node-emoji", path: "", - referrer: "file:///home/shun/molt/test/fixtures/direct-import/mod.ts", specifier: { from: "npm:node-emoji", to: "npm:node-emoji@2.1.0", }, - type: undefined, version: { from: undefined, to: "2.1.0", @@ -111,249 +96,227 @@ snapshot[`_create > npm:node-emoji - unversioned 1`] = ` `; snapshot[`collect > direct import 1`] = ` -[ - { - code: { - span: { - end: { - character: 66, - line: 0, - }, - start: { - character: 24, - line: 0, - }, +{ + code: { + span: { + end: { + character: 66, + line: 0, + }, + start: { + character: 24, + line: 0, }, - specifier: "https://deno.land/std@0.200.0/version.ts", - }, - map: undefined, - name: "deno.land/std", - path: "/version.ts", - referrer: "file:///home/shun/molt/test/fixtures/direct-import/lib.ts", - specifier: { - from: "https://deno.land/std@0.200.0/version.ts", - to: "https://deno.land/std@0.204.0/version.ts", - }, - type: undefined, - version: { - from: "0.200.0", - to: "0.204.0", }, + specifier: "https://deno.land/std@0.200.0/version.ts", }, - { - code: { - span: { - end: { - character: 74, - line: 1, - }, - start: { - character: 28, - line: 1, - }, + name: "deno.land/std", + path: "/version.ts", + specifier: { + from: "https://deno.land/std@0.200.0/version.ts", + to: "https://deno.land/std@0.204.0/version.ts", + }, + version: { + from: "0.200.0", + to: "0.204.0", + }, +} +`; + +snapshot[`collect > direct import 2`] = ` +{ + code: { + span: { + end: { + character: 74, + line: 1, + }, + start: { + character: 28, + line: 1, }, - specifier: "https://deno.land/x/deno_graph@0.50.0/mod.ts", - }, - map: undefined, - name: "deno.land/x/deno_graph", - path: "/mod.ts", - referrer: "file:///home/shun/molt/test/fixtures/direct-import/mod.ts", - specifier: { - from: "https://deno.land/x/deno_graph@0.50.0/mod.ts", - to: "https://deno.land/x/deno_graph@0.59.1/mod.ts", - }, - type: undefined, - version: { - from: "0.50.0", - to: "0.59.1", }, + specifier: "https://deno.land/x/deno_graph@0.50.0/mod.ts", + }, + name: "deno.land/x/deno_graph", + path: "/mod.ts", + specifier: { + from: "https://deno.land/x/deno_graph@0.50.0/mod.ts", + to: "https://deno.land/x/deno_graph@0.59.1/mod.ts", + }, + version: { + from: "0.50.0", + to: "0.59.1", }, - { - code: { - span: { - end: { - character: 46, - line: 2, - }, - start: { - character: 24, - line: 2, - }, +} +`; + +snapshot[`collect > direct import 3`] = ` +{ + code: { + span: { + end: { + character: 46, + line: 2, + }, + start: { + character: 24, + line: 2, }, - specifier: "npm:node-emoji@1.0.0", - }, - map: undefined, - name: "node-emoji", - path: "", - referrer: "file:///home/shun/molt/test/fixtures/direct-import/mod.ts", - specifier: { - from: "npm:node-emoji@1.0.0", - to: "npm:node-emoji@2.1.0", - }, - type: undefined, - version: { - from: "1.0.0", - to: "2.1.0", }, + specifier: "npm:node-emoji@1.0.0", }, - { - code: { - span: { - end: { - character: 66, - line: 0, - }, - start: { - character: 24, - line: 0, - }, + name: "node-emoji", + path: "", + specifier: { + from: "npm:node-emoji@1.0.0", + to: "npm:node-emoji@2.1.0", + }, + version: { + from: "1.0.0", + to: "2.1.0", + }, +} +`; + +snapshot[`collect > direct import 4`] = ` +{ + code: { + span: { + end: { + character: 66, + line: 0, + }, + start: { + character: 24, + line: 0, }, - specifier: "https://deno.land/std@0.200.0/version.ts", - }, - map: undefined, - name: "deno.land/std", - path: "/version.ts", - referrer: "file:///home/shun/molt/test/fixtures/direct-import/mod.ts", - specifier: { - from: "https://deno.land/std@0.200.0/version.ts", - to: "https://deno.land/std@0.204.0/version.ts", - }, - type: undefined, - version: { - from: "0.200.0", - to: "0.204.0", }, + specifier: "https://deno.land/std@0.200.0/version.ts", + }, + name: "deno.land/std", + path: "/version.ts", + specifier: { + from: "https://deno.land/std@0.200.0/version.ts", + to: "https://deno.land/std@0.204.0/version.ts", + }, + version: { + from: "0.200.0", + to: "0.204.0", }, -] +} `; snapshot[`collect > import map 1`] = ` -[ - { - code: { - span: { - end: { - character: 66, - line: 0, - }, - start: { - character: 24, - line: 0, - }, +{ + code: { + span: { + end: { + character: 66, + line: 0, + }, + start: { + character: 24, + line: 0, }, - specifier: "https://deno.land/std@0.200.0/version.ts", - }, - map: undefined, - name: "deno.land/std", - path: "/version.ts", - referrer: "file:///home/shun/molt/test/fixtures/import-map/lib.ts", - specifier: { - from: "https://deno.land/std@0.200.0/version.ts", - to: "https://deno.land/std@0.204.0/version.ts", - }, - type: undefined, - version: { - from: "0.200.0", - to: "0.204.0", }, + specifier: "https://deno.land/std@0.200.0/version.ts", + }, + name: "deno.land/std", + path: "/version.ts", + specifier: { + from: "https://deno.land/std@0.200.0/version.ts", + to: "https://deno.land/std@0.204.0/version.ts", }, - { - code: { - span: { - end: { - character: 40, - line: 1, - }, - start: { - character: 28, - line: 1, - }, + version: { + from: "0.200.0", + to: "0.204.0", + }, +} +`; + +snapshot[`collect > import map 2`] = ` +{ + code: { + span: { + end: { + character: 40, + line: 1, + }, + start: { + character: 28, + line: 1, }, - specifier: "deno_graph", - }, - map: { - from: "deno_graph", - source: "file:///home/shun/molt/test/fixtures/import-map/deno.json", - to: "https://deno.land/x/deno_graph@0.50.0/mod.ts", - }, - name: "deno.land/x/deno_graph", - path: "/mod.ts", - referrer: "file:///home/shun/molt/test/fixtures/import-map/mod.ts", - specifier: { - from: "https://deno.land/x/deno_graph@0.50.0/mod.ts", - to: "https://deno.land/x/deno_graph@0.59.1/mod.ts", - }, - type: undefined, - version: { - from: "0.50.0", - to: "0.59.1", }, + specifier: "deno_graph", + }, + name: "deno.land/x/deno_graph", + path: "/mod.ts", + specifier: { + from: "https://deno.land/x/deno_graph@0.50.0/mod.ts", + to: "https://deno.land/x/deno_graph@0.59.1/mod.ts", + }, + version: { + from: "0.50.0", + to: "0.59.1", }, - { - code: { - span: { - end: { - character: 36, - line: 2, - }, - start: { - character: 24, - line: 2, - }, +} +`; + +snapshot[`collect > import map 3`] = ` +{ + code: { + span: { + end: { + character: 36, + line: 2, + }, + start: { + character: 24, + line: 2, }, - specifier: "node-emoji", - }, - map: { - from: "node-emoji", - source: "file:///home/shun/molt/test/fixtures/import-map/deno.json", - to: "npm:node-emoji@1.0.0", - }, - name: "node-emoji", - path: "", - referrer: "file:///home/shun/molt/test/fixtures/import-map/mod.ts", - specifier: { - from: "npm:node-emoji@1.0.0", - to: "npm:node-emoji@2.1.0", - }, - type: undefined, - version: { - from: "1.0.0", - to: "2.1.0", }, + specifier: "node-emoji", }, - { - code: { - span: { - end: { - character: 40, - line: 0, - }, - start: { - character: 24, - line: 0, - }, + name: "node-emoji", + path: "", + specifier: { + from: "npm:node-emoji@1.0.0", + to: "npm:node-emoji@2.1.0", + }, + version: { + from: "1.0.0", + to: "2.1.0", + }, +} +`; + +snapshot[`collect > import map 4`] = ` +{ + code: { + span: { + end: { + character: 40, + line: 0, + }, + start: { + character: 24, + line: 0, }, - specifier: "std/version.ts", - }, - map: { - from: "std/", - source: "file:///home/shun/molt/test/fixtures/import-map/deno.json", - to: "https://deno.land/std@0.200.0/", - }, - name: "deno.land/std", - path: "/version.ts", - referrer: "file:///home/shun/molt/test/fixtures/import-map/mod.ts", - specifier: { - from: "https://deno.land/std@0.200.0/version.ts", - to: "https://deno.land/std@0.204.0/version.ts", - }, - type: undefined, - version: { - from: "0.200.0", - to: "0.204.0", }, + specifier: "std/version.ts", + }, + name: "deno.land/std", + path: "/version.ts", + specifier: { + from: "https://deno.land/std@0.200.0/version.ts", + to: "https://deno.land/std@0.204.0/version.ts", }, -] + version: { + from: "0.200.0", + to: "0.204.0", + }, +} `; snapshot[`applyToModule > https://deno.land/x/deno_graph 1`] = ` diff --git a/lib/file_test.ts b/lib/file_test.ts index fa25f77d..04b6cee1 100644 --- a/lib/file_test.ts +++ b/lib/file_test.ts @@ -9,8 +9,8 @@ import { import { assertArrayIncludes, assertEquals } from "./std/assert.ts"; import { assertFindSpyCallArg, - createWriteTextFileStub, FileSystemFake, + WriteTextFileStub, } from "./testing.ts"; import { DependencyUpdate } from "./update.ts"; import { FileUpdate } from "./file.ts"; @@ -56,7 +56,7 @@ describe("collect", () => { describe("writeAll", () => { let fs: FileSystemFake; - let writeTextFileStub: ReturnType; + let writeTextFileStub: WriteTextFileStub; afterEach(() => { writeTextFileStub.restore(); @@ -64,7 +64,7 @@ describe("writeAll", () => { beforeEach(() => { fs = new FileSystemFake(); - writeTextFileStub = createWriteTextFileStub(fs); + writeTextFileStub = WriteTextFileStub.create(fs); }); it("direct import", async (t) => { diff --git a/lib/git_test.ts b/lib/git_test.ts index 4ab110ac..29205a05 100644 --- a/lib/git_test.ts +++ b/lib/git_test.ts @@ -10,23 +10,19 @@ import { import { assertFindSpyCall, createCommandStub, - createReadTextFileStub, - createWriteTextFileStub, FileSystemFake, + ReadTextFileStub, + WriteTextFileStub, } from "./testing.ts"; import { URI } from "./uri.ts"; import { DependencyUpdate } from "./update.ts"; import { commitAll } from "./git.ts"; -function normalizePath(path: string) { - return Deno.build.os === "windows" ? path.replaceAll("/", "\\") : path; -} - describe("commitAll()", () => { let updates: DependencyUpdate[]; let fileSystemFake: FileSystemFake; - let writeTextFileStub: ReturnType; - let readTextFileStub: ReturnType; + let writeTextFileStub: WriteTextFileStub; + let readTextFileStub: ReadTextFileStub; let CommandStub: ReturnType; beforeAll(async () => { @@ -34,10 +30,10 @@ describe("commitAll()", () => { "./test/fixtures/direct-import/mod.ts", ); fileSystemFake = new FileSystemFake(); - readTextFileStub = createReadTextFileStub(fileSystemFake, { + readTextFileStub = ReadTextFileStub.create(fileSystemFake, { readThrough: true, }); - writeTextFileStub = createWriteTextFileStub(fileSystemFake); + writeTextFileStub = WriteTextFileStub.create(fileSystemFake); }); afterAll(() => { @@ -56,20 +52,10 @@ describe("commitAll()", () => { it("no grouping", async (t) => { await commitAll(updates); // TODO: Can't test this because of the order of targets is not guaranteed. - // assertSomeSpyCall(CommandStub, { - // args: [ - // "git", - // { args: ["add", "src/fixtures/mod.ts", "src/fixtures/lib.ts"] }, - // ], - // }); - assertFindSpyCall(CommandStub, { - args: [ - "git", - { args: ["commit", "-m", "build(deps): update dependencies"] }, - ], - }); + // assertGitAdd(CommandStub, "src/fixtures/mod.ts", "src/fixtures/lib.ts"); + assertGitCommit(CommandStub, "build(deps): update dependencies"); assertSpyCalls(CommandStub, 2); - await assertSnapshot(t, fileSystemFake); + await assertSnapshot(t, Array.from(fileSystemFake.values())); }); it("group by dependency name", async (t) => { @@ -77,101 +63,59 @@ describe("commitAll()", () => { groupBy: (update) => update.name, composeCommitMessage: ({ group }) => `build(deps): update ${group}`, }); - assertFindSpyCall(CommandStub, { - args: [ - "git", - { args: ["add", normalizePath("test/fixtures/direct-import/mod.ts")] }, - ], - }); - assertFindSpyCall(CommandStub, { - args: [ - "git", - { args: ["commit", "-m", "build(deps): update node-emoji"] }, - ], - }); - assertFindSpyCall(CommandStub, { - args: [ - "git", - { args: ["add", normalizePath("test/fixtures/direct-import/mod.ts")] }, - ], - }); - assertFindSpyCall(CommandStub, { - args: [ - "git", - { - args: ["commit", "-m", "build(deps): update deno.land/x/deno_graph"], - }, - ], - }); + assertGitAdd(CommandStub, "test/fixtures/direct-import/mod.ts"); + assertGitCommit(CommandStub, "build(deps): update node-emoji"); + assertGitAdd(CommandStub, "test/fixtures/direct-import/mod.ts"); + assertGitCommit(CommandStub, "build(deps): update deno.land/x/deno_graph"); // TODO: Can't test this because of the order of targets is not guaranteed. - // assertSomeSpyCall(CommandStub, { - // args: [ - // "git", - // { - // args: [ - // "add", - // normalizePath("test/fixtures/direct-import/mod.ts"), - // normalizePath("test/fixtures/direct-import/lib.ts"), - // ], - // }, - // ], - // }); - assertFindSpyCall(CommandStub, { - args: [ - "git", - { args: ["commit", "-m", "build(deps): update deno.land/std"] }, - ], - }); + // assertGitAdd(CommandStub, "src/fixtures/lib.ts", "src/fixtures/mod.ts"); + assertGitCommit(CommandStub, "build(deps): update deno.land/std"); assertSpyCalls(CommandStub, 6); - await assertSnapshot(t, fileSystemFake); + await assertSnapshot(t, Array.from(fileSystemFake.values())); }); it("group by module (file) name", async (t) => { await commitAll(updates, { - groupBy: (update) => URI.relative(update.referrer), - composeCommitMessage: ({ group }) => `build(deps): update ${group}`, - }); - assertFindSpyCall(CommandStub, { - args: [ - "git", - { args: ["add", normalizePath("test/fixtures/direct-import/mod.ts")] }, - ], - }); - assertFindSpyCall(CommandStub, { - args: [ - "git", - { - args: [ - "commit", - "-m", - normalizePath( - "build(deps): update test/fixtures/direct-import/mod.ts", - ), - ], - }, - ], - }); - assertFindSpyCall(CommandStub, { - args: [ - "git", - { args: ["add", normalizePath("test/fixtures/direct-import/lib.ts")] }, - ], - }); - assertFindSpyCall(CommandStub, { - args: [ - "git", - { - args: [ - "commit", - "-m", - normalizePath( - "build(deps): update test/fixtures/direct-import/lib.ts", - ), - ], - }, - ], + groupBy: (update) => update.referrer, + composeCommitMessage: ({ group }) => { + const uri = URI.ensure("file")(group); + const relative = URI.relative(uri); + return `build(deps): update ${relative}`; + } }); + assertGitAdd(CommandStub, "test/fixtures/direct-import/mod.ts"); + assertGitCommit(CommandStub, "build(deps): update test/fixtures/direct-import/mod.ts"); + assertGitAdd(CommandStub, "test/fixtures/direct-import/lib.ts"); + assertGitCommit(CommandStub, "build(deps): update test/fixtures/direct-import/lib.ts"); assertSpyCalls(CommandStub, 4); - await assertSnapshot(t, fileSystemFake); + await assertSnapshot(t, Array.from(fileSystemFake.values())); }); }); + +function normalizePath(path: string) { + return Deno.build.os === "windows" ? path.replaceAll("/", "\\") : path; +} + +function assertGitCommit( + Command: ReturnType, + message: string +) { + assertFindSpyCall(Command, { + args: [ + "git", + { args: ["commit", "-m", message] }, + ], + }); +} + +function assertGitAdd( + Command: ReturnType, + ...paths: string[] +) { + assertFindSpyCall(Command, { + args: [ + "git", + { args: ["add", ...paths.map(normalizePath)] }, + ], + }); +} diff --git a/lib/std/collections.ts b/lib/std/collections.ts index 5d442cde..f2f59274 100644 --- a/lib/std/collections.ts +++ b/lib/std/collections.ts @@ -1,2 +1,3 @@ export { distinct } from "https://deno.land/std@0.204.0/collections/distinct.ts"; export { maxBy } from "https://deno.land/std@0.204.0/collections/max_by.ts"; +export { filterKeys } from "https://deno.land/std@0.204.0/collections/filter_keys.ts"; diff --git a/lib/testing.ts b/lib/testing.ts index 83bcb299..72d32d1a 100644 --- a/lib/testing.ts +++ b/lib/testing.ts @@ -41,36 +41,42 @@ export function createCommandStub(): ConstructorSpy< export class FileSystemFake extends Map, string> {} -export function createReadTextFileStub( - fs: FileSystemFake, - options?: { - readThrough?: boolean; - }, -): Stub { - const original = Deno.readTextFile; - return stub( - Deno, - "readTextFile", - async (path) => { - return fs.get(URI.from(path)) ?? options?.readThrough - ? await original(path) - : _throw(new Deno.errors.NotFound(`File not found: ${path}`)); +export const ReadTextFileStub = { + create( + fs: FileSystemFake, + options?: { + readThrough?: boolean; }, - ); -} + ): Stub { + const original = Deno.readTextFile; + return stub( + Deno, + "readTextFile", + async (path) => { + return fs.get(URI.from(path)) ?? options?.readThrough + ? await original(path) + : _throw(new Deno.errors.NotFound(`File not found: ${path}`)); + }, + ); + }, +}; +export type ReadTextFileStub = ReturnType; -export function createWriteTextFileStub( - fs: FileSystemFake, -) { - return stub( - Deno, - "writeTextFile", - // deno-lint-ignore require-await - async (path, data) => { - fs.set(URI.from(path), data.toString()); - }, - ); -} +export const WriteTextFileStub = { + create( + fs: FileSystemFake, + ) { + return stub( + Deno, + "writeTextFile", + // deno-lint-ignore require-await + async (path, data) => { + fs.set(URI.from(path), data.toString()); + }, + ); + }, +}; +export type WriteTextFileStub = ReturnType; /** Asserts that a spy is called as expected at any index. */ export function assertFindSpyCall< diff --git a/lib/update_test.ts b/lib/update_test.ts index cdd05714..db069e90 100644 --- a/lib/update_test.ts +++ b/lib/update_test.ts @@ -7,10 +7,24 @@ import { assertObjectMatch, assertThrows, } from "./std/assert.ts"; +import { filterKeys } from "./std/collections.ts"; import { URI } from "./uri.ts"; import { _create, createVersionProp, DependencyUpdate } from "./update.ts"; import { ImportMap } from "./import_map.ts"; +async function assertUpdateSnapshot( + t: Deno.TestContext, + update: DependencyUpdate, +) { + await assertSnapshot( + t, + filterKeys( + update as Readonly>, + (key) => ["name", "version", "path", "specifier", "code"].includes(key), + ), + ); +} + describe("_create", () => { it("https://deno.land/std", async (t) => { const update = await _create({ @@ -20,7 +34,7 @@ describe("_create", () => { } as any, }, URI.from("test/fixtures/direct-import/mod.ts")); assertExists(update); - await assertSnapshot(t, update); + await assertUpdateSnapshot(t, update); }); it("https://deno.land/std - unversioned", async (t) => { const update = await _create({ @@ -30,7 +44,7 @@ describe("_create", () => { } as any, }, URI.from("test/fixtures/direct-import/mod.ts")); assertExists(update); - await assertSnapshot(t, update); + await assertUpdateSnapshot(t, update); }); it("https://deno.land/x/deno_graph", async (t) => { const update = await _create({ @@ -40,7 +54,7 @@ describe("_create", () => { } as any, }, URI.from("test/fixtures/direct-import/mod.ts")); assertExists(update); - await assertSnapshot(t, update); + await assertUpdateSnapshot(t, update); }); it("npm:node-emoji", async (t) => { const update = await _create({ @@ -50,7 +64,7 @@ describe("_create", () => { } as any, }, URI.from("test/fixtures/direct-import/mod.ts")); assertExists(update); - await assertSnapshot(t, update); + await assertUpdateSnapshot(t, update); }); it("npm:node-emoji - unversioned", async (t) => { const update = await _create({ @@ -60,7 +74,7 @@ describe("_create", () => { } as any, }, URI.from("test/fixtures/direct-import/mod.ts")); assertExists(update); - await assertSnapshot(t, update); + await assertUpdateSnapshot(t, update); }); }); @@ -111,7 +125,9 @@ describe("collect", () => { "./test/fixtures/direct-import/mod.ts", ); assertEquals(updates.length, 4); - await assertSnapshot(t, updates); + for (const update of updates) { + await assertUpdateSnapshot(t, update); + } }); it("import map", async (t) => { const updates = await DependencyUpdate.collect( @@ -121,7 +137,9 @@ describe("collect", () => { }, ); assertEquals(updates.length, 4); - await assertSnapshot(t, updates); + for (const update of updates) { + await assertUpdateSnapshot(t, update); + } }); });