|
| 1 | +import { strict as assert } from 'node:assert'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { Stats } from 'node:fs'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import * as utility from '../src/index.js'; |
| 6 | +import { exists } from '../src/index.js'; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = path.dirname(__filename); |
| 10 | + |
| 11 | +describe('test/fs.test.ts', () => { |
| 12 | + describe('exists()', () => { |
| 13 | + it('should work', async () => { |
| 14 | + let stats = await exists(__filename); |
| 15 | + assert(stats instanceof Stats); |
| 16 | + assert(stats.size > 0, 'stats.size > 0'); |
| 17 | + assert.equal(stats.isFile(), true); |
| 18 | + assert.equal(stats.isDirectory(), false); |
| 19 | + |
| 20 | + stats = await utility.exists(__dirname); |
| 21 | + assert(stats instanceof Stats); |
| 22 | + // assert(stats.size > 0, 'stats.size > 0'); |
| 23 | + assert.equal(stats.isDirectory(), true); |
| 24 | + assert.equal(stats.isFile(), false); |
| 25 | + assert.equal(await exists(__dirname + '/nonexistent'), false); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should throw error on Linux', async () => { |
| 29 | + if (process.platform !== 'linux') { |
| 30 | + return; |
| 31 | + } |
| 32 | + await assert.rejects(async () => { |
| 33 | + await exists('/root/../../../../../etc/passwd'); |
| 34 | + }, (err: any) => { |
| 35 | + // Error: EACCES: permission denied, stat '/root/../../../../../etc/passwd' |
| 36 | + assert.equal(err.code, 'EACCES'); |
| 37 | + return true; |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it.skip('should throw error on win32', async () => { |
| 42 | + if (process.platform !== 'win32') { |
| 43 | + return; |
| 44 | + } |
| 45 | + await assert.rejects(async () => { |
| 46 | + await exists('C:\\Windows\\System32\\drivers\\etc\\hosts'); |
| 47 | + }, (err: any) => { |
| 48 | + // Error: EACCES: permission denied, stat 'C:\Windows\System32\drivers\etc\hosts' |
| 49 | + assert.equal(err.code, 'EPERM'); |
| 50 | + return true; |
| 51 | + }); |
| 52 | + }); |
| 53 | + }); |
| 54 | +}); |
0 commit comments