Compare commits
1 Commits
main
...
zed-settin
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eafdabc48c |
@ -1,5 +1,9 @@
|
|||||||
import { describe, expect, it } from "bun:test";
|
import { describe, expect, it } from "bun:test";
|
||||||
import {
|
import {
|
||||||
|
execContainer,
|
||||||
|
findResourceInstance,
|
||||||
|
removeContainer,
|
||||||
|
runContainer,
|
||||||
runTerraformApply,
|
runTerraformApply,
|
||||||
runTerraformInit,
|
runTerraformInit,
|
||||||
testRequiredVariables,
|
testRequiredVariables,
|
||||||
@ -12,66 +16,67 @@ describe("zed", async () => {
|
|||||||
agent_id: "foo",
|
agent_id: "foo",
|
||||||
});
|
});
|
||||||
|
|
||||||
it("default output", async () => {
|
it("creates settings file with correct JSON", async () => {
|
||||||
|
const settings = {
|
||||||
|
theme: "One Dark",
|
||||||
|
buffer_font_size: 14,
|
||||||
|
vim_mode: true,
|
||||||
|
telemetry: {
|
||||||
|
diagnostics: false,
|
||||||
|
metrics: false,
|
||||||
|
},
|
||||||
|
// Test special characters: single quotes, backslashes, URLs
|
||||||
|
message: "it's working",
|
||||||
|
path: "C:\\Users\\test",
|
||||||
|
api_url: "https://api.example.com/v1?token=abc&user=test",
|
||||||
|
};
|
||||||
|
|
||||||
const state = await runTerraformApply(import.meta.dir, {
|
const state = await runTerraformApply(import.meta.dir, {
|
||||||
agent_id: "foo",
|
agent_id: "foo",
|
||||||
|
settings: JSON.stringify(settings),
|
||||||
});
|
});
|
||||||
expect(state.outputs.zed_url.value).toBe("zed://ssh/default.coder");
|
|
||||||
|
|
||||||
const coder_app = state.resources.find(
|
const instance = findResourceInstance(state, "coder_script");
|
||||||
(res) => res.type === "coder_app" && res.name === "zed",
|
const id = await runContainer("alpine:latest");
|
||||||
);
|
|
||||||
|
|
||||||
expect(coder_app).not.toBeNull();
|
try {
|
||||||
expect(coder_app?.instances.length).toBe(1);
|
const result = await execContainer(id, ["sh", "-c", instance.script]);
|
||||||
expect(coder_app?.instances[0].attributes.order).toBeNull();
|
expect(result.exitCode).toBe(0);
|
||||||
});
|
|
||||||
|
|
||||||
it("adds folder", async () => {
|
const catResult = await execContainer(id, [
|
||||||
|
"cat",
|
||||||
|
"/root/.config/zed/settings.json",
|
||||||
|
]);
|
||||||
|
expect(catResult.exitCode).toBe(0);
|
||||||
|
|
||||||
|
const written = JSON.parse(catResult.stdout.trim());
|
||||||
|
expect(written).toEqual(settings);
|
||||||
|
} finally {
|
||||||
|
await removeContainer(id);
|
||||||
|
}
|
||||||
|
}, 30000);
|
||||||
|
|
||||||
|
it("exits early with empty settings", async () => {
|
||||||
const state = await runTerraformApply(import.meta.dir, {
|
const state = await runTerraformApply(import.meta.dir, {
|
||||||
agent_id: "foo",
|
agent_id: "foo",
|
||||||
folder: "/foo/bar",
|
settings: "",
|
||||||
});
|
|
||||||
expect(state.outputs.zed_url.value).toBe("zed://ssh/default.coder/foo/bar");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("expect order to be set", async () => {
|
|
||||||
const state = await runTerraformApply(import.meta.dir, {
|
|
||||||
agent_id: "foo",
|
|
||||||
order: "22",
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const coder_app = state.resources.find(
|
const instance = findResourceInstance(state, "coder_script");
|
||||||
(res) => res.type === "coder_app" && res.name === "zed",
|
const id = await runContainer("alpine:latest");
|
||||||
);
|
|
||||||
|
|
||||||
expect(coder_app).not.toBeNull();
|
try {
|
||||||
expect(coder_app?.instances.length).toBe(1);
|
const result = await execContainer(id, ["sh", "-c", instance.script]);
|
||||||
expect(coder_app?.instances[0].attributes.order).toBe(22);
|
expect(result.exitCode).toBe(0);
|
||||||
});
|
|
||||||
|
|
||||||
it("expect display_name to be set", async () => {
|
// Settings file should not be created
|
||||||
const state = await runTerraformApply(import.meta.dir, {
|
const catResult = await execContainer(id, [
|
||||||
agent_id: "foo",
|
"cat",
|
||||||
display_name: "Custom Zed",
|
"/root/.config/zed/settings.json",
|
||||||
});
|
]);
|
||||||
|
expect(catResult.exitCode).not.toBe(0);
|
||||||
const coder_app = state.resources.find(
|
} finally {
|
||||||
(res) => res.type === "coder_app" && res.name === "zed",
|
await removeContainer(id);
|
||||||
);
|
}
|
||||||
|
}, 30000);
|
||||||
expect(coder_app).not.toBeNull();
|
|
||||||
expect(coder_app?.instances.length).toBe(1);
|
|
||||||
expect(coder_app?.instances[0].attributes.display_name).toBe("Custom Zed");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("adds agent_name to hostname", async () => {
|
|
||||||
const state = await runTerraformApply(import.meta.dir, {
|
|
||||||
agent_id: "foo",
|
|
||||||
agent_name: "myagent",
|
|
||||||
});
|
|
||||||
expect(state.outputs.zed_url.value).toBe(
|
|
||||||
"zed://ssh/myagent.default.default.coder",
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -65,6 +65,7 @@ locals {
|
|||||||
owner_name = lower(data.coder_workspace_owner.me.name)
|
owner_name = lower(data.coder_workspace_owner.me.name)
|
||||||
agent_name = lower(var.agent_name)
|
agent_name = lower(var.agent_name)
|
||||||
hostname = var.agent_name != "" ? "${local.agent_name}.${local.workspace_name}.${local.owner_name}.coder" : "${local.workspace_name}.coder"
|
hostname = var.agent_name != "" ? "${local.agent_name}.${local.workspace_name}.${local.owner_name}.coder" : "${local.workspace_name}.coder"
|
||||||
|
settings_b64 = var.settings != "" ? base64encode(var.settings) : ""
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "coder_script" "zed_settings" {
|
resource "coder_script" "zed_settings" {
|
||||||
@ -75,20 +76,14 @@ resource "coder_script" "zed_settings" {
|
|||||||
script = <<-EOT
|
script = <<-EOT
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -eu
|
set -eu
|
||||||
SETTINGS_JSON='${replace(var.settings, "\"", "\\\"")}'
|
SETTINGS_B64='${local.settings_b64}'
|
||||||
if [ -z "$${SETTINGS_JSON}" ] || [ "$${SETTINGS_JSON}" = "{}" ]; then
|
if [ -z "$${SETTINGS_B64}" ]; then
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
CONFIG_HOME="$${XDG_CONFIG_HOME:-$HOME/.config}"
|
CONFIG_HOME="$${XDG_CONFIG_HOME:-$HOME/.config}"
|
||||||
ZED_DIR="$${CONFIG_HOME}/zed"
|
ZED_DIR="$${CONFIG_HOME}/zed"
|
||||||
mkdir -p "$${ZED_DIR}"
|
mkdir -p "$${ZED_DIR}"
|
||||||
SETTINGS_FILE="$${ZED_DIR}/settings.json"
|
echo -n "$${SETTINGS_B64}" | base64 -d > "$${ZED_DIR}/settings.json"
|
||||||
if command -v jq >/dev/null 2>&1 && [ -s "$${SETTINGS_FILE}" ]; then
|
|
||||||
tmpfile="$(mktemp)"
|
|
||||||
jq -s '.[0] * .[1]' "$${SETTINGS_FILE}" <(printf '%s\n' "$${SETTINGS_JSON}") > "$${tmpfile}" && mv "$${tmpfile}" "$${SETTINGS_FILE}"
|
|
||||||
else
|
|
||||||
printf '%s\n' "$${SETTINGS_JSON}" > "$${SETTINGS_FILE}"
|
|
||||||
fi
|
|
||||||
EOT
|
EOT
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,20 @@ run "default_output" {
|
|||||||
agent_id = "foo"
|
agent_id = "foo"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override_data {
|
||||||
|
target = data.coder_workspace.me
|
||||||
|
values = {
|
||||||
|
name = "default"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override_data {
|
||||||
|
target = data.coder_workspace_owner.me
|
||||||
|
values = {
|
||||||
|
name = "default"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
assert {
|
assert {
|
||||||
condition = output.zed_url == "zed://ssh/default.coder"
|
condition = output.zed_url == "zed://ssh/default.coder"
|
||||||
error_message = "zed_url did not match expected default URL"
|
error_message = "zed_url did not match expected default URL"
|
||||||
@ -19,6 +33,20 @@ run "adds_folder" {
|
|||||||
folder = "/foo/bar"
|
folder = "/foo/bar"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override_data {
|
||||||
|
target = data.coder_workspace.me
|
||||||
|
values = {
|
||||||
|
name = "default"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override_data {
|
||||||
|
target = data.coder_workspace_owner.me
|
||||||
|
values = {
|
||||||
|
name = "default"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
assert {
|
assert {
|
||||||
condition = output.zed_url == "zed://ssh/default.coder/foo/bar"
|
condition = output.zed_url == "zed://ssh/default.coder/foo/bar"
|
||||||
error_message = "zed_url did not include provided folder path"
|
error_message = "zed_url did not include provided folder path"
|
||||||
@ -33,8 +61,54 @@ run "adds_agent_name" {
|
|||||||
agent_name = "myagent"
|
agent_name = "myagent"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override_data {
|
||||||
|
target = data.coder_workspace.me
|
||||||
|
values = {
|
||||||
|
name = "default"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override_data {
|
||||||
|
target = data.coder_workspace_owner.me
|
||||||
|
values = {
|
||||||
|
name = "default"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
assert {
|
assert {
|
||||||
condition = output.zed_url == "zed://ssh/myagent.default.default.coder"
|
condition = output.zed_url == "zed://ssh/myagent.default.default.coder"
|
||||||
error_message = "zed_url did not include agent_name in hostname"
|
error_message = "zed_url did not include agent_name in hostname"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
run "settings_base64_encoding" {
|
||||||
|
command = apply
|
||||||
|
|
||||||
|
variables {
|
||||||
|
agent_id = "foo"
|
||||||
|
settings = jsonencode({
|
||||||
|
theme = "dark"
|
||||||
|
fontSize = 14
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
# Verify settings are base64 encoded (eyJ = base64 prefix for JSON starting with {")
|
||||||
|
assert {
|
||||||
|
condition = can(regex("SETTINGS_B64='eyJ", coder_script.zed_settings.script))
|
||||||
|
error_message = "settings should be base64 encoded in the script"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
run "empty_settings" {
|
||||||
|
command = apply
|
||||||
|
|
||||||
|
variables {
|
||||||
|
agent_id = "foo"
|
||||||
|
settings = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
assert {
|
||||||
|
condition = can(regex("SETTINGS_B64=''", coder_script.zed_settings.script))
|
||||||
|
error_message = "empty settings should result in empty SETTINGS_B64"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user