diff --git a/aws-region/main.test.ts b/aws-region/main.test.ts index 0693e65a..06f8e56e 100644 --- a/aws-region/main.test.ts +++ b/aws-region/main.test.ts @@ -1,6 +1,5 @@ import { describe, expect, it } from "bun:test"; import { - executeScriptInContainer, runTerraformApply, runTerraformInit, testRequiredVariables, diff --git a/azure-region/main.test.ts b/azure-region/main.test.ts index bebc0c9f..8adbb48b 100644 --- a/azure-region/main.test.ts +++ b/azure-region/main.test.ts @@ -1,6 +1,5 @@ import { describe, expect, it } from "bun:test"; import { - executeScriptInContainer, runTerraformApply, runTerraformInit, testRequiredVariables, diff --git a/coder-login/main.test.ts b/coder-login/main.test.ts index d8fba354..aca43216 100644 --- a/coder-login/main.test.ts +++ b/coder-login/main.test.ts @@ -1,10 +1,5 @@ -import { describe, expect, it } from "bun:test"; -import { - executeScriptInContainer, - runTerraformApply, - runTerraformInit, - testRequiredVariables, -} from "../test"; +import { describe } from "bun:test"; +import { runTerraformInit, testRequiredVariables } from "../test"; describe("coder-login", async () => { await runTerraformInit(import.meta.dir); diff --git a/cursor/main.test.ts b/cursor/main.test.ts index cdf70e6b..3c164698 100644 --- a/cursor/main.test.ts +++ b/cursor/main.test.ts @@ -1,6 +1,5 @@ import { describe, expect, it } from "bun:test"; import { - executeScriptInContainer, runTerraformApply, runTerraformInit, testRequiredVariables, diff --git a/exoscale-zone/main.test.ts b/exoscale-zone/main.test.ts index ca8eeb72..1751cb14 100644 --- a/exoscale-zone/main.test.ts +++ b/exoscale-zone/main.test.ts @@ -1,6 +1,5 @@ import { describe, expect, it } from "bun:test"; import { - executeScriptInContainer, runTerraformApply, runTerraformInit, testRequiredVariables, diff --git a/github-upload-public-key/main.test.ts b/github-upload-public-key/main.test.ts index fb1b9776..6ce16d82 100644 --- a/github-upload-public-key/main.test.ts +++ b/github-upload-public-key/main.test.ts @@ -1,3 +1,4 @@ +import { type Server, serve } from "bun"; import { describe, expect, it } from "bun:test"; import { createJSONResponse, @@ -9,7 +10,6 @@ import { testRequiredVariables, writeCoder, } from "../test"; -import { Server, serve } from "bun"; describe("github-upload-public-key", async () => { await runTerraformInit(import.meta.dir); @@ -21,10 +21,12 @@ describe("github-upload-public-key", async () => { it("creates new key if one does not exist", async () => { const { instance, id, server } = await setupContainer(); await writeCoder(id, "echo foo"); - let exec = await execContainer(id, [ + + const url = server.url.toString().slice(0, -1); + const exec = await execContainer(id, [ "env", - "CODER_ACCESS_URL=" + server.url.toString().slice(0, -1), - "GITHUB_API_URL=" + server.url.toString().slice(0, -1), + `CODER_ACCESS_URL=${url}`, + `GITHUB_API_URL=${url}`, "CODER_OWNER_SESSION_TOKEN=foo", "CODER_EXTERNAL_AUTH_ID=github", "bash", @@ -42,10 +44,12 @@ describe("github-upload-public-key", async () => { const { instance, id, server } = await setupContainer(); // use keyword to make server return a existing key await writeCoder(id, "echo findkey"); - let exec = await execContainer(id, [ + + const url = server.url.toString().slice(0, -1); + const exec = await execContainer(id, [ "env", - "CODER_ACCESS_URL=" + server.url.toString().slice(0, -1), - "GITHUB_API_URL=" + server.url.toString().slice(0, -1), + `CODER_ACCESS_URL=${url}`, + `GITHUB_API_URL=${url}`, "CODER_OWNER_SESSION_TOKEN=foo", "CODER_EXTERNAL_AUTH_ID=github", "bash", @@ -95,7 +99,7 @@ const setupServer = async (): Promise => { } // case: key already exists - if (req.headers.get("Authorization") == "Bearer findkey") { + if (req.headers.get("Authorization") === "Bearer findkey") { return createJSONResponse([ { key: "foo", diff --git a/jfrog-oauth/main.test.ts b/jfrog-oauth/main.test.ts index da8b9bf6..7b0c1a5f 100644 --- a/jfrog-oauth/main.test.ts +++ b/jfrog-oauth/main.test.ts @@ -116,7 +116,7 @@ EOF`; const proxies = ["foo", "bar", "baz"] .map((r) => `https://${user}:@${fakeFrogApi}/go/${r}`) .join(","); - expect(proxyEnv["value"]).toEqual(proxies); + expect(proxyEnv.value).toEqual(proxies); const coderScript = findResourceInstance(state, "coder_script"); expect(coderScript.script).toContain( diff --git a/jfrog-token/main.test.ts b/jfrog-token/main.test.ts index a6fe6bcb..2c856720 100644 --- a/jfrog-token/main.test.ts +++ b/jfrog-token/main.test.ts @@ -151,7 +151,7 @@ EOF`; const proxies = ["foo", "bar", "baz"] .map((r) => `https://${user}:${token}@${fakeFrogApi}/go/${r}`) .join(","); - expect(proxyEnv["value"]).toEqual(proxies); + expect(proxyEnv.value).toEqual(proxies); const coderScript = findResourceInstance(state, "coder_script"); expect(coderScript.script).toContain( diff --git a/jupyterlab/main.test.ts b/jupyterlab/main.test.ts index 122de774..cf9ac1f0 100644 --- a/jupyterlab/main.test.ts +++ b/jupyterlab/main.test.ts @@ -1,20 +1,20 @@ import { describe, expect, it } from "bun:test"; import { + execContainer, executeScriptInContainer, + findResourceInstance, + runContainer, runTerraformApply, runTerraformInit, testRequiredVariables, - findResourceInstance, - runContainer, - TerraformState, - execContainer, + type TerraformState, } from "../test"; // executes the coder script after installing pip const executeScriptInContainerWithPip = async ( state: TerraformState, image: string, - shell: string = "sh", + shell = "sh", ): Promise<{ exitCode: number; stdout: string[]; diff --git a/nodejs/main.test.ts b/nodejs/main.test.ts index 07fc7a54..39e48f49 100644 --- a/nodejs/main.test.ts +++ b/nodejs/main.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "bun:test"; +import { describe } from "bun:test"; import { runTerraformInit, testRequiredVariables } from "../test"; describe("nodejs", async () => { diff --git a/personalize/main.test.ts b/personalize/main.test.ts index 9c8134ea..b499a0b7 100644 --- a/personalize/main.test.ts +++ b/personalize/main.test.ts @@ -1,13 +1,9 @@ -import { readableStreamToText, spawn } from "bun"; import { describe, expect, it } from "bun:test"; import { executeScriptInContainer, runTerraformApply, runTerraformInit, testRequiredVariables, - runContainer, - execContainer, - findResourceInstance, } from "../test"; describe("personalize", async () => { diff --git a/slackme/main.test.ts b/slackme/main.test.ts index eca4f5df..d8d06242 100644 --- a/slackme/main.test.ts +++ b/slackme/main.test.ts @@ -72,7 +72,7 @@ executed`, it("formats execution with milliseconds", async () => { await assertSlackMessage({ command: "echo test", - format: `$COMMAND took $DURATION`, + format: "$COMMAND took $DURATION", durationMS: 150, output: "echo test took 150ms", }); @@ -81,7 +81,7 @@ executed`, it("formats execution with seconds", async () => { await assertSlackMessage({ command: "echo test", - format: `$COMMAND took $DURATION`, + format: "$COMMAND took $DURATION", durationMS: 15000, output: "echo test took 15.0s", }); @@ -90,7 +90,7 @@ executed`, it("formats execution with minutes", async () => { await assertSlackMessage({ command: "echo test", - format: `$COMMAND took $DURATION`, + format: "$COMMAND took $DURATION", durationMS: 120000, output: "echo test took 2m 0.0s", }); @@ -99,7 +99,7 @@ executed`, it("formats execution with hours", async () => { await assertSlackMessage({ command: "echo test", - format: `$COMMAND took $DURATION`, + format: "$COMMAND took $DURATION", durationMS: 60000 * 60, output: "echo test took 1hr 0m 0.0s", }); diff --git a/test.ts b/test.ts index dd1b1c68..5437374f 100644 --- a/test.ts +++ b/test.ts @@ -1,6 +1,6 @@ import { readableStreamToText, spawn } from "bun"; -import { afterEach, expect, it } from "bun:test"; -import { readFile, unlink } from "fs/promises"; +import { expect, it } from "bun:test"; +import { readFile, unlink } from "node:fs/promises"; export const runContainer = async ( image: string, @@ -21,7 +21,8 @@ export const runContainer = async ( "-c", init, ]); - let containerID = await readableStreamToText(proc.stdout); + + const containerID = await readableStreamToText(proc.stdout); const exitCode = await proc.exited; if (exitCode !== 0) { throw new Error(containerID); @@ -36,7 +37,7 @@ export const runContainer = async ( export const executeScriptInContainer = async ( state: TerraformState, image: string, - shell: string = "sh", + shell = "sh", ): Promise<{ exitCode: number; stdout: string[]; @@ -116,6 +117,9 @@ export interface CoderScriptAttributes { url: string; } +export type ResourceInstance = + T extends "coder_script" ? CoderScriptAttributes : Record; + /** * finds the first instance of the given resource type in the given state. If * name is specified, it will only find the instance with the given name. @@ -124,10 +128,7 @@ export const findResourceInstance = ( state: TerraformState, type: T, name?: string, - // if type is "coder_script" return CoderScriptAttributes -): T extends "coder_script" - ? CoderScriptAttributes - : Record => { +): ResourceInstance => { const resource = state.resources.find( (resource) => resource.type === type && (name ? resource.name === name : true), @@ -140,7 +141,8 @@ export const findResourceInstance = ( `Resource ${type} has ${resource.instances.length} instances`, ); } - return resource.instances[0].attributes as any; + + return resource.instances[0].attributes as ResourceInstance; }; /** @@ -157,15 +159,15 @@ export const testRequiredVariables = ( }); const varNames = Object.keys(vars); - varNames.forEach((varName) => { + for (const varName of varNames) { // Ensures that every variable provided is required! - it("missing variable " + varName, async () => { + it(`missing variable: ${varName}`, async () => { const localVars: TerraformVariables = {}; - varNames.forEach((otherVarName) => { + for (const otherVarName of varNames) { if (otherVarName !== varName) { localVars[otherVarName] = vars[otherVarName]; } - }); + } try { await runTerraformApply(dir, localVars); @@ -181,7 +183,7 @@ export const testRequiredVariables = ( } throw new Error(`${varName} is not a required variable!`); }); - }); + } }; /** diff --git a/tsconfig.json b/tsconfig.json index 5de8ae31..c7a5d26e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,10 +1,14 @@ { "compilerOptions": { - "target": "esnext", - "module": "nodenext", + // If we were just compiling for the tests, we could safely target ESNext at + // all times, but just because we've been starting to add more runtime logic + // files to some of the modules, erring on the side of caution by having a + // older compilation target + "target": "ES6", + "module": "ESNext", "strict": true, "allowSyntheticDefaultImports": true, - "moduleResolution": "nodenext", + "moduleResolution": "node", "types": ["bun-types"] } } diff --git a/vscode-desktop/main.test.ts b/vscode-desktop/main.test.ts index 207b4923..7aa144ec 100644 --- a/vscode-desktop/main.test.ts +++ b/vscode-desktop/main.test.ts @@ -22,7 +22,7 @@ describe("vscode-desktop", async () => { ); const coder_app = state.resources.find( - (res) => res.type == "coder_app" && res.name == "vscode", + (res) => res.type === "coder_app" && res.name === "vscode", ); expect(coder_app).not.toBeNull(); @@ -79,7 +79,7 @@ describe("vscode-desktop", async () => { }); const coder_app = state.resources.find( - (res) => res.type == "coder_app" && res.name == "vscode", + (res) => res.type === "coder_app" && res.name === "vscode", ); expect(coder_app).not.toBeNull(); diff --git a/windows-rdp/main.test.ts b/windows-rdp/main.test.ts index 61075d9a..ba5e21a5 100644 --- a/windows-rdp/main.test.ts +++ b/windows-rdp/main.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "bun:test"; import { - TerraformState, + type TerraformState, runTerraformApply, runTerraformInit, testRequiredVariables,