0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-10 06:07:03 -04:00
deno/tests/unit/rename_test.ts
Nathan Whitaker ee4c14a550
chore: update to rust 1.85 (#28236)
Updates to use rust 1.85. Doesn't move to the 2024 edition, as that's a
fair bit more involved.

A nice side benefit is that the new rustc version seems to lead to a
slight reduction in binary size (at least on mac):

```
    FILE SIZE   
 -------------- 
  +4.3%  +102Ki    __DATA_CONST,__const
  [NEW] +69.3Ki    __TEXT,__literals
  [NEW] +68.5Ki    Rebase Info
  +5.0% +39.9Ki    __TEXT,__unwind_info
   +57% +8.85Ki    [__TEXT]
  [NEW] +8.59Ki    Lazy Binding Info
  [NEW] +5.16Ki    __TEXT,__stub_helper
  [NEW] +3.58Ki    Export Info
  [NEW] +3.42Ki    __DATA,__la_symbol_ptr
  -0.1%    -726    [12 Others]
 -21.4% -3.10Ki    [__DATA_CONST]
 -95.8% -3.39Ki    __DATA_CONST,__got
 -20.9% -3.43Ki    [__DATA]
  -0.5% -4.52Ki    Code Signature
 -100.0% -11.6Ki    [__LINKEDIT]
  -1.0% -43.5Ki    Symbol Table
  -1.6% -44.0Ki    __TEXT,__gcc_except_tab
  -0.2% -48.1Ki    __TEXT,__const
  -3.3% -78.6Ki    __TEXT,__eh_frame
  -0.7%  -320Ki    __TEXT,__text
  -1.5%  -334Ki    String Table
  -0.5%  -586Ki    TOTAL
```
2025-02-25 08:50:01 -08:00

267 lines
6.4 KiB
TypeScript

// Copyright 2018-2025 the Deno authors. 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.NotCapable);
},
);
Deno.test(
{ permissions: { read: true, write: false } },
function renameSyncWritePerm() {
assertThrows(() => {
const oldpath = "/oldbaddir";
const newpath = "/newbaddir";
Deno.renameSync(oldpath, newpath);
}, Deno.errors.NotCapable);
},
);
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);
},
Error,
"The directory is not empty",
);
assertThrows(
() => {
Deno.renameSync(olddir, file);
},
Error,
"The directory name is invalid",
);
// should succeed on Windows
Deno.renameSync(olddir, emptydir);
assertDirectory(emptydir);
},
);