Skip to content

Commit

Permalink
Merge pull request mozilla#18772 from Snuffleupagus/Node-unit-test-fs…
Browse files Browse the repository at this point in the history
…-promise

Use `fs/promises` in the Node.js unit-tests (PR 17714 follow-up)
  • Loading branch information
timvandermeij authored Sep 22, 2024
2 parents ea2172e + 1a1dfe6 commit ddd7b63
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
2 changes: 1 addition & 1 deletion test/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ class Driver {
}
}

if (task.skipPages && task.skipPages.includes(task.pageNum)) {
if (task.skipPages?.includes(task.pageNum)) {
this._log(
" Skipping page " + task.pageNum + "/" + task.pdfDoc.numPages + "...\n"
);
Expand Down
2 changes: 1 addition & 1 deletion test/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ function parseOptions() {
);
})
.check(argv => {
if (argv.testfilter && argv.testfilter.length > 0 && argv.xfaOnly) {
if (argv.testfilter?.length > 0 && argv.xfaOnly) {
throw new Error("--testfilter and --xfaOnly cannot be used together.");
}
return true;
Expand Down
60 changes: 27 additions & 33 deletions test/unit/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,8 @@ class DOMFileReaderFactory {

class NodeFileReaderFactory {
static async fetch(params) {
return new Promise((resolve, reject) => {
fs.readFile(params.path, (error, data) => {
if (error || !data) {
reject(error || new Error(`Empty file for: ${params.path}`));
return;
}
resolve(new Uint8Array(data));
});
});
const data = await fs.promises.readFile(params.path);
return new Uint8Array(data);
}
}

Expand Down Expand Up @@ -152,33 +145,34 @@ function createTemporaryNodeServer() {
const server = http
.createServer((request, response) => {
const filePath = process.cwd() + "/test/pdfs" + request.url;
fs.lstat(filePath, (error, stat) => {
if (error) {
fs.promises.lstat(filePath).then(
stat => {
if (!request.headers.range) {
const contentLength = stat.size;
const stream = fs.createReadStream(filePath);
response.writeHead(200, {
"Content-Type": "application/pdf",
"Content-Length": contentLength,
"Accept-Ranges": "bytes",
});
stream.pipe(response);
} else {
const [start, end] = request.headers.range
.split("=")[1]
.split("-")
.map(x => Number(x));
const stream = fs.createReadStream(filePath, { start, end });
response.writeHead(206, {
"Content-Type": "application/pdf",
});
stream.pipe(response);
}
},
error => {
response.writeHead(404);
response.end(`File ${request.url} not found!`);
return;
}
if (!request.headers.range) {
const contentLength = stat.size;
const stream = fs.createReadStream(filePath);
response.writeHead(200, {
"Content-Type": "application/pdf",
"Content-Length": contentLength,
"Accept-Ranges": "bytes",
});
stream.pipe(response);
} else {
const [start, end] = request.headers.range
.split("=")[1]
.split("-")
.map(x => Number(x));
const stream = fs.createReadStream(filePath, { start, end });
response.writeHead(206, {
"Content-Type": "application/pdf",
});
stream.pipe(response);
}
});
);
})
.listen(0); /* Listen on a random free port */

Expand Down

0 comments on commit ddd7b63

Please sign in to comment.