Merge pull request #3671 from code-yeongyu/fix/file-reference-env-var-expansion
fix(file-reference-resolver): expand $VAR env vars in @path file references (fixes #3476)
This commit is contained in:
@@ -1,8 +1,62 @@
|
|||||||
import { afterAll, beforeAll, describe, expect, test } from "bun:test"
|
import { afterAll, beforeAll, describe, expect, test } from "bun:test"
|
||||||
import { mkdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"
|
import { mkdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"
|
||||||
import { tmpdir } from "node:os"
|
import { tmpdir } from "node:os"
|
||||||
import { join } from "node:path"
|
import { join, resolve } from "node:path"
|
||||||
import { resolveFileReferencesInText } from "./file-reference-resolver"
|
import { resolveFilePath, resolveFileReferencesInText } from "./file-reference-resolver"
|
||||||
|
|
||||||
|
describe("resolveFilePath", () => {
|
||||||
|
const cwd = "/skills/gsd"
|
||||||
|
|
||||||
|
test("expands bare environment variables before resolving absolute paths", () => {
|
||||||
|
//#given
|
||||||
|
const homeDir = process.env.HOME
|
||||||
|
if (!homeDir) {
|
||||||
|
throw new Error("HOME must be set for file reference resolver tests")
|
||||||
|
}
|
||||||
|
|
||||||
|
//#when
|
||||||
|
const resolved = resolveFilePath("$HOME/foo.md", cwd)
|
||||||
|
|
||||||
|
//#then
|
||||||
|
expect(resolved).toBe(resolve(homeDir, "foo.md"))
|
||||||
|
})
|
||||||
|
|
||||||
|
test("expands braced environment variables before resolving absolute paths", () => {
|
||||||
|
//#given
|
||||||
|
const homeDir = process.env.HOME
|
||||||
|
if (!homeDir) {
|
||||||
|
throw new Error("HOME must be set for file reference resolver tests")
|
||||||
|
}
|
||||||
|
|
||||||
|
//#when
|
||||||
|
const resolved = resolveFilePath("${HOME}/foo.md", cwd)
|
||||||
|
|
||||||
|
//#then
|
||||||
|
expect(resolved).toBe(resolve(homeDir, "foo.md"))
|
||||||
|
})
|
||||||
|
|
||||||
|
test("keeps absolute paths absolute", () => {
|
||||||
|
//#given
|
||||||
|
const absolutePath = "/abs/path.md"
|
||||||
|
|
||||||
|
//#when
|
||||||
|
const resolved = resolveFilePath(absolutePath, cwd)
|
||||||
|
|
||||||
|
//#then
|
||||||
|
expect(resolved).toBe(resolve(absolutePath))
|
||||||
|
})
|
||||||
|
|
||||||
|
test("resolves relative paths from cwd", () => {
|
||||||
|
//#given
|
||||||
|
const relativePath = "relative/path.md"
|
||||||
|
|
||||||
|
//#when
|
||||||
|
const resolved = resolveFilePath(relativePath, cwd)
|
||||||
|
|
||||||
|
//#then
|
||||||
|
expect(resolved).toBe(resolve(cwd, relativePath))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe("resolveFileReferencesInText", () => {
|
describe("resolveFileReferencesInText", () => {
|
||||||
const fixtureRoot = join(tmpdir(), `file-reference-resolver-${Date.now()}`)
|
const fixtureRoot = join(tmpdir(), `file-reference-resolver-${Date.now()}`)
|
||||||
|
|||||||
@@ -30,12 +30,20 @@ function findFileReferences(text: string): FileMatch[] {
|
|||||||
return matches
|
return matches
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveFilePath(filePath: string, cwd: string): string {
|
export function resolveFilePath(filePath: string, cwd: string): string {
|
||||||
if (isAbsolute(filePath)) {
|
const expanded = filePath.replace(/\$\{(\w+)\}|\$(\w+)/g, (match, braced: string | undefined, bare: string | undefined) => {
|
||||||
return resolve(filePath)
|
const variableName = braced ?? bare
|
||||||
|
if (!variableName) {
|
||||||
|
return match
|
||||||
|
}
|
||||||
|
return process.env[variableName] ?? match
|
||||||
|
})
|
||||||
|
|
||||||
|
if (isAbsolute(expanded)) {
|
||||||
|
return resolve(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
return resolve(cwd, filePath)
|
return resolve(cwd, expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
function readFileContent(resolvedPath: string): string {
|
function readFileContent(resolvedPath: string): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user