1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 01:29:14 -05:00
denoland-deno/tests/unit/rename_test.ts
Matt Mastracci f5e46c9bf2
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.

This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.

While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).

And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.

For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 20:22:13 +00:00

274 lines
6.6 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertEquals,
AssertionError,
assertIsError,
assertThrows,
pathToAbsoluteFileUrl,
} from "./test_util.ts";
function assertMissing(path: string) {
let caughtErr = false;
let info;
try {
info = Deno.lstatSync(path);
} catch (e) {
caughtErr = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtErr);
assertEquals(info, undefined);
}
function assertFile(path: string) {
const info = Deno.lstatSync(path);
assert(info.isFile);
}
function assertDirectory(path: string, mode?: number) {
const info = Deno.lstatSync(path);
assert(info.isDirectory);
if (Deno.build.os !== "windows" && mode !== undefined) {
assertEquals(info.mode! & 0o777, mode & ~Deno.umask());
}
}
Deno.test(
{ permissions: { read: true, write: true } },
function renameSyncSuccess() {
const testDir = Deno.makeTempDirSync();
const oldpath = testDir + "/oldpath";
const newpath = testDir + "/newpath";
Deno.mkdirSync(oldpath);
Deno.renameSync(oldpath, newpath);
assertDirectory(newpath);
assertMissing(oldpath);
},
);
Deno.test(
{ permissions: { read: true, write: true } },
function renameSyncWithURL() {
const testDir = Deno.makeTempDirSync();
const oldpath = testDir + "/oldpath";
const newpath = testDir + "/newpath";
Deno.mkdirSync(oldpath);
Deno.renameSync(
pathToAbsoluteFileUrl(oldpath),
pathToAbsoluteFileUrl(newpath),
);
assertDirectory(newpath);
assertMissing(oldpath);
},
);
Deno.test(
{ permissions: { read: false, write: true } },
function renameSyncReadPerm() {
assertThrows(() => {
const oldpath = "/oldbaddir";
const newpath = "/newbaddir";
Deno.renameSync(oldpath, newpath);
}, Deno.errors.PermissionDenied);
},
);
Deno.test(
{ permissions: { read: true, write: false } },
function renameSyncWritePerm() {
assertThrows(() => {
const oldpath = "/oldbaddir";
const newpath = "/newbaddir";
Deno.renameSync(oldpath, newpath);
}, Deno.errors.PermissionDenied);
},
);
Deno.test(
{ permissions: { read: true, write: true } },
async function renameSuccess() {
const testDir = Deno.makeTempDirSync();
const oldpath = testDir + "/oldpath";
const newpath = testDir + "/newpath";
Deno.mkdirSync(oldpath);
await Deno.rename(oldpath, newpath);
assertDirectory(newpath);
assertMissing(oldpath);
},
);
Deno.test(
{ permissions: { read: true, write: true } },
async function renameWithURL() {
const testDir = Deno.makeTempDirSync();
const oldpath = testDir + "/oldpath";
const newpath = testDir + "/newpath";
Deno.mkdirSync(oldpath);
await Deno.rename(
pathToAbsoluteFileUrl(oldpath),
pathToAbsoluteFileUrl(newpath),
);
assertDirectory(newpath);
assertMissing(oldpath);
},
);
function readFileString(filename: string): string {
const dataRead = Deno.readFileSync(filename);
const dec = new TextDecoder("utf-8");
return dec.decode(dataRead);
}
function writeFileString(filename: string, s: string) {
const enc = new TextEncoder();
const data = enc.encode(s);
Deno.writeFileSync(filename, data, { mode: 0o666 });
}
Deno.test(
{
ignore: Deno.build.os === "windows",
permissions: { read: true, write: true },
},
function renameSyncErrorsUnix() {
const testDir = Deno.makeTempDirSync();
const oldfile = testDir + "/oldfile";
const olddir = testDir + "/olddir";
const emptydir = testDir + "/empty";
const fulldir = testDir + "/dir";
const file = fulldir + "/file";
writeFileString(oldfile, "Hello");
Deno.mkdirSync(olddir);
Deno.mkdirSync(emptydir);
Deno.mkdirSync(fulldir);
writeFileString(file, "world");
assertThrows(
() => {
Deno.renameSync(oldfile, emptydir);
},
Error,
"Is a directory",
);
try {
assertThrows(
() => {
Deno.renameSync(olddir, fulldir);
},
Error,
"Directory not empty",
);
} catch (e) {
// rename syscall may also return EEXIST, e.g. with XFS
assertIsError(
e,
AssertionError,
`Expected error message to include "Directory not empty", but got "File exists`,
);
}
assertThrows(
() => {
Deno.renameSync(olddir, file);
},
Error,
"Not a directory",
);
assertThrows(
() => {
Deno.renameSync(olddir, file);
},
Error,
`rename '${olddir}' -> '${file}'`,
);
const fileLink = testDir + "/fileLink";
const dirLink = testDir + "/dirLink";
const danglingLink = testDir + "/danglingLink";
Deno.symlinkSync(file, fileLink);
Deno.symlinkSync(emptydir, dirLink);
Deno.symlinkSync(testDir + "/nonexistent", danglingLink);
assertThrows(
() => {
Deno.renameSync(olddir, fileLink);
},
Error,
"Not a directory",
);
assertThrows(
() => {
Deno.renameSync(olddir, dirLink);
},
Error,
"Not a directory",
);
assertThrows(
() => {
Deno.renameSync(olddir, danglingLink);
},
Error,
"Not a directory",
);
// should succeed on Unix
Deno.renameSync(olddir, emptydir);
Deno.renameSync(oldfile, dirLink);
Deno.renameSync(dirLink, danglingLink);
assertFile(danglingLink);
assertEquals("Hello", readFileString(danglingLink));
},
);
Deno.test(
{
ignore: Deno.build.os !== "windows",
permissions: { read: true, write: true },
},
function renameSyncErrorsWin() {
const testDir = Deno.makeTempDirSync();
const oldfile = testDir + "/oldfile";
const olddir = testDir + "/olddir";
const emptydir = testDir + "/empty";
const fulldir = testDir + "/dir";
const file = fulldir + "/file";
writeFileString(oldfile, "Hello");
Deno.mkdirSync(olddir);
Deno.mkdirSync(emptydir);
Deno.mkdirSync(fulldir);
writeFileString(file, "world");
assertThrows(
() => {
Deno.renameSync(oldfile, emptydir);
},
Deno.errors.PermissionDenied,
"Access is denied",
);
assertThrows(
() => {
Deno.renameSync(olddir, fulldir);
},
Deno.errors.PermissionDenied,
"Access is denied",
);
assertThrows(
() => {
Deno.renameSync(olddir, emptydir);
},
Deno.errors.PermissionDenied,
"Access is denied",
);
assertThrows(
() => {
Deno.renameSync(olddir, emptydir);
},
Error,
`rename '${olddir}' -> '${emptydir}'`,
);
// should succeed on Windows
Deno.renameSync(olddir, file);
assertDirectory(file);
},
);