summaryrefslogtreecommitdiff
path: root/tests/unit_node/_fs/_fs_statfs_test.ts
blob: fc85bcf174ec5f2f800e8b9f3deca2f1ccda24bb (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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import * as fs from "node:fs";
import { assertEquals, assertRejects } from "@std/assert";
import * as path from "@std/path";

function assertStatFs(
  statFs: fs.StatsFsBase<unknown>,
  { bigint = false } = {},
) {
  assertEquals(statFs.constructor.name, "StatFs");
  const expectedType = bigint ? "bigint" : "number";
  assertEquals(typeof statFs.type, expectedType);
  assertEquals(typeof statFs.bsize, expectedType);
  assertEquals(typeof statFs.blocks, expectedType);
  assertEquals(typeof statFs.bfree, expectedType);
  assertEquals(typeof statFs.bavail, expectedType);
  assertEquals(typeof statFs.files, expectedType);
  assertEquals(typeof statFs.ffree, expectedType);
  if (Deno.build.os == "windows") {
    assertEquals(statFs.type, bigint ? 0n : 0);
    assertEquals(statFs.files, bigint ? 0n : 0);
    assertEquals(statFs.ffree, bigint ? 0n : 0);
  }
}

const filePath = path.fromFileUrl(import.meta.url);

Deno.test({
  name: "fs.statfs()",
  async fn() {
    await new Promise<fs.StatsFsBase<unknown>>((resolve, reject) => {
      fs.statfs(filePath, (err, statFs) => {
        if (err) reject(err);
        resolve(statFs);
      });
    }).then((statFs) => assertStatFs(statFs));
  },
});

Deno.test({
  name: "fs.statfs() bigint",
  async fn() {
    await new Promise<fs.StatsFsBase<unknown>>((resolve, reject) => {
      fs.statfs(filePath, { bigint: true }, (err, statFs) => {
        if (err) reject(err);
        resolve(statFs);
      });
    }).then((statFs) => assertStatFs(statFs, { bigint: true }));
  },
});

Deno.test({
  name: "fs.statfsSync()",
  fn() {
    const statFs = fs.statfsSync(filePath);
    assertStatFs(statFs);
  },
});

Deno.test({
  name: "fs.statfsSync() bigint",
  fn() {
    const statFs = fs.statfsSync(filePath, { bigint: true });
    assertStatFs(statFs, { bigint: true });
  },
});

Deno.test({
  name: "fs.statfs() non-existent path",
  async fn() {
    const nonExistentPath = path.join(filePath, "../non-existent");
    await assertRejects(async () => {
      await fs.promises.statfs(nonExistentPath);
    }, "NotFound");
  },
});