2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-04 11:31:14 -05:00
|
|
|
import { unitTest, assert, assertEquals } from "./test_util.ts";
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2019-03-06 16:54:58 -05:00
|
|
|
const isNotWindows = Deno.build.os !== "win";
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function chmodSyncSuccess(): void {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const filename = tempDir + "/test.txt";
|
|
|
|
Deno.writeFileSync(filename, data, { perm: 0o666 });
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
// On windows no effect, but should not crash
|
|
|
|
Deno.chmodSync(filename, 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
// Check success when not on windows
|
|
|
|
if (isNotWindows) {
|
2019-04-21 16:40:10 -04:00
|
|
|
const fileInfo = Deno.statSync(filename);
|
2020-02-19 15:36:18 -05:00
|
|
|
assert(fileInfo.mode);
|
2019-04-21 16:40:10 -04:00
|
|
|
assertEquals(fileInfo.mode & 0o777, 0o777);
|
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
}
|
|
|
|
);
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
// Check symlink when not on windows
|
|
|
|
unitTest(
|
|
|
|
{
|
|
|
|
skip: Deno.build.os === "win",
|
|
|
|
perms: { read: true, write: true }
|
|
|
|
},
|
|
|
|
function chmodSyncSymlinkSuccess(): void {
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest({ perms: { write: true } }, function chmodSyncFailure(): void {
|
2018-10-26 16:01:45 -04:00
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
const filename = "/badfile.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.chmodSync(filename, 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof 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 {
|
2018-10-26 16:01:45 -04:00
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.chmodSync("/somefile.txt", 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2018-10-26 16:01:45 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
async function chmodSuccess(): Promise<void> {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const filename = tempDir + "/test.txt";
|
|
|
|
Deno.writeFileSync(filename, data, { perm: 0o666 });
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
// On windows no effect, but should not crash
|
|
|
|
await Deno.chmod(filename, 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
// Check success when not on windows
|
|
|
|
if (isNotWindows) {
|
2019-04-21 16:40:10 -04:00
|
|
|
const fileInfo = Deno.statSync(filename);
|
2020-02-19 15:36:18 -05:00
|
|
|
assert(fileInfo.mode);
|
2019-04-21 16:40:10 -04:00
|
|
|
assertEquals(fileInfo.mode & 0o777, 0o777);
|
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check symlink when not on windows
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{
|
|
|
|
skip: Deno.build.os === "win",
|
|
|
|
perms: { read: true, write: true }
|
|
|
|
},
|
|
|
|
async function chmodSymlinkSuccess(): Promise<void> {
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest({ perms: { write: true } }, async function chmodFailure(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2018-10-26 16:01:45 -04:00
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
const filename = "/badfile.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.chmod(filename, 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof 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
|
|
|
|
> {
|
2018-10-26 16:01:45 -04:00
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.chmod("/somefile.txt", 0o777);
|
2018-10-26 16:01:45 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2018-10-26 16:01:45 -04:00
|
|
|
});
|