Skip to content

Commit 6d9374c

Browse files
committed
[INTERNAL] Memory Adapter: Fix directory globbing
1 parent afbcc3f commit 6d9374c

File tree

5 files changed

+539
-264
lines changed

5 files changed

+539
-264
lines changed

lib/adapters/Memory.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,25 @@ class Memory extends AbstractAdapter {
5050
}
5151

5252
const filePaths = Object.keys(this._virFiles);
53-
let matchedResources = micromatch(filePaths, patterns, {
53+
const matchedFilePaths = micromatch(filePaths, patterns, {
5454
dot: true
5555
});
56+
let matchedResources = matchedFilePaths.map((virPath) => {
57+
return this._virFiles[virPath];
58+
});
5659

5760
if (!options.nodir) {
5861
// TODO: Add tests for all this
5962
const dirPaths = Object.keys(this._virDirs);
6063
const matchedDirs = micromatch(dirPaths, patterns, {
6164
dot: true
6265
});
63-
matchedResources = matchedResources.concat(matchedDirs);
66+
matchedResources = matchedResources.concat(matchedDirs.map((virPath) => {
67+
return this._virDirs[virPath];
68+
}));
6469
}
6570

66-
return Promise.resolve(matchedResources.map((virPath) => {
67-
return this._virFiles[virPath];
68-
}));
71+
return Promise.resolve(matchedResources);
6972
}
7073

7174
/**
@@ -117,9 +120,9 @@ class Memory extends AbstractAdapter {
117120
pathSegments.pop(); // Remove last segment representing the resource itself
118121

119122
pathSegments.forEach((segment, i) => {
120-
segment = "/" + segment;
121-
if (i > 1) {
122-
segment = pathSegments[i - 1] + segment;
123+
// segment = "/" + segment;
124+
if (i >= 1) {
125+
segment = pathSegments[i - 1] + "/" + segment;
123126
}
124127
pathSegments[i] = segment;
125128
});
@@ -134,7 +137,7 @@ class Memory extends AbstractAdapter {
134137
return true;
135138
}
136139
},
137-
path: segment
140+
path: this._virBasePath + segment
138141
});
139142
}
140143
}

test/lib/adapters/FileSystem.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const {test} = require("ava");
2+
const {resourceFactory} = require("../../../");
3+
4+
test("GLOB resources from application.a w/ virtual base path prefix", async (t) => {
5+
const readerWriter = resourceFactory.createAdapter({
6+
fsBasePath: "./test/fixtures/application.a/webapp",
7+
virBasePath: "/app/"
8+
});
9+
10+
const resources = await readerWriter.byGlob("/app/**/*.html");
11+
t.deepEqual(resources.length, 1, "Found exactly one resource");
12+
});
13+
14+
test("GLOB resources from application.a w/o virtual base path prefix", async (t) => {
15+
const readerWriter = resourceFactory.createAdapter({
16+
fsBasePath: "./test/fixtures/application.a/webapp",
17+
virBasePath: "/app/"
18+
});
19+
20+
const resources = await readerWriter.byGlob("/**/*.html");
21+
t.deepEqual(resources.length, 1, "Found exactly one resource");
22+
});
23+
24+
25+
test("GLOB virtual directory w/o virtual base path prefix", async (t) => {
26+
const readerWriter = resourceFactory.createAdapter({
27+
fsBasePath: "./test/fixtures/application.a/webapp",
28+
virBasePath: "/app/"
29+
});
30+
31+
const resources = await readerWriter.byGlob("/*", {nodir: false});
32+
t.deepEqual(resources.length, 1, "Found exactly one resource");
33+
});
34+
35+
test("GLOB virtual directory w/ virtual base path prefix", async (t) => {
36+
const readerWriter = resourceFactory.createAdapter({
37+
fsBasePath: "./test/fixtures/application.a/webapp",
38+
virBasePath: "/app/one/two/"
39+
});
40+
41+
const resources = await readerWriter.byGlob("/app/*", {nodir: false});
42+
t.deepEqual(resources.length, 1, "Found exactly one resource");
43+
});
44+
45+
test("GLOB virtual directory w/o virtual base path prefix and nodir: true", async (t) => {
46+
const readerWriter = resourceFactory.createAdapter({
47+
fsBasePath: "./test/fixtures/application.a/webapp",
48+
virBasePath: "/app/"
49+
});
50+
51+
const resources = await readerWriter.byGlob("/*", {nodir: true});
52+
t.deepEqual(resources.length, 0, "Found no resources");
53+
});
54+
55+
test("GLOB virtual directory w/ virtual base path prefix and nodir: true", async (t) => {
56+
const readerWriter = resourceFactory.createAdapter({
57+
fsBasePath: "./test/fixtures/application.a/webapp",
58+
virBasePath: "/app/one/two/"
59+
});
60+
61+
const resources = await readerWriter.byGlob("/app/*", {nodir: true});
62+
t.deepEqual(resources.length, 0, "Found no resources");
63+
});

0 commit comments

Comments
 (0)