|
1 | | -import { vi, describe, it, expect, beforeEach } from "vitest" |
2 | | -import * as path from "path" |
3 | | - |
4 | | -// Mock ripgrep to avoid filesystem dependencies |
5 | | -vi.mock("../../ripgrep", () => ({ |
6 | | - getBinPath: vi.fn().mockResolvedValue("/mock/path/to/rg"), |
7 | | -})) |
8 | | - |
9 | | -// Mock vscode |
10 | | -vi.mock("vscode", () => ({ |
11 | | - env: { |
12 | | - appRoot: "/mock/app/root", |
13 | | - }, |
14 | | -})) |
15 | | - |
16 | | -// Mock filesystem operations |
17 | | -vi.mock("fs", () => ({ |
18 | | - promises: { |
19 | | - access: vi.fn().mockRejectedValue(new Error("Not found")), |
20 | | - readFile: vi.fn().mockResolvedValue(""), |
21 | | - readdir: vi.fn().mockResolvedValue([]), |
22 | | - }, |
23 | | -})) |
24 | | - |
25 | | -vi.mock("child_process", () => ({ |
26 | | - spawn: vi.fn(), |
27 | | -})) |
28 | | - |
29 | | -vi.mock("../../path", () => ({ |
30 | | - arePathsEqual: vi.fn().mockReturnValue(false), |
31 | | -})) |
32 | | - |
| 1 | +import { describe, it, expect, vi } from "vitest" |
33 | 2 | import { listFiles } from "../list-files" |
34 | | -import * as childProcess from "child_process" |
35 | | - |
36 | | -describe("list-files symlink support", () => { |
37 | | - beforeEach(() => { |
38 | | - vi.clearAllMocks() |
39 | | - }) |
40 | | - |
41 | | - it("should include --follow flag in ripgrep arguments", async () => { |
42 | | - const mockSpawn = vi.mocked(childProcess.spawn) |
43 | | - const mockProcess = { |
44 | | - stdout: { |
45 | | - on: vi.fn((event, callback) => { |
46 | | - if (event === "data") { |
47 | | - // Simulate some output to complete the process |
48 | | - setTimeout(() => callback("test-file.txt\n"), 10) |
49 | | - } |
50 | | - }), |
51 | | - }, |
52 | | - stderr: { |
53 | | - on: vi.fn(), |
54 | | - }, |
55 | | - on: vi.fn((event, callback) => { |
56 | | - if (event === "close") { |
57 | | - setTimeout(() => callback(0), 20) |
58 | | - } |
59 | | - if (event === "error") { |
60 | | - // No error simulation |
61 | | - } |
62 | | - }), |
63 | | - kill: vi.fn(), |
64 | | - } |
65 | 3 |
|
66 | | - mockSpawn.mockReturnValue(mockProcess as any) |
67 | | - |
68 | | - // Call listFiles to trigger ripgrep execution |
69 | | - await listFiles("/test/dir", false, 100) |
70 | | - |
71 | | - // Verify that spawn was called with --follow flag (the critical fix) |
72 | | - const [rgPath, args] = mockSpawn.mock.calls[0] |
73 | | - expect(rgPath).toBe("/mock/path/to/rg") |
74 | | - expect(args).toContain("--files") |
75 | | - expect(args).toContain("--hidden") |
76 | | - expect(args).toContain("--follow") // This is the critical assertion - the fix should add this flag |
77 | | - |
78 | | - // Platform-agnostic path check - verify the last argument is the resolved path |
79 | | - const expectedPath = path.resolve("/test/dir") |
80 | | - expect(args[args.length - 1]).toBe(expectedPath) |
81 | | - }) |
82 | | - |
83 | | - it("should include --follow flag for recursive listings too", async () => { |
84 | | - const mockSpawn = vi.mocked(childProcess.spawn) |
85 | | - const mockProcess = { |
86 | | - stdout: { |
87 | | - on: vi.fn((event, callback) => { |
88 | | - if (event === "data") { |
89 | | - setTimeout(() => callback("test-file.txt\n"), 10) |
90 | | - } |
91 | | - }), |
92 | | - }, |
93 | | - stderr: { |
94 | | - on: vi.fn(), |
95 | | - }, |
96 | | - on: vi.fn((event, callback) => { |
97 | | - if (event === "close") { |
98 | | - setTimeout(() => callback(0), 20) |
99 | | - } |
100 | | - if (event === "error") { |
101 | | - // No error simulation |
102 | | - } |
103 | | - }), |
104 | | - kill: vi.fn(), |
105 | | - } |
106 | | - |
107 | | - mockSpawn.mockReturnValue(mockProcess as any) |
108 | | - |
109 | | - // Call listFiles with recursive=true |
110 | | - await listFiles("/test/dir", true, 100) |
| 4 | +vi.mock("../list-files", async () => { |
| 5 | + const actual = await vi.importActual("../list-files") |
| 6 | + return { |
| 7 | + ...actual, |
| 8 | + handleSpecialDirectories: vi.fn(), |
| 9 | + } |
| 10 | +}) |
111 | 11 |
|
112 | | - // Verify that spawn was called with --follow flag (the critical fix) |
113 | | - const [rgPath, args] = mockSpawn.mock.calls[0] |
114 | | - expect(rgPath).toBe("/mock/path/to/rg") |
115 | | - expect(args).toContain("--files") |
116 | | - expect(args).toContain("--hidden") |
117 | | - expect(args).toContain("--follow") // This should be present in recursive mode too |
| 12 | +describe("listFiles", () => { |
| 13 | + it("should return empty array immediately when limit is 0", async () => { |
| 14 | + const result = await listFiles("/test/path", true, 0) |
118 | 15 |
|
119 | | - // Platform-agnostic path check - verify the last argument is the resolved path |
120 | | - const expectedPath = path.resolve("/test/dir") |
121 | | - expect(args[args.length - 1]).toBe(expectedPath) |
| 16 | + expect(result).toEqual([[], false]) |
122 | 17 | }) |
123 | 18 | }) |
0 commit comments