summaryrefslogtreecommitdiff
path: root/cli/js/realpath_test.ts
blob: 7443c78973940b2663d09ae35af4d0a57057e844 (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
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { testPerm, assert, assertEquals } from "./test_util.ts";

testPerm({ read: true }, function realpathSyncSuccess(): void {
  const incompletePath = "cli/tests/fixture.json";
  const realPath = Deno.realpathSync(incompletePath);
  if (Deno.build.os !== "win") {
    assert(realPath.startsWith("/"));
  } else {
    assert(/^[A-Z]/.test(realPath));
  }
  assert(realPath.endsWith(incompletePath));
});

if (Deno.build.os !== "win") {
  testPerm({ read: true, write: true }, function realpathSyncSymlink(): void {
    const testDir = Deno.makeTempDirSync();
    const target = testDir + "/target";
    const symlink = testDir + "/symln";
    Deno.mkdirSync(target);
    Deno.symlinkSync(target, symlink);
    const targetPath = Deno.realpathSync(symlink);
    assert(targetPath.startsWith("/"));
    assert(targetPath.endsWith("/target"));
  });
}

testPerm({ read: false }, function realpathSyncPerm(): void {
  let caughtError = false;
  try {
    Deno.realpathSync("some_file");
  } catch (e) {
    caughtError = true;
    assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
    assertEquals(e.name, "PermissionDenied");
  }
  assert(caughtError);
});

testPerm({ read: true }, function realpathSyncNotFound(): void {
  let caughtError = false;
  try {
    Deno.realpathSync("bad_filename");
  } catch (e) {
    caughtError = true;
    assertEquals(e.kind, Deno.ErrorKind.NotFound);
  }
  assert(caughtError);
});

testPerm({ read: true }, async function realpathSuccess(): Promise<void> {
  const incompletePath = "cli/tests/fixture.json";
  const realPath = await Deno.realpath(incompletePath);
  if (Deno.build.os !== "win") {
    assert(realPath.startsWith("/"));
  } else {
    assert(/^[A-Z]/.test(realPath));
  }
  assert(realPath.endsWith(incompletePath));
});

if (Deno.build.os !== "win") {
  testPerm(
    { read: true, write: true },
    async function realpathSymlink(): Promise<void> {
      const testDir = Deno.makeTempDirSync();
      const target = testDir + "/target";
      const symlink = testDir + "/symln";
      Deno.mkdirSync(target);
      Deno.symlinkSync(target, symlink);
      const targetPath = await Deno.realpath(symlink);
      assert(targetPath.startsWith("/"));
      assert(targetPath.endsWith("/target"));
    }
  );
}

testPerm({ read: false }, async function realpathPerm(): Promise<void> {
  let caughtError = false;
  try {
    await Deno.realpath("some_file");
  } catch (e) {
    caughtError = true;
    assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
    assertEquals(e.name, "PermissionDenied");
  }
  assert(caughtError);
});

testPerm({ read: true }, async function realpathNotFound(): Promise<void> {
  let caughtError = false;
  try {
    await Deno.realpath("bad_filename");
  } catch (e) {
    caughtError = true;
    assertEquals(e.kind, Deno.ErrorKind.NotFound);
  }
  assert(caughtError);
});