|
1 | 1 | import * as os from "os"
|
2 |
| -import { it, expect } from "vitest" |
3 |
| -import { getHeaders } from "./headers" |
| 2 | +import { it, expect, describe, beforeEach, afterEach, vi } from "vitest" |
| 3 | +import { WorkspaceConfiguration } from "vscode" |
| 4 | +import { getHeaderCommand, getHeaders } from "./headers" |
4 | 5 |
|
5 | 6 | const logger = {
|
6 | 7 | writeToCoderOutputChannel() {
|
7 | 8 | // no-op
|
8 | 9 | },
|
9 | 10 | }
|
10 | 11 |
|
11 |
| -it("should return no headers", async () => { |
12 |
| - await expect(getHeaders(undefined, undefined, logger)).resolves.toStrictEqual({}) |
13 |
| - await expect(getHeaders("localhost", undefined, logger)).resolves.toStrictEqual({}) |
14 |
| - await expect(getHeaders(undefined, "command", logger)).resolves.toStrictEqual({}) |
15 |
| - await expect(getHeaders("localhost", "", logger)).resolves.toStrictEqual({}) |
16 |
| - await expect(getHeaders("", "command", logger)).resolves.toStrictEqual({}) |
17 |
| - await expect(getHeaders("localhost", " ", logger)).resolves.toStrictEqual({}) |
18 |
| - await expect(getHeaders(" ", "command", logger)).resolves.toStrictEqual({}) |
19 |
| -}) |
| 12 | +describe("getHeaders", () => { |
| 13 | + it("should return no headers", async () => { |
| 14 | + await expect(getHeaders(undefined, undefined, logger)).resolves.toStrictEqual({}) |
| 15 | + await expect(getHeaders("localhost", undefined, logger)).resolves.toStrictEqual({}) |
| 16 | + await expect(getHeaders(undefined, "command", logger)).resolves.toStrictEqual({}) |
| 17 | + await expect(getHeaders("localhost", "", logger)).resolves.toStrictEqual({}) |
| 18 | + await expect(getHeaders("", "command", logger)).resolves.toStrictEqual({}) |
| 19 | + await expect(getHeaders("localhost", " ", logger)).resolves.toStrictEqual({}) |
| 20 | + await expect(getHeaders(" ", "command", logger)).resolves.toStrictEqual({}) |
| 21 | + }) |
20 | 22 |
|
21 |
| -it("should return headers", async () => { |
22 |
| - await expect(getHeaders("localhost", "printf 'foo=bar\\nbaz=qux'", logger)).resolves.toStrictEqual({ |
23 |
| - foo: "bar", |
24 |
| - baz: "qux", |
| 23 | + it("should return headers", async () => { |
| 24 | + await expect(getHeaders("localhost", "printf 'foo=bar\\nbaz=qux'", logger)).resolves.toStrictEqual({ |
| 25 | + foo: "bar", |
| 26 | + baz: "qux", |
| 27 | + }) |
| 28 | + await expect(getHeaders("localhost", "printf 'foo=bar\\r\\nbaz=qux'", logger)).resolves.toStrictEqual({ |
| 29 | + foo: "bar", |
| 30 | + baz: "qux", |
| 31 | + }) |
| 32 | + await expect(getHeaders("localhost", "printf 'foo=bar\\r\\n'", logger)).resolves.toStrictEqual({ foo: "bar" }) |
| 33 | + await expect(getHeaders("localhost", "printf 'foo=bar'", logger)).resolves.toStrictEqual({ foo: "bar" }) |
| 34 | + await expect(getHeaders("localhost", "printf 'foo=bar='", logger)).resolves.toStrictEqual({ foo: "bar=" }) |
| 35 | + await expect(getHeaders("localhost", "printf 'foo=bar=baz'", logger)).resolves.toStrictEqual({ foo: "bar=baz" }) |
| 36 | + await expect(getHeaders("localhost", "printf 'foo='", logger)).resolves.toStrictEqual({ foo: "" }) |
25 | 37 | })
|
26 |
| - await expect(getHeaders("localhost", "printf 'foo=bar\\r\\nbaz=qux'", logger)).resolves.toStrictEqual({ |
27 |
| - foo: "bar", |
28 |
| - baz: "qux", |
| 38 | + |
| 39 | + it("should error on malformed or empty lines", async () => { |
| 40 | + await expect(getHeaders("localhost", "printf 'foo=bar\\r\\n\\r\\n'", logger)).rejects.toMatch(/Malformed/) |
| 41 | + await expect(getHeaders("localhost", "printf '\\r\\nfoo=bar'", logger)).rejects.toMatch(/Malformed/) |
| 42 | + await expect(getHeaders("localhost", "printf '=foo'", logger)).rejects.toMatch(/Malformed/) |
| 43 | + await expect(getHeaders("localhost", "printf 'foo'", logger)).rejects.toMatch(/Malformed/) |
| 44 | + await expect(getHeaders("localhost", "printf ' =foo'", logger)).rejects.toMatch(/Malformed/) |
| 45 | + await expect(getHeaders("localhost", "printf 'foo =bar'", logger)).rejects.toMatch(/Malformed/) |
| 46 | + await expect(getHeaders("localhost", "printf 'foo foo=bar'", logger)).rejects.toMatch(/Malformed/) |
| 47 | + await expect(getHeaders("localhost", "printf ''", logger)).rejects.toMatch(/Malformed/) |
29 | 48 | })
|
30 |
| - await expect(getHeaders("localhost", "printf 'foo=bar\\r\\n'", logger)).resolves.toStrictEqual({ foo: "bar" }) |
31 |
| - await expect(getHeaders("localhost", "printf 'foo=bar'", logger)).resolves.toStrictEqual({ foo: "bar" }) |
32 |
| - await expect(getHeaders("localhost", "printf 'foo=bar='", logger)).resolves.toStrictEqual({ foo: "bar=" }) |
33 |
| - await expect(getHeaders("localhost", "printf 'foo=bar=baz'", logger)).resolves.toStrictEqual({ foo: "bar=baz" }) |
34 |
| - await expect(getHeaders("localhost", "printf 'foo='", logger)).resolves.toStrictEqual({ foo: "" }) |
35 |
| -}) |
36 | 49 |
|
37 |
| -it("should error on malformed or empty lines", async () => { |
38 |
| - await expect(getHeaders("localhost", "printf 'foo=bar\\r\\n\\r\\n'", logger)).rejects.toMatch(/Malformed/) |
39 |
| - await expect(getHeaders("localhost", "printf '\\r\\nfoo=bar'", logger)).rejects.toMatch(/Malformed/) |
40 |
| - await expect(getHeaders("localhost", "printf '=foo'", logger)).rejects.toMatch(/Malformed/) |
41 |
| - await expect(getHeaders("localhost", "printf 'foo'", logger)).rejects.toMatch(/Malformed/) |
42 |
| - await expect(getHeaders("localhost", "printf ' =foo'", logger)).rejects.toMatch(/Malformed/) |
43 |
| - await expect(getHeaders("localhost", "printf 'foo =bar'", logger)).rejects.toMatch(/Malformed/) |
44 |
| - await expect(getHeaders("localhost", "printf 'foo foo=bar'", logger)).rejects.toMatch(/Malformed/) |
45 |
| - await expect(getHeaders("localhost", "printf ''", logger)).rejects.toMatch(/Malformed/) |
46 |
| -}) |
| 50 | + it("should have access to environment variables", async () => { |
| 51 | + const coderUrl = "dev.coder.com" |
| 52 | + await expect( |
| 53 | + getHeaders(coderUrl, os.platform() === "win32" ? "printf url=%CODER_URL%" : "printf url=$CODER_URL", logger), |
| 54 | + ).resolves.toStrictEqual({ url: coderUrl }) |
| 55 | + }) |
47 | 56 |
|
48 |
| -it("should have access to environment variables", async () => { |
49 |
| - const coderUrl = "dev.coder.com" |
50 |
| - await expect( |
51 |
| - getHeaders(coderUrl, os.platform() === "win32" ? "printf url=%CODER_URL%" : "printf url=$CODER_URL", logger), |
52 |
| - ).resolves.toStrictEqual({ url: coderUrl }) |
| 57 | + it("should error on non-zero exit", async () => { |
| 58 | + await expect(getHeaders("localhost", "exit 10", logger)).rejects.toMatch(/exited unexpectedly with code 10/) |
| 59 | + }) |
53 | 60 | })
|
54 | 61 |
|
55 |
| -it("should error on non-zero exit", async () => { |
56 |
| - await expect(getHeaders("localhost", "exit 10", logger)).rejects.toMatch(/exited unexpectedly with code 10/) |
| 62 | +describe("getHeaderCommand", () => { |
| 63 | + beforeEach(() => { |
| 64 | + vi.stubEnv("CODER_HEADER_COMMAND", "") |
| 65 | + }) |
| 66 | + |
| 67 | + afterEach(() => { |
| 68 | + vi.unstubAllEnvs() |
| 69 | + }) |
| 70 | + |
| 71 | + it("should return undefined if coder.headerCommand is not set in config", () => { |
| 72 | + const config = { |
| 73 | + get: () => undefined, |
| 74 | + } as unknown as WorkspaceConfiguration |
| 75 | + |
| 76 | + expect(getHeaderCommand(config)).toBeUndefined() |
| 77 | + }) |
| 78 | + |
| 79 | + it("should return coder.headerCommand if set in config", () => { |
| 80 | + vi.stubEnv("CODER_HEADER_COMMAND", "printf 'x=y'") |
| 81 | + |
| 82 | + const config = { |
| 83 | + get: () => "printf 'foo=bar'", |
| 84 | + } as unknown as WorkspaceConfiguration |
| 85 | + |
| 86 | + expect(getHeaderCommand(config)).toBe("printf 'foo=bar'") |
| 87 | + }) |
| 88 | + |
| 89 | + it("should return CODER_HEADER_COMMAND if coder.headerCommand is not set in config and CODER_HEADER_COMMAND is set in environment", () => { |
| 90 | + vi.stubEnv("CODER_HEADER_COMMAND", "printf 'x=y'") |
| 91 | + |
| 92 | + const config = { |
| 93 | + get: () => undefined, |
| 94 | + } as unknown as WorkspaceConfiguration |
| 95 | + |
| 96 | + expect(getHeaderCommand(config)).toBe("printf 'x=y'") |
| 97 | + |
| 98 | + delete process.env.CODER_HEADER_COMMAND |
| 99 | + }) |
57 | 100 | })
|
0 commit comments