forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-operations-with-surrogate-pairs.js
More file actions
48 lines (38 loc) · 1.48 KB
/
Copy pathfile-operations-with-surrogate-pairs.js
File metadata and controls
48 lines (38 loc) · 1.48 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
'use strict';
const fs = require('node:fs');
const path = require('path');
const assert = require('assert/strict');
const { describe, it } = require('node:test');
describe('File operations with filenames containing surrogate pairs', () => {
it('should write, read, and delete a file with surrogate pairs in the filename', () => {
// Create a temporary directory
const tempdir = fs.mkdtempSync('emoji-fruit-🍇 🍈 🍉 🍊 🍋');
assert.strictEqual(fs.existsSync(tempdir), true);
const filename = '🚀🔥🛸.txt';
const content = 'Test content';
// Write content to a file
fs.writeFileSync(path.join(tempdir, filename), content);
// Read content from the file
const readContent = fs.readFileSync(path.join(tempdir, filename), 'utf8');
// Check if the content matches
assert.strictEqual(readContent, content);
// Get directory contents
const dirs = fs.readdirSync(tempdir);
assert.strictEqual(dirs.length > 0, true);
// Check if the file is in the directory contents
let match = false;
for (let i = 0; i < dirs.length; i++) {
if (dirs[i].endsWith(filename)) {
match = true;
break;
}
}
assert.strictEqual(match, true);
// Delete the file
fs.unlinkSync(path.join(tempdir, filename));
assert.strictEqual(fs.existsSync(path.join(tempdir, filename)), false);
// Remove the temporary directory
fs.rmdirSync(tempdir);
assert.strictEqual(fs.existsSync(tempdir), false);
});
});