Commit b902853e authored by David Sveningsson's avatar David Sveningsson
Browse files

fix(cli): `expandFiles` path normalization for windows

parent e2c062ac
Pipeline #147325255 passed with stages
in 11 minutes and 40 seconds
import path from "path";
import minimatch from "minimatch";
const glob: any = jest.requireActual("glob");
......@@ -9,7 +10,7 @@ interface Options {
let mockFiles: string[] = null;
function setMockFiles(files: string[]): void {
mockFiles = files;
mockFiles = files.map((cur) => path.normalize(cur));
}
function resetMock(): void {
......@@ -25,7 +26,10 @@ function syncMock(pattern: string, options: Options = {}): string[] {
/* slice the cwd away since it is prepended automatically and makes it harder
* to test with */
const cwd = options.cwd || "";
const dir = cwd.replace(process.cwd(), "").replace(/^\/(.*)/, "$1/");
const dir = cwd
.replace(process.cwd(), "")
.replace("\\", "/")
.replace(/^\/(.*)/, `$1${path.sep}`);
let src = mockFiles;
if (dir) {
......
......@@ -2,6 +2,7 @@ jest.mock("fs");
jest.mock("glob");
import fs from "fs";
import path from "path";
import glob from "glob";
import { CLI } from "./cli";
......@@ -41,9 +42,9 @@ describe("expandFiles()", () => {
expect.assertions(3);
const spy = jest.spyOn(glob, "sync");
expect(cli.expandFiles(["foo.html", "bar/**/*.html"])).toEqual([
"foo.html",
"bar/fred.html",
"bar/barney.html",
path.normalize("foo.html"),
path.normalize("bar/fred.html"),
path.normalize("bar/barney.html"),
]);
expect(spy).toHaveBeenCalledWith("foo.html", expect.anything());
expect(spy).toHaveBeenCalledWith("bar/**/*.html", expect.anything());
......@@ -52,26 +53,26 @@ describe("expandFiles()", () => {
it("should expand directories (default extensions)", () => {
expect.assertions(1);
expect(cli.expandFiles(["bar"])).toEqual([
"bar/fred.html",
"bar/barney.html",
path.normalize("bar/fred.html"),
path.normalize("bar/barney.html"),
]);
});
it("should expand directories (explicit extensions)", () => {
expect.assertions(1);
expect(cli.expandFiles(["bar"], { extensions: ["js", "json"] })).toEqual([
"bar/fred.json",
"bar/barney.js",
path.normalize("bar/fred.json"),
path.normalize("bar/barney.js"),
]);
});
it("should expand directories (no extensions => all files)", () => {
expect.assertions(1);
expect(cli.expandFiles(["bar"], { extensions: [] })).toEqual([
"bar/fred.html",
"bar/fred.json",
"bar/barney.html",
"bar/barney.js",
path.normalize("bar/fred.html"),
path.normalize("bar/fred.json"),
path.normalize("bar/barney.html"),
path.normalize("bar/barney.js"),
]);
});
......
......@@ -27,9 +27,9 @@ function directoryPattern(extensions: string[]): string {
case 0:
return "**";
case 1:
return `**/*.${extensions[0]}`;
return path.join("**", `*.${extensions[0]}`);
default:
return `**/*.{${extensions.join(",")}}`;
return path.join("**", `*.{${extensions.join(",")}}`);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment