forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chmod_test.ts
134 lines (113 loc) · 3.97 KB
/
chmod_test.ts
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
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { testPerm, assertEquals } from "./test_util.ts";
const isNotWindows = Deno.build.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);
assertEquals(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);
assertEquals(fileInfo.mode & 0o777, 0o777);
symlinkInfo = Deno.lstatSync(symlinkName);
assertEquals(symlinkInfo.mode & 0o777, symlinkMode);
});
}
testPerm({ write: true }, function chmodSyncFailure() {
let err;
try {
const filename = "/badfile.txt";
Deno.chmodSync(filename, 0o777);
} catch (e) {
err = e;
}
assertEquals(err.kind, Deno.ErrorKind.NotFound);
assertEquals(err.name, "NotFound");
});
testPerm({ write: false }, function chmodSyncPerm() {
let err;
try {
Deno.chmodSync("/somefile.txt", 0o777);
} catch (e) {
err = e;
}
assertEquals(err.kind, Deno.ErrorKind.PermissionDenied);
assertEquals(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);
assertEquals(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);
assertEquals(fileInfo.mode & 0o777, 0o777);
symlinkInfo = Deno.lstatSync(symlinkName);
assertEquals(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;
}
assertEquals(err.kind, Deno.ErrorKind.NotFound);
assertEquals(err.name, "NotFound");
});
testPerm({ write: false }, async function chmodPerm() {
let err;
try {
await Deno.chmod("/somefile.txt", 0o777);
} catch (e) {
err = e;
}
assertEquals(err.kind, Deno.ErrorKind.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
});