From 17835543a2dfc8f9830420a80eeed39f8666bacb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Elias=20Sj=C3=B6green?= <eliassjogreen1@gmail.com>
Date: Tue, 9 Apr 2024 12:17:44 +0200
Subject: [PATCH] chore: add JSR imports to deno.json and fmt/lint (#38)

---
 deno.json            |  6 ++++
 deps.ts              | 13 ---------
 download.ts          |  8 ++---
 download_test.ts     | 69 +++++++++++++++++++++++++++++++++++++++-----
 test_deps.ts         |  2 --
 test_import_map.json |  7 ++++-
 util.ts              | 12 ++------
 util_test.ts         |  9 ++----
 8 files changed, 84 insertions(+), 42 deletions(-)
 delete mode 100644 deps.ts
 delete mode 100644 test_deps.ts

diff --git a/deno.json b/deno.json
index ad56697..1ef472a 100644
--- a/deno.json
+++ b/deno.json
@@ -7,5 +7,11 @@
     "./download": "./download.ts",
     "./util": "./util.ts"
   },
+  "imports": {
+    "@std/encoding/hex": "jsr:@std/encoding@^0.221.0/hex",
+    "@std/fmt/colors": "jsr:@std/fmt@^0.221.0/colors",
+    "@std/fs": "jsr:@std/fs@^0.221.0",
+    "@std/path": "jsr:@std/path@^0.221.0"
+  },
   "lock": false
 }
diff --git a/deps.ts b/deps.ts
deleted file mode 100644
index 322521d..0000000
--- a/deps.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-export {
-  dirname,
-  extname,
-  fromFileUrl,
-  isAbsolute,
-  join,
-  normalize,
-  resolve,
-  toFileUrl,
-} from "jsr:@std/path@0.214";
-export { ensureDir } from "jsr:@std/fs@0.214";
-export { encodeHex as hex } from "jsr:@std/encoding@0.214/hex";
-export { green } from "jsr:@std/fmt@0.214/colors";
diff --git a/download.ts b/download.ts
index 642e733..b1cfa9f 100644
--- a/download.ts
+++ b/download.ts
@@ -6,15 +6,15 @@
 
 import {
   dirname,
-  ensureDir,
   extname,
   fromFileUrl,
-  green,
   join,
   normalize,
   resolve,
-} from "./deps.ts";
-import {
+} from "@std/path";
+import { ensureDir } from "@std/fs";
+import { green } from "@std/fmt/colors";
+import type {
   ArchRecord,
   CacheLocation,
   FetchOptions,
diff --git a/download_test.ts b/download_test.ts
index 029960a..236dc31 100644
--- a/download_test.ts
+++ b/download_test.ts
@@ -3,10 +3,9 @@ import {
   assertEquals,
   assertMatch,
   assertRejects,
-  basename,
-  dirname,
-  normalize,
-} from "./test_deps.ts";
+} from "jsr:@std/assert";
+import { basename, dirname, normalize } from "@std/path";
+
 import { createDownloadURL, ensureCacheLocation } from "./download.ts";
 
 const ALL_ARCHS = ["x86_64", "aarch64"];
