0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-31 09:14:20 -04:00
denoland-deno/cli/tests/unit/copy_file_test.ts

212 lines
6.3 KiB
TypeScript
Raw Normal View History

2022-01-20 02:10:16 -05:00
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertRejects, assertThrows } from "./test_util.ts";
2018-09-30 18:06:41 -04:00
function readFileString(filename: string | URL): string {
const dataRead = Deno.readFileSync(filename);
2018-09-30 18:06:41 -04:00
const dec = new TextDecoder("utf-8");
return dec.decode(dataRead);
}
function writeFileString(filename: string | URL, s: string) {
2018-09-30 18:06:41 -04:00
const enc = new TextEncoder();
const data = enc.encode(s);
Deno.writeFileSync(filename, data, { mode: 0o666 });
2018-09-30 18:06:41 -04:00
}
function assertSameContent(
filename1: string | URL,
filename2: string | URL,
) {
const data1 = Deno.readFileSync(filename1);
const data2 = Deno.readFileSync(filename2);
assertEquals(data1, data2);
2018-09-30 18:06:41 -04:00
}
Deno.test(
{ permissions: { read: true, write: true } },
function copyFileSyncSuccess() {
const tempDir = Deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
writeFileString(fromFilename, "Hello world!");
Deno.copyFileSync(fromFilename, toFilename);
// No change to original file
assertEquals(readFileString(fromFilename), "Hello world!");
// Original == Dest
assertSameContent(fromFilename, toFilename);
Deno.removeSync(tempDir, { recursive: true });
},
);
Deno.test(
{ permissions: { read: true, write: true } },
function copyFileSyncByUrl() {
const tempDir = Deno.makeTempDirSync();
const fromUrl = new URL(
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/from.txt`,
);
const toUrl = new URL(
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/to.txt`,
);
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 });
},
);
2018-09-30 18:06:41 -04:00
Deno.test(
{ permissions: { write: true, read: true } },
function copyFileSyncFailure() {
const tempDir = Deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
// We skip initial writing here, from.txt does not exist
assertThrows(
() => {
Deno.copyFileSync(fromFilename, toFilename);
},
Deno.errors.NotFound,
`copy '${fromFilename}' -> '${toFilename}'`,
);
Deno.removeSync(tempDir, { recursive: true });
},
);
Deno.test(
{ permissions: { write: true, read: false } },
function copyFileSyncPerm1() {
assertThrows(() => {
Deno.copyFileSync("/from.txt", "/to.txt");
}, Deno.errors.PermissionDenied);
},
);
Deno.test(
{ permissions: { write: false, read: true } },
function copyFileSyncPerm2() {
assertThrows(() => {
Deno.copyFileSync("/from.txt", "/to.txt");
}, Deno.errors.PermissionDenied);
},
);
2018-09-30 18:06:41 -04:00
Deno.test(
{ permissions: { read: true, write: true } },
function copyFileSyncOverwrite() {
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);
Deno.removeSync(tempDir, { recursive: true });
},
);
2018-09-30 18:06:41 -04:00
Deno.test(
{ permissions: { read: true, write: true } },
async function copyFileSuccess() {
const tempDir = Deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
writeFileString(fromFilename, "Hello world!");
await Deno.copyFile(fromFilename, toFilename);
// No change to original file
assertEquals(readFileString(fromFilename), "Hello world!");
// Original == Dest
assertSameContent(fromFilename, toFilename);
Deno.removeSync(tempDir, { recursive: true });
},
);
Deno.test(
{ permissions: { read: true, write: true } },
async function copyFileByUrl() {
const tempDir = Deno.makeTempDirSync();
const fromUrl = new URL(
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/from.txt`,
);
const toUrl = new URL(
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/to.txt`,
);
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 });
},
);
2018-09-30 18:06:41 -04:00
Deno.test(
{ permissions: { read: true, write: true } },
async function copyFileFailure() {
const tempDir = Deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
// We skip initial writing here, from.txt does not exist
await assertRejects(
async () => {
await Deno.copyFile(fromFilename, toFilename);
},
Deno.errors.NotFound,
`copy '${fromFilename}' -> '${toFilename}'`,
);
Deno.removeSync(tempDir, { recursive: true });
},
);
Deno.test(
{ permissions: { read: true, write: true } },
async function copyFileOverwrite() {
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);
Deno.removeSync(tempDir, { recursive: true });
},
);
2018-09-30 18:06:41 -04:00
Deno.test(
{ permissions: { read: false, write: true } },
async function copyFilePerm1() {
await assertRejects(async () => {
await Deno.copyFile("/from.txt", "/to.txt");
}, Deno.errors.PermissionDenied);
},
);
Deno.test(
{ permissions: { read: true, write: false } },
async function copyFilePerm2() {
await assertRejects(async () => {
await Deno.copyFile("/from.txt", "/to.txt");
}, Deno.errors.PermissionDenied);
},
);