Skip to content

Commit 92c55d5

Browse files
committed
Fix the MetaDataScanner.test.ts infinite recursion in its mock setup
1 parent 876742a commit 92c55d5

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

src/services/package-manager/__tests__/MetadataScanner.test.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,6 @@ describe("MetadataScanner", () => {
4545

4646
describe("Basic Metadata Scanning", () => {
4747
it("should discover components with English metadata", async () => {
48-
// Mock directory structure
49-
const mockDirents = [
50-
{
51-
name: "component1",
52-
isDirectory: () => true,
53-
isFile: () => false,
54-
},
55-
{
56-
name: "metadata.en.yml",
57-
isDirectory: () => false,
58-
isFile: () => true,
59-
},
60-
] as Dirent[]
61-
62-
// For subdirectories, return empty to prevent infinite recursion
63-
const mockEmptyDirents = [] as Dirent[]
64-
6548
// Setup mock implementations
6649
const mockStats = {
6750
isDirectory: () => true,
@@ -72,14 +55,39 @@ describe("MetadataScanner", () => {
7255
// Mock fs.promises methods using type assertions
7356
const mockedFs = jest.mocked(fs)
7457
mockedFs.stat.mockResolvedValue(mockStats)
75-
;(mockedFs.readdir as any).mockImplementation(async (path: any, options?: any) => {
76-
// Return empty array for nested component1 directories to prevent recursion
77-
if (path.toString().includes("/component1/")) {
78-
return options?.withFileTypes ? mockEmptyDirents : []
58+
59+
// Define specific Dirent objects
60+
const componentDirDirent: Dirent = {
61+
name: "component1",
62+
isDirectory: () => true,
63+
isFile: () => false,
64+
} as Dirent
65+
const metadataFileDirent: Dirent = {
66+
name: "metadata.en.yml",
67+
isDirectory: () => false,
68+
isFile: () => true,
69+
} as Dirent
70+
71+
// Refined mock implementation for fs.readdir
72+
;(mockedFs.readdir as any).mockImplementation(async (p: string, options?: any) => {
73+
const normalizedP = normalizePath(p)
74+
const normalizedBasePath = normalizePath(mockBasePath)
75+
const normalizedComponentPath = normalizePath(path.join(mockBasePath, "component1"))
76+
77+
if (normalizedP === normalizedBasePath) {
78+
// For the base path, return only the component directory
79+
const baseDirents = [componentDirDirent]
80+
return options?.withFileTypes ? baseDirents : baseDirents.map((d) => d.name)
81+
} else if (normalizedP === normalizedComponentPath) {
82+
// For the component1 directory, return only the metadata file
83+
const componentDirents = [metadataFileDirent]
84+
return options?.withFileTypes ? componentDirents : componentDirents.map((d) => d.name)
85+
} else {
86+
// For any other path (deeper recursion), return empty
87+
return options?.withFileTypes ? [] : []
7988
}
80-
// Return full directory listing for base component1 directory
81-
return options?.withFileTypes ? mockDirents : mockDirents.map((d) => d.name)
8289
})
90+
8391
mockedFs.readFile.mockResolvedValue(
8492
Buffer.from(`
8593
name: Test Component

0 commit comments

Comments
 (0)