forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-fs-mkdtempDisposableSync.js
More file actions
107 lines (77 loc) · 2.57 KB
/
test-fs-mkdtempDisposableSync.js
File metadata and controls
107 lines (77 loc) · 2.57 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
// Basic usage
{
const result = fs.mkdtempDisposableSync(tmpdir.resolve('foo.'));
assert.strictEqual(path.basename(result.path).length, 'foo.XXXXXX'.length);
assert.strictEqual(path.dirname(result.path), tmpdir.path);
assert(fs.existsSync(result.path));
result.remove();
assert(!fs.existsSync(result.path));
// Second removal does not throw error
result.remove();
}
// Usage with [Symbol.dispose]()
{
const result = fs.mkdtempDisposableSync(tmpdir.resolve('foo.'));
assert(fs.existsSync(result.path));
result[Symbol.dispose]();
assert(!fs.existsSync(result.path));
// Second removal does not throw error
result[Symbol.dispose]();
}
// `chdir`` does not affect removal
{
const originalCwd = process.cwd();
process.chdir(tmpdir.path);
const first = fs.mkdtempDisposableSync('first.');
const second = fs.mkdtempDisposableSync('second.');
const fullFirstPath = path.join(tmpdir.path, first.path);
const fullSecondPath = path.join(tmpdir.path, second.path);
assert(fs.existsSync(fullFirstPath));
assert(fs.existsSync(fullSecondPath));
process.chdir(fullFirstPath);
second.remove();
assert(!fs.existsSync(fullSecondPath));
process.chdir(tmpdir.path);
first.remove();
assert(!fs.existsSync(fullFirstPath));
process.chdir(originalCwd);
}
// Errors from cleanup are thrown
{
const base = fs.mkdtempDisposableSync(tmpdir.resolve('foo.'));
if (common.isWindows) {
// On Windows we can prevent removal by holding a file open
const testFile = path.join(base.path, 'locked-file.txt');
fs.writeFileSync(testFile, 'test');
const fd = fs.openSync(testFile, 'r');
assert.throws(() => {
base.remove();
}, /EBUSY|ENOTEMPTY|EPERM/);
fs.closeSync(fd);
fs.unlinkSync(testFile);
// Removal works once file is closed
base.remove();
assert(!fs.existsSync(base.path));
} else {
// On Unix we can prevent removal by making the parent directory read-only
const child = fs.mkdtempDisposableSync(path.join(base.path, 'bar.'));
const originalMode = fs.statSync(base.path).mode;
fs.chmodSync(base.path, 0o444);
assert.throws(() => {
child.remove();
}, /EACCES|EPERM/);
fs.chmodSync(base.path, originalMode);
// Removal works once permissions are reset
child.remove();
assert(!fs.existsSync(child.path));
base.remove();
assert(!fs.existsSync(base.path));
}
}