|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import { |
| 3 | + execContainer, |
| 4 | + executeScriptInContainer, |
| 5 | + findResourceInstance, |
| 6 | + runContainer, |
| 7 | + runTerraformApply, |
| 8 | + runTerraformInit, |
| 9 | + testRequiredVariables, |
| 10 | + type TerraformState, |
| 11 | +} from "../test"; |
| 12 | + |
| 13 | +const executeScriptInContainerWithPackageManager = async ( |
| 14 | + state: TerraformState, |
| 15 | + image: string, |
| 16 | + packageManager: string, |
| 17 | + shell = "sh", |
| 18 | +): Promise<{ |
| 19 | + exitCode: number; |
| 20 | + stdout: string[]; |
| 21 | + stderr: string[]; |
| 22 | +}> => { |
| 23 | + const instance = findResourceInstance(state, "coder_script"); |
| 24 | + const id = await runContainer(image); |
| 25 | + |
| 26 | + // Install the specified package manager |
| 27 | + if (packageManager === "npm") { |
| 28 | + await execContainer(id, [shell, "-c", "apk add nodejs npm"]); |
| 29 | + } else if (packageManager === "pnpm") { |
| 30 | + await execContainer(id, [ |
| 31 | + shell, |
| 32 | + "-c", |
| 33 | + "apk add nodejs npm && npm install -g pnpm", |
| 34 | + ]); |
| 35 | + } else if (packageManager === "yarn") { |
| 36 | + await execContainer(id, [ |
| 37 | + shell, |
| 38 | + "-c", |
| 39 | + "apk add nodejs npm && npm install -g yarn", |
| 40 | + ]); |
| 41 | + } |
| 42 | + |
| 43 | + const resp = await execContainer(id, [shell, "-c", instance.script]); |
| 44 | + const stdout = resp.stdout.trim().split("\n"); |
| 45 | + const stderr = resp.stderr.trim().split("\n"); |
| 46 | + return { |
| 47 | + exitCode: resp.exitCode, |
| 48 | + stdout, |
| 49 | + stderr, |
| 50 | + }; |
| 51 | +}; |
| 52 | + |
| 53 | +describe("devcontainers-cli", async () => { |
| 54 | + await runTerraformInit(import.meta.dir); |
| 55 | + |
| 56 | + testRequiredVariables(import.meta.dir, { |
| 57 | + agent_id: "some-agent-id", |
| 58 | + }); |
| 59 | + |
| 60 | + it("misses all package managers", async () => { |
| 61 | + const state = await runTerraformApply(import.meta.dir, { |
| 62 | + agent_id: "some-agent-id", |
| 63 | + }); |
| 64 | + const output = await executeScriptInContainer(state, "docker:dind"); |
| 65 | + expect(output.exitCode).toBe(1); |
| 66 | + expect(output.stderr).toEqual([ |
| 67 | + "ERROR: No supported package manager (npm, pnpm, yarn) is installed. Please install one first.", |
| 68 | + ]); |
| 69 | + }, 15000); |
| 70 | + |
| 71 | + it("installs devcontainers-cli with npm", async () => { |
| 72 | + const state = await runTerraformApply(import.meta.dir, { |
| 73 | + agent_id: "some-agent-id", |
| 74 | + }); |
| 75 | + |
| 76 | + const output = await executeScriptInContainerWithPackageManager( |
| 77 | + state, |
| 78 | + "docker:dind", |
| 79 | + "npm", |
| 80 | + ); |
| 81 | + expect(output.exitCode).toBe(0); |
| 82 | + |
| 83 | + expect(output.stdout[0]).toEqual( |
| 84 | + "Installing @devcontainers/cli using npm...", |
| 85 | + ); |
| 86 | + expect(output.stdout[output.stdout.length - 1]).toEqual( |
| 87 | + "🥳 @devcontainers/cli has been installed into /usr/local/bin/devcontainer!", |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it("installs devcontainers-cli with yarn", async () => { |
| 92 | + const state = await runTerraformApply(import.meta.dir, { |
| 93 | + agent_id: "some-agent-id", |
| 94 | + }); |
| 95 | + |
| 96 | + const output = await executeScriptInContainerWithPackageManager( |
| 97 | + state, |
| 98 | + "docker:dind", |
| 99 | + "yarn", |
| 100 | + ); |
| 101 | + expect(output.exitCode).toBe(0); |
| 102 | + |
| 103 | + expect(output.stdout[0]).toEqual( |
| 104 | + "Installing @devcontainers/cli using yarn...", |
| 105 | + ); |
| 106 | + expect(output.stdout[output.stdout.length - 1]).toEqual( |
| 107 | + "🥳 @devcontainers/cli has been installed into /usr/local/bin/devcontainer!", |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + it("displays warning if docker is not installed", async () => { |
| 112 | + const state = await runTerraformApply(import.meta.dir, { |
| 113 | + agent_id: "some-agent-id", |
| 114 | + }); |
| 115 | + |
| 116 | + const output = await executeScriptInContainerWithPackageManager( |
| 117 | + state, |
| 118 | + "alpine", |
| 119 | + "npm", |
| 120 | + ); |
| 121 | + expect(output.exitCode).toBe(0); |
| 122 | + |
| 123 | + expect(output.stdout[0]).toEqual( |
| 124 | + "WARNING: Docker was not found but is required to use @devcontainers/cli, please make sure it is available.", |
| 125 | + ); |
| 126 | + expect(output.stdout[output.stdout.length - 1]).toEqual( |
| 127 | + "🥳 @devcontainers/cli has been installed into /usr/local/bin/devcontainer!", |
| 128 | + ); |
| 129 | + }); |
| 130 | +}); |
0 commit comments