From 40e0f751193f6978e73438625a580d4d97685b21 Mon Sep 17 00:00:00 2001 From: Superchupu <53496941+SuperchupuDev@users.noreply.github.com> Date: Sat, 28 Sep 2024 01:26:32 +0200 Subject: [PATCH] fix: do not return an empty string for the root --- __tests__/fdir.test.ts | 75 +++++++++++++++++++++++++++++ src/api/functions/push-directory.ts | 9 ++-- src/builder/index.ts | 2 +- 3 files changed, 81 insertions(+), 5 deletions(-) diff --git a/__tests__/fdir.test.ts b/__tests__/fdir.test.ts index 6d9247d..f22a8fe 100644 --- a/__tests__/fdir.test.ts +++ b/__tests__/fdir.test.ts @@ -286,6 +286,68 @@ for (const type of apiTypes) { t.expect(paths.every((p) => !p.startsWith("node_modules"))).toBeTruthy(); }); + test(`[${type}] crawl and return relative paths with only dirs`, async (t) => { + mock({ + "/some/dir/dir1": { + file: "some file", + }, + "/some/dir/dir2": { + file: "some file", + }, + "/some/dir/dir2/dir3": { + file: "some file", + } + }); + + const api = new fdir({ excludeFiles: true, excludeSymlinks: true }) + .withDirs() + .withRelativePaths() + .crawl("/some"); + const paths = await api[type](); + + t.expect(paths.length).toBe(5); + t.expect( + paths.filter((p) => p === ".").length + ).toBe(1); + t.expect( + paths.filter((p) => p === "").length + ).toBe(0); + mock.restore(); + }); + + test(`[${type}] crawl and return relative paths with filters and only dirs`, async (t) => { + mock({ + "/some/dir/dir1": { + file: "some file", + }, + "/some/dir/dir2": { + file: "some file", + }, + "/some/dir/dir2/dir3": { + file: "some file", + } + }); + + const api = new fdir({ excludeFiles: true, excludeSymlinks: true }) + .withDirs() + .withRelativePaths() + .filter((p) => p !== path.join("dir", "dir1/")) + .crawl("/some"); + const paths = await api[type](); + + t.expect(paths.length).toBe(4); + t.expect( + paths.includes(path.join("dir", "dir1/")) + ).toBe(false); + t.expect( + paths.filter((p) => p === ".").length + ).toBe(1); + t.expect( + paths.filter((p) => p === "").length + ).toBe(0); + mock.restore(); + }); + test(`[${type}] crawl and return relative paths that end with /`, async (t) => { const api = new fdir().withRelativePaths().crawl("./node_modules/"); const paths = await api[type](); @@ -559,6 +621,14 @@ test(`paths should never start with ./`, async (t) => { } }); +test(`default to . if root is not provided`, async (t) => { + const files = await new fdir().crawl().withPromise(); + + const files2 = await new fdir().crawl(".").withPromise().then(f => f.sort()); + + t.expect(files.sort().every((r, i) => r === files2[i])).toBe(true); +}); + test(`ignore withRelativePath if root === ./`, async (t) => { const relativeFiles = await new fdir() .withRelativePaths() @@ -581,6 +651,11 @@ test(`there should be no empty directory when using withDirs`, async (t) => { t.expect(files.every((r) => r.length > 0)).toBe(true); }); +test(`there should be no empty directory when using withDirs and filters`, async (t) => { + const files = await new fdir().withDirs().filter(p => p !== "node_modules").crawl("./").withPromise(); + t.expect(files.every((r) => r.length > 0)).toBe(true); +}); + test(`do not convert \\\\ to \\`, async (t) => { t.expect(convertSlashes("\\\\wsl.localhost\\Ubuntu\\home\\", "\\")).toBe( "\\\\wsl.localhost\\Ubuntu\\home\\" diff --git a/src/api/functions/push-directory.ts b/src/api/functions/push-directory.ts index 5aaf565..2de954b 100644 --- a/src/api/functions/push-directory.ts +++ b/src/api/functions/push-directory.ts @@ -8,7 +8,7 @@ export type PushDirectoryFunction = ( function pushDirectoryWithRelativePath(root: string): PushDirectoryFunction { return function (directoryPath, paths) { - paths.push((directoryPath || ".").substring(root.length)); + paths.push(directoryPath.substring(root.length) || "."); }; } @@ -16,7 +16,7 @@ function pushDirectoryFilterWithRelativePath( root: string ): PushDirectoryFunction { return function (directoryPath, paths, filters) { - const relativePath = directoryPath.substring(root.length); + const relativePath = directoryPath.substring(root.length) || "."; if (filters!.every((filter) => filter(relativePath, true))) { paths.push(relativePath); } @@ -32,8 +32,9 @@ const pushDirectoryFilter: PushDirectoryFunction = ( paths, filters ) => { - if (filters!.every((filter) => filter(directoryPath, true))) { - paths.push(directoryPath); + const path = directoryPath || "."; + if (filters!.every((filter) => filter(path, true))) { + paths.push(path); } }; diff --git a/src/builder/index.ts b/src/builder/index.ts index 4d8ffb6..52b0203 100644 --- a/src/builder/index.ts +++ b/src/builder/index.ts @@ -124,7 +124,7 @@ export class Builder< return this as Builder; } - crawl(root: string) { + crawl(root?: string) { return new APIBuilder(root || ".", this.options); }