Welcome! Log In Create A New Profile

Advanced

[njs] Tests: refactored "fs" module tests.

Dmitry Volyntsev
November 02, 2021 08:42AM
details: https://hg.nginx.org/njs/rev/685adce92af1
branches:
changeset: 1734:685adce92af1
user: Dmitry Volyntsev <xeioex@nginx.com>
date: Tue Nov 02 12:35:34 2021 +0000
description:
Tests: refactored "fs" module tests.

diffstat:

test/fs/methods.js | 371 +++++++++++++++++++++++++++++++++++++++++++
test/js/fs_appendFile.js | 61 -------
test/js/fs_appendFileSync.js | 59 ------
test/js/fs_readFile.js | 40 ----
test/js/fs_readFileSync.js | 43 ----
test/js/fs_writeFile.js | 57 ------
test/js/fs_writeFileSync.js | 54 ------
test/njs_expect_test.exp | 200 +---------------------
8 files changed, 387 insertions(+), 498 deletions(-)

diffs (937 lines):

diff -r 8dd6868a8748 -r 685adce92af1 test/fs/methods.js
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/fs/methods.js Tue Nov 02 12:35:34 2021 +0000
@@ -0,0 +1,371 @@
+var fs = require('fs');
+
+async function run(tlist) {
+ function validate(t, r, i) {
+ if (r.status == "fulfilled") {
+ return r.value === "SUCCESS";
+ }
+
+ if (r.status == "rejected" && t[i].exception) {
+ if (process.argv[2] === '--match-exception-text') {
+ /* is not compatible with node.js format */
+ return r.reason.toString().startsWith(t[i].exception);
+ }
+
+ return true;
+ }
+
+ if (r.status == "rejected" && t[i].optional) {
+ return r.reason.toString().startsWith("Error: No such file or directory");
+ }
+
+ return false;
+ }
+
+ for (let k = 0; k < tlist.length; k++) {
+ let ts = tlist[k];
+ let results = await Promise.allSettled(ts.tests.map(t => ts.T(ts.prepare_args(t, ts.opts))));
+ let r = results.map((r, i) => validate(ts.tests, r, i));
+
+ console.log(`${ts.name} ${r.every(v=>v == true) ? "SUCCESS" : "FAILED"}`);
+
+ r.forEach((v, i) => {
+ if (!v) {
+ console.log(`FAILED ${i}: ${JSON.stringify(ts.tests[i])}\n with reason: ${results[i].reason}`);
+ }
+ })
+ }
+}
+
+function p(args, default_opts) {
+ let params = Object.assign({}, default_opts, args);
+
+ let fname = params.args[0];
+
+ if (fname[0] == '@') {
+ let gen = `build/test/fs_test_${Math.round(Math.random() * 1000000)}`;
+ params.args = params.args.map(v => v);
+ params.args[0] = gen + fname.slice(1);
+ }
+
+ return params;
+}
+
+function promisify(f) {
+ return function (...args) {
+ return new Promise((resolve, reject) => {
+ function callback(err, result) {
+ if (err) {
+ return reject(err);
+ } else {
+ resolve(result);
+ }
+ }
+
+ args.push(callback);
+ f.apply(this, args);
+ });
+ };
+}
+
+async function method(name, params) {
+ let data = null;
+
+ switch (params.type) {
+ case "sync":
+ try {
+ data = fs[name + "Sync"].apply(null, params.args);
+
+ } catch (e) {
+ if (!params.stringify) {
+ throw e;
+ }
+
+ data = Buffer.from(JSON.stringify(e));
+ }
+
+ break;
+
+ case "callback":
+ data = await promisify(fs[name]).apply(null, params.args)
+ .catch(e => {
+ if (!params.stringify) {
+ throw e;
+ }
+
+ return Buffer.from(JSON.stringify(e));
+ });
+
+ break;
+
+ case "promise":
+ data = await fs.promises[name].apply(null, params.args)
+ .catch(e => {
+ if (!params.stringify) {
+ throw e;
+ }
+
+ return Buffer.from(JSON.stringify(e));
+ });
+
+ break;
+ }
+
+ return data;
+}
+
+async function read_test(params) {
+ let data = await method("readFile", params);
+
+ if (params.slice) {
+ data = data.slice.apply(data, params.slice);
+ }
+
+ let success = true;
+ if (data instanceof Buffer) {
+ if (data.compare(params.expected) != 0) {
+ success = false;
+ }
+
+ } else if (data != params.expected) {
+ success = false;
+ }
+
+ if (!success) {
+ throw Error(`readFile unexpected data`);
+ }
+
+ return 'SUCCESS';
+}
+
+let read_tests = [
+ { args: ["test/fs/utf8"], expected: Buffer.from("αβZγ") },
+ { args: [Buffer.from("@test/fs/utf8").slice(1)], expected: Buffer.from("αβZγ") },
+ { args: ["test/fs/utf8", "utf8"], expected: "αβZγ" },
+ { args: ["test/fs/utf8", {encoding: "utf8", flags:"r+"}], expected: "αβZγ" },
+ { args: ["test/fs/nonexistent"], stringify: true,
+ expected: Buffer.from('{"errno":2,"code":"ENOENT","path":"test/fs/nonexistent","syscall":"open"}'),
+ exception: "Error: No such file or directory" },
+ { args: ["test/fs/non_utf8", "utf8"], expected: "��" },
+ { args: ["test/fs/non_utf8", {encoding: "hex"}], expected: "8080" },
+ { args: ["test/fs/non_utf8", "base64"], expected: "gIA=" },
+ { args: ["test/fs/ascii", "utf8"], expected: "x".repeat(600) },
+ { args: ["test/fs/ascii", { encoding:"utf8", flags: "r+"}], expected: "x".repeat(600) },
+
+ { args: [Buffer.from([0x80, 0x80])], exception: "Error: No such file or directory" },
+ { args: ['x'.repeat(8192)], exception: "TypeError: \"path\" is too long" },
+
+ { args: ["/proc/version"], slice:[0,5], expected: Buffer.from("Linux"), optional: true },
+ { args: ["/proc/cpuinfo"], slice:[0,9], expected: Buffer.from("processor"), optional: true },
+];
+
+let readFile_tsuite = {
+ name: "fs readFile",
+ T: read_test,
+ prepare_args: p,
+ opts: { type: "callback" },
+ tests: read_tests,
+};
+
+let readFileSync_tsuite = {
+ name: "fs readFileSync",
+ T: read_test,
+ prepare_args: p,
+ opts: { type: "sync" },
+ tests: read_tests,
+};
+
+let readFileP_tsuite = {
+ name: "fsp readFile",
+ T: read_test,
+ prepare_args: p,
+ opts: { type: "promise" },
+ tests: read_tests,
+};
+
+async function write_test(params) {
+ let fname = params.args[0];
+
+ try { fs.unlinkSync(fname); } catch (e) {}
+
+ let data = await method("writeFile", params);
+
+ if (!data) {
+ data = fs.readFileSync(fname);
+ }
+
+ try { fs.unlinkSync(fname); } catch (e) {}
+
+ if (data.compare(params.expected) != 0) {
+ throw Error(`writeFile unexpected data`);
+ }
+
+ return 'SUCCESS';
+}
+
+let write_tests = [
+ { args: ["@", Buffer.from(Buffer.alloc(4).fill(65).buffer, 1)],
+ expected: Buffer.from("AAA") },
+ { args: ["@", Buffer.from("XYZ"), "utf8"], expected: Buffer.from("XYZ") },
+ { args: ["@", Buffer.from("XYZ"), {encoding: "utf8", mode: 0o666}],
+ expected: Buffer.from("XYZ") },
+ { args: ["@", new DataView(Buffer.alloc(3).fill(66).buffer)],
+ expected: Buffer.from("BBB") },
+ { args: ["@", new Uint8Array(Buffer.from("ABCD"))],
+ expected: Buffer.from("ABCD")},
+ { args: ["@", "XYZ"], expected: Buffer.from("XYZ")},
+ { args: ["@", "78797a", "hex"], expected: Buffer.from("xyz") },
+ { args: ["@", "eHl6", "base64"], expected: Buffer.from("xyz") },
+ { args: ["@", "eHl6", {encoding: "base64url"}], expected: Buffer.from("xyz"),
+ optional: true },
+ { args: ["@", Symbol("XYZ")], exception: "TypeError: Cannot convert a Symbol value to a string"},
+ { args: ["/invalid_path", "XYZ"], stringify: true,
+ expected: Buffer.from('{"errno":13,"code":"EACCES","path":"/invalid_path","syscall":"open"}'),
+ exception: "Error: No such file or directory" },
+];
+
+let writeFile_tsuite = {
+ name: "fs writeFile",
+ T: write_test,
+ prepare_args: p,
+ opts: { type: "callback" },
+ tests: write_tests,
+};
+
+let writeFileSync_tsuite = {
+ name: "fs writeFileSync",
+ T: write_test,
+ prepare_args: p,
+ opts: { type: "sync" },
+ tests: write_tests,
+};
+
+let writeFileP_tsuite = {
+ name: "fsp writeFile",
+ T: write_test,
+ prepare_args: p,
+ opts: { type: "promise" },
+ tests: write_tests,
+};
+
+async function append_test(params) {
+ let fname = params.args[0];
+
+ try { fs.unlinkSync(fname); } catch (e) {}
+
+ let data = await method("appendFile", params);
+ data = await method("appendFile", params);
+
+ if (!data) {
+ data = fs.readFileSync(fname);
+ }
+
+ try { fs.unlinkSync(fname); } catch (e) {}
+
+ if (data.compare(params.expected) != 0) {
+ throw Error(`appendFile unexpected data`);
+ }
+
+ return 'SUCCESS';
+}
+
+let append_tests = [
+ { args: ["@", Buffer.from(Buffer.alloc(4).fill(65).buffer, 1)],
+ expected: Buffer.from("AAAAAA") },
+ { args: ["@", Buffer.from("XYZ"), "utf8"], expected: Buffer.from("XYZXYZ") },
+ { args: ["@", Buffer.from("XYZ"), {encoding: "utf8", mode: 0o666}],
+ expected: Buffer.from("XYZXYZ") },
+ { args: ["@", new DataView(Buffer.alloc(3).fill(66).buffer)],
+ expected: Buffer.from("BBBBBB") },
+ { args: ["@", new Uint8Array(Buffer.from("ABCD"))],
+ expected: Buffer.from("ABCDABCD")},
+ { args: ["@", "XYZ"], expected: Buffer.from("XYZXYZ")},
+ { args: ["@", "78797a", "hex"], expected: Buffer.from("xyzxyz") },
+ { args: ["@", "eHl6", "base64"], expected: Buffer.from("xyzxyz") },
+ { args: ["@", "eHl6", {encoding: "base64url"}], expected: Buffer.from("xyzxyz"),
+ optional: true },
+ { args: ["@", Symbol("XYZ")], exception: "TypeError: Cannot convert a Symbol value to a string"},
+ { args: ["/invalid_path", "XYZ"], stringify: true,
+ expected: Buffer.from('{"errno":13,"code":"EACCES","path":"/invalid_path","syscall":"open"}'),
+ exception: "Error: No such file or directory" },
+];
+
+let appendFile_tsuite = {
+ name: "fs appendFile",
+ T: append_test,
+ prepare_args: p,
+ opts: { type: "callback" },
+ tests: append_tests,
+};
+
+let appendFileSync_tsuite = {
+ name: "fs appendFileSync",
+ T: append_test,
+ prepare_args: p,
+ opts: { type: "sync" },
+ tests: append_tests,
+};
+
+let appendFileP_tsuite = {
+ name: "fsp appendFile",
+ T: append_test,
+ prepare_args: p,
+ opts: { type: "promise" },
+ tests: append_tests,
+};
+
+async function realpath_test(params) {
+ let data = await method("realpath", params);
+
+ if (!params.check(data)) {
+ throw Error(`realpath failed check`);
+ }
+
+ return 'SUCCESS';
+}
+
+let realpath_tests = [
+ { args: ["./build/test/.."],
+ check: (data) => data.endsWith("build") },
+ { args: ["./build/test/", {encoding:'buffer'}],
+ check: (data) => data instanceof Buffer },
+];
+
+let realpath_tsuite = {
+ name: "fs realpath",
+ T: realpath_test,
+ prepare_args: p,
+ opts: { type: "callback" },
+ tests: realpath_tests,
+};
+
+let realpathSync_tsuite = {
+ name: "fs realpathSync",
+ T: realpath_test,
+ prepare_args: p,
+ opts: { type: "sync" },
+ tests: realpath_tests,
+};
+
+let realpathP_tsuite = {
+ name: "fsp realpath",
+ T: realpath_test,
+ prepare_args: p,
+ opts: { type: "promise" },
+ tests: realpath_tests,
+};
+
+run([
+ readFile_tsuite,
+ readFileSync_tsuite,
+ readFileP_tsuite,
+ writeFile_tsuite,
+ writeFileSync_tsuite,
+ writeFileP_tsuite,
+ appendFile_tsuite,
+ appendFileSync_tsuite,
+ appendFileP_tsuite,
+ realpath_tsuite,
+ realpathSync_tsuite,
+ realpathP_tsuite,
+]);
diff -r 8dd6868a8748 -r 685adce92af1 test/js/fs_appendFile.js
--- a/test/js/fs_appendFile.js Mon Nov 01 15:53:30 2021 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,61 +0,0 @@
-var fs = require('fs');
-var fname = './build/test/fs_appendFile';
-
-var argv = process.argv.slice(2);
-
-var data = (() => {
- var value = argv[0];
- var type = argv[1];
- var offset = argv[2] ? parseInt(argv[2]) : 0;
-
- switch (type) {
- case 'Buffer':
- return Buffer.from(Buffer.from(value).buffer, offset);
- case 'DataView':
- return new DataView(Buffer.from(value).buffer, offset);
- case 'Object':
- return {toString(){return value}};
- case 'String':
- return String(value);
- case 'Symbol':
- return Symbol(value);
- case 'Uint8Array':
- return new Uint8Array(Buffer.from(value).buffer, offset);
- default:
- throw new Error(`Unknown data type:${type}`);
- }
-})();
-
-var options = (() => {
- var encoding = argv[2];
- var mode = argv[3] ? parseInt(argv[3].slice(2), 8) : 0;
-
- if (encoding && mode) {
- return {encoding, mode};
-
- } else if (encoding) {
- return encoding;
- }
-
- return undefined;
-})();
-
-try { fs.unlinkSync(fname); } catch (e) {}
-
-function done(e) {
- if (e) {throw e};
- var data = fs.readFileSync(fname);
- console.log(String(data));
-}
-
-function append(cb) {
- if (options) {
- var path = Buffer.from(`@${fname}`).slice(1);
- fs.appendFile(path, data, options, cb);
-
- } else {
- fs.appendFile(fname, data, cb);
- }
-}
-
-append((e) => {if (e) {throw e}; append(done);})
diff -r 8dd6868a8748 -r 685adce92af1 test/js/fs_appendFileSync.js
--- a/test/js/fs_appendFileSync.js Mon Nov 01 15:53:30 2021 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,59 +0,0 @@
-var fs = require('fs');
-var fname = './build/test/fs_appendFileSync';
-
-var argv = process.argv.slice(2);
-
-var data = (() => {
- var value = argv[0];
- var type = argv[1];
- var offset = argv[2] ? parseInt(argv[2]) : 0;
-
- switch (type) {
- case 'Buffer':
- return Buffer.from(Buffer.from(value).buffer, offset);
- case 'DataView':
- return new DataView(Buffer.from(value).buffer, offset);
- case 'Object':
- return {toString(){return value}};
- case 'String':
- return String(value);
- case 'Symbol':
- return Symbol(value);
- case 'Uint8Array':
- return new Uint8Array(Buffer.from(value).buffer, offset);
- default:
- throw new Error(`Unknown data type:${type}`);
- }
-})();
-
-var options = (() => {
- var encoding = argv[2];
- var mode = argv[3] ? parseInt(argv[3].slice(2), 8) : 0;
-
- if (encoding && mode) {
- return {encoding, mode};
-
- } else if (encoding) {
- return encoding;
- }
-
- return undefined;
-})();
-
-function append() {
- if (options) {
- var path = Buffer.from(`@${fname}`).slice(1);
- fs.appendFileSync(path, data, options);
-
- } else {
- fs.appendFileSync(fname, data);
- }
-}
-
-try { fs.unlinkSync(fname); } catch (e) {}
-
-append();
-append();
-
-var ret = fs.readFileSync(fname);
-console.log(String(ret));
diff -r 8dd6868a8748 -r 685adce92af1 test/js/fs_readFile.js
--- a/test/js/fs_readFile.js Mon Nov 01 15:53:30 2021 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,40 +0,0 @@
-var fs = require('fs');
-
-var argv = process.argv.slice(2);
-var fname = argv[0];
-
-var options = (() => {
- var encoding = argv[1];
- var flags = argv[2];
-
- if (encoding && flags) {
- return {encoding, flags};
-
- } else if (encoding) {
- return encoding;
- }
-
- return undefined;
-})();
-
-function type(v) {
- if (v instanceof Buffer) {
- return 'Buffer';
- }
-
- return typeof v;
-}
-
-function done(e, data) {
- if (e) {console.log(JSON.stringify(e))};
- console.log(String(data), type(data), data.length);
-}
-
-if (options) {
- var path = Buffer.from(`@${fname}`).slice(1);
- fs.readFile(path, options, done);
-
-} else {
- fs.readFile(fname, done);
-}
-
diff -r 8dd6868a8748 -r 685adce92af1 test/js/fs_readFileSync.js
--- a/test/js/fs_readFileSync.js Mon Nov 01 15:53:30 2021 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-var fs = require('fs');
-
-var argv = process.argv.slice(2);
-var fname = argv[0];
-
-var options = (() => {
- var encoding = argv[1];
- var flags = argv[2];
-
- if (encoding && flags) {
- return {encoding, flags};
-
- } else if (encoding) {
- return encoding;
- }
-
- return undefined;
-})();
-
-function type(v) {
- if (v instanceof Buffer) {
- return 'Buffer';
- }
-
- return typeof v;
-}
-
-var data;
-
-try {
- if (options) {
- var path = Buffer.from(`@${fname}`).slice(1);
- data = fs.readFileSync(path, options);
-
- } else {
- data = fs.readFileSync(fname);
- }
-
-} catch (e) {
- console.log(JSON.stringify(e));
-}
-
-console.log(String(data), type(data), data.length);
diff -r 8dd6868a8748 -r 685adce92af1 test/js/fs_writeFile.js
--- a/test/js/fs_writeFile.js Mon Nov 01 15:53:30 2021 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,57 +0,0 @@
-var fs = require('fs');
-var fname = './build/test/fs_writeFile';
-
-var argv = process.argv.slice(2);
-
-var data = (() => {
- var value = argv[0];
- var type = argv[1];
- var offset = argv[2] ? parseInt(argv[2]) : 0;
-
- switch (type) {
- case 'Buffer':
- return Buffer.from(Buffer.from(value).buffer, offset);
- case 'DataView':
- return new DataView(Buffer.from(value).buffer, offset);
- case 'Object':
- return {toString(){return value}};
- case 'String':
- return String(value);
- case 'Symbol':
- return Symbol(value);
- case 'Uint8Array':
- return new Uint8Array(Buffer.from(value).buffer, offset);
- default:
- throw new Error(`Unknown data type:${type}`);
- }
-})();
-
-var options = (() => {
- var encoding = argv[2];
- var mode = argv[3] ? parseInt(argv[3].slice(2), 8) : 0;
-
- if (encoding && mode) {
- return {encoding, mode};
-
- } else if (encoding) {
- return encoding;
- }
-
- return undefined;
-})();
-
-try { fs.unlinkSync(fname); } catch (e) {}
-
-function cb(e) {
- if (e) {throw e};
- var data = fs.readFileSync(fname);
- console.log(String(data));
-}
-
-if (options) {
- var path = Buffer.from(`@${fname}`).slice(1);
- fs.writeFile(path, data, options, cb);
-
-} else {
- fs.writeFile(fname, data, cb);
-}
diff -r 8dd6868a8748 -r 685adce92af1 test/js/fs_writeFileSync.js
--- a/test/js/fs_writeFileSync.js Mon Nov 01 15:53:30 2021 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,54 +0,0 @@
-var fs = require('fs');
-var fname = './build/test/fs_writeFileSync';
-
-var argv = process.argv.slice(2);
-
-var data = (() => {
- var value = argv[0];
- var type = argv[1];
- var offset = argv[2] ? parseInt(argv[2]) : 0;
-
- switch (type) {
- case 'Buffer':
- return Buffer.from(Buffer.from(value).buffer, offset);
- case 'DataView':
- return new DataView(Buffer.from(value).buffer, offset);
- case 'Object':
- return {toString(){return value}};
- case 'String':
- return String(value);
- case 'Symbol':
- return Symbol(value);
- case 'Uint8Array':
- return new Uint8Array(Buffer.from(value).buffer, offset);
- default:
- throw new Error(`Unknown data type:${type}`);
- }
-})();
-
-var options = (() => {
- var encoding = argv[2];
- var mode = argv[3] ? parseInt(argv[3].slice(2), 8) : 0;
-
- if (encoding && mode) {
- return {encoding, mode};
-
- } else if (encoding) {
- return encoding;
- }
-
- return undefined;
-})();
-
-try { fs.unlinkSync(fname); } catch (e) {}
-
-if (options) {
- var path = Buffer.from(`@${fname}`).slice(1);
- fs.writeFileSync(path, data, options);
-
-} else {
- fs.writeFileSync(fname, data);
-}
-
-var ret = fs.readFileSync(fname);
-console.log(String(ret));
diff -r 8dd6868a8748 -r 685adce92af1 test/njs_expect_test.exp
--- a/test/njs_expect_test.exp Mon Nov 01 15:53:30 2021 +0000
+++ b/test/njs_expect_test.exp Tue Nov 02 12:35:34 2021 +0000
@@ -459,189 +459,6 @@ njs_test {
"queue.toString()\r\n'0,1,2,3,4,5'"}
}

