From a6f1405047f6dc07e30a5c474225ae3071308d6a Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Fri, 20 Sep 2024 18:13:57 +0000 Subject: [PATCH] chore: cleanup imports and value declarations for tests --- aws-region/main.test.ts | 1 - azure-region/main.test.ts | 1 - coder-login/main.test.ts | 9 ++------- cursor/main.test.ts | 1 - exoscale-zone/main.test.ts | 1 - github-upload-public-key/main.test.ts | 20 ++++++++++++-------- jfrog-oauth/main.test.ts | 12 ++++-------- jfrog-token/main.test.ts | 2 +- jupyterlab/main.test.ts | 10 +++++----- nodejs/main.test.ts | 2 +- personalize/main.test.ts | 4 ---- slackme/main.test.ts | 8 ++++---- test.ts | 19 ++++++++++--------- vscode-desktop/main.test.ts | 4 ++-- windows-rdp/main.test.ts | 2 +- 15 files changed, 42 insertions(+), 54 deletions(-) diff --git a/aws-region/main.test.ts b/aws-region/main.test.ts index 0693e65..06f8e56 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 bebc0c9..8adbb48 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 d8fba35..aca4321 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 cdf70e6..3c16469 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 ca8eeb7..1751cb1 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 fb1b977..6ce16d8 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 3397eeb..e3a26eb 100644 --- a/jfrog-oauth/main.test.ts +++ b/jfrog-oauth/main.test.ts @@ -1,10 +1,8 @@ -import { serve } from "bun"; +/** + * @todo Add more tests + */ import { describe } from "bun:test"; -import { - createJSONResponse, - runTerraformInit, - testRequiredVariables, -} from "../test"; +import { runTerraformInit, testRequiredVariables } from "../test"; describe("jfrog-oauth", async () => { await runTerraformInit(import.meta.dir); @@ -15,5 +13,3 @@ describe("jfrog-oauth", async () => { package_managers: "{}", }); }); - -//TODO add more tests diff --git a/jfrog-token/main.test.ts b/jfrog-token/main.test.ts index b3b8df9..be5a7be 100644 --- a/jfrog-token/main.test.ts +++ b/jfrog-token/main.test.ts @@ -34,7 +34,7 @@ describe("jfrog-token", async () => { testRequiredVariables(import.meta.dir, { agent_id: "some-agent-id", - jfrog_url: "http://" + fakeFrogHost.hostname + ":" + fakeFrogHost.port, + jfrog_url: `http://${fakeFrogHost.hostname}:${fakeFrogHost.port}`, artifactory_access_token: "XXXX", package_managers: "{}", }); diff --git a/jupyterlab/main.test.ts b/jupyterlab/main.test.ts index 2597dc2..57db24c 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 07fc7a5..39e48f4 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 9c8134e..b499a0b 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 eca4f5d..d8d0624 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 6bdf9d9..a52ef26 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[]; @@ -155,15 +156,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: Record = {}; - varNames.forEach((otherVarName) => { + for (const otherVarName of varNames) { if (otherVarName !== varName) { localVars[otherVarName] = vars[otherVarName]; } - }); + } try { await runTerraformApply(dir, localVars); @@ -179,7 +180,7 @@ export const testRequiredVariables = >( } throw new Error(`${varName} is not a required variable!`); }); - }); + } }; /** diff --git a/vscode-desktop/main.test.ts b/vscode-desktop/main.test.ts index 207b492..7aa144e 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 61075d9..ba5e21a 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,