-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathfs.test.ts
More file actions
54 lines (49 loc) · 1.77 KB
/
fs.test.ts
File metadata and controls
54 lines (49 loc) · 1.77 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
import { strict as assert } from 'node:assert';
import path from 'node:path';
import { Stats } from 'node:fs';
import { fileURLToPath } from 'node:url';
import * as utility from '../src/index.js';
import { exists } from '../src/index.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
describe('test/fs.test.ts', () => {
describe('exists()', () => {
it('should work', async () => {
let stats = await exists(__filename);
assert(stats instanceof Stats);
assert(stats.size > 0, 'stats.size > 0');
assert.equal(stats.isFile(), true);
assert.equal(stats.isDirectory(), false);
stats = await utility.exists(__dirname);
assert(stats instanceof Stats);
// assert(stats.size > 0, 'stats.size > 0');
assert.equal(stats.isDirectory(), true);
assert.equal(stats.isFile(), false);
assert.equal(await exists(__dirname + '/nonexistent'), false);
});
it('should throw error on Linux', async () => {
if (process.platform !== 'linux') {
return;
}
await assert.rejects(async () => {
await exists('/root/../../../../../etc/passwd');
}, (err: any) => {
// Error: EACCES: permission denied, stat '/root/../../../../../etc/passwd'
assert.equal(err.code, 'EACCES');
return true;
});
});
it.skip('should throw error on win32', async () => {
if (process.platform !== 'win32') {
return;
}
await assert.rejects(async () => {
await exists('C:\\Windows\\System32\\drivers\\etc\\hosts');
}, (err: any) => {
// Error: EACCES: permission denied, stat 'C:\Windows\System32\drivers\etc\hosts'
assert.equal(err.code, 'EPERM');
return true;
});
});
});
});