|
1 | 1 | import test from 'ava' |
2 | 2 | import { statSync, stat, lstatSync, lstat } from '../index.js' |
3 | 3 | import * as nodeFs from 'node:fs' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { tmpdir } from 'node:os' |
| 6 | + |
| 7 | +function tmpDir(): string { |
| 8 | + const dir = join(tmpdir(), `hyper-fs-test-stat-${Date.now()}-${Math.random().toString(36).slice(2)}`) |
| 9 | + nodeFs.mkdirSync(dir, { recursive: true }) |
| 10 | + return dir |
| 11 | +} |
4 | 12 |
|
5 | 13 | test('statSync: should return stats for a file', (t) => { |
6 | 14 | const s = statSync('./package.json') |
@@ -62,3 +70,55 @@ test('statSync: atimeMs/mtimeMs/ctimeMs/birthtimeMs should be numbers', (t) => { |
62 | 70 | t.is(typeof s.birthtimeMs, 'number') |
63 | 71 | t.true(s.mtimeMs > 0) |
64 | 72 | }) |
| 73 | + |
| 74 | +test('statSync: atime/mtime/ctime/birthtime should be Date objects', (t) => { |
| 75 | + const s = statSync('./package.json') |
| 76 | + t.true(s.atime instanceof Date) |
| 77 | + t.true(s.mtime instanceof Date) |
| 78 | + t.true(s.ctime instanceof Date) |
| 79 | + t.true(s.birthtime instanceof Date) |
| 80 | + t.true(s.mtime.getTime() > 0) |
| 81 | +}) |
| 82 | + |
| 83 | +test('statSync: atime.getTime() should be close to atimeMs', (t) => { |
| 84 | + const s = statSync('./package.json') |
| 85 | + t.true(Math.abs(s.atime.getTime() - s.atimeMs) < 1000) |
| 86 | +}) |
| 87 | + |
| 88 | +test('statSync: should match node:fs atime/mtime Date values', (t) => { |
| 89 | + const nodeStat = nodeFs.statSync('./package.json') |
| 90 | + const hyperStat = statSync('./package.json') |
| 91 | + t.is(hyperStat.mtime.getTime(), nodeStat.mtime.getTime()) |
| 92 | +}) |
| 93 | + |
| 94 | +test('lstatSync: dual-run — symlink should report isSymbolicLink()', (t) => { |
| 95 | + const dir = tmpDir() |
| 96 | + const target = join(dir, 'target.txt') |
| 97 | + const link = join(dir, 'link.txt') |
| 98 | + nodeFs.writeFileSync(target, 'hello') |
| 99 | + nodeFs.symlinkSync(target, link) |
| 100 | + |
| 101 | + const nodeLstat = nodeFs.lstatSync(link) |
| 102 | + const hyperLstat = lstatSync(link) |
| 103 | + |
| 104 | + t.is(hyperLstat.isSymbolicLink(), nodeLstat.isSymbolicLink()) |
| 105 | + t.true(hyperLstat.isSymbolicLink()) |
| 106 | + t.is(hyperLstat.isFile(), nodeLstat.isFile()) |
| 107 | + t.false(hyperLstat.isFile()) |
| 108 | +}) |
| 109 | + |
| 110 | +test('statSync: dual-run — stat follows symlink (shows target not link)', (t) => { |
| 111 | + const dir = tmpDir() |
| 112 | + const target = join(dir, 'target.txt') |
| 113 | + const link = join(dir, 'link.txt') |
| 114 | + nodeFs.writeFileSync(target, 'hello') |
| 115 | + nodeFs.symlinkSync(target, link) |
| 116 | + |
| 117 | + const nodeStat = nodeFs.statSync(link) |
| 118 | + const hyperStat = statSync(link) |
| 119 | + |
| 120 | + t.is(hyperStat.isFile(), nodeStat.isFile()) |
| 121 | + t.true(hyperStat.isFile()) |
| 122 | + t.is(hyperStat.isSymbolicLink(), nodeStat.isSymbolicLink()) |
| 123 | + t.false(hyperStat.isSymbolicLink()) |
| 124 | +}) |
0 commit comments