-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-fs-utf8-encoding-case-insensitive.js
More file actions
74 lines (60 loc) · 2.22 KB
/
test-fs-utf8-encoding-case-insensitive.js
File metadata and controls
74 lines (60 loc) · 2.22 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
// Test that fs.readFileSync and fs.writeFileSync accept all valid
// UTF-8 encoding names (case-insensitive) for the fast path.
// Refs: https://github.com/nodejs/node/issues/49888
const common = require('../common');
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const testFile = path.join(tmpdir.path, 'test-utf8-encoding.txt');
const testContent = 'Hello, World! 你好,世界!';
// All valid UTF-8 encoding variants that should use the fast path
const utf8Variants = [
'utf8',
'utf-8',
'UTF8',
'UTF-8',
'Utf8',
'Utf-8',
'uTf8',
'uTf-8',
'utF8',
'utF-8',
'UTf8',
'UTf-8',
'uTF8',
'uTF-8',
];
// Test writeFileSync with all UTF-8 variants
for (const encoding of utf8Variants) {
const testPath = path.join(tmpdir.path, `test-write-${encoding}.txt`);
// Should not throw and should write the file correctly
fs.writeFileSync(testPath, testContent, { encoding });
// Verify the file was written correctly
const content = fs.readFileSync(testPath, 'utf8');
assert.strictEqual(content, testContent,
`writeFileSync should work with encoding "${encoding}"`);
}
// Test readFileSync with all UTF-8 variants
for (const encoding of utf8Variants) {
const testPath = path.join(tmpdir.path, `test-read-${encoding}.txt`);
// Create a test file
fs.writeFileSync(testPath, testContent, 'utf8');
// Should read the file correctly with any UTF-8 variant
const content = fs.readFileSync(testPath, { encoding });
assert.strictEqual(content, testContent,
`readFileSync should work with encoding "${encoding}"`);
}
// Test that non-UTF-8 encodings still work correctly
const otherEncodings = ['ascii', 'base64', 'hex', 'latin1'];
for (const encoding of otherEncodings) {
const testPath = path.join(tmpdir.path, `test-${encoding}.txt`);
// These should not use the UTF-8 fast path but should still work
fs.writeFileSync(testPath, testContent, { encoding });
const content = fs.readFileSync(testPath, { encoding });
assert.strictEqual(content, testContent,
`Should work with encoding "${encoding}"`);
}
console.log('All tests passed!');