Skip to content

Commit 053671e

Browse files
committed
Appease ellipsis
1 parent 9dce0e8 commit 053671e

File tree

2 files changed

+57
-22
lines changed

2 files changed

+57
-22
lines changed

src/core/prompts/sections/__tests__/custom-instructions.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,45 @@ import fs from "fs/promises"
55
jest.mock("fs/promises")
66
const mockedFs = jest.mocked(fs)
77

8+
describe("safeReadFile", () => {
9+
beforeEach(() => {
10+
jest.clearAllMocks()
11+
})
12+
13+
it("should read and trim file content", async () => {
14+
mockedFs.readFile.mockResolvedValue(" content with spaces ")
15+
const result = await loadRuleFiles("/fake/path")
16+
expect(mockedFs.readFile).toHaveBeenCalled()
17+
expect(result).toBe(
18+
"\n# Rules from .clinerules:\ncontent with spaces\n" +
19+
"\n# Rules from .cursorrules:\ncontent with spaces\n" +
20+
"\n# Rules from .windsurfrules:\ncontent with spaces\n",
21+
)
22+
})
23+
24+
it("should handle ENOENT error", async () => {
25+
mockedFs.readFile.mockRejectedValue({ code: "ENOENT" })
26+
const result = await loadRuleFiles("/fake/path")
27+
expect(result).toBe("")
28+
})
29+
30+
it("should handle EISDIR error", async () => {
31+
mockedFs.readFile.mockRejectedValue({ code: "EISDIR" })
32+
const result = await loadRuleFiles("/fake/path")
33+
expect(result).toBe("")
34+
})
35+
36+
it("should throw on unexpected errors", async () => {
37+
const error = new Error("Permission denied") as NodeJS.ErrnoException
38+
error.code = "EPERM"
39+
mockedFs.readFile.mockRejectedValue(error)
40+
41+
await expect(async () => {
42+
await loadRuleFiles("/fake/path")
43+
}).rejects.toThrow()
44+
})
45+
})
46+
847
describe("loadRuleFiles", () => {
948
beforeEach(() => {
1049
jest.clearAllMocks()

src/core/prompts/sections/custom-instructions.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
import fs from "fs/promises"
22
import path from "path"
33

4+
async function safeReadFile(filePath: string): Promise<string> {
5+
try {
6+
const content = await fs.readFile(filePath, "utf-8")
7+
return content.trim()
8+
} catch (err) {
9+
const errorCode = (err as NodeJS.ErrnoException).code
10+
if (!errorCode || !["ENOENT", "EISDIR"].includes(errorCode)) {
11+
throw err
12+
}
13+
return ""
14+
}
15+
}
16+
417
export async function loadRuleFiles(cwd: string): Promise<string> {
518
const ruleFiles = [".clinerules", ".cursorrules", ".windsurfrules"]
619
let combinedRules = ""
720

821
for (const file of ruleFiles) {
9-
try {
10-
const content = await fs.readFile(path.join(cwd, file), "utf-8")
11-
if (content.trim()) {
12-
combinedRules += `\n# Rules from ${file}:\n${content.trim()}\n`
13-
}
14-
} catch (err) {
15-
const errorCode = (err as NodeJS.ErrnoException).code
16-
if (!errorCode || !["ENOENT", "EISDIR"].includes(errorCode)) {
17-
throw err
18-
}
22+
const content = await safeReadFile(path.join(cwd, file))
23+
if (content) {
24+
combinedRules += `\n# Rules from ${file}:\n${content}\n`
1925
}
2026
}
2127

@@ -34,18 +40,8 @@ export async function addCustomInstructions(
3440
// Load mode-specific rules if mode is provided
3541
let modeRuleContent = ""
3642
if (mode) {
37-
try {
38-
const modeRuleFile = `.clinerules-${mode}`
39-
const content = await fs.readFile(path.join(cwd, modeRuleFile), "utf-8")
40-
if (content.trim()) {
41-
modeRuleContent = content.trim()
42-
}
43-
} catch (err) {
44-
const errorCode = (err as NodeJS.ErrnoException).code
45-
if (!errorCode || !["ENOENT", "EISDIR"].includes(errorCode)) {
46-
throw err
47-
}
48-
}
43+
const modeRuleFile = `.clinerules-${mode}`
44+
modeRuleContent = await safeReadFile(path.join(cwd, modeRuleFile))
4945
}
5046

5147
// Add language preference if provided

0 commit comments

Comments
 (0)