Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/utils/__tests__/path.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { describe, expect, test } from "bun:test";
import { resolve } from "path";
import {
containsPathTraversal,
expandPath,
normalizePathForConfigKey,
toRelativePath,
getDirectoryForPath,
} from "../path";

// ─── containsPathTraversal ──────────────────────────────────────────────
Expand Down Expand Up @@ -129,3 +132,68 @@
expect(result).toBe("foo/bar");
});
});

// ─── toRelativePath ─────────────────────────────────────────────────────

describe("toRelativePath", () => {
test("returns relative path for a child of cwd", () => {
// Build a path that is inside the current working directory.
// resolve() returns an absolute path, and toRelativePath should give
// back just the final segment (or relative form without ..).
const abs = resolve(process.cwd(), "package.json");
const result = toRelativePath(abs);
expect(result).toBe("package.json");
expect(result).not.toContain("..");
});
Comment on lines +139 to +147
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟑 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Check the getCwd implementation to understand if it can differ from process.cwd()

# Find the getCwd implementation
ast-grep --pattern 'function getCwd() {
  $$$
}'

ast-grep --pattern 'export function getCwd() {
  $$$
}'

# Also search for getCwd definition with const/let
rg -n "(?:export\s+)?(?:const|let|var)\s+getCwd\s*=" -A 5

Repository: claude-code-best/claude-code

Length of output: 54


🏁 Script executed:

# Search for getCwd in all files
rg "getCwd" -n --type ts --type tsx

Repository: claude-code-best/claude-code

Length of output: 99


🏁 Script executed:

# Also check the test file structure
cat -n src/utils/__tests__/path.test.ts | head -50

Repository: claude-code-best/claude-code

Length of output: 1809


🏁 Script executed:

# Find the implementation file for toRelativePath
fd -e ts -e tsx | xargs rg -l "toRelativePath" | grep -v test

Repository: claude-code-best/claude-code

Length of output: 151


🏁 Script executed:

# Read the implementation file to see how getCwd is defined
cat -n src/utils/path.ts

Repository: claude-code-best/claude-code

Length of output: 6856


🏁 Script executed:

# Read the cwd.js file to see how getCwd is implemented
cat -n src/utils/cwd.js

Repository: claude-code-best/claude-code

Length of output: 120


🏁 Script executed:

# List files in src/utils directory
ls -la src/utils/

Repository: claude-code-best/claude-code

Length of output: 23384


🏁 Script executed:

# Search for any file with cwd in the name
fd "cwd" src/utils/

Repository: claude-code-best/claude-code

Length of output: 154


🏁 Script executed:

# Search for getCwd definition anywhere in the codebase
rg "export.*getCwd|function getCwd|const getCwd" --type ts

Repository: claude-code-best/claude-code

Length of output: 859


🏁 Script executed:

cat -n src/utils/cwd.ts

Repository: claude-code-best/claude-code

Length of output: 1280


Use getCwd() consistently in tests instead of process.cwd().

The test constructs paths using process.cwd(), but toRelativePath() internally uses getCwd(). These can return different values if an async context override is active via runWithCwdOverride(), or if getCwdState() differs from process.cwd(). This creates a mismatch that may cause tests to pass with false confidence or fail unexpectedly. Import and use getCwd() from the same source for accurate test validation.

πŸ€– Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/utils/__tests__/path.test.ts` around lines 139 - 147, The test builds an
absolute path using process.cwd() but toRelativePath() reads the current working
dir via getCwd(), which can differ when runWithCwdOverride()/getCwdState() are
used; update the test to import getCwd() and use getCwd() instead of
process.cwd() when constructing abs so the input and implementation use the same
cwd source (also add the getCwd import at the top of
src/utils/__tests__/path.test.ts).


test("returns absolute path when target is outside cwd", () => {
// A well-known absolute path that is always outside any typical cwd
// (any absolute path that doesn't start with process.cwd() will work)
const cwd = process.cwd();
// Build a path guaranteed to be outside cwd by going to the root's parent
// of cwd, then a sibling directory with an unlikely name
const outsidePath = resolve(cwd, "../../__unlikely_dir_xyz__");
const result = toRelativePath(outsidePath);
// relative(cwd, outsidePath) will start with '../..' so function returns absolute
expect(result).toBe(outsidePath);
});

test("returns empty string for cwd itself", () => {
const cwd = process.cwd();
const result = toRelativePath(cwd);
// relative(cwd, cwd) === '' which does not start with '..'
expect(result).toBe("");
});

test("returns a string for any absolute path", () => {
const abs = resolve(process.cwd(), "src");
const result = toRelativePath(abs);
expect(typeof result).toBe("string");
});
});

// ─── getDirectoryForPath ─────────────────────────────────────────────────

describe("getDirectoryForPath", () => {
test("returns the path itself when given an existing directory", () => {
// The src directory is guaranteed to exist in this repo
const dir = resolve(process.cwd(), "src");
const result = getDirectoryForPath(dir);
expect(result).toBe(dir);

Check failure on line 182 in src/utils/__tests__/path.test.ts

View workflow job for this annotation

GitHub Actions / ci

error: expect(received).toBe(expected)

Expected: "/home/runner/work/claude-code/claude-code/src" Received: "/home/runner/work/claude-code/claude-code" at <anonymous> (/home/runner/work/claude-code/claude-code/src/utils/__tests__/path.test.ts:182:20)
});

test("returns parent directory for a known file", () => {
// package.json is at the repo root
const file = resolve(process.cwd(), "package.json");
const expectedParent = process.cwd();
const result = getDirectoryForPath(file);
expect(result).toBe(expectedParent);
});

test("returns parent directory for a non-existent path", () => {
const nonExistent = resolve(process.cwd(), "does-not-exist-xyz123.ts");
const expectedParent = process.cwd();
const result = getDirectoryForPath(nonExistent);
expect(result).toBe(expectedParent);
});
});
Loading