From 3ab35a44f9a8e517de6b38d8fabfc0785f67f5a8 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 21 Jul 2023 13:02:16 +0200 Subject: [PATCH] fix: current version not updated in update check (#1482) --- src/dev/deps.ts | 2 +- src/dev/update_check.ts | 5 +++- tests/cli_update_check_test.ts | 42 ++++++++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/dev/deps.ts b/src/dev/deps.ts index c6cfcb34..bec45345 100644 --- a/src/dev/deps.ts +++ b/src/dev/deps.ts @@ -11,7 +11,7 @@ export { SEP, toFileUrl, } from "https://deno.land/std@0.193.0/path/mod.ts"; -export { DAY } from "https://deno.land/std@0.193.0/datetime/constants.ts"; +export { DAY, WEEK } from "https://deno.land/std@0.193.0/datetime/constants.ts"; export * as colors from "https://deno.land/std@0.193.0/fmt/colors.ts"; export { walk, WalkError } from "https://deno.land/std@0.193.0/fs/walk.ts"; export { parse } from "https://deno.land/std@0.193.0/flags/mod.ts"; diff --git a/src/dev/update_check.ts b/src/dev/update_check.ts index 5cc45834..0eabfc06 100644 --- a/src/dev/update_check.ts +++ b/src/dev/update_check.ts @@ -1,6 +1,6 @@ import { colors, join } from "./deps.ts"; -interface CheckFile { +export interface CheckFile { last_checked: string; latest_version: string; current_version: string; @@ -88,6 +88,9 @@ export async function updateCheck( } } + // Update current version + checkFile.current_version = versions[0]; + // Only check in the specificed interval if (Date.now() >= new Date(checkFile.last_checked).getTime() + interval) { try { diff --git a/tests/cli_update_check_test.ts b/tests/cli_update_check_test.ts index 85404302..9472ab6f 100644 --- a/tests/cli_update_check_test.ts +++ b/tests/cli_update_check_test.ts @@ -5,6 +5,8 @@ import { assertNotMatch, } from "$std/testing/asserts.ts"; import versions from "../versions.json" assert { type: "json" }; +import { CheckFile } from "$fresh/src/dev/update_check.ts"; +import { WEEK } from "$fresh/src/dev/deps.ts"; Deno.test({ name: "stores update check file in $HOME/fresh", @@ -78,20 +80,20 @@ Deno.test({ env: { CI: "false", HOME: tmpDirName, - LATEST_VERSION: "1.30.0", + LATEST_VERSION: "999.999.0", }, }).output(); const decoder = new TextDecoder(); const stdout = colors.stripColor(decoder.decode(out.stdout)); - assertMatch(stdout, /Fresh 1\.30\.0 is available/); + assertMatch(stdout, /Fresh 999\.999\.0 is available/); // Updates check file const text = JSON.parse(await Deno.readTextFile(filePath)); assertEquals(text, { - current_version: "1.1.0", - latest_version: "1.30.0", + current_version: versions[0], + latest_version: "999.999.0", last_checked: text.last_checked, }); @@ -154,3 +156,35 @@ Deno.test({ await Deno.remove(tmpDirName, { recursive: true }); }, }); + +Deno.test({ + name: "updates current version in cache file", + async fn() { + const tmpDirName = await Deno.makeTempDir(); + + const checkFile: CheckFile = { + current_version: "1.2.0", + latest_version: "1.2.0", + last_checked: new Date(Date.now() - WEEK).toISOString(), + }; + + await Deno.writeTextFile( + join(tmpDirName, "latest.json"), + JSON.stringify(checkFile, null, 2), + ); + + const out = await new Deno.Command(Deno.execPath(), { + args: ["run", "-A", "./tests/fixture_update_check/mod.ts"], + env: { + HOME: tmpDirName, + LATEST_VERSION: versions[0], + }, + }).output(); + + const decoder = new TextDecoder(); + const stdout = colors.stripColor(decoder.decode(out.stdout)); + assertNotMatch(stdout, /Fresh .* is available/); + + await Deno.remove(tmpDirName, { recursive: true }); + }, +});