summaryrefslogtreecommitdiff
path: root/js/chmod_test.ts
blob: e3676b5472fe6865dba6fe1c2647c84096bdd065 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { testPerm, assertEqual } from "./test_util.ts";
import * as deno from "deno";

const isNotWindows = deno.platform.os !== "win";

testPerm({ read: true, write: true }, function chmodSyncSuccess() {
  const enc = new TextEncoder();
  const data = enc.encode("Hello");
  const tempDir = deno.makeTempDirSync();
  const filename = tempDir + "/test.txt";
  deno.writeFileSync(filename, data, { perm: 0o666 });

  // On windows no effect, but should not crash
  deno.chmodSync(filename, 0o777);

  // Check success when not on windows
  if (isNotWindows) {
    const fileInfo = deno.statSync(filename);
    assertEqual(fileInfo.mode & 0o777, 0o777);
  }
});

// Check symlink when not on windows
if (isNotWindows) {
  testPerm({ read: true, write: true }, function chmodSyncSymlinkSuccess() {
    const enc = new TextEncoder();
    const data = enc.encode("Hello");
    const tempDir = deno.makeTempDirSync();

    const filename = tempDir + "/test.txt";
    deno.writeFileSync(filename, data, { perm: 0o666 });
    const symlinkName = tempDir + "/test_symlink.txt";
    deno.symlinkSync(filename, symlinkName);

    let symlinkInfo = deno.lstatSync(symlinkName);
    const symlinkMode = symlinkInfo.mode & 0o777; // platform dependent

    deno.chmodSync(symlinkName, 0o777);

    // Change actual file mode, not symlink
    const fileInfo = deno.statSync(filename);
    assertEqual(fileInfo.mode & 0o777, 0o777);
    symlinkInfo = deno.lstatSync(symlinkName);
    assertEqual(symlinkInfo.mode & 0o777, symlinkMode);
  });
}

testPerm({ write: true }, function chmodSyncFailure() {
  let err;
  try {
    const filename = "/badfile.txt";
    deno.chmodSync(filename, 0o777);
  } catch (e) {
    err = e;
  }
  assertEqual(err.kind, deno.ErrorKind.NotFound);
  assertEqual(err.name, "NotFound");
});

testPerm({ write: false }, function chmodSyncPerm() {
  let err;
  try {
    deno.chmodSync("/somefile.txt", 0o777);
  } catch (e) {
    err = e;
  }
  assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
  assertEqual(err.name, "PermissionDenied");
});

testPerm({ read: true, write: true }, async function chmodSuccess() {
  const enc = new TextEncoder();
  const data = enc.encode("Hello");
  const tempDir = deno.makeTempDirSync();
  const filename = tempDir + "/test.txt";
  deno.writeFileSync(filename, data, { perm: 0o666 });

  // On windows no effect, but should not crash
  await deno.chmod(filename, 0o777);

  // Check success when not on windows
  if (isNotWindows) {
    const fileInfo = deno.statSync(filename);
    assertEqual(fileInfo.mode & 0o777, 0o777);
  }
});

// Check symlink when not on windows
if (isNotWindows) {
  testPerm({ read: true, write: true }, async function chmodSymlinkSuccess() {
    const enc = new TextEncoder();
    const data = enc.encode("Hello");
    const tempDir = deno.makeTempDirSync();

    const filename = tempDir + "/test.txt";
    deno.writeFileSync(filename, data, { perm: 0o666 });
    const symlinkName = tempDir + "/test_symlink.txt";
    deno.symlinkSync(filename, symlinkName);

    let symlinkInfo = deno.lstatSync(symlinkName);
    const symlinkMode = symlinkInfo.mode & 0o777; // platform dependent

    await deno.chmod(symlinkName, 0o777);

    // Just change actual file mode, not symlink
    const fileInfo = deno.statSync(filename);
    assertEqual(fileInfo.mode & 0o777, 0o777);
    symlinkInfo = deno.lstatSync(symlinkName);
    assertEqual(symlinkInfo.mode & 0o777, symlinkMode);
  });
}

testPerm({ write: true }, async function chmodFailure() {
  let err;
  try {
    const filename = "/badfile.txt";
    await deno.chmod(filename, 0o777);
  } catch (e) {
    err = e;
  }
  assertEqual(err.kind, deno.ErrorKind.NotFound);
  assertEqual(err.name, "NotFound");
});

testPerm({ write: false }, async function chmodPerm() {
  let err;
  try {
    await deno.chmod("/somefile.txt", 0o777);
  } catch (e) {
    err = e;
  }
  assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
  assertEqual(err.name, "PermissionDenied");
});