Skip to content

Commit a396557

Browse files
committed
fix: export resolveEnvVars and import in tests instead of duplicating
Addresses cubic-dev-ai review: tests were exercising a copy of the function, not the production code. Now imports from src/mcp directly.
1 parent 8e1671f commit a396557

File tree

2 files changed

+2
-25
lines changed

2 files changed

+2
-25
lines changed

packages/opencode/src/mcp/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { Telemetry } from "@/telemetry"
3131
const ENV_VAR_PATTERN =
3232
/\$\$(\{[A-Za-z_][A-Za-z0-9_]*(?::-[^}]*)?\})|(?<!\$)\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\}|\{env:([^}]+)\}/g
3333

34-
function resolveEnvVars(environment: Record<string, string>): Record<string, string> {
34+
export function resolveEnvVars(environment: Record<string, string>): Record<string, string> {
3535
const resolved: Record<string, string> = {}
3636
for (const [key, value] of Object.entries(environment)) {
3737
resolved[key] = value.replace(ENV_VAR_PATTERN, (match, escaped, dollarVar, dollarDefault, braceVar) => {

packages/opencode/test/mcp/env-var-interpolation.test.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,12 @@ import { describe, test, expect, beforeEach, afterEach } from "bun:test"
33
import { mkdtemp, rm, mkdir, writeFile } from "fs/promises"
44
import { tmpdir } from "os"
55
import path from "path"
6+
import { resolveEnvVars } from "../../src/mcp"
67

78
// -------------------------------------------------------------------------
89
// resolveEnvVars — safety-net resolver at MCP launch site
910
// -------------------------------------------------------------------------
1011

11-
// Import the module to access resolveEnvVars indirectly via the env spread.
12-
// Since resolveEnvVars is a module-level function (not inside the MCP namespace),
13-
// we test it directly by importing the file and extracting the function.
14-
// For now, inline the same logic here for unit testing.
15-
16-
const ENV_VAR_PATTERN =
17-
/\$\$(\{[A-Za-z_][A-Za-z0-9_]*(?::-[^}]*)?\})|(?<!\$)\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\}|\{env:([^}]+)\}/g
18-
19-
function resolveEnvVars(environment: Record<string, string>): Record<string, string> {
20-
const resolved: Record<string, string> = {}
21-
for (const [key, value] of Object.entries(environment)) {
22-
resolved[key] = value.replace(ENV_VAR_PATTERN, (match, escaped, dollarVar, dollarDefault, braceVar) => {
23-
if (escaped !== undefined) return "$" + escaped
24-
if (dollarVar !== undefined) {
25-
const envValue = process.env[dollarVar]
26-
return envValue !== undefined && envValue !== "" ? envValue : (dollarDefault ?? "")
27-
}
28-
if (braceVar !== undefined) return process.env[braceVar] || ""
29-
return match
30-
})
31-
}
32-
return resolved
33-
}
34-
3512
describe("resolveEnvVars", () => {
3613
const ORIGINAL_ENV = { ...process.env }
3714

0 commit comments

Comments
 (0)