@@ -195,11 +194,34 @@ Deno.test("createDownloadURL", async ({ step }) => {
 });
 
 Deno.test("ensureCacheLocation", async ({ step }) => {
-  await step("deno", async () => {
+  await step("deno", async ({ step }) => {
     const location = await ensureCacheLocation("deno");
     assertEquals(basename(location), "plug");
     assertEquals(basename(dirname(location)), "deno");
     assert(await isDirectory(location));
+
+    await step("missing", async () => {
+      // Save a snapshot of `Deno.build`
+      const build = structuredClone(Deno.build);
+      const HOME = Deno.env.get("HOME");
+      const DENO_DIR = Deno.env.get("DENO_DIR");
+      // @ts-ignore TS2540
+      Deno.build = { os: "linux" };
+      Deno.env.delete("HOME");
+      Deno.env.delete("DENO_DIR");
+
+      await assertRejects(
+        () => ensureCacheLocation("deno"),
+        Error,
+        "Could not get the deno cache directory, try using another CacheLocation in the plug options.",
+      );
+
+      // @ts-ignore TS2540
+      // Restore the snapshot of `Deno.build`
+      Deno.build = build;
+      if (HOME) Deno.env.set("HOME", HOME);
+      if (DENO_DIR) Deno.env.set("DENO_DIR", DENO_DIR);
+    });
   });
 
   await step("cwd", async () => {
@@ -209,10 +231,33 @@ Deno.test("ensureCacheLocation", async ({ step }) => {
     assert(await isDirectory(location));
   });
 
-  await step("cache", async () => {
+  await step("cache", async ({ step }) => {
     const location = await ensureCacheLocation("cache");
     assertEquals(basename(location), "plug");
     assert(await isDirectory(location));
+
+    await step("missing", async () => {
+      // Save a snapshot of `Deno.build`
+      const build = structuredClone(Deno.build);
+      const HOME = Deno.env.get("HOME");
+      const XDG_CACHE_HOME = Deno.env.get("XDG_CACHE_HOME");
+      // @ts-ignore TS2540
+      Deno.build = { os: "linux" };
+      Deno.env.delete("HOME");
+      Deno.env.delete("XDG_CACHE_HOME");
+
+      await assertRejects(
+        () => ensureCacheLocation("cache"),
+        Error,
+        "Could not get the cache directory, try using another CacheLocation in the plug options.",
+      );
+
+      // @ts-ignore TS2540
+      // Restore the snapshot of `Deno.build`
+      Deno.build = build;
+      if (HOME) Deno.env.set("HOME", HOME);
+      if (XDG_CACHE_HOME) Deno.env.set("XDG_CACHE_HOME", XDG_CACHE_HOME);
+    });
   });
 
   await step("tmp", async () => {
@@ -221,7 +266,7 @@ Deno.test("ensureCacheLocation", async ({ step }) => {
     assert(await isDirectory(location));
   });
 
-  await step("string", async () => {
+  await step("path string", async () => {
     const location = await ensureCacheLocation("./plug/cache/");
     assertEquals(basename(location), "cache");
     assertEquals(basename(dirname(location)), "plug");
@@ -229,6 +274,16 @@ Deno.test("ensureCacheLocation", async ({ step }) => {
     assert(await isDirectory(location));
   });
 
+  await step("file string", async () => {
+    const location = await ensureCacheLocation(
+      new URL("./plug/cache/", import.meta.url).href,
+    );
+    assertEquals(basename(location), "cache");
+    assertEquals(basename(dirname(location)), "plug");
+    assertEquals(normalize(dirname(dirname(location))), Deno.cwd());
+    assert(await isDirectory(location));
+  });
+
   await step("URL", async () => {
     const location = await ensureCacheLocation(
       new URL("./plug/cache/url", import.meta.url),
diff --git a/test_deps.ts b/test_deps.ts
deleted file mode 100644
index 5064119..0000000
--- a/test_deps.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export * from "jsr:@std/assert@0.214";
-export * from "jsr:@std/path@0.214";
diff --git a/test_import_map.json b/test_import_map.json
index 94c20d3..7a09b5e 100644
--- a/test_import_map.json
+++ b/test_import_map.json
@@ -4,6 +4,11 @@
     "@denosaurs/plug": "./mod.ts",
     "@denosaurs/plug/types": "./types.ts",
     "@denosaurs/plug/download": "./download.ts",
-    "@denosaurs/plug/util": "./util.ts"
+    "@denosaurs/plug/util": "./util.ts",
+    "@std/assert": "jsr:@std/assert@^0.221.0",
+    "@std/encoding/hex": "jsr:@std/encoding@^0.221.0/hex",
+    "@std/fmt/colors": "jsr:@std/fmt@^0.221.0/colors",
+    "@std/fs": "jsr:@std/fs@^0.221.0",
+    "@std/path": "jsr:@std/path@^0.221.0"
   }
 }
diff --git a/util.ts b/util.ts
index 4f7123c..74f88a0 100644
--- a/util.ts
+++ b/util.ts
@@ -4,14 +4,8 @@
  * @module
  */
 
-import {
-  hex,
-  isAbsolute,
-  join,
-  normalize,
-  resolve,
-  toFileUrl,
-} from "./deps.ts";
+import { isAbsolute, join, normalize, resolve, toFileUrl } from "@std/path";
+import { encodeHex } from "@std/encoding/hex";
 
 const encoder = new TextEncoder();
 
@@ -61,7 +55,7 @@ export function stringToURL(url: string): URL {
  * @private
  */
 export async function hash(value: string): Promise<string> {
-  return hex(
+  return encodeHex(
     new Uint8Array(
       await crypto.subtle.digest("SHA-256", encoder.encode(value)),
     ),
diff --git a/util_test.ts b/util_test.ts
index c144e98..827f82c 100644
--- a/util_test.ts
+++ b/util_test.ts
@@ -3,12 +3,9 @@ import {
   assertEquals,
   assertRejects,
   assertThrows,
-  basename,
-  dirname,
-  fromFileUrl,
-  join,
-  normalize,
-} from "./test_deps.ts";
+} from "jsr:@std/assert";
+import { basename, dirname, fromFileUrl, join, normalize } from "@std/path";
+
 import {
   cacheDir,
   denoCacheDir,