forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-fs-cpsync-error-handling.js
More file actions
86 lines (73 loc) Β· 2.54 KB
/
Copy pathtest-fs-cpsync-error-handling.js
File metadata and controls
86 lines (73 loc) Β· 2.54 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
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
// Test that fs.cpSync properly handles directory iteration errors
// instead of causing process abort.
//
// This test ensures that when directory_iterator construction fails
// (e.g., due to permission issues or malformed paths), the error
// is properly converted to a JavaScript exception rather than
// triggering std::terminate or __libcpp_verbose_abort.
const common = require('../common');
const { cpSync, mkdirSync, writeFileSync } = require('fs');
const { join } = require('path');
const assert = require('assert');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
// Test 1: Copying from non-existent directory should throw, not abort
{
const nonExistent = join(tmpdir.path, 'does-not-exist');
const dest = join(tmpdir.path, 'dest1');
assert.throws(
() => cpSync(nonExistent, dest, { recursive: true }),
{
code: 'ENOENT',
message: /ENOENT/
},
'cpSync should throw ENOENT for non-existent source directory'
);
}
// Test 2: Copying directory with unreadable subdirectory
// (This test is platform-specific and may be skipped on Windows)
if (!common.isWindows) {
const srcDir = join(tmpdir.path, 'src-unreadable');
const unreadableSubdir = join(srcDir, 'unreadable');
const dest = join(tmpdir.path, 'dest2');
mkdirSync(srcDir);
mkdirSync(unreadableSubdir);
writeFileSync(join(unreadableSubdir, 'file.txt'), 'content');
try {
require('fs').chmodSync(unreadableSubdir, 0o000);
// Should throw error, not abort
assert.throws(
() => cpSync(srcDir, dest, { recursive: true }),
{
code: 'EACCES',
},
'cpSync should throw EACCES for unreadable directory'
);
} finally {
// Restore permissions for cleanup
require('fs').chmodSync(unreadableSubdir, 0o755);
}
}
// Test 3: Basic successful copy to ensure fix doesn't break normal operation
{
const srcDir = join(tmpdir.path, 'src-normal');
const dest = join(tmpdir.path, 'dest-normal');
mkdirSync(srcDir);
writeFileSync(join(srcDir, 'file1.txt'), 'content1');
mkdirSync(join(srcDir, 'subdir'));
writeFileSync(join(srcDir, 'subdir', 'file2.txt'), 'content2');
// Should not throw
cpSync(srcDir, dest, { recursive: true });
const fs = require('fs');
assert.ok(fs.existsSync(join(dest, 'file1.txt')));
assert.ok(fs.existsSync(join(dest, 'subdir', 'file2.txt')));
assert.strictEqual(
fs.readFileSync(join(dest, 'file1.txt'), 'utf8'),
'content1'
);
assert.strictEqual(
fs.readFileSync(join(dest, 'subdir', 'file2.txt'), 'utf8'),
'content2'
);
}