-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-fs-utf8-fast-path-casing.js
More file actions
49 lines (37 loc) · 1.56 KB
/
test-fs-utf8-fast-path-casing.js
File metadata and controls
49 lines (37 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
// This test ensures that fs.readFileSync and fs.writeFileSync
// accept all valid UTF8 encoding variants (utf8, utf-8, UTF8, UTF-8).
// Refs: https://github.com/nodejs/node/issues/49888
const common = require('../common');
const tmpdir = require('../../test/common/tmpdir');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
tmpdir.refresh();
const testContent = 'Hello, World! 你好,世界!';
const encodings = ['utf8', 'utf-8', 'UTF8', 'UTF-8'];
// Test writeFileSync and readFileSync with different UTF8 variants
for (const encoding of encodings) {
const testFile = tmpdir.resolve(`test_utf8_fast_path_${encoding}.txt`);
// Test writeFileSync
fs.writeFileSync(testFile, testContent, { encoding });
// Test readFileSync
const result = fs.readFileSync(testFile, { encoding });
assert.strictEqual(result, testContent,
`readFileSync should return correct content for encoding "${encoding}"`);
}
// Test with file descriptor
for (const encoding of encodings) {
const testFile = tmpdir.resolve(`test_utf8_fast_path_fd_${encoding}.txt`);
// Write with fd
const fdWrite = fs.openSync(testFile, 'w');
fs.writeFileSync(fdWrite, testContent, { encoding });
fs.closeSync(fdWrite);
// Read with fd
const fdRead = fs.openSync(testFile, 'r');
const result = fs.readFileSync(fdRead, { encoding });
fs.closeSync(fdRead);
assert.strictEqual(result, testContent,
`readFileSync with fd should return correct content for encoding "${encoding}"`);
}
console.log('All UTF8 fast path casing tests passed!');