Skip to content

Commit

Permalink
fix: support optional options in getKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j committed Jan 2, 2025
1 parent f688562 commit f42bccc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/drivers/fs-lite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export default defineDriver((opts: FSStorageOptions = {}) => {
}
return unlink(r(key));
},
getKeys(_base, { maxDepth }) {
return readdirRecursive(r("."), opts.ignore, maxDepth);
getKeys(_base, topts) {
return readdirRecursive(r("."), opts.ignore, topts?.maxDepth);
},
async clear() {
if (opts.readOnly || opts.noClear) {
Expand Down
8 changes: 6 additions & 2 deletions src/drivers/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ export default defineDriver((opts: FSStorageOptions = {}) => {
}
return unlink(r(key));
},
getKeys(_base, { maxDepth }) {
return readdirRecursive(r("."), anymatch(opts.ignore || []), maxDepth);
getKeys(_base, topts) {
return readdirRecursive(
r("."),
anymatch(opts.ignore || []),
topts?.maxDepth
);
},
async clear() {
if (opts.readOnly || opts.noClear) {
Expand Down
53 changes: 29 additions & 24 deletions test/drivers/fs-lite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,37 @@ describe("drivers: fs-lite", () => {
expect(await ctx.storage.getItem("s1/te..st..js")).toBe("ok");
});

it("supports depth in getKeys", async () => {
await ctx.storage.setItem("depth-test/file0.md", "boop");
await ctx.storage.setItem("depth-test/depth0/file1.md", "boop");
await ctx.storage.setItem("depth-test/depth0/depth1/file2.md", "boop");
it("natively supports maxDepth in getKeys", async () => {
await ctx.storage.setItem("file0.md", "boop");
await ctx.storage.setItem("depth-test/file1.md", "boop");
await ctx.storage.setItem("depth-test/depth0/file2.md", "boop");
await ctx.storage.setItem("depth-test/depth0/depth1/file3.md", "boop");
await ctx.storage.setItem("depth-test/depth0/depth1/file4.md", "boop");

const depth1Result = await ctx.storage.getKeys(undefined, {
depth: 1,
});
const depth2Result = await ctx.storage.getKeys(undefined, {
depth: 2,
});

expect(depth1Result).includes.members(["depth-test:file0.md"]);
expect(depth1Result).not.include.members([
"depth-test:depth0:file1.md",
"depth-test:depth0:depth1:file2.md",
"depth-test:depth0:depth1:file3.md",
]);
expect(depth2Result).includes.members([
"depth-test:file0.md",
"depth-test:depth0:file1.md",
]);
expect(depth2Result).not.include.members([
"depth-test:depth0:depth1:file2.md",
"depth-test:depth0:depth1:file3.md",
expect(
(
await ctx.driver.getKeys("", {
maxDepth: 0,
})
).sort()
).toMatchObject(["file0.md"]);
expect(
(
await ctx.driver.getKeys("", {
maxDepth: 1,
})
).sort()
).toMatchObject(["depth-test/file1.md", "file0.md"]);
expect(
(
await ctx.driver.getKeys("", {
maxDepth: 2,
})
).sort()
).toMatchObject([
"depth-test/depth0/file2.md",
"depth-test/file1.md",
"file0.md",
]);
});
},
Expand Down
20 changes: 4 additions & 16 deletions test/drivers/fs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, it, expect, vi } from "vitest";
import { resolve } from "node:path";
import { stat } from "node:fs/promises";
import { readFile, writeFile } from "../../src/drivers/utils/node-fs";
import { testDriver } from "./utils";
import driver from "../../src/drivers/fs";
Expand Down Expand Up @@ -48,32 +47,21 @@ describe("drivers: fs", () => {
await ctx.storage.setItem("depth-test/depth0/depth1/file2.md", "boop");
await ctx.storage.setItem("depth-test/depth0/depth1/file3.md", "boop");

const originalStat = await stat(
resolve(dir, "depth-test/depth0/file1.md")
);

expect(
(
await ctx.storage.getKeys(undefined, {
await ctx.driver.getKeys("", {
maxDepth: 1,
})
).sort()
).toMatchObject(["depth-test:file0.md"]);

const newStat = await stat(resolve(dir, "depth-test/depth0/file1.md"));

// assert that the driver didn't access `file1.md`
// this tells us that the native filtering worked, rather than
// the higher level filter catching it
expect(originalStat.atime).toEqual(newStat.atime);
).toMatchObject(["depth-test/file0.md"]);

expect(
(
await ctx.storage.getKeys(undefined, {
await ctx.driver.getKeys("", {
maxDepth: 2,
})
).sort()
).toMatchObject(["depth-test:depth0:file1.md", "depth-test:file0.md"]);
).toMatchObject(["depth-test/depth0/file1.md", "depth-test/file0.md"]);
});
},
});
Expand Down

0 comments on commit f42bccc

Please sign in to comment.