Commits (9)
# html-validate changelog
## [5.4.0](https://gitlab.com/html-validate/html-validate/compare/v5.3.0...v5.4.0) (2021-08-27)
### Features
- **cli:** expand relative paths to absolute paths ([bdc3019](https://gitlab.com/html-validate/html-validate/commit/bdc30197be25c031999e156fd7dcb4f456fc7f0e)), closes [#131](https://gitlab.com/html-validate/html-validate/issues/131)
### Bug Fixes
- **cli:** handle absolute paths as input filenames ([c860af6](https://gitlab.com/html-validate/html-validate/commit/c860af66d02bf46e24a11756d5611b4c9b449ccb)), closes [#131](https://gitlab.com/html-validate/html-validate/issues/131)
## [5.3.0](https://gitlab.com/html-validate/html-validate/compare/v5.2.1...v5.3.0) (2021-08-23)
### Features
......
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"name": "html-validate",
"version": "5.3.0",
"version": "5.4.0",
"description": "Offline html5 validator",
"keywords": [
"html",
......@@ -192,7 +192,7 @@
"grunt-contrib-copy": "1.0.0",
"grunt-sass": "3.1.0",
"highlight.js": "11.2.0",
"husky": "7.0.1",
"husky": "7.0.2",
"jest": "27.0.6",
"jest-diff": "27.0.6",
"jquery": "3.6.0",
......@@ -203,11 +203,11 @@
"npm-pkg-lint": "1.4.0",
"postcss": "8.3.6",
"prettier": "2.3.2",
"rollup": "2.56.2",
"rollup": "2.56.3",
"rollup-plugin-copy": "3.4.0",
"rollup-plugin-dts": "3.0.2",
"sass": "1.38.0",
"semantic-release": "17.4.5",
"sass": "1.38.1",
"semantic-release": "17.4.7",
"serve-static": "1.14.1",
"stringmap": "0.2.2",
"ts-jest": "27.0.5",
......
......@@ -32,7 +32,10 @@ export class CLI {
}
/**
* Returns list of files matching patterns and are not ignored.
* Returns list of files matching patterns and are not ignored. Filenames will
* have absolute paths.
*
* @public
*/
public expandFiles(patterns: string[], options: ExpandOptions = {}): string[] {
return expandFiles(patterns, options).filter((filename) => !this.isIgnored(filename));
......
......@@ -15,7 +15,12 @@ declare module "glob" {
function resetMock(): void;
}
function resolve(cwd: string, filePath: string): string {
return path.normalize(path.join(cwd, filePath));
}
let cli: CLI;
const cwd = path.join(__dirname, "../..");
beforeEach(() => {
fs.mockReset();
......@@ -32,46 +37,61 @@ beforeEach(() => {
describe("expandFiles()", () => {
it("should expand globs", () => {
expect.assertions(1);
expect(cli.expandFiles(["foo.html", "bar/**/*.html"])).toEqual([
path.normalize("foo.html"),
path.normalize("bar/barney.html"),
path.normalize("bar/fred.html"),
expect(cli.expandFiles(["foo.html", "bar/**/*.html"], { cwd })).toEqual([
resolve(cwd, "foo.html"),
resolve(cwd, "bar/barney.html"),
resolve(cwd, "bar/fred.html"),
]);
});
it("should expand directories (default extensions)", () => {
expect.assertions(1);
expect(cli.expandFiles(["bar"])).toEqual([
path.normalize("bar/barney.html"),
path.normalize("bar/fred.html"),
expect(cli.expandFiles(["bar"], { cwd })).toEqual([
resolve(cwd, "bar/barney.html"),
resolve(cwd, "bar/fred.html"),
]);
});
it("should expand directories (explicit extensions)", () => {
expect.assertions(1);
expect(cli.expandFiles(["bar"], { extensions: ["js", "json"] })).toEqual([
path.normalize("bar/barney.js"),
path.normalize("bar/fred.json"),
resolve(cwd, "bar/barney.js"),
resolve(cwd, "bar/fred.json"),
]);
});
it("should expand directories (no extensions => all files)", () => {
expect.assertions(1);
expect(cli.expandFiles(["bar"], { extensions: [] })).toEqual([
path.normalize("bar/barney.html"),
path.normalize("bar/barney.js"),
path.normalize("bar/fred.html"),
path.normalize("bar/fred.json"),
expect(cli.expandFiles(["bar"], { extensions: [], cwd })).toEqual([
resolve(cwd, "bar/barney.html"),
resolve(cwd, "bar/barney.js"),
resolve(cwd, "bar/fred.html"),
resolve(cwd, "bar/fred.json"),
]);
});
it("should handle absolute paths", () => {
expect.assertions(1);
const patterns = [path.join(cwd, "foo.html"), path.join(cwd, "bar")];
expect(cli.expandFiles(patterns, { cwd })).toEqual([
resolve(cwd, "foo.html"),
resolve(cwd, "bar/barney.html"),
resolve(cwd, "bar/fred.html"),
]);
});
it("should remove duplicates", () => {
expect.assertions(1);
expect(cli.expandFiles(["foo.html", "foo.html"])).toEqual(["foo.html"]);
expect(cli.expandFiles(["foo.html", "foo.html"], { cwd })).toEqual([resolve(cwd, "foo.html")]);
});
it("should fallback on process.cwd()", () => {
expect.assertions(1);
expect(cli.expandFiles(["foo.html"])).toEqual([resolve(process.cwd(), "foo.html")]);
});
it("should replace - placeholder", () => {
expect.assertions(1);
expect(cli.expandFiles(["-"])).toEqual(["/dev/stdin"]);
expect(cli.expandFiles(["-"], { cwd })).toEqual(["/dev/stdin"]);
});
});
......@@ -22,6 +22,14 @@ function isDirectory(filename: string): boolean {
return st.isDirectory();
}
function join(stem: string, filename: string): string {
if (path.isAbsolute(filename)) {
return path.normalize(filename);
} else {
return path.normalize(path.join(stem, filename));
}
}
function directoryPattern(extensions: string[]): string {
switch (extensions.length) {
case 0:
......@@ -50,14 +58,14 @@ export function expandFiles(patterns: string[], options: ExpandOptions): string[
for (const filename of glob.sync(pattern, { cwd })) {
/* if file is a directory recursively expand files from it */
const fullpath = path.join(cwd, filename);
const fullpath = join(cwd, filename);
if (isDirectory(fullpath)) {
const dir = expandFiles([directoryPattern(extensions)], { ...options, cwd: fullpath });
result = result.concat(dir.map((cur) => path.join(filename, cur)));
result = result.concat(dir.map((cur) => join(filename, cur)));
continue;
}
result.push(filename);
result.push(fullpath);
}
return result;
......
......@@ -60,12 +60,13 @@ describe("integration tests", () => {
it("expandFiles() should only return files not ignored", () => {
expect.assertions(1);
const cwd = path.join(__dirname, "../..");
const cli = new CLI();
const files = cli.expandFiles(["test-files/ignored"], { extensions: ["html", "vue"] });
const files = cli.expandFiles(["test-files/ignored"], { extensions: ["html", "vue"], cwd });
expect(files).toEqual([
"test-files/ignored/bar.html",
"test-files/ignored/included/file.html",
"test-files/ignored/subdir/foo.vue",
path.join(cwd, "test-files/ignored/bar.html"),
path.join(cwd, "test-files/ignored/included/file.html"),
path.join(cwd, "test-files/ignored/subdir/foo.vue"),
]);
});
......