chore: cleanup all test files (#293)

## Changes made
- Removed all unused imports, and made sure type imports were labeled
correctly
- Updated all comparisons to be more strict
- Simplified loops to remove unneeded closure functions
- Removed all explicit `any` types
- Updated how strings were defined to follow general TypeScript best
practices

## Notes
- We definitely want some kind of linting setup for this repo. I'm going
to bring this up when Blueberry has its next team meeting next week
pull/310/head
Michael Smith 9 months ago committed by GitHub
parent bd6747f9bc
commit 438c904567
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,6 +1,5 @@
import { describe, expect, it } from "bun:test"; import { describe, expect, it } from "bun:test";
import { import {
executeScriptInContainer,
runTerraformApply, runTerraformApply,
runTerraformInit, runTerraformInit,
testRequiredVariables, testRequiredVariables,

@ -1,6 +1,5 @@
import { describe, expect, it } from "bun:test"; import { describe, expect, it } from "bun:test";
import { import {
executeScriptInContainer,
runTerraformApply, runTerraformApply,
runTerraformInit, runTerraformInit,
testRequiredVariables, testRequiredVariables,

@ -1,10 +1,5 @@
import { describe, expect, it } from "bun:test"; import { describe } from "bun:test";
import { import { runTerraformInit, testRequiredVariables } from "../test";
executeScriptInContainer,
runTerraformApply,
runTerraformInit,
testRequiredVariables,
} from "../test";
describe("coder-login", async () => { describe("coder-login", async () => {
await runTerraformInit(import.meta.dir); await runTerraformInit(import.meta.dir);

@ -1,6 +1,5 @@
import { describe, expect, it } from "bun:test"; import { describe, expect, it } from "bun:test";
import { import {
executeScriptInContainer,
runTerraformApply, runTerraformApply,
runTerraformInit, runTerraformInit,
testRequiredVariables, testRequiredVariables,

@ -1,6 +1,5 @@
import { describe, expect, it } from "bun:test"; import { describe, expect, it } from "bun:test";
import { import {
executeScriptInContainer,
runTerraformApply, runTerraformApply,
runTerraformInit, runTerraformInit,
testRequiredVariables, testRequiredVariables,

@ -1,3 +1,4 @@
import { type Server, serve } from "bun";
import { describe, expect, it } from "bun:test"; import { describe, expect, it } from "bun:test";
import { import {
createJSONResponse, createJSONResponse,
@ -9,7 +10,6 @@ import {
testRequiredVariables, testRequiredVariables,
writeCoder, writeCoder,
} from "../test"; } from "../test";
import { Server, serve } from "bun";
describe("github-upload-public-key", async () => { describe("github-upload-public-key", async () => {
await runTerraformInit(import.meta.dir); 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 () => { it("creates new key if one does not exist", async () => {
const { instance, id, server } = await setupContainer(); const { instance, id, server } = await setupContainer();
await writeCoder(id, "echo foo"); await writeCoder(id, "echo foo");
let exec = await execContainer(id, [
const url = server.url.toString().slice(0, -1);
const exec = await execContainer(id, [
"env", "env",
"CODER_ACCESS_URL=" + server.url.toString().slice(0, -1), `CODER_ACCESS_URL=${url}`,
"GITHUB_API_URL=" + server.url.toString().slice(0, -1), `GITHUB_API_URL=${url}`,
"CODER_OWNER_SESSION_TOKEN=foo", "CODER_OWNER_SESSION_TOKEN=foo",
"CODER_EXTERNAL_AUTH_ID=github", "CODER_EXTERNAL_AUTH_ID=github",
"bash", "bash",
@ -42,10 +44,12 @@ describe("github-upload-public-key", async () => {
const { instance, id, server } = await setupContainer(); const { instance, id, server } = await setupContainer();
// use keyword to make server return a existing key // use keyword to make server return a existing key
await writeCoder(id, "echo findkey"); await writeCoder(id, "echo findkey");
let exec = await execContainer(id, [
const url = server.url.toString().slice(0, -1);
const exec = await execContainer(id, [
"env", "env",
"CODER_ACCESS_URL=" + server.url.toString().slice(0, -1), `CODER_ACCESS_URL=${url}`,
"GITHUB_API_URL=" + server.url.toString().slice(0, -1), `GITHUB_API_URL=${url}`,
"CODER_OWNER_SESSION_TOKEN=foo", "CODER_OWNER_SESSION_TOKEN=foo",
"CODER_EXTERNAL_AUTH_ID=github", "CODER_EXTERNAL_AUTH_ID=github",
"bash", "bash",
@ -95,7 +99,7 @@ const setupServer = async (): Promise<Server> => {
} }
// case: key already exists // case: key already exists
if (req.headers.get("Authorization") == "Bearer findkey") { if (req.headers.get("Authorization") === "Bearer findkey") {
return createJSONResponse([ return createJSONResponse([
{ {
key: "foo", key: "foo",

@ -116,7 +116,7 @@ EOF`;
const proxies = ["foo", "bar", "baz"] const proxies = ["foo", "bar", "baz"]
.map((r) => `https://${user}:@${fakeFrogApi}/go/${r}`) .map((r) => `https://${user}:@${fakeFrogApi}/go/${r}`)
.join(","); .join(",");
expect(proxyEnv["value"]).toEqual(proxies); expect(proxyEnv.value).toEqual(proxies);
const coderScript = findResourceInstance(state, "coder_script"); const coderScript = findResourceInstance(state, "coder_script");
expect(coderScript.script).toContain( expect(coderScript.script).toContain(

@ -151,7 +151,7 @@ EOF`;
const proxies = ["foo", "bar", "baz"] const proxies = ["foo", "bar", "baz"]
.map((r) => `https://${user}:${token}@${fakeFrogApi}/go/${r}`) .map((r) => `https://${user}:${token}@${fakeFrogApi}/go/${r}`)
.join(","); .join(",");
expect(proxyEnv["value"]).toEqual(proxies); expect(proxyEnv.value).toEqual(proxies);
const coderScript = findResourceInstance(state, "coder_script"); const coderScript = findResourceInstance(state, "coder_script");
expect(coderScript.script).toContain( expect(coderScript.script).toContain(

@ -1,20 +1,20 @@
import { describe, expect, it } from "bun:test"; import { describe, expect, it } from "bun:test";
import { import {
execContainer,
executeScriptInContainer, executeScriptInContainer,
findResourceInstance,
runContainer,
runTerraformApply, runTerraformApply,
runTerraformInit, runTerraformInit,
testRequiredVariables, testRequiredVariables,
findResourceInstance, type TerraformState,
runContainer,
TerraformState,
execContainer,
} from "../test"; } from "../test";
// executes the coder script after installing pip // executes the coder script after installing pip
const executeScriptInContainerWithPip = async ( const executeScriptInContainerWithPip = async (
state: TerraformState, state: TerraformState,
image: string, image: string,
shell: string = "sh", shell = "sh",
): Promise<{ ): Promise<{
exitCode: number; exitCode: number;
stdout: string[]; stdout: string[];

@ -1,4 +1,4 @@
import { describe, expect, it } from "bun:test"; import { describe } from "bun:test";
import { runTerraformInit, testRequiredVariables } from "../test"; import { runTerraformInit, testRequiredVariables } from "../test";
describe("nodejs", async () => { describe("nodejs", async () => {

@ -1,13 +1,9 @@
import { readableStreamToText, spawn } from "bun";
import { describe, expect, it } from "bun:test"; import { describe, expect, it } from "bun:test";
import { import {
executeScriptInContainer, executeScriptInContainer,
runTerraformApply, runTerraformApply,
runTerraformInit, runTerraformInit,
testRequiredVariables, testRequiredVariables,
runContainer,
execContainer,
findResourceInstance,
} from "../test"; } from "../test";
describe("personalize", async () => { describe("personalize", async () => {

@ -72,7 +72,7 @@ executed`,
it("formats execution with milliseconds", async () => { it("formats execution with milliseconds", async () => {
await assertSlackMessage({ await assertSlackMessage({
command: "echo test", command: "echo test",
format: `$COMMAND took $DURATION`, format: "$COMMAND took $DURATION",
durationMS: 150, durationMS: 150,
output: "echo test took 150ms", output: "echo test took 150ms",
}); });
@ -81,7 +81,7 @@ executed`,
it("formats execution with seconds", async () => { it("formats execution with seconds", async () => {
await assertSlackMessage({ await assertSlackMessage({
command: "echo test", command: "echo test",
format: `$COMMAND took $DURATION`, format: "$COMMAND took $DURATION",
durationMS: 15000, durationMS: 15000,
output: "echo test took 15.0s", output: "echo test took 15.0s",
}); });
@ -90,7 +90,7 @@ executed`,
it("formats execution with minutes", async () => { it("formats execution with minutes", async () => {
await assertSlackMessage({ await assertSlackMessage({
command: "echo test", command: "echo test",
format: `$COMMAND took $DURATION`, format: "$COMMAND took $DURATION",
durationMS: 120000, durationMS: 120000,
output: "echo test took 2m 0.0s", output: "echo test took 2m 0.0s",
}); });
@ -99,7 +99,7 @@ executed`,
it("formats execution with hours", async () => { it("formats execution with hours", async () => {
await assertSlackMessage({ await assertSlackMessage({
command: "echo test", command: "echo test",
format: `$COMMAND took $DURATION`, format: "$COMMAND took $DURATION",
durationMS: 60000 * 60, durationMS: 60000 * 60,
output: "echo test took 1hr 0m 0.0s", output: "echo test took 1hr 0m 0.0s",
}); });

@ -1,6 +1,6 @@
import { readableStreamToText, spawn } from "bun"; import { readableStreamToText, spawn } from "bun";
import { afterEach, expect, it } from "bun:test"; import { expect, it } from "bun:test";
import { readFile, unlink } from "fs/promises"; import { readFile, unlink } from "node:fs/promises";
export const runContainer = async ( export const runContainer = async (
image: string, image: string,
@ -21,7 +21,8 @@ export const runContainer = async (
"-c", "-c",
init, init,
]); ]);
let containerID = await readableStreamToText(proc.stdout);
const containerID = await readableStreamToText(proc.stdout);
const exitCode = await proc.exited; const exitCode = await proc.exited;
if (exitCode !== 0) { if (exitCode !== 0) {
throw new Error(containerID); throw new Error(containerID);
@ -36,7 +37,7 @@ export const runContainer = async (
export const executeScriptInContainer = async ( export const executeScriptInContainer = async (
state: TerraformState, state: TerraformState,
image: string, image: string,
shell: string = "sh", shell = "sh",
): Promise<{ ): Promise<{
exitCode: number; exitCode: number;
stdout: string[]; stdout: string[];
@ -116,6 +117,9 @@ export interface CoderScriptAttributes {
url: string; url: string;
} }
export type ResourceInstance<T extends string = string> =
T extends "coder_script" ? CoderScriptAttributes : Record<string, string>;
/** /**
* finds the first instance of the given resource type in the given state. If * 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. * name is specified, it will only find the instance with the given name.
@ -124,10 +128,7 @@ export const findResourceInstance = <T extends string>(
state: TerraformState, state: TerraformState,
type: T, type: T,
name?: string, name?: string,
// if type is "coder_script" return CoderScriptAttributes ): ResourceInstance<T> => {
): T extends "coder_script"
? CoderScriptAttributes
: Record<string, string> => {
const resource = state.resources.find( const resource = state.resources.find(
(resource) => (resource) =>
resource.type === type && (name ? resource.name === name : true), resource.type === type && (name ? resource.name === name : true),
@ -140,7 +141,8 @@ export const findResourceInstance = <T extends string>(
`Resource ${type} has ${resource.instances.length} instances`, `Resource ${type} has ${resource.instances.length} instances`,
); );
} }
return resource.instances[0].attributes as any;
return resource.instances[0].attributes as ResourceInstance<T>;
}; };
/** /**
@ -157,15 +159,15 @@ export const testRequiredVariables = <TVars extends TerraformVariables>(
}); });
const varNames = Object.keys(vars); const varNames = Object.keys(vars);
varNames.forEach((varName) => { for (const varName of varNames) {
// Ensures that every variable provided is required! // Ensures that every variable provided is required!
it("missing variable " + varName, async () => { it(`missing variable: ${varName}`, async () => {
const localVars: TerraformVariables = {}; const localVars: TerraformVariables = {};
varNames.forEach((otherVarName) => { for (const otherVarName of varNames) {
if (otherVarName !== varName) { if (otherVarName !== varName) {
localVars[otherVarName] = vars[otherVarName]; localVars[otherVarName] = vars[otherVarName];
} }
}); }
try { try {
await runTerraformApply(dir, localVars); await runTerraformApply(dir, localVars);
@ -181,7 +183,7 @@ export const testRequiredVariables = <TVars extends TerraformVariables>(
} }
throw new Error(`${varName} is not a required variable!`); throw new Error(`${varName} is not a required variable!`);
}); });
}); }
}; };
/** /**

@ -1,10 +1,14 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "esnext", // If we were just compiling for the tests, we could safely target ESNext at
"module": "nodenext", // 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, "strict": true,
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"moduleResolution": "nodenext", "moduleResolution": "node",
"types": ["bun-types"] "types": ["bun-types"]
} }
} }

@ -22,7 +22,7 @@ describe("vscode-desktop", async () => {
); );
const coder_app = state.resources.find( 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(); expect(coder_app).not.toBeNull();
@ -79,7 +79,7 @@ describe("vscode-desktop", async () => {
}); });
const coder_app = state.resources.find( 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(); expect(coder_app).not.toBeNull();

@ -1,6 +1,6 @@
import { describe, expect, it } from "bun:test"; import { describe, expect, it } from "bun:test";
import { import {
TerraformState, type TerraformState,
runTerraformApply, runTerraformApply,
runTerraformInit, runTerraformInit,
testRequiredVariables, testRequiredVariables,

Loading…
Cancel
Save