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