summaryrefslogtreecommitdiff
path: root/test/parallel/test-fs-stat-bigint.js
blob: 943c0d55d86ba843e236815080ce358156460964 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';

const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const promiseFs = require('fs').promises;
const path = require('path');
const tmpdir = require('../common/tmpdir');
const { isDate } = require('util').types;

tmpdir.refresh();

const fn = path.join(tmpdir.path, 'test-file');
fs.writeFileSync(fn, 'test');

let link;
if (!common.isWindows) {
  link = path.join(tmpdir.path, 'symbolic-link');
  fs.symlinkSync(fn, link);
}

function verifyStats(bigintStats, numStats) {
  for (const key of Object.keys(numStats)) {
    const val = numStats[key];
    if (isDate(val)) {
      const time = val.getTime();
      const time2 = bigintStats[key].getTime();
      assert(
        Math.abs(time - time2) < 2,
        `difference of ${key}.getTime() should < 2.\n` +
        `Number version ${time}, BigInt version ${time2}n`);
    } else if (key === 'mode') {
      assert.strictEqual(bigintStats[key], BigInt(val));
      assert.strictEqual(
        bigintStats.isBlockDevice(),
        numStats.isBlockDevice()
      );
      assert.strictEqual(
        bigintStats.isCharacterDevice(),
        numStats.isCharacterDevice()
      );
      assert.strictEqual(
        bigintStats.isDirectory(),
        numStats.isDirectory()
      );
      assert.strictEqual(
        bigintStats.isFIFO(),
        numStats.isFIFO()
      );
      assert.strictEqual(
        bigintStats.isFile(),
        numStats.isFile()
      );
      assert.strictEqual(
        bigintStats.isSocket(),
        numStats.isSocket()
      );
      assert.strictEqual(
        bigintStats.isSymbolicLink(),
        numStats.isSymbolicLink()
      );
    } else if (common.isWindows && (key === 'blksize' || key === 'blocks')) {
      assert.strictEqual(bigintStats[key], undefined);
      assert.strictEqual(numStats[key], undefined);
    } else if (Number.isSafeInteger(val)) {
      assert.strictEqual(bigintStats[key], BigInt(val));
    } else {
      assert(
        Math.abs(Number(bigintStats[key]) - val) < 1,
        `${key} is not a safe integer, difference should < 1.\n` +
        `Number version ${val}, BigInt version ${bigintStats[key]}n`);
    }
  }
}

{
  const bigintStats = fs.statSync(fn, { bigint: true });
  const numStats = fs.statSync(fn);
  verifyStats(bigintStats, numStats);
}

if (!common.isWindows) {
  const bigintStats = fs.lstatSync(link, { bigint: true });
  const numStats = fs.lstatSync(link);
  verifyStats(bigintStats, numStats);
}

{
  const fd = fs.openSync(fn, 'r');
  const bigintStats = fs.fstatSync(fd, { bigint: true });
  const numStats = fs.fstatSync(fd);
  verifyStats(bigintStats, numStats);
  fs.closeSync(fd);
}

{
  fs.stat(fn, { bigint: true }, (err, bigintStats) => {
    fs.stat(fn, (err, numStats) => {
      verifyStats(bigintStats, numStats);
    });
  });
}

if (!common.isWindows) {
  fs.lstat(link, { bigint: true }, (err, bigintStats) => {
    fs.lstat(link, (err, numStats) => {
      verifyStats(bigintStats, numStats);
    });
  });
}

{
  const fd = fs.openSync(fn, 'r');
  fs.fstat(fd, { bigint: true }, (err, bigintStats) => {
    fs.fstat(fd, (err, numStats) => {
      verifyStats(bigintStats, numStats);
      fs.closeSync(fd);
    });
  });
}

(async function() {
  const bigintStats = await promiseFs.stat(fn, { bigint: true });
  const numStats = await promiseFs.stat(fn);
  verifyStats(bigintStats, numStats);
})();

if (!common.isWindows) {
  (async function() {
    const bigintStats = await promiseFs.lstat(link, { bigint: true });
    const numStats = await promiseFs.lstat(link);
    verifyStats(bigintStats, numStats);
  })();
}

(async function() {
  const handle = await promiseFs.open(fn, 'r');
  const bigintStats = await handle.stat({ bigint: true });
  const numStats = await handle.stat();
  verifyStats(bigintStats, numStats);
  await handle.close();
})();