Skip to content

Commit

Permalink
chore: cleanup imports and value declarations for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Parkreiner committed Sep 20, 2024
1 parent 93c4fb3 commit a6f1405
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 54 deletions.
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
12 changes: 4 additions & 8 deletions jfrog-oauth/main.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -15,5 +13,3 @@ describe("jfrog-oauth", async () => {
package_managers: "{}",
});
});

//TODO add more tests
2 changes: 1 addition & 1 deletion jfrog-token/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "{}",
});
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
19 changes: 10 additions & 9 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 @@ -155,15 +156,15 @@ export const testRequiredVariables = <TVars extends Record<string, string>>(
});

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<string, string> = {};
varNames.forEach((otherVarName) => {
for (const otherVarName of varNames) {
if (otherVarName !== varName) {
localVars[otherVarName] = vars[otherVarName];
}
});
}

try {
await runTerraformApply(dir, localVars);
Expand All @@ -179,7 +180,7 @@ export const testRequiredVariables = <TVars extends Record<string, string>>(
}
throw new Error(`${varName} is not a required variable!`);
});
});
}
};

/**
Expand Down
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

0 comments on commit a6f1405

Please sign in to comment.