From 9d64aa282f332922c233403cf05dc339f9d9df6a Mon Sep 17 00:00:00 2001 From: Benjamin Chauvette Date: Mon, 4 Mar 2019 19:28:08 -0500 Subject: [PATCH 1/2] Fix: Empty glob pattern incorrectly expands to "/**" --- lib/util/glob-utils.js | 4 +++- tests/lib/util/glob-utils.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/util/glob-utils.js b/lib/util/glob-utils.js index b05c354439d..e4b9f147974 100644 --- a/lib/util/glob-utils.js +++ b/lib/util/glob-utils.js @@ -166,7 +166,9 @@ function resolveFileGlobPatterns(patterns, options) { const processPathExtensions = processPath(options); - return patterns.map(processPathExtensions); + return patterns + .filter(pattern => pattern.length > 0) + .map(processPathExtensions); } const dotfilesPattern = /(?:(?:^\.)|(?:[/\\]\.))[^/\\.].*/u; diff --git a/tests/lib/util/glob-utils.js b/tests/lib/util/glob-utils.js index 6b61f8b4061..fb27ca37fe0 100644 --- a/tests/lib/util/glob-utils.js +++ b/tests/lib/util/glob-utils.js @@ -69,6 +69,16 @@ describe("globUtils", () => { assert.deepStrictEqual(result, ["one-js-file"]); }); + it("should not convert empty path string to a glob pattern", () => { + const patterns = [""]; + const opts = { + cwd: getFixturePath("glob-util") + }; + const result = globUtils.resolveFileGlobPatterns(patterns, opts); + + assert.deepStrictEqual(result, []); + }); + it("should convert an absolute directory name with no provided extensions into a posix glob pattern", () => { const patterns = [getFixturePath("glob-util", "one-js-file")]; const opts = { From c0638304b647c9d6d7508beb0cc677aa61fc661d Mon Sep 17 00:00:00 2001 From: Benjamin Chauvette Date: Mon, 4 Mar 2019 22:06:30 -0500 Subject: [PATCH 2/2] Chore: Move empty pattern ignoring to listFilesToProcess It didn't work inside resolveFileGlobPatterns because that function needs to return an array of the same length as its input (15d77bd). --- lib/util/glob-utils.js | 17 +++++++++++++---- tests/lib/util/glob-utils.js | 17 +++++++---------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/util/glob-utils.js b/lib/util/glob-utils.js index e4b9f147974..33cb8e7c885 100644 --- a/lib/util/glob-utils.js +++ b/lib/util/glob-utils.js @@ -70,6 +70,10 @@ function processPath(options) { * @private */ return function(pathname) { + if (pathname === "") { + return ""; + } + let newPath = pathname; const resolvedPath = path.resolve(cwd, pathname); @@ -166,9 +170,7 @@ function resolveFileGlobPatterns(patterns, options) { const processPathExtensions = processPath(options); - return patterns - .filter(pattern => pattern.length > 0) - .map(processPathExtensions); + return patterns.map(processPathExtensions); } const dotfilesPattern = /(?:(?:^\.)|(?:[/\\]\.))[^/\\.].*/u; @@ -203,6 +205,13 @@ function listFilesToProcess(globPatterns, providedOptions) { debug("Creating list of files to process."); const resolvedPathsByGlobPattern = resolvedGlobPatterns.map(pattern => { + if (pattern === "") { + return [{ + filename: "", + behavior: SILENTLY_IGNORE + }]; + } + const file = path.resolve(cwd, pattern); if (options.globInputPaths === false || (fs.existsSync(file) && fs.statSync(file).isFile())) { @@ -242,7 +251,7 @@ function listFilesToProcess(globPatterns, providedOptions) { }); const allPathDescriptors = resolvedPathsByGlobPattern.reduce((pathsForAllGlobs, pathsForCurrentGlob, index) => { - if (pathsForCurrentGlob.every(pathDescriptor => pathDescriptor.behavior === SILENTLY_IGNORE)) { + if (pathsForCurrentGlob.every(pathDescriptor => pathDescriptor.behavior === SILENTLY_IGNORE && pathDescriptor.filename !== "")) { throw new (pathsForCurrentGlob.length ? AllFilesIgnoredError : NoFilesFoundError)(globPatterns[index]); } diff --git a/tests/lib/util/glob-utils.js b/tests/lib/util/glob-utils.js index fb27ca37fe0..9bafcfa0779 100644 --- a/tests/lib/util/glob-utils.js +++ b/tests/lib/util/glob-utils.js @@ -69,16 +69,6 @@ describe("globUtils", () => { assert.deepStrictEqual(result, ["one-js-file"]); }); - it("should not convert empty path string to a glob pattern", () => { - const patterns = [""]; - const opts = { - cwd: getFixturePath("glob-util") - }; - const result = globUtils.resolveFileGlobPatterns(patterns, opts); - - assert.deepStrictEqual(result, []); - }); - it("should convert an absolute directory name with no provided extensions into a posix glob pattern", () => { const patterns = [getFixturePath("glob-util", "one-js-file")]; const opts = { @@ -339,6 +329,13 @@ describe("globUtils", () => { }, `No files matching '${patterns[0]}' were found.`); }); + it("should ignore empty patterns", () => { + const patterns = [""]; + const result = globUtils.listFilesToProcess(patterns); + + assert.deepStrictEqual(result, []); + }); + it("should return an ignored file, if ignore option is turned off", () => { const options = { ignore: false }; const patterns = [getFixturePath("glob-util", "ignored", "**/*.js")];