Skip to content

test: add coverage for toRelativePath and getDirectoryForPath#95

Merged
claude-code-best merged 1 commit intoclaude-code-best:mainfrom
JiayuuWang:contribot/test-path-utils
Apr 3, 2026
Merged

test: add coverage for toRelativePath and getDirectoryForPath#95
claude-code-best merged 1 commit intoclaude-code-best:mainfrom
JiayuuWang:contribot/test-path-utils

Conversation

@JiayuuWang
Copy link
Copy Markdown
Contributor

@JiayuuWang JiayuuWang commented Apr 3, 2026

Summary

  • toRelativePath and getDirectoryForPath are exported from src/utils/path.ts and used throughout the codebase but had no unit tests
  • Added 4 tests for toRelativePath: returns a relative path for children of CWD, keeps the absolute path when the target is outside CWD (relative would start with ..), returns "" for CWD itself, and type-safety invariant
  • Added 3 tests for getDirectoryForPath: returns the path itself for an existing directory, returns the parent directory for a known file, and returns the parent directory for a non-existent path

Test plan

  • Run bun test src/utils/__tests__/path.test.ts — all existing tests continue to pass, new tests pass
  • Tests use process.cwd() and resolve() so they work against the actual repo structure without any mocking (the src/ directory and package.json are guaranteed to exist at the repo root)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Tests
    • Expanded test coverage for path utility functions with new test cases verifying behavior in various scenarios.

These two exported functions in src/utils/path.ts lacked unit tests.

toRelativePath wraps Node's path.relative() and keeps the absolute
path when the result would start with '..' (i.e. the target is outside
the CWD). Four tests cover: child-of-cwd, outside-cwd, cwd-itself,
and the type-safety invariant.

getDirectoryForPath uses statSync to distinguish directories from files,
falling back to dirname() for non-existent paths and bypassing the
filesystem for UNC paths (NTLM credential-leak prevention). Three tests
cover: existing directory, known file, and non-existent path.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 3, 2026

📝 Walkthrough

Walkthrough

The pull request extends the test suite for path utilities by adding comprehensive test coverage for two newly exported functions: toRelativePath and getDirectoryForPath. The changes include importing Node's path.resolve utility and implementing test cases that verify path conversion and directory detection behavior across various scenarios.

Changes

Cohort / File(s) Summary
Path Utilities Test Suite
src/utils/__tests__/path.test.ts
Added comprehensive test coverage for toRelativePath (relative path conversion, boundary cases) and getDirectoryForPath (directory vs. file differentiation). Introduced resolve import from Node's path module for constructing absolute test paths.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Poem

🐰 Hop skip and a bound, through paths I have found,
Tests now abound with directories round,
Relative paths, parent delight,
Path utilities tested just right!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding test coverage for two specific utility functions (toRelativePath and getDirectoryForPath).
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/utils/__tests__/path.test.ts (1)

185-191: Tests rely on specific repository structure.

These tests assume package.json exists at the repository root and use process.cwd() to construct paths. Similar to the previous test, if process.cwd() differs from getCwd() (used internally by the implementation), these tests may produce unreliable results.

Consider documenting this assumption or making the tests more resilient by checking for file/directory existence before asserting expected behavior.

Also applies to: 193-198

🤖 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 185 - 191, The test "returns
parent directory for a known file" (and the similar test at 193-198) assumes
package.json lives at process.cwd(), which can diverge from the implementation's
getCwd() and break CI; update the tests that call getDirectoryForPath to first
verify the target file/directory exists and use getCwd() (or mock
getCwd/process.cwd consistently) to build the expected path, or skip/assert
conditionally when the file is missing so tests don't rely on repository layout;
specifically modify the tests referencing process.cwd(), getDirectoryForPath,
and any use of getCwd to either mock/align the working directory or check
fs.existsSync before asserting.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/utils/__tests__/path.test.ts`:
- Around line 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).

---

Nitpick comments:
In `@src/utils/__tests__/path.test.ts`:
- Around line 185-191: The test "returns parent directory for a known file" (and
the similar test at 193-198) assumes package.json lives at process.cwd(), which
can diverge from the implementation's getCwd() and break CI; update the tests
that call getDirectoryForPath to first verify the target file/directory exists
and use getCwd() (or mock getCwd/process.cwd consistently) to build the expected
path, or skip/assert conditionally when the file is missing so tests don't rely
on repository layout; specifically modify the tests referencing process.cwd(),
getDirectoryForPath, and any use of getCwd to either mock/align the working
directory or check fs.existsSync before asserting.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ab4505cc-adea-4c02-b972-3344adb0b82b

📥 Commits

Reviewing files that changed from the base of the PR and between 465e9f0 and 20decb9.

📒 Files selected for processing (1)
  • src/utils/__tests__/path.test.ts

Comment on lines +139 to +147
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("..");
});
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).

@claude-code-best claude-code-best merged commit fdb2442 into claude-code-best:main Apr 3, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants