Skip to content

Commit 5f5f1c2

Browse files
committed
Add simple 100% test coverage for fs-virtual.js.
1 parent 1e6a84a commit 5f5f1c2

File tree

3 files changed

+97
-8
lines changed

3 files changed

+97
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"lint-dockerfile": "docker run --rm -i hadolint/hadolint:latest-alpine < docker/Dockerfile",
3636
"lint-watch": "git ls-files | entr npm run lint",
3737
"schema": "cpy ./node_modules/markdownlint/schema/markdownlint-config-schema.json ./schema --flat",
38-
"test": "ava --timeout=1m test/append-to-array-test.js test/fs-mock-test.js test/markdownlint-cli2-test.js test/markdownlint-cli2-test-exec.js test/markdownlint-cli2-test-exports.js test/markdownlint-cli2-test-fs.js test/markdownlint-cli2-test-main.js test/merge-options-test.js test/resolve-and-require-test.js",
38+
"test": "ava --timeout=1m test/append-to-array-test.js test/fs-mock-test.js test/fs-virtual-test.js test/markdownlint-cli2-test.js test/markdownlint-cli2-test-exec.js test/markdownlint-cli2-test-exports.js test/markdownlint-cli2-test-fs.js test/markdownlint-cli2-test-main.js test/merge-options-test.js test/resolve-and-require-test.js",
3939
"test-cover": "c8 --100 npm test",
4040
"test-docker-hub-image": "VERSION=$(node -e \"process.stdout.write(require('./package.json').version)\") && docker image rm davidanson/markdownlint-cli2:v$VERSION davidanson/markdownlint-cli2:latest || true && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2:v$VERSION \"*.md\" && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2:latest \"*.md\"",
4141
"test-docker-hub-image-rules": "VERSION=$(node -e \"process.stdout.write(require('./package.json').version)\") && docker image rm davidanson/markdownlint-cli2-rules:v$VERSION davidanson/markdownlint-cli2-rules:latest || true && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2-rules:v$VERSION \"*.md\" && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2-rules:latest \"*.md\"",

test/fs-virtual-test.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// @ts-check
2+
3+
"use strict";
4+
5+
const path = require("node:path");
6+
const { promisify } = require("node:util");
7+
const test = require("ava").default;
8+
const FsVirtual = require("../webworker/fs-virtual");
9+
10+
const mockPath = "/mock";
11+
const thisFile = path.basename(__filename);
12+
const testFile = path.join(mockPath, thisFile);
13+
const missingFile = `${mockPath}/missing`;
14+
15+
const virtualFiles = [
16+
[ "/mock/fs-virtual-test.js", "// content" ]
17+
];
18+
19+
test("fsVirtual.stat", async (t) => {
20+
t.plan(1);
21+
const fs = new FsVirtual(virtualFiles);
22+
const fsStat = promisify(fs.stat);
23+
// @ts-ignore
24+
const stat = await fsStat(testFile);
25+
t.truthy(stat);
26+
});
27+
28+
test("fsVirtual.lstat", async (t) => {
29+
t.plan(10);
30+
const fs = new FsVirtual(virtualFiles);
31+
const fsLstat = promisify(fs.lstat);
32+
// @ts-ignore
33+
const stat = await fsLstat(testFile);
34+
t.truthy(stat);
35+
t.false(stat.isBlockDevice());
36+
t.false(stat.isCharacterDevice());
37+
t.false(stat.isDirectory());
38+
t.false(stat.isFIFO());
39+
t.true(stat.isFile());
40+
t.false(stat.isSocket());
41+
t.false(stat.isSymbolicLink());
42+
// @ts-ignore
43+
const missingStat = await fsLstat(missingFile);
44+
t.truthy(missingStat);
45+
t.true(missingStat.isDirectory());
46+
});
47+
48+
test("fsVirtual.readdir", async (t) => {
49+
t.plan(3);
50+
const fs = new FsVirtual(virtualFiles);
51+
const fsReaddir = promisify(fs.readdir);
52+
// @ts-ignore
53+
const files = await fsReaddir(`${mockPath}/`);
54+
t.true(Array.isArray(files));
55+
t.true(files.length > 0);
56+
t.true(files.includes(thisFile));
57+
});
58+
59+
test("fsVirtual.*", async (t) => {
60+
t.plan(3);
61+
const fs = new FsVirtual(virtualFiles);
62+
const fsAccess = promisify(fs.access);
63+
// @ts-ignore
64+
await fsAccess(testFile);
65+
const fsLstat = promisify(fs.lstat);
66+
// @ts-ignore
67+
await fsLstat(testFile);
68+
const fsStat = promisify(fs.lstat);
69+
// @ts-ignore
70+
await fsStat(testFile);
71+
const fsReadFile = promisify(fs.readFile);
72+
// @ts-ignore
73+
const content = await fsReadFile(testFile, "utf8");
74+
t.true(content.length > 0);
75+
// @ts-ignore
76+
await t.throwsAsync(() => fsAccess(missingFile));
77+
// @ts-ignore
78+
await t.throwsAsync(() => fsReadFile(missingFile));
79+
});
80+
81+
test("fsVirtual.promises.*", async (t) => {
82+
t.plan(3);
83+
const fs = new FsVirtual(virtualFiles);
84+
const tempName = "fs-mock.tmp";
85+
const tempFile = path.join(mockPath, tempName);
86+
await t.throwsAsync(() => fs.promises.access(tempFile));
87+
await fs.promises.writeFile(tempFile, tempFile, "utf8");
88+
await fs.promises.access(tempFile);
89+
await fs.promises.stat(tempFile);
90+
t.is(await fs.promises.readFile(tempFile, "utf8"), tempFile);
91+
await t.throwsAsync(() => fs.promises.readFile(missingFile, "utf8"));
92+
});

webworker/fs-virtual.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
"use strict";
44

5-
/* c8 ignore start */
6-
75
const dirent = (path, directory) => {
86
const name = path.replace(/^.*\//u, "");
97
return {
@@ -67,7 +65,8 @@ class FsVirtual {
6765
return (callback || mode)(new Error(`fs-virtual:access(${path})`));
6866
};
6967

70-
this.lstat = (path, callback) => {
68+
// eslint-disable-next-line no-multi-assign
69+
this.stat = this.lstat = (path, callback) => {
7170
path = normalize(path);
7271
if (this.files.has(path)) {
7372
return callback(null, dirent(path, false));
@@ -79,8 +78,8 @@ class FsVirtual {
7978
path = normalize(path);
8079
const names = [];
8180
for (const file of this.files.keys()) {
82-
if (file.startsWith(path)) {
83-
const name = file.slice(path.length).replace(/\/.*$/u, "");
81+
if (file.startsWith(`${path}`)) {
82+
const [ name ] = file.slice(path.length).split("/");
8483
if (!names.includes(name)) {
8584
names.push(name);
8685
}
@@ -100,8 +99,6 @@ class FsVirtual {
10099
}
101100
}
102101

103-
/* c8 ignore stop */
104-
105102
if (typeof module !== "undefined") {
106103
module.exports = FsVirtual;
107104
}

0 commit comments

Comments
 (0)