2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-06-24 18:57:08 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
assertThrows,
|
|
|
|
assertThrowsAsync,
|
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 } },
|
2020-03-04 11:31:14 -05:00
|
|
|
function chmodSyncSuccess(): void {
|
|
|
|
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 } },
|
|
|
|
function chmodSyncUrl(): void {
|
|
|
|
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
|
|
|
},
|
|
|
|
function chmodSyncSymlinkSuccess(): void {
|
|
|
|
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
|
|
|
);
|
|
|
|
|
|
|
|
unitTest({ perms: { write: true } }, function chmodSyncFailure(): void {
|
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
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { write: false } }, function chmodSyncPerm(): void {
|
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 } },
|
2020-03-04 11:31:14 -05:00
|
|
|
async function chmodSuccess(): Promise<void> {
|
|
|
|
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 } },
|
|
|
|
async function chmodUrl(): Promise<void> {
|
|
|
|
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
|
|
|
},
|
|
|
|
async function chmodSymlinkSuccess(): Promise<void> {
|
|
|
|
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
|
|
|
);
|
|
|
|
|
|
|
|
unitTest({ perms: { write: true } }, async function chmodFailure(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(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
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { write: false } }, async function chmodPerm(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(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
|
|
|
});
|