2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-11-23 11:45:18 -05:00
|
|
|
import { assertEquals, assertRejects, assertThrows } from "./test_util.ts";
|
2018-09-30 18:06:41 -04:00
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
function readFileString(filename: string | URL): string {
|
2019-02-12 10:08:56 -05:00
|
|
|
const dataRead = Deno.readFileSync(filename);
|
2018-09-30 18:06:41 -04:00
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
return dec.decode(dataRead);
|
|
|
|
}
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
function writeFileString(filename: string | URL, s: string) {
|
2018-09-30 18:06:41 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode(s);
|
2020-03-07 22:29:12 -05:00
|
|
|
Deno.writeFileSync(filename, data, { mode: 0o666 });
|
2018-09-30 18:06:41 -04:00
|
|
|
}
|
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
function assertSameContent(
|
|
|
|
filename1: string | URL,
|
2020-07-14 15:24:17 -04:00
|
|
|
filename2: string | URL,
|
2021-08-05 07:08:58 -04:00
|
|
|
) {
|
2019-02-12 10:08:56 -05:00
|
|
|
const data1 = Deno.readFileSync(filename1);
|
|
|
|
const data2 = Deno.readFileSync(filename2);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(data1, data2);
|
2018-09-30 18:06:41 -04:00
|
|
|
}
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function copyFileSyncSuccess() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fromFilename = tempDir + "/from.txt";
|
|
|
|
const toFilename = tempDir + "/to.txt";
|
|
|
|
writeFileString(fromFilename, "Hello world!");
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.copyFileSync(fromFilename, toFilename);
|
2020-03-04 11:31:14 -05:00
|
|
|
// No change to original file
|
|
|
|
assertEquals(readFileString(fromFilename), "Hello world!");
|
|
|
|
// Original == Dest
|
|
|
|
assertSameContent(fromFilename, toFilename);
|
2020-06-11 12:36:20 -04:00
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function copyFileSyncByUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fromUrl = new URL(
|
2020-07-14 15:24:17 -04:00
|
|
|
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/from.txt`,
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
const toUrl = new URL(
|
2020-07-14 15:24:17 -04:00
|
|
|
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/to.txt`,
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
writeFileString(fromUrl, "Hello world!");
|
|
|
|
Deno.copyFileSync(fromUrl, toUrl);
|
|
|
|
// No change to original file
|
|
|
|
assertEquals(readFileString(fromUrl), "Hello world!");
|
|
|
|
// Original == Dest
|
|
|
|
assertSameContent(fromUrl, toUrl);
|
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-09-30 18:06:41 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { write: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function copyFileSyncFailure() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fromFilename = tempDir + "/from.txt";
|
|
|
|
const toFilename = tempDir + "/to.txt";
|
|
|
|
// We skip initial writing here, from.txt does not exist
|
2021-10-11 09:21:18 -04:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.copyFileSync(fromFilename, toFilename);
|
|
|
|
},
|
|
|
|
Deno.errors.NotFound,
|
|
|
|
`copy '${fromFilename}' -> '${toFilename}'`,
|
|
|
|
);
|
2020-06-11 12:36:20 -04:00
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-02-08 15:59:38 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { write: true, read: false } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function copyFileSyncPerm1() {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-03-04 11:31:14 -05:00
|
|
|
Deno.copyFileSync("/from.txt", "/to.txt");
|
2024-09-10 14:12:24 -04:00
|
|
|
}, Deno.errors.NotCapable);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-02-08 15:59:38 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { write: false, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function copyFileSyncPerm2() {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-03-04 11:31:14 -05:00
|
|
|
Deno.copyFileSync("/from.txt", "/to.txt");
|
2024-09-10 14:12:24 -04:00
|
|
|
}, Deno.errors.NotCapable);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-09-30 18:06:41 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function copyFileSyncOverwrite() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fromFilename = tempDir + "/from.txt";
|
|
|
|
const toFilename = tempDir + "/to.txt";
|
|
|
|
writeFileString(fromFilename, "Hello world!");
|
|
|
|
// Make Dest exist and have different content
|
|
|
|
writeFileString(toFilename, "Goodbye!");
|
|
|
|
Deno.copyFileSync(fromFilename, toFilename);
|
|
|
|
// No change to original file
|
|
|
|
assertEquals(readFileString(fromFilename), "Hello world!");
|
|
|
|
// Original == Dest
|
|
|
|
assertSameContent(fromFilename, toFilename);
|
2020-06-11 12:36:20 -04:00
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-09-30 18:06:41 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function copyFileSuccess() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fromFilename = tempDir + "/from.txt";
|
|
|
|
const toFilename = tempDir + "/to.txt";
|
|
|
|
writeFileString(fromFilename, "Hello world!");
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.copyFile(fromFilename, toFilename);
|
2020-03-04 11:31:14 -05:00
|
|
|
// No change to original file
|
|
|
|
assertEquals(readFileString(fromFilename), "Hello world!");
|
|
|
|
// Original == Dest
|
|
|
|
assertSameContent(fromFilename, toFilename);
|
2020-06-11 12:36:20 -04:00
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function copyFileByUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fromUrl = new URL(
|
2020-07-14 15:24:17 -04:00
|
|
|
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/from.txt`,
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
const toUrl = new URL(
|
2020-07-14 15:24:17 -04:00
|
|
|
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/to.txt`,
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
writeFileString(fromUrl, "Hello world!");
|
|
|
|
await Deno.copyFile(fromUrl, toUrl);
|
|
|
|
// No change to original file
|
|
|
|
assertEquals(readFileString(fromUrl), "Hello world!");
|
|
|
|
// Original == Dest
|
|
|
|
assertSameContent(fromUrl, toUrl);
|
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-09-30 18:06:41 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function copyFileFailure() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fromFilename = tempDir + "/from.txt";
|
|
|
|
const toFilename = tempDir + "/to.txt";
|
|
|
|
// We skip initial writing here, from.txt does not exist
|
2021-10-11 09:21:18 -04:00
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await Deno.copyFile(fromFilename, toFilename);
|
|
|
|
},
|
|
|
|
Deno.errors.NotFound,
|
|
|
|
`copy '${fromFilename}' -> '${toFilename}'`,
|
|
|
|
);
|
2020-06-11 12:36:20 -04:00
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function copyFileOverwrite() {
|
2019-04-21 16:40:10 -04:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fromFilename = tempDir + "/from.txt";
|
|
|
|
const toFilename = tempDir + "/to.txt";
|
|
|
|
writeFileString(fromFilename, "Hello world!");
|
|
|
|
// Make Dest exist and have different content
|
|
|
|
writeFileString(toFilename, "Goodbye!");
|
|
|
|
await Deno.copyFile(fromFilename, toFilename);
|
|
|
|
// No change to original file
|
|
|
|
assertEquals(readFileString(fromFilename), "Hello world!");
|
|
|
|
// Original == Dest
|
|
|
|
assertSameContent(fromFilename, toFilename);
|
2020-06-11 12:36:20 -04:00
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
2018-09-30 18:06:41 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: false, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function copyFilePerm1() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2020-03-04 11:31:14 -05:00
|
|
|
await Deno.copyFile("/from.txt", "/to.txt");
|
2024-09-10 14:12:24 -04:00
|
|
|
}, Deno.errors.NotCapable);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-02-08 15:59:38 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: false } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function copyFilePerm2() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2020-03-04 11:31:14 -05:00
|
|
|
await Deno.copyFile("/from.txt", "/to.txt");
|
2024-09-10 14:12:24 -04:00
|
|
|
}, Deno.errors.NotCapable);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2023-01-14 08:45:30 -05:00
|
|
|
|
|
|
|
function copyFileSyncMode(content: string): void {
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fromFilename = tempDir + "/from.txt";
|
|
|
|
const toFilename = tempDir + "/to.txt";
|
|
|
|
Deno.writeTextFileSync(fromFilename, content);
|
|
|
|
Deno.chmodSync(fromFilename, 0o100755);
|
|
|
|
|
|
|
|
Deno.copyFileSync(fromFilename, toFilename);
|
|
|
|
const toStat = Deno.statSync(toFilename);
|
|
|
|
assertEquals(toStat.mode!, 0o100755);
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { read: true, write: true },
|
|
|
|
},
|
|
|
|
function copyFileSyncChmod() {
|
|
|
|
// this Tests different optimization paths on MacOS:
|
|
|
|
//
|
|
|
|
// < 128 KB clonefile() w/ fallback to copyfile()
|
|
|
|
// > 128 KB
|
|
|
|
copyFileSyncMode("Hello world!");
|
|
|
|
copyFileSyncMode("Hello world!".repeat(128 * 1024));
|
|
|
|
},
|
|
|
|
);
|
2024-01-31 23:36:09 -05:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
async function copyFileNulPath() {
|
|
|
|
const fromFilename = "from.txt\0";
|
|
|
|
const toFilename = "to.txt\0";
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await Deno.copyFile(fromFilename, toFilename);
|
|
|
|
}, TypeError);
|
|
|
|
},
|
|
|
|
);
|