2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-09-30 18:06:41 -04:00
|
|
|
import { testPerm, assert, assertEqual } from "./test_util.ts";
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeFileString(filename: string, s: string) {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode(s);
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.writeFileSync(filename, data, { perm: 0o666 });
|
2018-09-30 18:06:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function assertSameContent(filename1: string, filename2: string) {
|
2019-02-12 10:08:56 -05:00
|
|
|
const data1 = Deno.readFileSync(filename1);
|
|
|
|
const data2 = Deno.readFileSync(filename2);
|
2018-09-30 18:06:41 -04:00
|
|
|
assertEqual(data1, data2);
|
|
|
|
}
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, function copyFileSyncSuccess() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
2018-09-30 18:06:41 -04:00
|
|
|
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);
|
2018-09-30 18:06:41 -04:00
|
|
|
// No change to original file
|
|
|
|
assertEqual(readFileString(fromFilename), "Hello world!");
|
|
|
|
// Original == Dest
|
|
|
|
assertSameContent(fromFilename, toFilename);
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ write: true, read: true }, function copyFileSyncFailure() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
2018-09-30 18:06:41 -04:00
|
|
|
const fromFilename = tempDir + "/from.txt";
|
|
|
|
const toFilename = tempDir + "/to.txt";
|
|
|
|
// We skip initial writing here, from.txt does not exist
|
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.copyFileSync(fromFilename, toFilename);
|
2018-09-30 18:06:41 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-10-17 01:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
2018-09-30 18:06:41 -04:00
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ write: true, read: false }, function copyFileSyncPerm1() {
|
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.copyFileSync("/from.txt", "/to.txt");
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
assertEqual(e.name, "PermissionDenied");
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: false, read: true }, function copyFileSyncPerm2() {
|
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.copyFileSync("/from.txt", "/to.txt");
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
assertEqual(e.name, "PermissionDenied");
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ read: true, write: true }, function copyFileSyncOverwrite() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
2018-09-30 18:06:41 -04:00
|
|
|
const fromFilename = tempDir + "/from.txt";
|
|
|
|
const toFilename = tempDir + "/to.txt";
|
|
|
|
writeFileString(fromFilename, "Hello world!");
|
|
|
|
// Make Dest exist and have different content
|
|
|
|
writeFileString(toFilename, "Goodbye!");
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.copyFileSync(fromFilename, toFilename);
|
2018-09-30 18:06:41 -04:00
|
|
|
// No change to original file
|
|
|
|
assertEqual(readFileString(fromFilename), "Hello world!");
|
|
|
|
// Original == Dest
|
|
|
|
assertSameContent(fromFilename, toFilename);
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, async function copyFileSuccess() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
2018-09-30 18:06:41 -04:00
|
|
|
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);
|
2018-09-30 18:06:41 -04:00
|
|
|
// No change to original file
|
|
|
|
assertEqual(readFileString(fromFilename), "Hello world!");
|
|
|
|
// Original == Dest
|
|
|
|
assertSameContent(fromFilename, toFilename);
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, async function copyFileFailure() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
2018-09-30 18:06:41 -04:00
|
|
|
const fromFilename = tempDir + "/from.txt";
|
|
|
|
const toFilename = tempDir + "/to.txt";
|
|
|
|
// We skip initial writing here, from.txt does not exist
|
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.copyFile(fromFilename, toFilename);
|
2018-09-30 18:06:41 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-10-17 01:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
2018-09-30 18:06:41 -04:00
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, async function copyFileOverwrite() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
2018-09-30 18:06:41 -04:00
|
|
|
const fromFilename = tempDir + "/from.txt";
|
|
|
|
const toFilename = tempDir + "/to.txt";
|
|
|
|
writeFileString(fromFilename, "Hello world!");
|
|
|
|
// Make Dest exist and have different content
|
|
|
|
writeFileString(toFilename, "Goodbye!");
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.copyFile(fromFilename, toFilename);
|
2018-09-30 18:06:41 -04:00
|
|
|
// No change to original file
|
|
|
|
assertEqual(readFileString(fromFilename), "Hello world!");
|
|
|
|
// Original == Dest
|
|
|
|
assertSameContent(fromFilename, toFilename);
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: false, write: true }, async function copyFilePerm1() {
|
|
|
|
let caughtError = false;
|
2018-09-30 18:06:41 -04:00
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.copyFile("/from.txt", "/to.txt");
|
2018-09-30 18:06:41 -04:00
|
|
|
} catch (e) {
|
2019-02-08 15:59:38 -05:00
|
|
|
caughtError = true;
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
assertEqual(e.name, "PermissionDenied");
|
2018-09-30 18:06:41 -04:00
|
|
|
}
|
2019-02-08 15:59:38 -05:00
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ read: true, write: false }, async function copyFilePerm2() {
|
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.copyFile("/from.txt", "/to.txt");
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
assertEqual(e.name, "PermissionDenied");
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
2018-09-30 18:06:41 -04:00
|
|
|
});
|