@dotenc/cli
Version:
🔐 Secure, encrypted environment variables that live in your codebase
65 lines (64 loc) • 2.73 kB
JavaScript
import { existsSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
import { editCommand } from "../commands/edit.js";
import { initCommand } from "../commands/init.js";
import { keyRotateCommand } from "../commands/key/rotate.js";
import { runCommand } from "../commands/run.js";
import { getKey } from "../helpers/key.js";
import { cleanupProjectKeys } from "./helpers/cleanupProjectKeys.js";
import { waitForFile } from "./helpers/waitForFile.js";
const localEnvFilePath = path.join(process.cwd(), ".env");
const encryptedEnvFilePath = path.join(process.cwd(), ".env.test.enc");
const projectFilePath = path.join(process.cwd(), "dotenc.json");
const outputFilePath = path.join(process.cwd(), "e2e.txt");
vi.mock("node:child_process", async (importOriginal) => {
const actual = await importOriginal();
return {
...actual,
// Mock for the edit command
execSync: () => {
const tempFilePath = path.join(os.tmpdir(), ".env.test");
writeFileSync(tempFilePath, "DOTENC_HELLO=Hello, world!");
},
};
});
describe("e2e", () => {
beforeAll(() => {
vi.spyOn(console, "log").mockImplementation(() => { });
vi.spyOn(process, "exit").mockImplementation(() => ({}));
});
test("should initialize an environment", async () => {
await initCommand("test");
expect(existsSync(localEnvFilePath)).toBe(true);
expect(existsSync(encryptedEnvFilePath)).toBe(true);
expect(existsSync(projectFilePath)).toBe(true);
});
test("should edit an environment", async () => {
const initialContent = readFileSync(encryptedEnvFilePath, "utf-8");
await editCommand("test");
const editedContent = readFileSync(encryptedEnvFilePath, "utf-8");
expect(editedContent).not.toBe(initialContent);
});
test("should rotate a key", async () => {
const currentKey = await getKey("test");
await keyRotateCommand("test");
const rotatedKey = await getKey("test");
expect(rotatedKey).not.toBe(currentKey);
});
test("should run a command in an environment", async () => {
await runCommand("sh", [path.join(__dirname, "helpers", "e2e.sh")], {
env: "test",
});
const output = await waitForFile(outputFilePath);
expect(output).toBe("Hello, world!\n");
});
afterAll(async () => {
await cleanupProjectKeys();
unlinkSync(localEnvFilePath);
unlinkSync(encryptedEnvFilePath);
unlinkSync(projectFilePath);
unlinkSync(outputFilePath);
});
});