Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup all test files #293

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion aws-region/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it } from "bun:test";
import {
executeScriptInContainer,
runTerraformApply,
runTerraformInit,
testRequiredVariables,
Expand Down
1 change: 0 additions & 1 deletion azure-region/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it } from "bun:test";
import {
executeScriptInContainer,
runTerraformApply,
runTerraformInit,
testRequiredVariables,
Expand Down
9 changes: 2 additions & 7 deletions coder-login/main.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
1 change: 0 additions & 1 deletion cursor/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it } from "bun:test";
import {
executeScriptInContainer,
runTerraformApply,
runTerraformInit,
testRequiredVariables,
Expand Down
1 change: 0 additions & 1 deletion exoscale-zone/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it } from "bun:test";
import {
executeScriptInContainer,
runTerraformApply,
runTerraformInit,
testRequiredVariables,
Expand Down
20 changes: 12 additions & 8 deletions github-upload-public-key/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type Server, serve } from "bun";
import { describe, expect, it } from "bun:test";
import {
createJSONResponse,
Expand All @@ -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);
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -95,7 +99,7 @@ const setupServer = async (): Promise<Server> => {
}

// case: key already exists
if (req.headers.get("Authorization") == "Bearer findkey") {
if (req.headers.get("Authorization") === "Bearer findkey") {
return createJSONResponse([
{
key: "foo",
Expand Down
2 changes: 1 addition & 1 deletion jfrog-oauth/main.test.ts
Parkreiner marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion jfrog-token/main.test.ts
Parkreiner marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions jupyterlab/main.test.ts
Original file line number Diff line number Diff line change
@@ -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[];
Expand Down
2 changes: 1 addition & 1 deletion nodejs/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from "bun:test";
import { describe } from "bun:test";
import { runTerraformInit, testRequiredVariables } from "../test";

describe("nodejs", async () => {
Expand Down
4 changes: 0 additions & 4 deletions personalize/main.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down
8 changes: 4 additions & 4 deletions slackme/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
Expand All @@ -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",
});
Expand All @@ -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",
});
Expand All @@ -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",
});
Expand Down
30 changes: 16 additions & 14 deletions test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
Expand All @@ -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[];
Expand Down Expand Up @@ -116,6 +117,9 @@ export interface CoderScriptAttributes {
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
* name is specified, it will only find the instance with the given name.
Expand All @@ -124,10 +128,7 @@ export const findResourceInstance = <T extends string>(
state: TerraformState,
type: T,
name?: string,
// if type is "coder_script" return CoderScriptAttributes
): T extends "coder_script"
? CoderScriptAttributes
: Record<string, string> => {
): ResourceInstance<T> => {
const resource = state.resources.find(
(resource) =>
resource.type === type && (name ? resource.name === name : true),
Expand All @@ -140,7 +141,8 @@ export const findResourceInstance = <T extends string>(
`Resource ${type} has ${resource.instances.length} instances`,
);
}
return resource.instances[0].attributes as any;

return resource.instances[0].attributes as ResourceInstance<T>;
};

/**
Expand All @@ -157,15 +159,15 @@ export const testRequiredVariables = <TVars extends TerraformVariables>(
});

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);
Expand All @@ -181,7 +183,7 @@ export const testRequiredVariables = <TVars extends TerraformVariables>(
}
throw new Error(`${varName} is not a required variable!`);
});
});
}
};

/**
Expand Down
10 changes: 7 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only have the runtime logic in the windows-rdp module, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, yeah

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But one of the other benefits of having an older compilation target is that, because I have that JavaScript file set up to be part of the TypeScript project, the TypeScript LSP can catch any function calls that might be using features that are too recent

// 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"]
}
}
4 changes: 2 additions & 2 deletions vscode-desktop/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion windows-rdp/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from "bun:test";
import {
TerraformState,
type TerraformState,
runTerraformApply,
runTerraformInit,
testRequiredVariables,
Expand Down