| 1 | // Generated by continue |
| 2 | import fs from "fs"; |
| 3 | import path from "path"; |
| 4 | import { |
| 5 | walkDir, |
| 6 | walkDirAsync, |
| 7 | walkDirCache, |
| 8 | walkDirs, |
| 9 | } from "../indexing/walkDir"; |
| 10 | import { testIde } from "../test/fixtures"; |
| 11 | import { |
| 12 | addToTestDir, |
| 13 | setUpTestDir, |
| 14 | tearDownTestDir, |
| 15 | TEST_DIR_PATH, |
| 16 | } from "../test/testDir"; |
| 17 | |
| 18 | describe("walkDir functions", () => { |
| 19 | beforeEach(async () => { |
| 20 | setUpTestDir(); |
| 21 | }); |
| 22 | |
| 23 | afterEach(async () => { |
| 24 | tearDownTestDir(); |
| 25 | walkDirCache.invalidate(); |
| 26 | }); |
| 27 | |
| 28 | // describe.only("test speed", () => { |
| 29 | // it("should walk a simple directory structure", async () => { |
| 30 | // addToTestDir([ |
| 31 | // "src/", |
| 32 | // ["src/file1.ts", "content1"], |
| 33 | // ["src/file2.ts", "content2"], |
| 34 | // "src/nested/", |
| 35 | // ["src/nested/file3.ts", "content3"], |
| 36 | // ]); |
| 37 | |
| 38 | // const dir = await testIde.getWorkspaceDirs(); |
| 39 | |
| 40 | // const start = Date.now(); |
| 41 | // for (let i = 0; i < 10000; i++) { |
| 42 | // await walkDir(dir[0], testIde); |
| 43 | // } |
| 44 | // const passed = Date.now() - start; |
| 45 | // console.log(`Test took ${passed} ms`); |
| 46 | // }); |
| 47 | // }); |
| 48 | |
| 49 | describe("walkDir", () => { |
| 50 | it("should walk a simple directory structure", async () => { |
| 51 | addToTestDir([ |
| 52 | "src/", |
| 53 | ["src/file1.ts", "content1"], |
| 54 | ["src/file2.ts", "content2"], |
| 55 | "src/nested/", |
| 56 | ["src/nested/file3.ts", "content3"], |
| 57 | ]); |
| 58 | |
| 59 | const files = await walkDir( |
| 60 | (await testIde.getWorkspaceDirs())[0], |
| 61 | testIde, |
| 62 | ); |
| 63 | |
| 64 | expect(files.sort()).toEqual( |
| 65 | [ |
| 66 | expect.stringContaining("src/file1.ts"), |
| 67 | expect.stringContaining("src/file2.ts"), |
| 68 | expect.stringContaining("src/nested/file3.ts"), |
| 69 | ].sort(), |
| 70 | ); |
| 71 | }); |
| 72 | |
| 73 | it("should respect .gitignore", async () => { |
| 74 | addToTestDir([ |
| 75 | ["src/file1.ts", "content1"], |
| 76 | ["src/file2.ts", "content2"], |
| 77 | "node_modules/", |
| 78 | ["node_modules/pkg/file.js", "ignored"], |
| 79 | [".gitignore", "node_modules/"], |
| 80 | ]); |
| 81 | |
| 82 | const files = await walkDir( |
| 83 | (await testIde.getWorkspaceDirs())[0], |
| 84 | testIde, |
| 85 | ); |
| 86 | |
| 87 | expect(files).toEqual( |
| 88 | expect.not.arrayContaining([ |
| 89 | expect.stringContaining("node_modules/pkg/file.js"), |
| 90 | ]), |
| 91 | ); |
| 92 | expect(files.sort()).toEqual( |
| 93 | [ |
| 94 | expect.stringContaining("src/file1.ts"), |
| 95 | expect.stringContaining("src/file2.ts"), |
| 96 | ].sort(), |
| 97 | ); |
| 98 | }); |
| 99 | |
| 100 | it("should handle include option", async () => { |
| 101 | addToTestDir([ |
| 102 | "src/", |
| 103 | ["src/file1.ts", "content1"], |
| 104 | "src/nested/", |
| 105 | ["src/nested/file2.ts", "content2"], |
| 106 | ]); |
| 107 | |
| 108 | const dirs = await walkDir( |
| 109 | (await testIde.getWorkspaceDirs())[0], |
| 110 | testIde, |
| 111 | { |
| 112 | include: "dirs", |
| 113 | }, |
| 114 | ); |
| 115 | |
| 116 | expect(dirs.sort()).toEqual( |
| 117 | [ |
| 118 | expect.stringContaining("src"), |
| 119 | expect.stringContaining("src/nested"), |
| 120 | ].sort(), |
| 121 | ); |
| 122 | }); |
| 123 | }); |
| 124 | |
| 125 | describe("walkDirAsync", () => { |
| 126 | it("should yield files asynchronously", async () => { |
| 127 | addToTestDir([ |
| 128 | "src/", |
| 129 | ["src/file1.ts", "content1"], |
| 130 | ["src/file2.ts", "content2"], |
| 131 | ]); |
| 132 | |
| 133 | const files: string[] = []; |
| 134 | for await (const file of walkDirAsync( |
| 135 | (await testIde.getWorkspaceDirs())[0], |
| 136 | testIde, |
| 137 | )) { |
| 138 | files.push(file); |
| 139 | } |
| 140 | |
| 141 | expect(files.sort()).toEqual( |
| 142 | [ |
| 143 | expect.stringContaining("src/file1.ts"), |
| 144 | expect.stringContaining("src/file2.ts"), |
| 145 | ].sort(), |
| 146 | ); |
| 147 | }); |
| 148 | |
| 149 | it("should handle relative paths option", async () => { |
| 150 | addToTestDir([ |
| 151 | "src/", |
| 152 | ["src/file1.ts", "content1"], |
| 153 | ["src/file2.ts", "content2"], |
| 154 | ]); |
| 155 | |
| 156 | const files: string[] = []; |
| 157 | for await (const file of walkDirAsync( |
| 158 | (await testIde.getWorkspaceDirs())[0], |
| 159 | testIde, |
| 160 | { |
| 161 | returnRelativeUrisPaths: true, |
| 162 | }, |
| 163 | )) { |
| 164 | files.push(file); |
| 165 | } |
| 166 | |
| 167 | expect(files.sort()).toEqual(["src/file1.ts", "src/file2.ts"].sort()); |
| 168 | }); |
| 169 | }); |
| 170 | |
| 171 | describe("walkDirs", () => { |
| 172 | it("should walk multiple workspace directories", async () => { |
| 173 | addToTestDir([ |
| 174 | "workspace1/src/", |
| 175 | ["workspace1/src/file1.ts", "content1"], |
| 176 | "workspace2/src/", |
| 177 | ["workspace2/src/file2.ts", "content2"], |
| 178 | ]); |
| 179 | |
| 180 | const files = await walkDirs(testIde, undefined, [ |
| 181 | (await testIde.getWorkspaceDirs())[0] + "/workspace1", |
| 182 | (await testIde.getWorkspaceDirs())[0] + "/workspace2", // Second workspace dir |
| 183 | ]); |
| 184 | |
| 185 | expect(files).toContainEqual(expect.stringContaining("file1.ts")); |
| 186 | expect(files).toContainEqual(expect.stringContaining("file2.ts")); |
| 187 | }); |
| 188 | |
| 189 | it("should handle empty directories", async () => { |
| 190 | addToTestDir(["empty/"]); |
| 191 | |
| 192 | const files = await walkDirs(testIde); |
| 193 | console.log("EMPTY DIR", files); |
| 194 | |
| 195 | expect(files).toEqual([]); |
| 196 | }); |
| 197 | |
| 198 | it("should skip symlinks", async () => { |
| 199 | const filePath = path.join(TEST_DIR_PATH, "real.ts"); |
| 200 | addToTestDir([["real.ts", "content"]]); |
| 201 | fs.symlinkSync(filePath, path.join(TEST_DIR_PATH, "symlink.ts"), "file"); |
| 202 | |
| 203 | const files = await walkDirs(testIde); |
| 204 | |
| 205 | expect(files).not.toContainEqual(expect.stringContaining("symlink.ts")); |
| 206 | }); |
| 207 | }); |
| 208 | |
| 209 | describe("walkDir ignore patterns", () => { |
| 210 | it("should handle negation patterns in gitignore", async () => { |
| 211 | addToTestDir([ |
| 212 | [".gitignore", "**/*\n!*.py"], |
| 213 | ["a.txt", "content"], |
| 214 | ["b.py", "content"], |
| 215 | ["c.ts", "content"], |
| 216 | ]); |
| 217 | |
| 218 | const files = await walkDir( |
| 219 | (await testIde.getWorkspaceDirs())[0], |
| 220 | testIde, |
| 221 | { |
| 222 | returnRelativeUrisPaths: true, |
| 223 | }, |
| 224 | ); |
| 225 | |
| 226 | expect(files).toContain("b.py"); |
| 227 | expect(files).not.toContain("a.txt"); |
| 228 | expect(files).not.toContain("c.ts"); |
| 229 | }); |
| 230 | |
| 231 | it("should handle leading slash patterns", async () => { |
| 232 | addToTestDir([ |
| 233 | [".gitignore", "/no.txt"], |
| 234 | ["no.txt", "content"], |
| 235 | "sub/", |
| 236 | ["sub/no.txt", "content"], |
| 237 | ["a.txt", "content"], |
| 238 | ]); |
| 239 | |
| 240 | const files = await walkDir( |
| 241 | (await testIde.getWorkspaceDirs())[0], |
| 242 | testIde, |
| 243 | { |
| 244 | returnRelativeUrisPaths: true, |
| 245 | }, |
| 246 | ); |
| 247 | |
| 248 | expect(files).not.toContain("no.txt"); |
| 249 | expect(files).toContain("sub/no.txt"); |
| 250 | expect(files).toContain("a.txt"); |
| 251 | }); |
| 252 | |
| 253 | it("should handle multiple gitignore files in nested structure", async () => { |
| 254 | addToTestDir([ |
| 255 | [".gitignore", "*.txt"], |
| 256 | ["a.py", "content"], |
| 257 | ["b.txt", "content"], |
| 258 | "c/", |
| 259 | ["c/.gitignore", "*.py"], |
| 260 | ["c/d.txt", "content"], |
| 261 | ["c/e.py", "content"], |
| 262 | ]); |
| 263 | |
| 264 | const files = await walkDir( |
| 265 | (await testIde.getWorkspaceDirs())[0], |
| 266 | testIde, |
| 267 | { |
| 268 | returnRelativeUrisPaths: true, |
| 269 | }, |
| 270 | ); |
| 271 | |
| 272 | expect(files).toContain("a.py"); |
| 273 | expect(files).not.toContain("b.txt"); |
| 274 | expect(files).not.toContain("c/d.txt"); |
| 275 | expect(files).not.toContain("c/e.py"); |
| 276 | }); |
| 277 | |
| 278 | it("should handle both gitignore and continueignore", async () => { |
| 279 | addToTestDir([ |
| 280 | [".gitignore", "*.py"], |
| 281 | [".continueignore", "*.ts"], |
| 282 | ["a.txt", "content"], |
| 283 | ["b.py", "content"], |
| 284 | ["c.ts", "content"], |
| 285 | ["d.js", "content"], |
| 286 | ]); |
| 287 | |
| 288 | const files = await walkDir( |
| 289 | (await testIde.getWorkspaceDirs())[0], |
| 290 | testIde, |
| 291 | { |
| 292 | returnRelativeUrisPaths: true, |
| 293 | }, |
| 294 | ); |
| 295 | |
| 296 | expect(files).toContain("a.txt"); |
| 297 | expect(files).toContain("d.js"); |
| 298 | expect(files).not.toContain("b.py"); |
| 299 | expect(files).not.toContain("c.ts"); |
| 300 | }); |
| 301 | |
| 302 | it("should handle complex wildcard patterns", async () => { |
| 303 | addToTestDir([ |
| 304 | [".gitignore", "*.what\n!important.what\ntemp/\n/root_only.txt"], |
| 305 | ["a.what", "content"], |
| 306 | ["important.what", "content"], |
| 307 | ["root_only.txt", "content"], |
| 308 | "subdir/", |
| 309 | ["subdir/root_only.txt", "content"], |
| 310 | ["subdir/b.what", "content"], |
| 311 | "temp/", |
| 312 | ["temp/c.txt", "content"], |
| 313 | ]); |
| 314 | |
| 315 | const files = await walkDir( |
| 316 | (await testIde.getWorkspaceDirs())[0], |
| 317 | testIde, |
| 318 | { |
| 319 | returnRelativeUrisPaths: true, |
| 320 | }, |
| 321 | ); |
| 322 | |
| 323 | expect(files).toContain("important.what"); |
| 324 | expect(files).toContain("subdir/root_only.txt"); |
| 325 | expect(files).not.toContain("a.what"); |
| 326 | expect(files).not.toContain("root_only.txt"); |
| 327 | expect(files).not.toContain("subdir/b.what"); |
| 328 | expect(files).not.toContain("temp/c.txt"); |
| 329 | }); |
| 330 | |
| 331 | it("should skip common system directories by default", async () => { |
| 332 | addToTestDir([ |
| 333 | ["normal/file.txt", "content"], |
| 334 | [".git/config", "content"], |
| 335 | ["node_modules/package/index.js", "content"], |
| 336 | ["dist/bundle.js", "content"], |
| 337 | ["coverage/lcov.env", "content"], |
| 338 | ["nested/node_modules/package/index.js", "content"], |
| 339 | ["nested/dist/bundle.js", "content"], |
| 340 | ["nested/coverage/lcov.env", "content"], |
| 341 | ]); |
| 342 | |
| 343 | const files = await walkDir( |
| 344 | (await testIde.getWorkspaceDirs())[0], |
| 345 | testIde, |
| 346 | { |
| 347 | returnRelativeUrisPaths: true, |
| 348 | }, |
| 349 | ); |
| 350 | |
| 351 | expect(files).toContain("normal/file.txt"); |
| 352 | expect(files).not.toContain(".git/config"); |
| 353 | expect(files).not.toContain("node_modules/package/index.js"); |
| 354 | expect(files).not.toContain("coverage/lcov.info"); |
| 355 | expect(files).not.toContain("nested/node_modules/package/index.js"); |
| 356 | expect(files).not.toContain("nested/dist/bundle.js"); |
| 357 | expect(files).not.toContain("nested/coverage/lcov.env"); |
| 358 | }); |
| 359 | |
| 360 | it("should handle directory-specific ignore patterns correctly", async () => { |
| 361 | addToTestDir([ |
| 362 | [".gitignore", "/abc"], |
| 363 | ["abc", "content"], |
| 364 | "xyz/", |
| 365 | ["xyz/.gitignore", "xyz"], |
| 366 | ["xyz/abc", "content"], |
| 367 | ["xyz/xyz", "content"], |
| 368 | ["xyz/normal.txt", "content"], |
| 369 | ]); |
| 370 | |
| 371 | const files = await walkDir( |
| 372 | (await testIde.getWorkspaceDirs())[0], |
| 373 | testIde, |
| 374 | { |
| 375 | returnRelativeUrisPaths: true, |
| 376 | }, |
| 377 | ); |
| 378 | |
| 379 | expect(files).not.toContain("abc"); |
| 380 | expect(files).toContain("xyz/abc"); |
| 381 | expect(files).not.toContain("xyz/xyz"); |
| 382 | expect(files).toContain("xyz/normal.txt"); |
| 383 | }); |
| 384 | }); |
| 385 | }); |