Skip to content

Commit 20dbbeb

Browse files
committed
fix(tests): properly normalize directory paths in all mock fs implementations
- Add normalized variable to handle '.', 'src', and './src' paths - Replace string replace with regex for proper path normalization - Fixes Jest parse error from previous malformed regex attempt
1 parent b4cb49a commit 20dbbeb

1 file changed

Lines changed: 51 additions & 40 deletions

File tree

__tests__/slop-analyzers.test.js

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,13 +1987,14 @@ module.exports = { helper };
19871987

19881988
// Mock file system
19891989
mockFs.readdirSync.mockImplementation((dir) => {
1990-
const normalizedDir = dir.replace(/^\.\//, '').replace(/\/$/, '');
1991-
if (normalizedDir === '' || normalizedDir === '.') {
1990+
// Normalize path - handle both '.', 'src', and './src'
1991+
const normalized = dir.replace(/^\.\//, '').replace(/\/$/, '') || '.';
1992+
if (normalized === '.') {
19921993
return [
19931994
{ name: 'src', isDirectory: () => true, isFile: () => false }
19941995
];
19951996
}
1996-
if (normalizedDir === 'src') {
1997+
if (normalized === 'src') {
19971998
return [
19981999
{ name: 'app.js', isDirectory: () => false, isFile: () => true },
19992000
{ name: 'utils.js', isDirectory: () => false, isFile: () => true }
@@ -2040,13 +2041,14 @@ module.exports = { getData };
20402041
};
20412042

20422043
mockFs.readdirSync.mockImplementation((dir) => {
2043-
const normalizedDir = dir.replace(/^.//, '').replace(//$/, '');
2044-
if (normalizedDir === '' || normalizedDir === '.') {
2044+
// Normalize path - handle both '.', 'src', and './src'
2045+
const normalized = dir.replace(/^\.\//, '').replace(/\/$/, '') || '.';
2046+
if (normalized === '.') {
20452047
return [
20462048
{ name: 'src', isDirectory: () => true, isFile: () => false }
20472049
];
20482050
}
2049-
if (normalizedDir === 'src') {
2051+
if (normalized === 'src') {
20502052
return [
20512053
{ name: 'app.js', isDirectory: () => false, isFile: () => true }
20522054
];
@@ -2055,7 +2057,7 @@ module.exports = { getData };
20552057
});
20562058

20572059
mockFs.readFileSync.mockImplementation((path) => {
2058-
const file = path.replace(/^.//, '');
2060+
const file = path.replace(/^\.\//, '');
20592061
if (mockFiles[file]) return mockFiles[file];
20602062
throw new Error(`File not found: ${path}`);
20612063
});
@@ -2088,13 +2090,14 @@ module.exports = {};
20882090
};
20892091

20902092
mockFs.readdirSync.mockImplementation((dir) => {
2091-
const normalizedDir = dir.replace(/^.//, '').replace(//$/, '');
2092-
if (normalizedDir === '' || normalizedDir === '.') {
2093+
// Normalize path - handle both '.', 'src', and './src'
2094+
const normalized = dir.replace(/^\.\//, '').replace(/\/$/, '') || '.';
2095+
if (normalized === '.') {
20932096
return [
20942097
{ name: 'src', isDirectory: () => true, isFile: () => false }
20952098
];
20962099
}
2097-
if (normalizedDir === 'src') {
2100+
if (normalized === 'src') {
20982101
return [
20992102
{ name: 'db.js', isDirectory: () => false, isFile: () => true }
21002103
];
@@ -2103,7 +2106,7 @@ module.exports = {};
21032106
});
21042107

21052108
mockFs.readFileSync.mockImplementation((path) => {
2106-
const file = path.replace(/^.//, '');
2109+
const file = path.replace(/^\.\//, '');
21072110
if (mockFiles[file]) return mockFiles[file];
21082111
throw new Error(`File not found: ${path}`);
21092112
});
@@ -2141,8 +2144,9 @@ if __name__ == '__main__':
21412144
};
21422145

21432146
mockFs.readdirSync.mockImplementation((dir) => {
2144-
const normalizedDir = dir.replace(/^.//, '').replace(//$/, '');
2145-
if (normalizedDir === '' || normalizedDir === '.') {
2147+
// Normalize path - handle both '.', 'src', and './src'
2148+
const normalized = dir.replace(/^\.\//, '').replace(/\/$/, '') || '.';
2149+
if (normalized === '.') {
21462150
return [
21472151
{ name: 'app.py', isDirectory: () => false, isFile: () => true }
21482152
];
@@ -2151,7 +2155,7 @@ if __name__ == '__main__':
21512155
});
21522156

21532157
mockFs.readFileSync.mockImplementation((path) => {
2154-
const file = path.replace(/^.//, '');
2158+
const file = path.replace(/^\.\//, '');
21552159
if (mockFiles[file]) return mockFiles[file];
21562160
throw new Error(`File not found: ${path}`);
21572161
});
@@ -2191,8 +2195,9 @@ func main() {
21912195
};
21922196

21932197
mockFs.readdirSync.mockImplementation((dir) => {
2194-
const normalizedDir = dir.replace(/^.//, '').replace(//$/, '');
2195-
if (normalizedDir === '' || normalizedDir === '.') {
2198+
// Normalize path - handle both '.', 'src', and './src'
2199+
const normalized = dir.replace(/^\.\//, '').replace(/\/$/, '') || '.';
2200+
if (normalized === '.') {
21962201
return [
21972202
{ name: 'main.go', isDirectory: () => false, isFile: () => true }
21982203
];
@@ -2201,7 +2206,7 @@ func main() {
22012206
});
22022207

22032208
mockFs.readFileSync.mockImplementation((path) => {
2204-
const file = path.replace(/^.//, '');
2209+
const file = path.replace(/^\.\//, '');
22052210
if (mockFiles[file]) return mockFiles[file];
22062211
throw new Error(`File not found: ${path}`);
22072212
});
@@ -2241,8 +2246,9 @@ func main() {
22412246
};
22422247

22432248
mockFs.readdirSync.mockImplementation((dir) => {
2244-
const normalizedDir = dir.replace(/^.//, '').replace(//$/, '');
2245-
if (normalizedDir === '' || normalizedDir === '.') {
2249+
// Normalize path - handle both '.', 'src', and './src'
2250+
const normalized = dir.replace(/^\.\//, '').replace(/\/$/, '') || '.';
2251+
if (normalized === '.') {
22462252
return [
22472253
{ name: 'main.go', isDirectory: () => false, isFile: () => true }
22482254
];
@@ -2251,7 +2257,7 @@ func main() {
22512257
});
22522258

22532259
mockFs.readFileSync.mockImplementation((path) => {
2254-
const file = path.replace(/^.//, '');
2260+
const file = path.replace(/^\.\//, '');
22552261
if (mockFiles[file]) return mockFiles[file];
22562262
throw new Error(`File not found: ${path}`);
22572263
});
@@ -2283,13 +2289,14 @@ fn main() {
22832289
};
22842290

22852291
mockFs.readdirSync.mockImplementation((dir) => {
2286-
const normalizedDir = dir.replace(/^.//, '').replace(//$/, '');
2287-
if (normalizedDir === '' || normalizedDir === '.') {
2292+
// Normalize path - handle both '.', 'src', and './src'
2293+
const normalized = dir.replace(/^\.\//, '').replace(/\/$/, '') || '.';
2294+
if (normalized === '.') {
22882295
return [
22892296
{ name: 'src', isDirectory: () => true, isFile: () => false }
22902297
];
22912298
}
2292-
if (normalizedDir === 'src') {
2299+
if (normalized === 'src') {
22932300
return [
22942301
{ name: 'main.rs', isDirectory: () => false, isFile: () => true }
22952302
];
@@ -2298,7 +2305,7 @@ fn main() {
22982305
});
22992306

23002307
mockFs.readFileSync.mockImplementation((path) => {
2301-
const file = path.replace(/^.//, '');
2308+
const file = path.replace(/^\.\//, '');
23022309
if (mockFiles[file]) return mockFiles[file];
23032310
throw new Error(`File not found: ${path}`);
23042311
});
@@ -2341,13 +2348,14 @@ module.exports = { getUsers };
23412348
};
23422349

23432350
mockFs.readdirSync.mockImplementation((dir) => {
2344-
const normalizedDir = dir.replace(/^.//, '').replace(//$/, '');
2345-
if (normalizedDir === '' || normalizedDir === '.') {
2351+
// Normalize path - handle both '.', 'src', and './src'
2352+
const normalized = dir.replace(/^\.\//, '').replace(/\/$/, '') || '.';
2353+
if (normalized === '.') {
23462354
return [
23472355
{ name: 'src', isDirectory: () => true, isFile: () => false }
23482356
];
23492357
}
2350-
if (normalizedDir === 'src') {
2358+
if (normalized === 'src') {
23512359
return [
23522360
{ name: 'db.js', isDirectory: () => false, isFile: () => true },
23532361
{ name: 'queries.js', isDirectory: () => false, isFile: () => true }
@@ -2357,7 +2365,7 @@ module.exports = { getUsers };
23572365
});
23582366

23592367
mockFs.readFileSync.mockImplementation((path) => {
2360-
const file = path.replace(/^.//, '');
2368+
const file = path.replace(/^\.\//, '');
23612369
if (mockFiles[file]) return mockFiles[file];
23622370
throw new Error(`File not found: ${path}`);
23632371
});
@@ -2387,13 +2395,14 @@ export const redisClient = redis.createClient({
23872395
};
23882396

23892397
mockFs.readdirSync.mockImplementation((dir) => {
2390-
const normalizedDir = dir.replace(/^.//, '').replace(//$/, '');
2391-
if (normalizedDir === '' || normalizedDir === '.') {
2398+
// Normalize path - handle both '.', 'src', and './src'
2399+
const normalized = dir.replace(/^\.\//, '').replace(/\/$/, '') || '.';
2400+
if (normalized === '.') {
23922401
return [
23932402
{ name: 'src', isDirectory: () => true, isFile: () => false }
23942403
];
23952404
}
2396-
if (normalizedDir === 'src') {
2405+
if (normalized === 'src') {
23972406
return [
23982407
{ name: 'redis.js', isDirectory: () => false, isFile: () => true }
23992408
];
@@ -2402,7 +2411,7 @@ export const redisClient = redis.createClient({
24022411
});
24032412

24042413
mockFs.readFileSync.mockImplementation((path) => {
2405-
const file = path.replace(/^.//, '');
2414+
const file = path.replace(/^\.\//, '');
24062415
if (mockFiles[file]) return mockFiles[file];
24072416
throw new Error(`File not found: ${path}`);
24082417
});
@@ -2430,13 +2439,14 @@ module.exports.pool = pool;
24302439
};
24312440

24322441
mockFs.readdirSync.mockImplementation((dir) => {
2433-
const normalizedDir = dir.replace(/^.//, '').replace(//$/, '');
2434-
if (normalizedDir === '' || normalizedDir === '.') {
2442+
// Normalize path - handle both '.', 'src', and './src'
2443+
const normalized = dir.replace(/^\.\//, '').replace(/\/$/, '') || '.';
2444+
if (normalized === '.') {
24352445
return [
24362446
{ name: 'src', isDirectory: () => true, isFile: () => false }
24372447
];
24382448
}
2439-
if (normalizedDir === 'src') {
2449+
if (normalized === 'src') {
24402450
return [
24412451
{ name: 'db.js', isDirectory: () => false, isFile: () => true }
24422452
];
@@ -2445,7 +2455,7 @@ module.exports.pool = pool;
24452455
});
24462456

24472457
mockFs.readFileSync.mockImplementation((path) => {
2448-
const file = path.replace(/^.//, '');
2458+
const file = path.replace(/^\.\//, '');
24492459
if (mockFiles[file]) return mockFiles[file];
24502460
throw new Error(`File not found: ${path}`);
24512461
});
@@ -2475,13 +2485,14 @@ describe('Redis tests', () => {
24752485
};
24762486

24772487
mockFs.readdirSync.mockImplementation((dir) => {
2478-
const normalizedDir = dir.replace(/^.//, '').replace(//$/, '');
2479-
if (normalizedDir === '' || normalizedDir === '.') {
2488+
// Normalize path - handle both '.', 'src', and './src'
2489+
const normalized = dir.replace(/^\.\//, '').replace(/\/$/, '') || '.';
2490+
if (normalized === '.') {
24802491
return [
24812492
{ name: 'test', isDirectory: () => true, isFile: () => false }
24822493
];
24832494
}
2484-
if (dir === 'test') {
2495+
if (normalized === 'test') {
24852496
return [
24862497
{ name: 'redis.test.js', isDirectory: () => false, isFile: () => true }
24872498
];
@@ -2490,7 +2501,7 @@ describe('Redis tests', () => {
24902501
});
24912502

24922503
mockFs.readFileSync.mockImplementation((path) => {
2493-
const file = path.replace(/^.//, '');
2504+
const file = path.replace(/^\.\//, '');
24942505
if (mockFiles[file]) return mockFiles[file];
24952506
throw new Error(`File not found: ${path}`);
24962507
});

0 commit comments

Comments
 (0)