Fix env-sensitive worktree and runtime config tests
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -2,7 +2,7 @@ import fs from "node:fs";
|
|||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { execFileSync } from "node:child_process";
|
import { execFileSync } from "node:child_process";
|
||||||
import { describe, expect, it } from "vitest";
|
import { afterEach, describe, expect, it } from "vitest";
|
||||||
import {
|
import {
|
||||||
copyGitHooksToWorktreeGitDir,
|
copyGitHooksToWorktreeGitDir,
|
||||||
copySeededSecretsKey,
|
copySeededSecretsKey,
|
||||||
@@ -22,6 +22,20 @@ import {
|
|||||||
} from "../commands/worktree-lib.js";
|
} from "../commands/worktree-lib.js";
|
||||||
import type { PaperclipConfig } from "../config/schema.js";
|
import type { PaperclipConfig } from "../config/schema.js";
|
||||||
|
|
||||||
|
const ORIGINAL_CWD = process.cwd();
|
||||||
|
const ORIGINAL_ENV = { ...process.env };
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
process.chdir(ORIGINAL_CWD);
|
||||||
|
for (const key of Object.keys(process.env)) {
|
||||||
|
if (!(key in ORIGINAL_ENV)) delete process.env[key];
|
||||||
|
}
|
||||||
|
for (const [key, value] of Object.entries(ORIGINAL_ENV)) {
|
||||||
|
if (value === undefined) delete process.env[key];
|
||||||
|
else process.env[key] = value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
function buildSourceConfig(): PaperclipConfig {
|
function buildSourceConfig(): PaperclipConfig {
|
||||||
return {
|
return {
|
||||||
$meta: {
|
$meta: {
|
||||||
|
|||||||
@@ -119,6 +119,14 @@ function nonEmpty(value: string | null | undefined): string | null {
|
|||||||
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
|
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isCurrentSourceConfigPath(sourceConfigPath: string): boolean {
|
||||||
|
const currentConfigPath = process.env.PAPERCLIP_CONFIG;
|
||||||
|
if (!currentConfigPath || currentConfigPath.trim().length === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return path.resolve(currentConfigPath) === path.resolve(sourceConfigPath);
|
||||||
|
}
|
||||||
|
|
||||||
function resolveWorktreeMakeName(name: string): string {
|
function resolveWorktreeMakeName(name: string): string {
|
||||||
const value = nonEmpty(name);
|
const value = nonEmpty(name);
|
||||||
if (!value) {
|
if (!value) {
|
||||||
@@ -440,9 +448,10 @@ export function copySeededSecretsKey(input: {
|
|||||||
|
|
||||||
mkdirSync(path.dirname(input.targetKeyFilePath), { recursive: true });
|
mkdirSync(path.dirname(input.targetKeyFilePath), { recursive: true });
|
||||||
|
|
||||||
|
const allowProcessEnvFallback = isCurrentSourceConfigPath(input.sourceConfigPath);
|
||||||
const sourceInlineMasterKey =
|
const sourceInlineMasterKey =
|
||||||
nonEmpty(input.sourceEnvEntries.PAPERCLIP_SECRETS_MASTER_KEY) ??
|
nonEmpty(input.sourceEnvEntries.PAPERCLIP_SECRETS_MASTER_KEY) ??
|
||||||
nonEmpty(process.env.PAPERCLIP_SECRETS_MASTER_KEY);
|
(allowProcessEnvFallback ? nonEmpty(process.env.PAPERCLIP_SECRETS_MASTER_KEY) : null);
|
||||||
if (sourceInlineMasterKey) {
|
if (sourceInlineMasterKey) {
|
||||||
writeFileSync(input.targetKeyFilePath, sourceInlineMasterKey, {
|
writeFileSync(input.targetKeyFilePath, sourceInlineMasterKey, {
|
||||||
encoding: "utf8",
|
encoding: "utf8",
|
||||||
@@ -458,7 +467,7 @@ export function copySeededSecretsKey(input: {
|
|||||||
|
|
||||||
const sourceKeyFileOverride =
|
const sourceKeyFileOverride =
|
||||||
nonEmpty(input.sourceEnvEntries.PAPERCLIP_SECRETS_MASTER_KEY_FILE) ??
|
nonEmpty(input.sourceEnvEntries.PAPERCLIP_SECRETS_MASTER_KEY_FILE) ??
|
||||||
nonEmpty(process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE);
|
(allowProcessEnvFallback ? nonEmpty(process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE) : null);
|
||||||
const sourceConfiguredKeyPath = sourceKeyFileOverride ?? input.sourceConfig.secrets.localEncrypted.keyFilePath;
|
const sourceConfiguredKeyPath = sourceKeyFileOverride ?? input.sourceConfig.secrets.localEncrypted.keyFilePath;
|
||||||
const sourceKeyFilePath = resolveRuntimeLikePath(sourceConfiguredKeyPath, input.sourceConfigPath);
|
const sourceKeyFilePath = resolveRuntimeLikePath(sourceConfiguredKeyPath, input.sourceConfigPath);
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ describe("resolveDatabaseTarget", () => {
|
|||||||
const projectDir = path.join(tempDir, "repo");
|
const projectDir = path.join(tempDir, "repo");
|
||||||
fs.mkdirSync(projectDir, { recursive: true });
|
fs.mkdirSync(projectDir, { recursive: true });
|
||||||
process.chdir(projectDir);
|
process.chdir(projectDir);
|
||||||
|
delete process.env.PAPERCLIP_CONFIG;
|
||||||
writeJson(path.join(projectDir, ".paperclip", "config.json"), {
|
writeJson(path.join(projectDir, ".paperclip", "config.json"), {
|
||||||
database: { mode: "embedded-postgres", embeddedPostgresPort: 54329 },
|
database: { mode: "embedded-postgres", embeddedPostgresPort: 54329 },
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user