-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath-exists-test.js
More file actions
58 lines (44 loc) · 1.82 KB
/
path-exists-test.js
File metadata and controls
58 lines (44 loc) · 1.82 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
'use strict';
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
(async () => {
const mod = await import('path-exists');
const { pathExists, pathExistsSync } = mod;
assert.equal(typeof pathExists, 'function');
assert.equal(typeof pathExistsSync, 'function');
// Set up a temp directory with a real file
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'topn-pathexists-'));
const existingFile = path.join(tmpDir, 'exists.txt');
const missingFile = path.join(tmpDir, 'does-not-exist.txt');
const existingDir = path.join(tmpDir, 'subdir');
fs.writeFileSync(existingFile, 'hello');
fs.mkdirSync(existingDir);
try {
// Async: existing file returns true
assert.equal(await pathExists(existingFile), true);
// Async: non-existent file returns false
assert.equal(await pathExists(missingFile), false);
// Async: existing directory returns true
assert.equal(await pathExists(existingDir), true);
// Async: completely bogus path returns false
assert.equal(await pathExists('/this/path/definitely/does/not/exist'), false);
// Sync: existing file returns true
assert.equal(pathExistsSync(existingFile), true);
// Sync: non-existent file returns false
assert.equal(pathExistsSync(missingFile), false);
// Sync: existing directory returns true
assert.equal(pathExistsSync(existingDir), true);
// Sync: bogus path returns false
assert.equal(pathExistsSync('/this/path/definitely/does/not/exist'), false);
} finally {
// Clean up
fs.rmSync(tmpDir, { recursive: true, force: true });
}
console.log('path-exists-test:ok');
})().catch((err) => {
console.error('path-exists-test:fail');
console.error(err && err.stack ? err.stack : String(err));
process.exit(1);
});