envshed 0.23.0 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/skill-content.d.ts +1 -1
- package/dist/agent/skill-content.d.ts.map +1 -1
- package/dist/agent/skill-content.js +13 -0
- package/dist/agent/skill-content.js.map +1 -1
- package/dist/api.d.ts +1 -0
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +10 -1
- package/dist/api.js.map +1 -1
- package/dist/client-header.test.d.ts +2 -0
- package/dist/client-header.test.d.ts.map +1 -0
- package/dist/client-header.test.js +24 -0
- package/dist/client-header.test.js.map +1 -0
- package/dist/commands/import.d.ts +2 -0
- package/dist/commands/import.d.ts.map +1 -1
- package/dist/commands/import.js +224 -110
- package/dist/commands/import.js.map +1 -1
- package/dist/commands/import.test.js +357 -350
- package/dist/commands/import.test.js.map +1 -1
- package/dist/commands/init.d.ts +3 -0
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +11 -0
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/init.test.js +25 -2
- package/dist/commands/init.test.js.map +1 -1
- package/dist/commands/push.d.ts.map +1 -1
- package/dist/commands/push.js +18 -4
- package/dist/commands/push.js.map +1 -1
- package/dist/commands/push.test.js +28 -0
- package/dist/commands/push.test.js.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/file-import.d.ts +37 -0
- package/dist/lib/file-import.d.ts.map +1 -0
- package/dist/lib/file-import.js +54 -0
- package/dist/lib/file-import.js.map +1 -0
- package/dist/lib/file-import.test.d.ts +2 -0
- package/dist/lib/file-import.test.d.ts.map +1 -0
- package/dist/lib/file-import.test.js +46 -0
- package/dist/lib/file-import.test.js.map +1 -0
- package/dist/lib/importers/dotenv.d.ts +6 -0
- package/dist/lib/importers/dotenv.d.ts.map +1 -1
- package/dist/lib/importers/dotenv.js +29 -0
- package/dist/lib/importers/dotenv.js.map +1 -1
- package/dist/lib/importers/dotenv.test.js +10 -0
- package/dist/lib/importers/dotenv.test.js.map +1 -1
- package/dist/lib/importers/index.d.ts.map +1 -1
- package/dist/lib/importers/index.js +2 -1
- package/dist/lib/importers/index.js.map +1 -1
- package/dist/lib/importers/index.test.js +2 -0
- package/dist/lib/importers/index.test.js.map +1 -1
- package/dist/lib/init-import.d.ts +76 -0
- package/dist/lib/init-import.d.ts.map +1 -0
- package/dist/lib/init-import.js +444 -0
- package/dist/lib/init-import.js.map +1 -0
- package/dist/lib/init-import.test.d.ts +2 -0
- package/dist/lib/init-import.test.d.ts.map +1 -0
- package/dist/lib/init-import.test.js +228 -0
- package/dist/lib/init-import.test.js.map +1 -0
- package/dist/lib/secret-push.d.ts +30 -0
- package/dist/lib/secret-push.d.ts.map +1 -0
- package/dist/lib/secret-push.js +67 -0
- package/dist/lib/secret-push.js.map +1 -0
- package/dist/lib/setup-guide.d.ts +0 -1
- package/dist/lib/setup-guide.d.ts.map +1 -1
- package/dist/lib/setup-guide.js +1 -23
- package/dist/lib/setup-guide.js.map +1 -1
- package/dist/lib/setup-guide.test.js +4 -25
- package/dist/lib/setup-guide.test.js.map +1 -1
- package/dist/locales/en.json +94 -10
- package/dist/locales/es.json +94 -10
- package/dist/locales/pt-BR.json +94 -10
- package/package.json +1 -1
|
@@ -1,384 +1,391 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import path from "path";
|
|
4
4
|
import os from "os";
|
|
5
|
+
/**
|
|
6
|
+
* `envshed import <file>` as a coding agent runs it: no terminal, output captured.
|
|
7
|
+
* Every secret value in these tests contains CANARY, and every test checks that no
|
|
8
|
+
* output line (stdout, stderr, JSON) contains it.
|
|
9
|
+
*/
|
|
5
10
|
vi.mock("../api.js", () => ({
|
|
6
11
|
apiGet: vi.fn(),
|
|
7
12
|
apiPut: vi.fn(),
|
|
13
|
+
apiPost: vi.fn(),
|
|
14
|
+
readJson: (res) => res.json(),
|
|
15
|
+
ApiError: class ApiError extends Error {
|
|
16
|
+
status;
|
|
17
|
+
constructor(status) {
|
|
18
|
+
super(String(status));
|
|
19
|
+
this.status = status;
|
|
20
|
+
}
|
|
21
|
+
},
|
|
8
22
|
}));
|
|
9
23
|
vi.mock("../i18n.js", () => ({
|
|
10
24
|
t: (key, opts) => (opts ? `${key}:${JSON.stringify(opts)}` : key),
|
|
11
25
|
}));
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
succeed: vi.fn(),
|
|
18
|
-
fail: vi.fn(),
|
|
19
|
-
warn: vi.fn(),
|
|
20
|
-
};
|
|
21
|
-
const spinner = {
|
|
22
|
-
start: (...args) => { calls.start(...args); return spinner; },
|
|
23
|
-
stop: (...args) => { calls.stop(...args); return spinner; },
|
|
24
|
-
succeed: (...args) => { calls.succeed(...args); return spinner; },
|
|
25
|
-
fail: (...args) => { calls.fail(...args); return spinner; },
|
|
26
|
-
warn: (...args) => { calls.warn(...args); return spinner; },
|
|
27
|
-
text: "",
|
|
28
|
-
calls,
|
|
29
|
-
};
|
|
30
|
-
return spinner;
|
|
26
|
+
const spinner = vi.hoisted(() => {
|
|
27
|
+
const s = { text: "" };
|
|
28
|
+
for (const name of ["start", "stop", "succeed", "fail", "warn", "info"])
|
|
29
|
+
s[name] = vi.fn(() => s);
|
|
30
|
+
return s;
|
|
31
31
|
});
|
|
32
|
-
vi.mock("ora", () => ({ default: () =>
|
|
33
|
-
vi.mock("inquirer", () => ({
|
|
34
|
-
default: {
|
|
35
|
-
prompt: vi.fn().mockResolvedValue({ shouldContinue: true }),
|
|
36
|
-
},
|
|
37
|
-
}));
|
|
32
|
+
vi.mock("ora", () => ({ default: () => spinner }));
|
|
33
|
+
vi.mock("inquirer", () => ({ default: { prompt: vi.fn() } }));
|
|
38
34
|
vi.mock("../config.js", () => ({
|
|
39
35
|
resolveOpts: vi.fn((opts) => ({
|
|
40
|
-
org: opts.org || "
|
|
41
|
-
project: opts.project || "
|
|
36
|
+
org: opts.org || "acme",
|
|
37
|
+
project: opts.project || "api",
|
|
42
38
|
env: opts.env || "development",
|
|
43
39
|
})),
|
|
44
|
-
getOriginalCwd: vi.fn(() => "/tmp
|
|
40
|
+
getOriginalCwd: vi.fn(() => "/tmp"),
|
|
45
41
|
logContext: vi.fn(),
|
|
46
42
|
hasProjectConfig: vi.fn(() => true),
|
|
47
43
|
}));
|
|
48
|
-
vi.mock("../lib/
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
vi.mock("../utils.js", () => ({
|
|
53
|
-
checkForBreakingChanges: vi.fn(() => ({ modified: [], deleted: [] })),
|
|
54
|
-
}));
|
|
55
|
-
import { apiGet, apiPut } from "../api.js";
|
|
56
|
-
import { getOriginalCwd, resolveOpts } from "../config.js";
|
|
57
|
-
import { detectFormat, parseImportFile } from "../lib/importers/index.js";
|
|
58
|
-
import { checkForBreakingChanges } from "../utils.js";
|
|
44
|
+
vi.mock("../lib/init-import.js", () => ({ gitStatus: vi.fn(() => ({ tracked: false, ignored: false, top: "/tmp" })) }));
|
|
45
|
+
import { apiGet, apiPut, apiPost } from "../api.js";
|
|
46
|
+
import { getOriginalCwd } from "../config.js";
|
|
47
|
+
import { gitStatus } from "../lib/init-import.js";
|
|
59
48
|
import inquirer from "inquirer";
|
|
60
|
-
|
|
61
|
-
|
|
49
|
+
const ENV_FILE = [
|
|
50
|
+
"DATABASE_URL=postgres://u:CANARY_db_1@db:5432/app",
|
|
51
|
+
"API_KEY=CANARY_key_new_2",
|
|
52
|
+
"STRIPE_SECRET_KEY=sk_test_CANARY_stripe_3",
|
|
53
|
+
"PORT=3000",
|
|
54
|
+
].join("\n");
|
|
55
|
+
let tmpDir;
|
|
56
|
+
let output;
|
|
57
|
+
let remote;
|
|
58
|
+
let envs;
|
|
59
|
+
const isTTY = { stdin: process.stdin.isTTY, stdout: process.stdout.isTTY };
|
|
60
|
+
function mockApi() {
|
|
61
|
+
vi.mocked(apiGet).mockImplementation(async (url) => {
|
|
62
|
+
if (url.startsWith("/environments/")) {
|
|
63
|
+
return { json: async () => ({ environments: envs.map((slug) => ({ slug, name: slug })) }) };
|
|
64
|
+
}
|
|
65
|
+
return { json: async () => ({ secrets: remote }) };
|
|
66
|
+
});
|
|
67
|
+
vi.mocked(apiPut).mockResolvedValue({ ok: true, version: 2 });
|
|
68
|
+
vi.mocked(apiPost).mockImplementation(async (url, body) => {
|
|
69
|
+
if (url.startsWith("/environments/"))
|
|
70
|
+
envs.push(body.name);
|
|
71
|
+
return {};
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
async function run(args) {
|
|
75
|
+
vi.resetModules();
|
|
76
|
+
const { importCommand } = await import("./import.js");
|
|
77
|
+
const { Command } = await import("commander");
|
|
78
|
+
const program = new Command();
|
|
79
|
+
program.exitOverride();
|
|
80
|
+
program.addCommand(importCommand);
|
|
81
|
+
let exit;
|
|
82
|
+
try {
|
|
83
|
+
await program.parseAsync(["node", "envshed", "import", ...args], { from: "node" });
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
const match = /^process\.exit:(\d+)$/.exec(err.message);
|
|
87
|
+
if (!match)
|
|
88
|
+
throw err;
|
|
89
|
+
exit = Number(match[1]);
|
|
90
|
+
}
|
|
91
|
+
return { exit, text: output.join("\n") };
|
|
92
|
+
}
|
|
93
|
+
function writeFile(name, content) {
|
|
94
|
+
fs.writeFileSync(path.join(tmpDir, name), content);
|
|
95
|
+
return name;
|
|
96
|
+
}
|
|
97
|
+
function expectNoValues(text) {
|
|
98
|
+
expect(text).not.toMatch(/CANARY/);
|
|
99
|
+
for (const call of vi.mocked(spinner.fail).mock.calls)
|
|
100
|
+
expect(String(call[0])).not.toMatch(/CANARY/);
|
|
101
|
+
}
|
|
102
|
+
function written() {
|
|
103
|
+
return vi.mocked(apiPut).mock.calls[0]?.[1]?.secrets ?? {};
|
|
104
|
+
}
|
|
105
|
+
beforeEach(() => {
|
|
106
|
+
vi.clearAllMocks();
|
|
107
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "import-test-"));
|
|
108
|
+
vi.mocked(getOriginalCwd).mockReturnValue(tmpDir);
|
|
109
|
+
output = [];
|
|
110
|
+
const capture = (...args) => { output.push(args.map(String).join(" ")); };
|
|
111
|
+
vi.spyOn(console, "log").mockImplementation(capture);
|
|
112
|
+
vi.spyOn(console, "error").mockImplementation(capture);
|
|
113
|
+
vi.spyOn(process, "exit").mockImplementation(((code) => {
|
|
114
|
+
throw new Error(`process.exit:${code ?? 0}`);
|
|
115
|
+
}));
|
|
116
|
+
// An agent's shell: no terminal on stdin or stdout.
|
|
117
|
+
Object.defineProperty(process.stdin, "isTTY", { value: false, configurable: true });
|
|
118
|
+
Object.defineProperty(process.stdout, "isTTY", { value: false, configurable: true });
|
|
119
|
+
remote = {};
|
|
120
|
+
envs = ["development", "production"];
|
|
121
|
+
mockApi();
|
|
122
|
+
});
|
|
123
|
+
afterEach(() => {
|
|
124
|
+
Object.defineProperty(process.stdin, "isTTY", { value: isTTY.stdin, configurable: true });
|
|
125
|
+
Object.defineProperty(process.stdout, "isTTY", { value: isTTY.stdout, configurable: true });
|
|
126
|
+
vi.restoreAllMocks();
|
|
127
|
+
});
|
|
128
|
+
describe("envshed import <file>: new environment content", () => {
|
|
129
|
+
it("imports every key into an empty environment and prints only names", async () => {
|
|
130
|
+
writeFile(".env", ENV_FILE);
|
|
131
|
+
const { exit, text } = await run([".env"]);
|
|
132
|
+
expect(exit).toBeUndefined();
|
|
133
|
+
expect(Object.keys(written()).sort()).toEqual(["API_KEY", "DATABASE_URL", "PORT", "STRIPE_SECRET_KEY"]);
|
|
134
|
+
expect(text).toContain("import.importedPlan");
|
|
135
|
+
expect(text).toContain("DATABASE_URL");
|
|
136
|
+
expect(text).toContain("import.nextSteps");
|
|
137
|
+
expectNoValues(text);
|
|
138
|
+
});
|
|
139
|
+
it("detects JSON, YAML and CSV files", async () => {
|
|
140
|
+
writeFile("s.json", '{"JSON_KEY":"CANARY_json_4"}');
|
|
141
|
+
writeFile("s.yaml", "YAML_KEY: CANARY_yaml_5\n");
|
|
142
|
+
writeFile("s.csv", "key,value\nCSV_KEY,CANARY_csv_6\n");
|
|
143
|
+
for (const [file, key] of [["s.json", "JSON_KEY"], ["s.yaml", "YAML_KEY"], ["s.csv", "CSV_KEY"]]) {
|
|
144
|
+
vi.mocked(apiPut).mockClear();
|
|
145
|
+
const { exit, text } = await run([file, "--on-conflict", "fail"]);
|
|
146
|
+
expect(exit).toBeUndefined();
|
|
147
|
+
expect(Object.keys(written())).toEqual([key]);
|
|
148
|
+
expectNoValues(text);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
it("does not send keys whose value is already the same", async () => {
|
|
152
|
+
writeFile(".env", ENV_FILE);
|
|
153
|
+
remote = { PORT: "3000" };
|
|
154
|
+
await run([".env"]);
|
|
155
|
+
expect(written()).not.toHaveProperty("PORT");
|
|
156
|
+
});
|
|
157
|
+
it("writes nothing when every key is unchanged", async () => {
|
|
158
|
+
writeFile(".env", "PORT=3000\n");
|
|
159
|
+
remote = { PORT: "3000" };
|
|
160
|
+
const { exit } = await run([".env"]);
|
|
161
|
+
expect(exit).toBeUndefined();
|
|
162
|
+
expect(apiPut).not.toHaveBeenCalled();
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
describe("envshed import <file>: keys that already exist with another value", () => {
|
|
62
166
|
beforeEach(() => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
it("auto-detects .env format", async () => {
|
|
91
|
-
const file = path.join(tmpDir, ".env");
|
|
92
|
-
fs.writeFileSync(file, "KEY=value");
|
|
93
|
-
vi.mocked(detectFormat).mockReturnValue("env");
|
|
94
|
-
const { importCommand } = await import("./import.js");
|
|
95
|
-
const { Command } = await import("commander");
|
|
96
|
-
const program = new Command();
|
|
97
|
-
program.addCommand(importCommand);
|
|
98
|
-
await program.parseAsync(["node", "test", "import", "--force", file], { from: "node" });
|
|
99
|
-
expect(detectFormat).toHaveBeenCalledWith(file);
|
|
100
|
-
expect(parseImportFile).toHaveBeenCalledWith("env", expect.any(String));
|
|
101
|
-
});
|
|
102
|
-
it("auto-detects .json format", async () => {
|
|
103
|
-
const file = path.join(tmpDir, "secrets.json");
|
|
104
|
-
fs.writeFileSync(file, '{"KEY":"value"}');
|
|
105
|
-
vi.mocked(detectFormat).mockReturnValue("json");
|
|
106
|
-
const { importCommand } = await import("./import.js");
|
|
107
|
-
const { Command } = await import("commander");
|
|
108
|
-
const program = new Command();
|
|
109
|
-
program.addCommand(importCommand);
|
|
110
|
-
await program.parseAsync(["node", "test", "import", "--force", file], { from: "node" });
|
|
111
|
-
expect(detectFormat).toHaveBeenCalledWith(file);
|
|
112
|
-
expect(parseImportFile).toHaveBeenCalledWith("json", expect.any(String));
|
|
113
|
-
});
|
|
114
|
-
it("auto-detects .yaml format", async () => {
|
|
115
|
-
const file = path.join(tmpDir, "secrets.yaml");
|
|
116
|
-
fs.writeFileSync(file, "KEY: value");
|
|
117
|
-
vi.mocked(detectFormat).mockReturnValue("yaml");
|
|
118
|
-
const { importCommand } = await import("./import.js");
|
|
119
|
-
const { Command } = await import("commander");
|
|
120
|
-
const program = new Command();
|
|
121
|
-
program.addCommand(importCommand);
|
|
122
|
-
await program.parseAsync(["node", "test", "import", "--force", file], { from: "node" });
|
|
123
|
-
expect(detectFormat).toHaveBeenCalledWith(file);
|
|
124
|
-
expect(parseImportFile).toHaveBeenCalledWith("yaml", expect.any(String));
|
|
125
|
-
});
|
|
126
|
-
it("auto-detects .csv format", async () => {
|
|
127
|
-
const file = path.join(tmpDir, "secrets.csv");
|
|
128
|
-
fs.writeFileSync(file, "key,value\nKEY,val");
|
|
129
|
-
vi.mocked(detectFormat).mockReturnValue("csv");
|
|
130
|
-
const { importCommand } = await import("./import.js");
|
|
131
|
-
const { Command } = await import("commander");
|
|
132
|
-
const program = new Command();
|
|
133
|
-
program.addCommand(importCommand);
|
|
134
|
-
await program.parseAsync(["node", "test", "import", "--force", file], { from: "node" });
|
|
135
|
-
expect(detectFormat).toHaveBeenCalledWith(file);
|
|
136
|
-
expect(parseImportFile).toHaveBeenCalledWith("csv", expect.any(String));
|
|
137
|
-
});
|
|
138
|
-
// -------------------------------------------------------------------------
|
|
139
|
-
// --format flag overrides auto-detection
|
|
140
|
-
// -------------------------------------------------------------------------
|
|
141
|
-
it("uses --format flag and skips detectFormat", async () => {
|
|
142
|
-
const file = path.join(tmpDir, "secrets.txt");
|
|
143
|
-
fs.writeFileSync(file, "KEY=value");
|
|
144
|
-
const { importCommand } = await import("./import.js");
|
|
145
|
-
const { Command } = await import("commander");
|
|
146
|
-
const program = new Command();
|
|
147
|
-
program.addCommand(importCommand);
|
|
148
|
-
await program.parseAsync(["node", "test", "import", "--force", "--format", "env", file], { from: "node" });
|
|
149
|
-
expect(detectFormat).not.toHaveBeenCalled();
|
|
150
|
-
expect(parseImportFile).toHaveBeenCalledWith("env", expect.any(String));
|
|
151
|
-
});
|
|
152
|
-
it("exits with error for unrecognized --format value", async () => {
|
|
153
|
-
const file = path.join(tmpDir, "secrets.txt");
|
|
154
|
-
fs.writeFileSync(file, "KEY=value");
|
|
155
|
-
const { importCommand } = await import("./import.js");
|
|
156
|
-
const { Command } = await import("commander");
|
|
157
|
-
const program = new Command();
|
|
158
|
-
program.addCommand(importCommand);
|
|
159
|
-
await expect(program.parseAsync(["node", "test", "import", "--force", "--format", "toml", file], { from: "node" })).rejects.toThrow("process.exit");
|
|
160
|
-
expect(spinnerMock.calls.fail).toHaveBeenCalled();
|
|
167
|
+
writeFile(".env", ENV_FILE);
|
|
168
|
+
remote = { API_KEY: "CANARY_key_old_7", PORT: "3000" };
|
|
169
|
+
});
|
|
170
|
+
it("stops with exit 2 and writes nothing when there is no terminal and no policy", async () => {
|
|
171
|
+
const { exit, text } = await run([".env"]);
|
|
172
|
+
expect(exit).toBe(2);
|
|
173
|
+
expect(apiPut).not.toHaveBeenCalled();
|
|
174
|
+
expect(inquirer.prompt).not.toHaveBeenCalled();
|
|
175
|
+
expect(text).toContain("import.conflictNeedsPolicy");
|
|
176
|
+
expect(text).toContain("API_KEY");
|
|
177
|
+
expect(text).toContain("import.conflictHint");
|
|
178
|
+
expectNoValues(text);
|
|
179
|
+
});
|
|
180
|
+
it("keeps the current values with --on-conflict skip", async () => {
|
|
181
|
+
const { exit, text } = await run([".env", "--on-conflict", "skip"]);
|
|
182
|
+
expect(exit).toBeUndefined();
|
|
183
|
+
expect(Object.keys(written()).sort()).toEqual(["DATABASE_URL", "STRIPE_SECRET_KEY"]);
|
|
184
|
+
expect(text).toContain("import.labelSkipped");
|
|
185
|
+
expectNoValues(text);
|
|
186
|
+
});
|
|
187
|
+
it("overwrites with --on-conflict overwrite", async () => {
|
|
188
|
+
await run([".env", "--on-conflict", "overwrite"]);
|
|
189
|
+
expect(Object.keys(written()).sort()).toEqual(["API_KEY", "DATABASE_URL", "STRIPE_SECRET_KEY"]);
|
|
190
|
+
});
|
|
191
|
+
it("fails with exit 2 and writes nothing with --on-conflict fail", async () => {
|
|
192
|
+
const { exit, text } = await run([".env", "--on-conflict", "fail"]);
|
|
193
|
+
expect(exit).toBe(2);
|
|
161
194
|
expect(apiPut).not.toHaveBeenCalled();
|
|
195
|
+
expect(text).toContain("import.conflictFailed");
|
|
196
|
+
expectNoValues(text);
|
|
162
197
|
});
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
fs.writeFileSync(file, "KEY=new-value");
|
|
169
|
-
vi.mocked(parseImportFile).mockReturnValue({ KEY: "new-value" });
|
|
170
|
-
vi.mocked(apiGet).mockResolvedValue({
|
|
171
|
-
json: () => Promise.resolve({ secrets: { KEY: "old-value" } }),
|
|
172
|
-
});
|
|
173
|
-
vi.mocked(checkForBreakingChanges).mockReturnValue({ modified: ["KEY"], deleted: [] });
|
|
174
|
-
vi.mocked(inquirer.prompt).mockResolvedValue({ shouldContinue: true });
|
|
175
|
-
const { importCommand } = await import("./import.js");
|
|
176
|
-
const { Command } = await import("commander");
|
|
177
|
-
const program = new Command();
|
|
178
|
-
program.addCommand(importCommand);
|
|
179
|
-
await program.parseAsync(["node", "test", "import", file], { from: "node" });
|
|
180
|
-
expect(checkForBreakingChanges).toHaveBeenCalledWith({ KEY: "old-value" }, { KEY: "new-value" });
|
|
181
|
-
expect(inquirer.prompt).toHaveBeenCalled();
|
|
182
|
-
expect(apiPut).toHaveBeenCalled();
|
|
183
|
-
});
|
|
184
|
-
it("aborts import when user declines conflict confirmation", async () => {
|
|
185
|
-
const file = path.join(tmpDir, ".env");
|
|
186
|
-
fs.writeFileSync(file, "KEY=new-value");
|
|
187
|
-
vi.mocked(parseImportFile).mockReturnValue({ KEY: "new-value" });
|
|
188
|
-
vi.mocked(apiGet).mockResolvedValue({
|
|
189
|
-
json: () => Promise.resolve({ secrets: { KEY: "old-value" } }),
|
|
190
|
-
});
|
|
191
|
-
vi.mocked(checkForBreakingChanges).mockReturnValue({ modified: ["KEY"], deleted: [] });
|
|
198
|
+
it("asks in a terminal and writes nothing when the person declines", async () => {
|
|
199
|
+
Object.defineProperty(process.stdin, "isTTY", { value: true, configurable: true });
|
|
200
|
+
Object.defineProperty(process.stdout, "isTTY", { value: true, configurable: true });
|
|
201
|
+
const ci = process.env.CI;
|
|
202
|
+
delete process.env.CI;
|
|
192
203
|
vi.mocked(inquirer.prompt).mockResolvedValue({ shouldContinue: false });
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
204
|
+
try {
|
|
205
|
+
const { exit, text } = await run([".env"]);
|
|
206
|
+
expect(inquirer.prompt).toHaveBeenCalledTimes(1);
|
|
207
|
+
expect(exit).toBe(0);
|
|
208
|
+
expect(apiPut).not.toHaveBeenCalled();
|
|
209
|
+
expectNoValues(text);
|
|
210
|
+
}
|
|
211
|
+
finally {
|
|
212
|
+
if (ci !== undefined)
|
|
213
|
+
process.env.CI = ci;
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
it("rejects an unknown policy", async () => {
|
|
217
|
+
const { exit, text } = await run([".env", "--on-conflict", "merge"]);
|
|
218
|
+
expect(exit).toBe(1);
|
|
219
|
+
expect(text).toContain("import.badPolicy");
|
|
198
220
|
expect(apiPut).not.toHaveBeenCalled();
|
|
199
221
|
});
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
expect(
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
expect(
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
it("
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
222
|
+
});
|
|
223
|
+
describe("envshed import <file> --dry-run", () => {
|
|
224
|
+
it("prints the plan with key names and writes nothing", async () => {
|
|
225
|
+
writeFile(".env", ENV_FILE);
|
|
226
|
+
remote = { API_KEY: "CANARY_key_old_7", PORT: "3000" };
|
|
227
|
+
const { exit, text } = await run([".env", "--dry-run"]);
|
|
228
|
+
expect(exit).toBeUndefined();
|
|
229
|
+
expect(apiPut).not.toHaveBeenCalled();
|
|
230
|
+
expect(apiPost).not.toHaveBeenCalled();
|
|
231
|
+
expect(text).toContain("import.dryRunTitle");
|
|
232
|
+
expect(text).toContain("import.labelChangedOverwrite");
|
|
233
|
+
expect(text).toContain("import.dryRunNeedsPolicy");
|
|
234
|
+
expect(text).toContain("import.dryRunNothingWritten");
|
|
235
|
+
expectNoValues(text);
|
|
236
|
+
});
|
|
237
|
+
it("does not create a missing environment", async () => {
|
|
238
|
+
writeFile(".env", ENV_FILE);
|
|
239
|
+
const { exit, text } = await run([".env", "-e", "qa", "--create-env", "--dry-run"]);
|
|
240
|
+
expect(exit).toBeUndefined();
|
|
241
|
+
expect(apiPost).not.toHaveBeenCalled();
|
|
242
|
+
expect(apiPut).not.toHaveBeenCalled();
|
|
243
|
+
expect(text).toContain("import.dryRunCreatesEnv");
|
|
244
|
+
});
|
|
245
|
+
it("cannot be combined with --force", async () => {
|
|
246
|
+
writeFile(".env", ENV_FILE);
|
|
247
|
+
const { exit } = await run([".env", "--dry-run", "--force"]);
|
|
248
|
+
expect(exit).toBe(1);
|
|
249
|
+
expect(apiPut).not.toHaveBeenCalled();
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
describe("envshed import <file> --output json", () => {
|
|
253
|
+
it("prints one JSON document with key names and no values", async () => {
|
|
254
|
+
writeFile(".env", ENV_FILE);
|
|
255
|
+
remote = { API_KEY: "CANARY_key_old_7" };
|
|
256
|
+
const { exit, text } = await run([".env", "--on-conflict", "skip", "--output", "json"]);
|
|
257
|
+
expect(exit).toBeUndefined();
|
|
258
|
+
const doc = JSON.parse(text);
|
|
259
|
+
expect(doc.target).toEqual({ org: "acme", project: "api", env: "development" });
|
|
260
|
+
expect(doc.keys.new).toEqual(["DATABASE_URL", "PORT", "STRIPE_SECRET_KEY"]);
|
|
261
|
+
expect(doc.keys.changed).toEqual(["API_KEY"]);
|
|
262
|
+
expect(doc.keys.skipped).toEqual(["API_KEY"]);
|
|
263
|
+
expect(doc.keys.written).toEqual(["DATABASE_URL", "PORT", "STRIPE_SECRET_KEY"]);
|
|
264
|
+
expect(doc.git).toEqual({ tracked: false, ignored: false });
|
|
265
|
+
expectNoValues(text);
|
|
266
|
+
});
|
|
267
|
+
it("reports a conflict as JSON with exit 2", async () => {
|
|
268
|
+
writeFile(".env", ENV_FILE);
|
|
269
|
+
remote = { API_KEY: "CANARY_key_old_7" };
|
|
270
|
+
const { exit, text } = await run([".env", "--output", "json"]);
|
|
271
|
+
expect(exit).toBe(2);
|
|
272
|
+
expect(JSON.parse(text)).toEqual({ error: "conflict", message: expect.any(String), keys: ["API_KEY"] });
|
|
273
|
+
expectNoValues(text);
|
|
274
|
+
});
|
|
275
|
+
it("reports the git state of the file", async () => {
|
|
276
|
+
vi.mocked(gitStatus).mockReturnValue({ tracked: true, ignored: false, top: tmpDir });
|
|
277
|
+
writeFile(".env", ENV_FILE);
|
|
278
|
+
const { text } = await run([".env", "--output", "json"]);
|
|
279
|
+
expect(JSON.parse(text).git).toEqual({ tracked: true, ignored: false });
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
describe("envshed import <file>: environments", () => {
|
|
283
|
+
it("names the missing environment instead of failing with a 403 from the write", async () => {
|
|
284
|
+
writeFile(".env", ENV_FILE);
|
|
285
|
+
const { exit, text } = await run([".env", "-e", "qa"]);
|
|
286
|
+
expect(exit).toBe(1);
|
|
287
|
+
expect(text).toContain("import.envNotFound");
|
|
252
288
|
expect(apiPut).not.toHaveBeenCalled();
|
|
253
289
|
});
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
expect(
|
|
290
|
+
it("creates the environment with --create-env", async () => {
|
|
291
|
+
writeFile(".env", ENV_FILE);
|
|
292
|
+
const { exit } = await run([".env", "-e", "qa", "--create-env"]);
|
|
293
|
+
expect(exit).toBeUndefined();
|
|
294
|
+
expect(apiPost).toHaveBeenCalledWith("/environments/acme/api", { name: "qa" });
|
|
295
|
+
expect(vi.mocked(apiPut).mock.calls[0][0]).toBe("/secrets/acme/api/qa");
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
describe("envshed import <file>: errors never show values", () => {
|
|
299
|
+
it("YAML parse errors keep only the line number", async () => {
|
|
300
|
+
writeFile("bad.yaml", "KEY: [CANARY_yaml_8\n");
|
|
301
|
+
const { exit, text } = await run(["bad.yaml"]);
|
|
302
|
+
expect(exit).toBe(1);
|
|
303
|
+
expect(text).toMatch(/import\.parseFailed(Line|Safe)/);
|
|
304
|
+
expectNoValues(text);
|
|
305
|
+
});
|
|
306
|
+
it("JSON parse errors do not quote the file", async () => {
|
|
307
|
+
writeFile("bad.json", '{"KEY": CANARY_json_9}');
|
|
308
|
+
const { exit, text } = await run(["bad.json"]);
|
|
309
|
+
expect(exit).toBe(1);
|
|
310
|
+
expectNoValues(text);
|
|
311
|
+
});
|
|
312
|
+
it("CSV parse errors do not quote the file", async () => {
|
|
313
|
+
writeFile("bad.csv", 'key,value\nKEY,"CANARY_csv_10\n');
|
|
314
|
+
const { exit, text } = await run(["bad.csv"]);
|
|
315
|
+
expect(exit).toBe(1);
|
|
316
|
+
expectNoValues(text);
|
|
317
|
+
});
|
|
318
|
+
it("an API error is reported without values", async () => {
|
|
319
|
+
writeFile(".env", ENV_FILE);
|
|
320
|
+
vi.mocked(apiPut).mockRejectedValue(new Error("Request failed (403)"));
|
|
321
|
+
const { exit, text } = await run([".env"]);
|
|
322
|
+
expect(exit).toBe(1);
|
|
323
|
+
expect(text).toContain("import.importFailed");
|
|
324
|
+
expectNoValues(text);
|
|
325
|
+
});
|
|
326
|
+
it("names invalid keys before writing, and hides names that look like values", async () => {
|
|
327
|
+
writeFile("s.json", '{"MY-KEY":"CANARY_a","GOOD":"CANARY_b","sk_live CANARY_c":"x"}');
|
|
328
|
+
const { exit, text } = await run(["s.json"]);
|
|
329
|
+
expect(exit).toBe(1);
|
|
267
330
|
expect(apiPut).not.toHaveBeenCalled();
|
|
331
|
+
expect(text).toContain("import.invalidKeys");
|
|
332
|
+
expect(text).toContain("MY-KEY");
|
|
333
|
+
expectNoValues(text);
|
|
268
334
|
});
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
const
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
const
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
// So all 3 keys are "new", none are modified or unchanged
|
|
296
|
-
const { importCommand } = await import("./import.js");
|
|
297
|
-
const { Command } = await import("commander");
|
|
298
|
-
const program = new Command();
|
|
299
|
-
program.addCommand(importCommand);
|
|
300
|
-
await program.parseAsync(["node", "test", "import", "--force", file], { from: "node" });
|
|
301
|
-
expect(spinnerMock.calls.succeed).toHaveBeenCalledWith(expect.stringContaining("import.imported"));
|
|
302
|
-
});
|
|
303
|
-
it("shows correct new/modified/unchanged counts when remote has existing secrets", async () => {
|
|
304
|
-
const file = path.join(tmpDir, ".env");
|
|
305
|
-
fs.writeFileSync(file, "NEW=added\nEXISTING=changed\nSAME=same");
|
|
306
|
-
vi.mocked(parseImportFile).mockReturnValue({
|
|
307
|
-
NEW: "added",
|
|
308
|
-
EXISTING: "changed",
|
|
309
|
-
SAME: "same",
|
|
310
|
-
});
|
|
311
|
-
vi.mocked(apiGet).mockResolvedValue({
|
|
312
|
-
json: () => Promise.resolve({ secrets: { EXISTING: "old-value", SAME: "same" } }),
|
|
313
|
-
});
|
|
314
|
-
vi.mocked(checkForBreakingChanges).mockReturnValue({ modified: ["EXISTING"], deleted: [] });
|
|
315
|
-
const { importCommand } = await import("./import.js");
|
|
316
|
-
const { Command } = await import("commander");
|
|
317
|
-
const program = new Command();
|
|
318
|
-
program.addCommand(importCommand);
|
|
319
|
-
await program.parseAsync(["node", "test", "import", file], { from: "node" });
|
|
320
|
-
// succeed called with summary containing counts: total=3, new=1, modified=1, unchanged=1
|
|
321
|
-
expect(spinnerMock.calls.succeed).toHaveBeenCalledWith(expect.stringContaining(JSON.stringify({ total: 3, target: "my-org/my-project/development", new: 1, modified: 1, unchanged: 1 })));
|
|
322
|
-
});
|
|
323
|
-
// -------------------------------------------------------------------------
|
|
324
|
-
// Missing org/project/env
|
|
325
|
-
// -------------------------------------------------------------------------
|
|
326
|
-
it("exits when org/project/env are missing", async () => {
|
|
327
|
-
vi.mocked(resolveOpts).mockReturnValue({
|
|
328
|
-
org: undefined,
|
|
329
|
-
project: undefined,
|
|
330
|
-
env: undefined,
|
|
331
|
-
});
|
|
332
|
-
const file = path.join(tmpDir, ".env");
|
|
333
|
-
fs.writeFileSync(file, "KEY=value");
|
|
334
|
-
const { importCommand } = await import("./import.js");
|
|
335
|
-
const { Command } = await import("commander");
|
|
336
|
-
const program = new Command();
|
|
337
|
-
program.addCommand(importCommand);
|
|
338
|
-
await expect(program.parseAsync(["node", "test", "import", "--force", file], { from: "node" })).rejects.toThrow("process.exit");
|
|
339
|
-
expect(console.error).toHaveBeenCalledWith(expect.stringContaining("common.missingOrgProjectEnv"));
|
|
340
|
-
});
|
|
341
|
-
// -------------------------------------------------------------------------
|
|
342
|
-
// API errors
|
|
343
|
-
// -------------------------------------------------------------------------
|
|
344
|
-
it("exits with error when apiGet fails during conflict check", async () => {
|
|
345
|
-
const file = path.join(tmpDir, ".env");
|
|
346
|
-
fs.writeFileSync(file, "KEY=value");
|
|
347
|
-
vi.mocked(apiGet).mockRejectedValue(new Error("Network error"));
|
|
348
|
-
const { importCommand } = await import("./import.js");
|
|
349
|
-
const { Command } = await import("commander");
|
|
350
|
-
const program = new Command();
|
|
351
|
-
program.addCommand(importCommand);
|
|
352
|
-
await expect(program.parseAsync(["node", "test", "import", file], { from: "node" })).rejects.toThrow("process.exit");
|
|
353
|
-
expect(spinnerMock.calls.fail).toHaveBeenCalled();
|
|
335
|
+
it("reports skipped .env lines by number, in text and JSON, without their content", async () => {
|
|
336
|
+
writeFile(".env", "GOOD=CANARY_g\nisto nao e uma linha CANARY_line\nOTHER=CANARY_o\n");
|
|
337
|
+
const { exit, text } = await run([".env", "--on-conflict", "skip", "--output", "json"]);
|
|
338
|
+
expect(exit).toBeUndefined();
|
|
339
|
+
expect(text).toContain("import.ignoredLines");
|
|
340
|
+
// stderr carries the warning; stdout carries only the JSON document.
|
|
341
|
+
const doc = JSON.parse(output.find((line) => line.startsWith("{\n")));
|
|
342
|
+
expect(doc.ignoredLines).toEqual([2]);
|
|
343
|
+
expect(doc.keys.written).toEqual(["GOOD", "OTHER"]);
|
|
344
|
+
expectNoValues(text);
|
|
345
|
+
});
|
|
346
|
+
it("a missing file exits 1", async () => {
|
|
347
|
+
const { exit, text } = await run(["nope.env"]);
|
|
348
|
+
expect(exit).toBe(1);
|
|
349
|
+
expect(text).toContain("import.fileNotFound");
|
|
350
|
+
});
|
|
351
|
+
it("an unknown --format exits 1", async () => {
|
|
352
|
+
writeFile("x.env", ENV_FILE);
|
|
353
|
+
const { exit, text } = await run(["x.env", "--format", "toml"]);
|
|
354
|
+
expect(exit).toBe(1);
|
|
355
|
+
expect(text).toContain("import.detectFailed");
|
|
356
|
+
});
|
|
357
|
+
it("a file with no keys writes nothing", async () => {
|
|
358
|
+
writeFile(".env", "# only a comment\n");
|
|
359
|
+
const { exit } = await run([".env"]);
|
|
360
|
+
expect(exit).toBeUndefined();
|
|
354
361
|
expect(apiPut).not.toHaveBeenCalled();
|
|
355
362
|
});
|
|
356
|
-
it("exits
|
|
357
|
-
const
|
|
358
|
-
|
|
359
|
-
vi.mocked(
|
|
360
|
-
|
|
361
|
-
const {
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
expect(
|
|
363
|
+
it("exits when org, project or env cannot be resolved", async () => {
|
|
364
|
+
const { resolveOpts, hasProjectConfig } = await import("../config.js");
|
|
365
|
+
vi.mocked(resolveOpts).mockReturnValueOnce({ org: undefined, project: undefined, env: undefined });
|
|
366
|
+
vi.mocked(hasProjectConfig).mockReturnValueOnce(false);
|
|
367
|
+
writeFile(".env", ENV_FILE);
|
|
368
|
+
const { exit, text } = await run([".env"]);
|
|
369
|
+
expect(exit).toBe(1);
|
|
370
|
+
expect(text).toContain("common.noProjectConfig");
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
describe("envshed import <file> --force", () => {
|
|
374
|
+
it("writes every key without reading the environment", async () => {
|
|
375
|
+
writeFile(".env", ENV_FILE);
|
|
376
|
+
const { exit, text } = await run([".env", "--force"]);
|
|
377
|
+
expect(exit).toBeUndefined();
|
|
378
|
+
expect(vi.mocked(apiGet).mock.calls.some(([url]) => url.startsWith("/secrets/"))).toBe(false);
|
|
379
|
+
expect(Object.keys(written()).sort()).toEqual(["API_KEY", "DATABASE_URL", "PORT", "STRIPE_SECRET_KEY"]);
|
|
380
|
+
expect(text).toContain("import.forceWarning");
|
|
381
|
+
expect(text).toContain("import.importedForce");
|
|
382
|
+
expectNoValues(text);
|
|
383
|
+
});
|
|
384
|
+
it("cannot be combined with --on-conflict", async () => {
|
|
385
|
+
writeFile(".env", ENV_FILE);
|
|
386
|
+
const { exit, text } = await run([".env", "--force", "--on-conflict", "skip"]);
|
|
387
|
+
expect(exit).toBe(1);
|
|
388
|
+
expect(text).toContain("import.forceWithPolicy");
|
|
382
389
|
expect(apiPut).not.toHaveBeenCalled();
|
|
383
390
|
});
|
|
384
391
|
});
|