Skip to content

Commit

Permalink
[INTERNAL] Memory Adapter: Fix directory globbing
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomByte committed Jan 30, 2019
1 parent afbcc3f commit 6e6b70e
Show file tree
Hide file tree
Showing 5 changed files with 448 additions and 290 deletions.
21 changes: 12 additions & 9 deletions lib/adapters/Memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,25 @@ class Memory extends AbstractAdapter {
}

const filePaths = Object.keys(this._virFiles);
let matchedResources = micromatch(filePaths, patterns, {
const matchedFilePaths = micromatch(filePaths, patterns, {
dot: true
});
let matchedResources = matchedFilePaths.map((virPath) => {
return this._virFiles[virPath];
});

if (!options.nodir) {
// TODO: Add tests for all this
const dirPaths = Object.keys(this._virDirs);
const matchedDirs = micromatch(dirPaths, patterns, {
dot: true
});
matchedResources = matchedResources.concat(matchedDirs);
matchedResources = matchedResources.concat(matchedDirs.map((virPath) => {
return this._virDirs[virPath];
}));
}

return Promise.resolve(matchedResources.map((virPath) => {
return this._virFiles[virPath];
}));
return Promise.resolve(matchedResources);
}

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

pathSegments.forEach((segment, i) => {
segment = "/" + segment;
if (i > 1) {
segment = pathSegments[i - 1] + segment;
// segment = "/" + segment;
if (i >= 1) {
segment = pathSegments[i - 1] + "/" + segment;
}
pathSegments[i] = segment;
});
Expand All @@ -134,7 +137,7 @@ class Memory extends AbstractAdapter {
return true;
}
},
path: segment
path: this._virBasePath + segment
});
}
}
Expand Down
63 changes: 63 additions & 0 deletions test/lib/adapters/FileSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const {test} = require("ava");
const {resourceFactory} = require("../../../");

test("GLOB resources from application.a w/ virtual base path prefix", async (t) => {
const readerWriter = resourceFactory.createAdapter({
fsBasePath: "./test/fixtures/application.a/webapp",
virBasePath: "/app/"
});

const resources = await readerWriter.byGlob("/app/**/*.html");
t.deepEqual(resources.length, 1, "Found exactly one resource");
});

test("GLOB resources from application.a w/o virtual base path prefix", async (t) => {
const readerWriter = resourceFactory.createAdapter({
fsBasePath: "./test/fixtures/application.a/webapp",
virBasePath: "/app/"
});

const resources = await readerWriter.byGlob("/**/*.html");
t.deepEqual(resources.length, 1, "Found exactly one resource");
});


test("GLOB virtual directory w/o virtual base path prefix", async (t) => {
const readerWriter = resourceFactory.createAdapter({
fsBasePath: "./test/fixtures/application.a/webapp",
virBasePath: "/app/"
});

const resources = await readerWriter.byGlob("/*", {nodir: false});
t.deepEqual(resources.length, 1, "Found exactly one resource");
});

test("GLOB virtual directory w/ virtual base path prefix", async (t) => {
const readerWriter = resourceFactory.createAdapter({
fsBasePath: "./test/fixtures/application.a/webapp",
virBasePath: "/app/one/two/"
});

const resources = await readerWriter.byGlob("/app/*", {nodir: false});
t.deepEqual(resources.length, 1, "Found exactly one resource");
});

test("GLOB virtual directory w/o virtual base path prefix and nodir: true", async (t) => {
const readerWriter = resourceFactory.createAdapter({
fsBasePath: "./test/fixtures/application.a/webapp",
virBasePath: "/app/"
});

const resources = await readerWriter.byGlob("/*", {nodir: true});
t.deepEqual(resources.length, 0, "Found no resources");
});

test("GLOB virtual directory w/ virtual base path prefix and nodir: true", async (t) => {
const readerWriter = resourceFactory.createAdapter({
fsBasePath: "./test/fixtures/application.a/webapp",
virBasePath: "/app/one/two/"
});

const resources = await readerWriter.byGlob("/app/*", {nodir: true});
t.deepEqual(resources.length, 0, "Found no resources");
});
Loading

0 comments on commit 6e6b70e

Please sign in to comment.