From 1f62c325c8dee1e9923c7453737731beb74b378c Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Thu, 18 Jul 2024 13:19:48 -0700 Subject: [PATCH] test: add dir read check for walkDir (#1773) * test: add dir read check for walkDir * Update ignore.ts * Update walkDir.test.ts * Update walkDir.test.ts --- core/indexing/ignore.ts | 1 + core/indexing/walkDir.ts | 9 +++------ core/test/walkDir.test.ts | 33 ++++++++++++++++++++++++++++++--- core/util/filesystem.ts | 4 ++-- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/core/indexing/ignore.ts b/core/indexing/ignore.ts index 07e6d873f..35fd84aa1 100644 --- a/core/indexing/ignore.ts +++ b/core/indexing/ignore.ts @@ -64,6 +64,7 @@ export const DEFAULT_IGNORE_FILETYPES = [ ".gitkeep", ".continueignore", "config.json", + ".csv", // "*.prompt", // can be incredibly confusing for the LLM to have another set of instructions injected into the prompt ]; export const defaultIgnoreFile = ignore().add(DEFAULT_IGNORE_FILETYPES); diff --git a/core/indexing/walkDir.ts b/core/indexing/walkDir.ts index 98636b857..7a3c2573e 100644 --- a/core/indexing/walkDir.ts +++ b/core/indexing/walkDir.ts @@ -1,8 +1,8 @@ import { EventEmitter } from "events"; import { Minimatch } from "minimatch"; import path from "node:path"; -import { FileType, IDE } from ".."; -import { DEFAULT_IGNORE_DIRS, DEFAULT_IGNORE_FILETYPES } from "./ignore"; +import { FileType, IDE } from "../index.js"; +import { DEFAULT_IGNORE_DIRS, DEFAULT_IGNORE_FILETYPES } from "./ignore.js"; export interface WalkerOptions { isSymbolicLink?: boolean; @@ -34,10 +34,7 @@ class Walker extends EventEmitter { sawError: boolean; exact: boolean | undefined; onlyDirs: boolean | undefined; - constructor( - opts: WalkerOptions = {}, - protected readonly ide: IDE, - ) { + constructor(opts: WalkerOptions = {}, protected readonly ide: IDE) { super(opts as any); this.isSymbolicLink = opts.isSymbolicLink || false; this.path = opts.path || process.cwd(); diff --git a/core/test/walkDir.test.ts b/core/test/walkDir.test.ts index 5ed3851f0..0f9ad602f 100644 --- a/core/test/walkDir.test.ts +++ b/core/test/walkDir.test.ts @@ -1,12 +1,13 @@ import path from "path"; -import { walkDir, WalkerOptions } from "../indexing/walkDir"; -import FileSystemIde from "../util/filesystem"; +import { walkDir, WalkerOptions } from "../indexing/walkDir.js"; +import FileSystemIde from "../util/filesystem.js"; import { addToTestDir, setUpTestDir, tearDownTestDir, TEST_DIR, -} from "./testUtils/testDir"; +} from "./testUtils/testDir.js"; + const ide = new FileSystemIde(TEST_DIR); async function walkTestDir( @@ -273,4 +274,30 @@ describe("walkDir", () => { ); expect(results.some((file) => file.includes(".tmLanguage"))).toBe(false); }); + + test("should perform the same number of dir reads as 1 + the number of dirs that contain files", async () => { + const files = [ + "a.txt", + "b.py", + "c.ts", + "d/", + "d/e.txt", + "d/f.py", + "d/g/", + "d/g/h.ts", + "d/g/i/", + "d/g/i/j.ts", + ]; + + const numDirs = files.filter((file) => !file.includes(".")).length; + const numDirsPlusTopLevelRead = numDirs + 1; + + addToTestDir(files); + + const mockListDir = jest.spyOn(ide, "listDir"); + + await walkTestDir(); + + expect(mockListDir).toHaveBeenCalledTimes(numDirsPlusTopLevelRead); + }); }); diff --git a/core/util/filesystem.ts b/core/util/filesystem.ts index dd8e152a9..7b849cb33 100644 --- a/core/util/filesystem.ts +++ b/core/util/filesystem.ts @@ -61,8 +61,8 @@ class FileSystemIde implements IDE { dirent.isDirectory() ? (2 as FileType.Directory) : dirent.isSymbolicLink() - ? (64 as FileType.SymbolicLink) - : (1 as FileType.File), + ? (64 as FileType.SymbolicLink) + : (1 as FileType.File), ]); return Promise.resolve(all); }