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

unitTest(
  { perms: { read: true, write: true } },
  function renameSyncSuccess(): void {
    const testDir = Deno.makeTempDirSync();
    const oldpath = testDir + "/oldpath";
    const newpath = testDir + "/newpath";
    Deno.mkdirSync(oldpath);
    Deno.renameSync(oldpath, newpath);
    const newPathInfo = Deno.statSync(newpath);
    assert(newPathInfo.isDirectory());

    let caughtErr = false;
    let oldPathInfo;

    try {
      oldPathInfo = Deno.statSync(oldpath);
    } catch (e) {
      caughtErr = true;
      assert(e instanceof Deno.errors.NotFound);
    }
    assert(caughtErr);
    assertEquals(oldPathInfo, undefined);
  }
);

unitTest(
  { perms: { read: false, write: true } },
  function renameSyncReadPerm(): void {
    let err;
    try {
      const oldpath = "/oldbaddir";
      const newpath = "/newbaddir";
      Deno.renameSync(oldpath, newpath);
    } catch (e) {
      err = e;
    }
    assert(err instanceof Deno.errors.PermissionDenied);
    assertEquals(err.name, "PermissionDenied");
  }
);

unitTest(
  { perms: { read: true, write: false } },
  function renameSyncWritePerm(): void {
    let err;
    try {
      const oldpath = "/oldbaddir";
      const newpath = "/newbaddir";
      Deno.renameSync(oldpath, newpath);
    } catch (e) {
      err = e;
    }
    assert(err instanceof Deno.errors.PermissionDenied);
    assertEquals(err.name, "PermissionDenied");
  }
);

unitTest(
  { perms: { read: true, write: true } },
  async function renameSuccess(): Promise<void> {
    const testDir = Deno.makeTempDirSync();
    const oldpath = testDir + "/oldpath";
    const newpath = testDir + "/newpath";
    Deno.mkdirSync(oldpath);
    await Deno.rename(oldpath, newpath);
    const newPathInfo = Deno.statSync(newpath);
    assert(newPathInfo.isDirectory());

    let caughtErr = false;
    let oldPathInfo;

    try {
      oldPathInfo = Deno.statSync(oldpath);
    } catch (e) {
      caughtErr = true;
      assert(e instanceof Deno.errors.NotFound);
    }
    assert(caughtErr);
    assertEquals(oldPathInfo, undefined);
  }
);