Skip to content

Commit

Permalink
Fix FS globbing with empty patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomByte committed May 29, 2019
1 parent 5dce36b commit cb1905d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/adapters/FileSystem.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const log = require("@ui5/logger").getLogger("resources:adapters:FileSystem");
const path = require("path");
const fs = require("graceful-fs");
const glob = require("globby");
const globby = require("globby");
const makeDir = require("make-dir");
const {PassThrough} = require("stream");
const Resource = require("../Resource");
Expand Down Expand Up @@ -48,7 +48,7 @@ class FileSystem extends AbstractAdapter {
trace.globCall();

const promises = [];
if (!opt.onlyFiles && patterns[0] === "") { // Match physical root directory
if (!opt.onlyFiles && patterns.includes("")) { // Match physical root directory
promises.push(new Promise((resolve, reject) => {
fs.stat(this._fsBasePath, (err, stat) => {
if (err) {
Expand All @@ -66,10 +66,16 @@ class FileSystem extends AbstractAdapter {
});
}));
}
// Only glob if the only pattern is an empty string.

// Remove empty string glob patterns
// Starting with globby v8 or v9 empty glob patterns "" act like "**"
if (patterns.length > 1 || patterns[0] !== "") {
const matches = await glob(patterns, opt);
// Micromatch throws on empty strings. We just ignore them since they are
// typically caused by our normalization in the AbstractAdapter
const globbyPatterns = patterns.filter((pattern) => {
return pattern !== "";
});
if (globbyPatterns.length > 0) {
const matches = await globby(globbyPatterns, opt);
for (let i = matches.length - 1; i >= 0; i--) {
promises.push(new Promise((resolve, reject) => {
const fsPath = path.join(this._fsBasePath, matches[i]);
Expand Down
26 changes: 26 additions & 0 deletions test/lib/adapters/FileSystem_read.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ test("glob virtual directory w/o virtual base path prefix", async (t) => {
t.deepEqual(resources.length, 1, "Found exactly one resource");
});

test("glob virtual directory w/o virtual base path prefix and multiple patterns", 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",
Expand All @@ -73,6 +86,19 @@ test("glob virtual directory w/o virtual base path prefix and nodir: true", asyn
t.deepEqual(resources.length, 0, "Found no resources");
});

test("glob virtual directory w/o virtual base path prefix and nodir: true and multiple patterns", 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",
Expand Down
25 changes: 25 additions & 0 deletions test/lib/adapters/Memory_read.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ test("glob virtual directory w/o virtual base path prefix", async (t) => {
t.deepEqual(resources.length, 1, "Found exactly one resource");
});

test("glob virtual directory w/o virtual base path prefix and multiple patterns", async (t) => {
// TODO: Add similar test (globbing on empty directory) for FS RL
const readerWriter = resourceFactory.createAdapter({
virBasePath: "/app/one/two"
});

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) => {
// TODO: Add similar test (globbing on empty directory) for FS RL
const readerWriter = resourceFactory.createAdapter({
Expand Down Expand Up @@ -97,6 +110,18 @@ test("glob virtual directory w/ virtual base path prefix and nodir: true", async
t.deepEqual(resources.length, 0, "Found no resources");
});

test("glob virtual directory w/ virtual base path prefix and nodir: true and multiple patterns", async (t) => {
const readerWriter = resourceFactory.createAdapter({
virBasePath: "/app/one/two/"
});

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

/* Load more data from FS into memory */
test("glob all", async (t) => {
const readerWriter = resourceFactory.createAdapter({
Expand Down

0 comments on commit cb1905d

Please sign in to comment.