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-09-30 18:06:41 -04:00
|
|
|
|
|
|
|
function readFileString(filename: string): 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);
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function writeFileString(filename: string, s: string): void {
|
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
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function assertSameContent(filename1: string, filename2: string): void {
|
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
|
|
|
}
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function copyFileSyncSuccess(): void {
|
|
|
|
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);
|
2018-09-30 18:06:41 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-09-30 18:06:41 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { write: true, read: true } },
|
|
|
|
function copyFileSyncFailure(): void {
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fromFilename = tempDir + "/from.txt";
|
|
|
|
const toFilename = tempDir + "/to.txt";
|
|
|
|
// We skip initial writing here, from.txt does not exist
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
Deno.copyFileSync(fromFilename, toFilename);
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2019-02-08 15:59:38 -05:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-02-08 15:59:38 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { write: true, read: false } },
|
|
|
|
function copyFileSyncPerm1(): void {
|
|
|
|
let caughtError = false;
|
|
|
|
try {
|
|
|
|
Deno.copyFileSync("/from.txt", "/to.txt");
|
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
|
|
|
assert(e instanceof Deno.errors.PermissionDenied);
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
2019-02-08 15:59:38 -05:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-02-08 15:59:38 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { write: false, read: true } },
|
|
|
|
function copyFileSyncPerm2(): void {
|
|
|
|
let caughtError = false;
|
|
|
|
try {
|
|
|
|
Deno.copyFileSync("/from.txt", "/to.txt");
|
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
|
|
|
assert(e instanceof Deno.errors.PermissionDenied);
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
}
|
|
|
|
);
|
2018-09-30 18:06:41 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function copyFileSyncOverwrite(): void {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|
2018-09-30 18:06:41 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
async function copyFileSuccess(): Promise<void> {
|
|
|
|
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);
|
2018-09-30 18:06:41 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-09-30 18:06:41 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
async function copyFileFailure(): Promise<void> {
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fromFilename = tempDir + "/from.txt";
|
|
|
|
const toFilename = tempDir + "/to.txt";
|
|
|
|
// We skip initial writing here, from.txt does not exist
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await Deno.copyFile(fromFilename, toFilename);
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
2019-04-21 16:40:10 -04:00
|
|
|
async function copyFileOverwrite(): Promise<void> {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|
2018-09-30 18:06:41 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: false, write: true } },
|
|
|
|
async function copyFilePerm1(): Promise<void> {
|
|
|
|
let caughtError = false;
|
|
|
|
try {
|
|
|
|
await Deno.copyFile("/from.txt", "/to.txt");
|
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
|
|
|
assert(e instanceof Deno.errors.PermissionDenied);
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
2018-09-30 18:06:41 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-02-08 15:59:38 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: false } },
|
|
|
|
async function copyFilePerm2(): Promise<void> {
|
|
|
|
let caughtError = false;
|
|
|
|
try {
|
|
|
|
await Deno.copyFile("/from.txt", "/to.txt");
|
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
|
|
|
assert(e instanceof Deno.errors.PermissionDenied);
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
2019-02-08 15:59:38 -05:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|