2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-06-24 18:57:08 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2021-09-22 09:21:11 -04:00
|
|
|
assertRejects,
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows,
|
2020-09-27 06:22:32 -04:00
|
|
|
unitTest,
|
2020-06-24 18:57:08 -04:00
|
|
|
} from "./test_util.ts";
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", perms: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function chmodSyncSuccess() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const filename = tempDir + "/test.txt";
|
2020-03-07 22:29:12 -05:00
|
|
|
Deno.writeFileSync(filename, data, { mode: 0o666 });
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
Deno.chmodSync(filename, 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2020-03-20 16:03:04 -04:00
|
|
|
const fileInfo = Deno.statSync(filename);
|
|
|
|
assert(fileInfo.mode);
|
|
|
|
assertEquals(fileInfo.mode & 0o777, 0o777);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
unitTest(
|
|
|
|
{ ignore: Deno.build.os === "windows", perms: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function chmodSyncUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fileUrl = new URL(`file://${tempDir}/test.txt`);
|
|
|
|
Deno.writeFileSync(fileUrl, data, { mode: 0o666 });
|
|
|
|
|
|
|
|
Deno.chmodSync(fileUrl, 0o777);
|
|
|
|
|
|
|
|
const fileInfo = Deno.statSync(fileUrl);
|
|
|
|
assert(fileInfo.mode);
|
|
|
|
assertEquals(fileInfo.mode & 0o777, 0o777);
|
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
// Check symlink when not on windows
|
|
|
|
unitTest(
|
|
|
|
{
|
2020-04-28 12:35:23 -04:00
|
|
|
ignore: Deno.build.os === "windows",
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { read: true, write: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
function chmodSyncSymlinkSuccess() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
|
|
|
|
const filename = tempDir + "/test.txt";
|
2020-03-07 22:29:12 -05:00
|
|
|
Deno.writeFileSync(filename, data, { mode: 0o666 });
|
2020-03-04 11:31:14 -05:00
|
|
|
const symlinkName = tempDir + "/test_symlink.txt";
|
|
|
|
Deno.symlinkSync(filename, symlinkName);
|
|
|
|
|
|
|
|
let symlinkInfo = Deno.lstatSync(symlinkName);
|
|
|
|
assert(symlinkInfo.mode);
|
|
|
|
const symlinkMode = symlinkInfo.mode & 0o777; // platform dependent
|
|
|
|
|
|
|
|
Deno.chmodSync(symlinkName, 0o777);
|
|
|
|
|
|
|
|
// Change actual file mode, not symlink
|
|
|
|
const fileInfo = Deno.statSync(filename);
|
|
|
|
assert(fileInfo.mode);
|
|
|
|
assertEquals(fileInfo.mode & 0o777, 0o777);
|
|
|
|
symlinkInfo = Deno.lstatSync(symlinkName);
|
|
|
|
assert(symlinkInfo.mode);
|
|
|
|
assertEquals(symlinkInfo.mode & 0o777, symlinkMode);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { write: true } }, function chmodSyncFailure() {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2018-10-26 16:01:45 -04:00
|
|
|
const filename = "/badfile.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.chmodSync(filename, 0o777);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2018-10-26 16:01:45 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { write: false } }, function chmodSyncPerm() {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.chmodSync("/somefile.txt", 0o777);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2018-10-26 16:01:45 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", perms: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function chmodSuccess() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const filename = tempDir + "/test.txt";
|
2020-03-07 22:29:12 -05:00
|
|
|
Deno.writeFileSync(filename, data, { mode: 0o666 });
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
await Deno.chmod(filename, 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2020-03-20 16:03:04 -04:00
|
|
|
const fileInfo = Deno.statSync(filename);
|
|
|
|
assert(fileInfo.mode);
|
|
|
|
assertEquals(fileInfo.mode & 0o777, 0o777);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
unitTest(
|
|
|
|
{ ignore: Deno.build.os === "windows", perms: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function chmodUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fileUrl = new URL(`file://${tempDir}/test.txt`);
|
|
|
|
Deno.writeFileSync(fileUrl, data, { mode: 0o666 });
|
|
|
|
|
|
|
|
await Deno.chmod(fileUrl, 0o777);
|
|
|
|
|
|
|
|
const fileInfo = Deno.statSync(fileUrl);
|
|
|
|
assert(fileInfo.mode);
|
|
|
|
assertEquals(fileInfo.mode & 0o777, 0o777);
|
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
// Check symlink when not on windows
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{
|
2020-04-28 12:35:23 -04:00
|
|
|
ignore: Deno.build.os === "windows",
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { read: true, write: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function chmodSymlinkSuccess() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
|
|
|
|
const filename = tempDir + "/test.txt";
|
2020-03-07 22:29:12 -05:00
|
|
|
Deno.writeFileSync(filename, data, { mode: 0o666 });
|
2020-03-04 11:31:14 -05:00
|
|
|
const symlinkName = tempDir + "/test_symlink.txt";
|
|
|
|
Deno.symlinkSync(filename, symlinkName);
|
|
|
|
|
|
|
|
let symlinkInfo = Deno.lstatSync(symlinkName);
|
|
|
|
assert(symlinkInfo.mode);
|
|
|
|
const symlinkMode = symlinkInfo.mode & 0o777; // platform dependent
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
await Deno.chmod(symlinkName, 0o777);
|
|
|
|
|
|
|
|
// Just change actual file mode, not symlink
|
|
|
|
const fileInfo = Deno.statSync(filename);
|
|
|
|
assert(fileInfo.mode);
|
|
|
|
assertEquals(fileInfo.mode & 0o777, 0o777);
|
|
|
|
symlinkInfo = Deno.lstatSync(symlinkName);
|
|
|
|
assert(symlinkInfo.mode);
|
|
|
|
assertEquals(symlinkInfo.mode & 0o777, symlinkMode);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { write: true } }, async function chmodFailure() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2018-10-26 16:01:45 -04:00
|
|
|
const filename = "/badfile.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.chmod(filename, 0o777);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2018-10-26 16:01:45 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { write: false } }, async function chmodPerm() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.chmod("/somefile.txt", 0o777);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2018-10-26 16:01:45 -04:00
|
|
|
});
|