-# require('fs').readFile()
-
-njs_run {"./test/js/fs_readFile.js" "test/fs/utf8"} "αβZγ Buffer 7"
-njs_run {"./test/js/fs_readFile.js" "test/fs/utf8" "utf8"} "αβZγ string 4"
-njs_run {"./test/js/fs_readFile.js" "test/fs/utf8" "utf8" "r+"} "αβZγ string 4"
-njs_run {"./test/js/fs_readFile.js" "test/fs/nonexistent"} \
- "{\"errno\":2,\"code\":\"ENOENT\",\"path\":\"test/fs/nonexistent\",\"syscall\":\"open\"}"
-njs_run {"./test/js/fs_readFile.js" "test/fs/non_utf8" "utf8"} "�� string 2"
-njs_run {"./test/js/fs_readFile.js" "test/fs/non_utf8" "hex"} "8080 string 4"
-njs_run {"./test/js/fs_readFile.js" "test/fs/non_utf8" "base64"} "gIA= string 4"
-
-njs_test {
- {"var fs = require('fs')\r\n"
- "undefined\r\n>> "}
- {"fs.readFile('test/fs/ascii', 'utf8', function (e, data) {console.log(data[599], data[600])})\r\n"
- "undefined\r\nx undefined\r\n>> "}
- {"fs.readFile('test/fs/ascii', {encoding:'utf8',flag:'r+'}, function (e, data) {console.log(data[599], data[600])})\r\n"
- "undefined\r\nx undefined\r\n>> "}
- {"fs.readFile(Buffer.from([0x80,0x80]), function(e) {console.log(e.path.codePointAt())})\r\n"
- "undefined\r\n65533"}
-}
-
-njs_test {
- {"var fs = require('fs')\r\n"
- "undefined\r\n>> "}
- {"fs.readFile('/proc/version', (e, data) => {console.log(e || data.slice(0,5) == 'Linux')})\r\n"
- "undefined\r\ntrue\r\n>> "}
- {"fs.readFile('/proc/cpuinfo', (e, data) => {console.log(e || data.slice(0,9) == 'processor')})\r\n"
- "undefined\r\ntrue\r\n>> "}
-}
-
-# require('fs').readFileSync()
-
-njs_run {"./test/js/fs_readFileSync.js" "test/fs/utf8"} "αβZγ Buffer 7"
-njs_run {"./test/js/fs_readFileSync.js" "test/fs/utf8" "utf8"} "αβZγ string 4"
-njs_run {"./test/js/fs_readFileSync.js" "test/fs/utf8" "utf8" "r+"} "αβZγ string 4"
-njs_run {"./test/js/fs_readFileSync.js" "test/fs/nonexistent"} \
- "{\"errno\":2,\"code\":\"ENOENT\",\"path\":\"test/fs/nonexistent\",\"syscall\":\"open\"}"
-njs_run {"./test/js/fs_readFileSync.js" "test/fs/non_utf8" "utf8"} "�� string 2"
-njs_run {"./test/js/fs_readFileSync.js" "test/fs/non_utf8" "hex"} "8080 string 4"
-njs_run {"./test/js/fs_readFileSync.js" "test/fs/non_utf8" "base64"} "gIA= string 4"
-
-njs_test {
- {"var fs = require('fs')\r\n"
- "undefined\r\n>> "}
- {"fs.readFileSync('test/fs/non_utf8', 'utf8').charCodeAt(1)\r\n"
- "65533"}
- {"fs.readFile('x'.repeat(8192))\r\n"
- "TypeError: \"path\" is too long >= 4096"}
-}
-
-njs_test {
- {"var fs = require('fs'), file\r\n"
- "undefined\r\n>> "}
- {"try { file = fs.readFileSync('/proc/version')} catch (e) {}\r\n"
- "undefined"}
- {"(!file || file.slice(0,5) == 'Linux')\r\n"
- "true"}
- {"try { file = fs.readFileSync('/proc/cpuinfo')} catch (e) {}\r\n"
- "undefined"}
- {"(!file || file.slice(0,9) == 'processor')\r\n"
- "true"}
-}
-
-# require('fs').writeFile()
-
-njs_run {"./test/js/fs_writeFile.js" "ABCD" "Buffer" "1"} "BCD"
-njs_run {"./test/js/fs_writeFile.js" "ABC" "DataView"} "ABC"
-njs_run {"./test/js/fs_writeFile.js" "414243" "Object" "hex"} "ABC"
-njs_run {"./test/js/fs_writeFile.js" "ABC" "String"} "ABC"
-njs_run {"./test/js/fs_writeFile.js" "ABC" "Symbol"} "TypeError: Cannot convert a Symbol value to a string*"
-njs_run {"./test/js/fs_writeFile.js" "ABC" "Uint8Array"} "ABC"
-
-njs_run {"./test/js/fs_writeFile.js" "ABC" "String" "utf8"} "ABC"
-njs_run {"./test/js/fs_writeFile.js" "ABC" "String" "utf8" "0o666"} "ABC"
-njs_run {"./test/js/fs_writeFile.js" "ABC" "String" "utf8" "0o222"} "Error: Permission denied*"
-njs_run {"./test/js/fs_writeFile.js" "414243" "String" "hex"} "ABC"
-njs_run {"./test/js/fs_writeFile.js" "QUJD" "String" "base64"} "ABC"
-njs_run {"./test/js/fs_writeFile.js" "QUJD" "String" "base64url"} "ABC"
-
-njs_test {
- {"var fs = require('fs')\r\n"
- "undefined\r\n>> "}
- {"fs.writeFile('/invalid_path', 'ABC', function (e) { console.log(JSON.stringify(e))})\r\n"
- "undefined\r\n{\"errno\":13,\"code\":\"EACCES\",\"path\":\"/invalid_path\",\"syscall\":\"open\"}\r\n>> "}
- {"fs.writeFile(Buffer.from('/invalid_path'), 'ABC', function (e) { console.log(typeof e.path)})\r\n"
- "undefined\r\nstring\r\n>> "}
-}
-
-# require('fs').writeFileSync()
-
-njs_run {"./test/js/fs_writeFileSync.js" "ABCD" "Buffer" "1"} "BCD"
-njs_run {"./test/js/fs_writeFileSync.js" "ABC" "DataView"} "ABC"
-njs_run {"./test/js/fs_writeFileSync.js" "414243" "Object" "hex"} "ABC"
-njs_run {"./test/js/fs_writeFileSync.js" "ABC" "String"} "ABC"
-njs_run {"./test/js/fs_writeFileSync.js" "ABC" "Symbol"} "TypeError: Cannot convert a Symbol value to a string*"
-njs_run {"./test/js/fs_writeFileSync.js" "ABC" "Uint8Array"} "ABC"
-
-njs_run {"./test/js/fs_writeFileSync.js" "ABC" "String" "utf8"} "ABC"
-njs_run {"./test/js/fs_writeFileSync.js" "ABC" "String" "utf8" "0o666"} "ABC"
-njs_run {"./test/js/fs_writeFileSync.js" "ABC" "String" "utf8" "0o222"} "Error: Permission denied*"
-njs_run {"./test/js/fs_writeFileSync.js" "78797a" "String" "hex"} "xyz"
-njs_run {"./test/js/fs_writeFileSync.js" "eHl6" "String" "base64"} "xyz"
-njs_run {"./test/js/fs_writeFileSync.js" "eHl6" "String" "base64url"} "xyz"
-
-njs_test {
- {"var fs = require('fs'), fn = './build/test/file2';\r\n"
- "undefined\r\n>> "}
- {"fs.writeFileSync(fn, 'ABC')\r\n"
- "undefined\r\n>> "}
- {"fs.writeFileSync(fn, 'ABC')\r\n"
- "undefined\r\n>> "}
- {"fs.readFileSync(fn, 'utf8')\r\n"
- "'ABC'\r\n>> "}
-}
-
-# require('fs').appendFile()
-
-njs_run {"./test/js/fs_appendFile.js" "ABCD" "Buffer" "1"} "BCDBCD"
-njs_run {"./test/js/fs_appendFile.js" "ABC" "DataView"} "ABCABC"
-njs_run {"./test/js/fs_appendFile.js" "414243" "Object" "hex"} "ABCABC"
-njs_run {"./test/js/fs_appendFile.js" "ABC" "String"} "ABCABC"
-njs_run {"./test/js/fs_appendFile.js" "ABC" "Symbol"} "TypeError: Cannot convert a Symbol value to a string*"
-njs_run {"./test/js/fs_appendFile.js" "ABC" "Uint8Array"} "ABCABC"
-
-njs_run {"./test/js/fs_appendFile.js" "ABC" "String" "utf8"} "ABC"
-njs_run {"./test/js/fs_appendFile.js" "ABC" "String" "utf8" "0o666"} "ABC"
-njs_run {"./test/js/fs_appendFile.js" "ABC" "String" "utf8" "0o222"} "Error: Permission denied*"
-njs_run {"./test/js/fs_appendFile.js" "414243" "String" "hex"} "ABC"
-njs_run {"./test/js/fs_appendFile.js" "QUJD" "String" "base64"} "ABC"
-njs_run {"./test/js/fs_appendFile.js" "QUJD" "String" "base64url"} "ABC"
-
-# require('fs').appendFileSync()
-
-njs_run {"./test/js/fs_appendFileSync.js" "ABCD" "Buffer" "1"} "BCDBCD"
-njs_run {"./test/js/fs_appendFileSync.js" "ABC" "DataView"} "ABCABC"
-njs_run {"./test/js/fs_appendFileSync.js" "414243" "Object" "hex"} "ABCABC"
-njs_run {"./test/js/fs_appendFileSync.js" "ABC" "String"} "ABCABC"
-njs_run {"./test/js/fs_appendFileSync.js" "ABC" "Symbol"} "TypeError: Cannot convert a Symbol value to a string*"
-njs_run {"./test/js/fs_appendFileSync.js" "ABC" "Uint8Array"} "ABCABC"
-
-njs_run {"./test/js/fs_appendFileSync.js" "ABC" "String" "utf8"} "ABC"
-njs_run {"./test/js/fs_appendFileSync.js" "ABC" "String" "utf8" "0o666"} "ABC"
-njs_run {"./test/js/fs_appendFileSync.js" "ABC" "String" "utf8" "0o222"} "Error: Permission denied*"
-njs_run {"./test/js/fs_appendFileSync.js" "414243" "String" "hex"} "ABC"
-njs_run {"./test/js/fs_appendFileSync.js" "QUJD" "String" "base64"} "ABC"
-njs_run {"./test/js/fs_appendFileSync.js" "QUJD" "String" "base64url"} "ABC"
-
-# require('fs').renameSync()
-
-njs_test {
- {"var fs = require('fs'), mktemp = ()=> `/tmp/njs_${Math.round(Math.random() * 1000000)}`\r\n"
- "undefined\r\n>> "}
- {"var fn1 = mktemp(), fn2 = mktemp();\r\n"
- "undefined\r\n>> "}
- {"fs.writeFileSync(fn1, 'ABC')\r\n"
- "undefined\r\n>> "}
- {"fs.renameSync(fn1, fn2)\r\n"
- "undefined\r\n>> "}
- {"String(fs.readFileSync(fn2))\r\n"
- "'ABC'\r\n>> "}
-}
-
-njs_test {
- {"var fs = require('fs'), fn = './build/test/file2'\r\n"
- "undefined\r\n>> "}
- {"fs.writeFileSync(fn, 'ABC')\r\n"
- "undefined\r\n>> "}
- {"fs.renameSync(fn, 'test/fs/')\r\n"
- "Error: Not a directory*"}
-}
-
-# require('fs').realpathSync()
-
-njs_test {
- {"var fs = require('fs')\r\n"
- "undefined\r\n>> "}
- {"fs.realpathSync('./build/test/..').endsWith('build')\r\n"
- "true\r\n>> "}
- {"fs.realpathSync('./build/test/..', {encoding:'buffer'}) instanceof Buffer\r\n"
- "true\r\n>> "}
-}
-
njs_run {"-c" "setTimeout(() => {console.log('A'.repeat(1024))}, 0); ref"} \
"^Thrown:
ReferenceError: \"ref\" is not defined
@@ -832,7 +649,6 @@ njs_test {

njs_run {"-v"} "\\d+\.\\d+\.\\d+"

-
# Promise

njs_run {"./test/js/promise_set_timeout.js"} \
@@ -1003,6 +819,22 @@ PatchedPromise.constructor
PatchedPromise.constructor
PatchedPromise async done"

+# fs
+
+njs_run {"./test/fs/methods.js" "--match-exception-text"} \
+"fs readFile SUCCESS
+fs readFileSync SUCCESS
+fsp readFile SUCCESS
+fs writeFile SUCCESS
+fs writeFileSync SUCCESS
+fsp writeFile SUCCESS
+fs appendFile SUCCESS
+fs appendFileSync SUCCESS
+fsp appendFile SUCCESS
+fs realpath SUCCESS
+fs realpathSync SUCCESS
+fsp realpath SUCCESS"
+
njs_run {"./test/js/fs_promises_001.js"} \
"init ok true
short circut ok true
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Tests: refactored "fs" module tests.

Dmitry Volyntsev 219 November 02, 2021 08:42AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 46
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready