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

testPerm({ 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;
    assertEquals(e.kind, Deno.ErrorKind.NotFound);
  }
  assert(caughtErr);
  assertEquals(oldPathInfo, undefined);
});

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

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

testPerm({ 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;
    assertEquals(e.kind, Deno.ErrorKind.NotFound);
  }
  assert(caughtErr);
  assertEquals(oldPathInfo, undefined);
});