chore: cleanup imports and value declarations for tests

pull/293/head
Michael Smith 10 months ago
parent 93c4fb3a8d
commit a6f1405047

@ -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",

@ -1,10 +1,8 @@
import { serve } from "bun"; /**
* @todo Add more tests
*/
import { describe } from "bun:test"; import { describe } from "bun:test";
import { import { runTerraformInit, testRequiredVariables } from "../test";
createJSONResponse,
runTerraformInit,
testRequiredVariables,
} from "../test";
describe("jfrog-oauth", async () => { describe("jfrog-oauth", async () => {
await runTerraformInit(import.meta.dir); await runTerraformInit(import.meta.dir);
@ -15,5 +13,3 @@ describe("jfrog-oauth", async () => {
package_managers: "{}", package_managers: "{}",
}); });
}); });
//TODO add more tests

@ -34,7 +34,7 @@ describe("jfrog-token", async () => {
testRequiredVariables(import.meta.dir, { testRequiredVariables(import.meta.dir, {
agent_id: "some-agent-id", agent_id: "some-agent-id",
jfrog_url: "http://" + fakeFrogHost.hostname + ":" + fakeFrogHost.port, jfrog_url: `http://${fakeFrogHost.hostname}:${fakeFrogHost.port}`,
artifactory_access_token: "XXXX", artifactory_access_token: "XXXX",
package_managers: "{}", package_managers: "{}",
}); });

@ -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[];
@ -155,15 +156,15 @@ export const testRequiredVariables = <TVars extends Record<string, string>>(
}); });
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: Record<string, string> = {}; const localVars: Record<string, string> = {};
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);
@ -179,7 +180,7 @@ export const testRequiredVariables = <TVars extends Record<string, string>>(
} }
throw new Error(`${varName} is not a required variable!`); throw new Error(`${varName} is not a required variable!`);
}); });
}); }
}; };
/** /**

@ -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