1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-26 16:09:27 -05:00
denoland-deno/js/copy_file_test.ts
Kitson Kelly a21a5ad2fa Add Deno global namespace (#1748)
Resolves #1705

This PR adds the Deno APIs as a global namespace named `Deno`. For backwards
compatibility, the ability to `import * from "deno"` is preserved. I have tried
to convert every test and internal code the references the module to use the
namespace instead, but because I didn't break compatibility I am not sure.

On the REPL, `deno` no longer exists, replaced only with `Deno` to align with
the regular runtime.

The runtime type library includes both the namespace and module. This means it
duplicates the whole type information. When we remove the functionality from the
runtime, it will be a one line change to the library generator to remove the
module definition from the type library.

I marked a `TODO` in a couple places where to remove the `"deno"` module, but
there are additional places I know I didn't mark.
2019-02-12 10:08:56 -05:00

152 lines
4.9 KiB
TypeScript

// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { testPerm, assert, assertEqual } from "./test_util.ts";
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, { perm: 0o666 });
}
function assertSameContent(filename1: string, filename2: string) {
const data1 = Deno.readFileSync(filename1);
const data2 = Deno.readFileSync(filename2);
assertEqual(data1, data2);
}
testPerm({ 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
assertEqual(readFileString(fromFilename), "Hello world!");
// Original == Dest
assertSameContent(fromFilename, toFilename);
});
testPerm({ 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
let err;
try {
Deno.copyFileSync(fromFilename, toFilename);
} catch (e) {
err = e;
}
assert(!!err);
assertEqual(err.kind, Deno.ErrorKind.NotFound);
assertEqual(err.name, "NotFound");
});
testPerm({ write: true, read: false }, function copyFileSyncPerm1() {
let caughtError = false;
try {
Deno.copyFileSync("/from.txt", "/to.txt");
} catch (e) {
caughtError = true;
assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});
testPerm({ write: false, read: true }, function copyFileSyncPerm2() {
let caughtError = false;
try {
Deno.copyFileSync("/from.txt", "/to.txt");
} catch (e) {
caughtError = true;
assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});
testPerm({ 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
assertEqual(readFileString(fromFilename), "Hello world!");
// Original == Dest
assertSameContent(fromFilename, toFilename);
});
testPerm({ 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
assertEqual(readFileString(fromFilename), "Hello world!");
// Original == Dest
assertSameContent(fromFilename, toFilename);
});
testPerm({ 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
let err;
try {
await Deno.copyFile(fromFilename, toFilename);
} catch (e) {
err = e;
}
assert(!!err);
assertEqual(err.kind, Deno.ErrorKind.NotFound);
assertEqual(err.name, "NotFound");
});
testPerm({ 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
assertEqual(readFileString(fromFilename), "Hello world!");
// Original == Dest
assertSameContent(fromFilename, toFilename);
});
testPerm({ read: false, write: true }, async function copyFilePerm1() {
let caughtError = false;
try {
await Deno.copyFile("/from.txt", "/to.txt");
} catch (e) {
caughtError = true;
assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});
testPerm({ read: true, write: false }, async function copyFilePerm2() {
let caughtError = false;
try {
await Deno.copyFile("/from.txt", "/to.txt");
} catch (e) {
caughtError = true;
assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});