summaryrefslogtreecommitdiff
path: root/js/rename_test.ts
blob: 450760dae61b6a8cc0721e602c1a620ca72678d5 (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
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { test, testPerm, assert, assertEqual } from "./test_util.ts";
import * as deno from "deno";

testPerm({ write: true }, function renameSyncSuccess() {
  const testDir = deno.makeTempDirSync() + "/test-rename-sync";
  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;
    assertEqual(e.kind, deno.ErrorKind.NotFound);
  }
  assert(caughtErr);
  assertEqual(oldPathInfo, undefined);
});

testPerm({ write: false }, function renameSyncPerm() {
  let err;
  try {
    const oldpath = "/oldbaddir";
    const newpath = "/newbaddir";
    deno.renameSync(oldpath, newpath);
  } catch (e) {
    err = e;
  }
  assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
  assertEqual(err.name, "PermissionDenied");
});

testPerm({ write: true }, async function renameSuccess() {
  const testDir = deno.makeTempDirSync() + "/test-rename";
  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;
    assertEqual(e.kind, deno.ErrorKind.NotFound);
  }
  assert(caughtErr);
  assertEqual(oldPathInfo, undefined);
});