Skip to content

Commit

Permalink
fix: do not return an empty string for the root
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev authored and thecodrr committed Sep 29, 2024
1 parent bcff43f commit 40e0f75
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
75 changes: 75 additions & 0 deletions __tests__/fdir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]();
Expand Down Expand Up @@ -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()
Expand All @@ -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\\"
Expand Down
9 changes: 5 additions & 4 deletions src/api/functions/push-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ export type PushDirectoryFunction = (

function pushDirectoryWithRelativePath(root: string): PushDirectoryFunction {
return function (directoryPath, paths) {
paths.push((directoryPath || ".").substring(root.length));
paths.push(directoryPath.substring(root.length) || ".");
};
}

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);
}
Expand All @@ -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);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class Builder<
return this as Builder<OnlyCountsOutput, TGlobFunction>;
}

crawl(root: string) {
crawl(root?: string) {
return new APIBuilder<TReturnType>(root || ".", this.options);
}

Expand Down

0 comments on commit 40e0f75

Please sign in to comment.