-
Notifications
You must be signed in to change notification settings - Fork 0
fix(tools): list_files returns paths relative to queried directory — closes #24 #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -415,6 +415,109 @@ func TestGrep_RecursiveSearch(t *testing.T) { | |||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // ── listFiles tests ── | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| func TestListFiles_PathsRelativeToDirectory(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||
| dir := t.TempDir() | ||||||||||||||||||||||||||||||||||||||
| sub := filepath.Join(dir, "sub") | ||||||||||||||||||||||||||||||||||||||
| os.MkdirAll(sub, 0o755) | ||||||||||||||||||||||||||||||||||||||
| os.WriteFile(filepath.Join(dir, "a.go"), []byte(""), 0o644) | ||||||||||||||||||||||||||||||||||||||
| os.WriteFile(filepath.Join(sub, "b.go"), []byte(""), 0o644) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| r := listFiles(map[string]string{"directory": dir}, 0) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if !r.Success { | ||||||||||||||||||||||||||||||||||||||
| t.Fatalf("expected success, got error: %s", r.Error) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| // Paths must be relative to dir, not cwd | ||||||||||||||||||||||||||||||||||||||
| for _, line := range strings.Split(r.Output, "\n") { | ||||||||||||||||||||||||||||||||||||||
| if strings.HasPrefix(line, dir) { | ||||||||||||||||||||||||||||||||||||||
| t.Fatalf("got absolute path %q — expected relative to listed dir", line) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+432
to
+436
|
||||||||||||||||||||||||||||||||||||||
| // Paths must be relative to dir, not cwd | |
| for _, line := range strings.Split(r.Output, "\n") { | |
| if strings.HasPrefix(line, dir) { | |
| t.Fatalf("got absolute path %q — expected relative to listed dir", line) | |
| } | |
| // Paths must be relative to dir, not cwd, and must not contain parent traversals. | |
| for _, raw := range strings.Split(r.Output, "\n") { | |
| line := strings.TrimSpace(raw) | |
| if line == "" { | |
| continue | |
| } | |
| if strings.HasPrefix(line, dir) { | |
| t.Fatalf("got absolute path %q — expected relative to listed dir", line) | |
| } | |
| clean := filepath.Clean(line) | |
| if clean == ".." || strings.HasPrefix(clean, ".."+string(filepath.Separator)) { | |
| t.Fatalf("got path with parent traversal %q (clean: %q) — expected path relative to listed dir", line, clean) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
filepath.Rel(dir, path)errors are currently ignored. If Rel fails (e.g., different volume on Windows, malformed input),relbecomes an empty string and the code may append "" or "/" to the output, producing incorrect paths. Handle the error explicitly (e.g., return it to fail the tool call, or skip the entry) before usingrel.