summaryrefslogtreecommitdiff
path: root/js/read_dir_test.ts
blob: 54cc468b7eadb477ae403321da81b7ba93015ebc (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
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { testPerm, assert, assertEqual } from "./test_util.ts";

type FileInfo = Deno.FileInfo;

function assertSameContent(files: FileInfo[]) {
  let counter = 0;

  for (const file of files) {
    if (file.name === "subdir") {
      assert(file.isDirectory());
      counter++;
    }

    if (file.name === "002_hello.ts") {
      assertEqual(file.path, `tests/${file.name}`);
      assertEqual(file.mode!, Deno.statSync(`tests/${file.name}`).mode!);
      counter++;
    }
  }

  assertEqual(counter, 2);
}

testPerm({ read: true }, function readDirSyncSuccess() {
  const files = Deno.readDirSync("tests/");
  assertSameContent(files);
});

testPerm({ read: false }, function readDirSyncPerm() {
  let caughtError = false;
  try {
    const files = Deno.readDirSync("tests/");
  } catch (e) {
    caughtError = true;
    assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
    assertEqual(e.name, "PermissionDenied");
  }
  assert(caughtError);
});

testPerm({ read: true }, function readDirSyncNotDir() {
  let caughtError = false;
  let src;

  try {
    src = Deno.readDirSync("package.json");
  } catch (err) {
    caughtError = true;
    assertEqual(err.kind, Deno.ErrorKind.Other);
  }
  assert(caughtError);
  assertEqual(src, undefined);
});

testPerm({ read: true }, function readDirSyncNotFound() {
  let caughtError = false;
  let src;

  try {
    src = Deno.readDirSync("bad_dir_name");
  } catch (err) {
    caughtError = true;
    assertEqual(err.kind, Deno.ErrorKind.NotFound);
  }
  assert(caughtError);
  assertEqual(src, undefined);
});

testPerm({ read: true }, async function readDirSuccess() {
  const files = await Deno.readDir("tests/");
  assertSameContent(files);
});

testPerm({ read: false }, async function readDirPerm() {
  let caughtError = false;
  try {
    const files = await Deno.readDir("tests/");
  } catch (e) {
    caughtError = true;
    assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
    assertEqual(e.name, "PermissionDenied");
  }
  assert(caughtError);
});