mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 12:49:10 +00:00
refactor: rewrite testPerm into unitTest (#4231)
Rewrite "testPerm" helper function used for testing of internal runtime code. It's been renamed to "unitTest" and provides API that is extensible in the future by accepting optional "UnitTestOptions" argument. "test" helper was also removed and replaced by overloaded version of "unitTest" that takes only function argument. "UnitTestOptions" currently supports "perms" and "skip" options, where former works exactly as first argument to "testPerm" did, while the latter allows to conditionally skip tests.
This commit is contained in:
parent
30682cf74f
commit
8d96dffa41
60 changed files with 2389 additions and 2047 deletions
|
@ -1,5 +1,5 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
import { testPerm, assert, assertEquals } from "./test_util.ts";
|
||||
import { unitTest, assert, assertEquals } from "./test_util.ts";
|
||||
|
||||
function readFileString(filename: string): string {
|
||||
const dataRead = Deno.readFileSync(filename);
|
||||
|
@ -19,102 +19,119 @@ function assertSameContent(filename1: string, filename2: string): void {
|
|||
assertEquals(data1, data2);
|
||||
}
|
||||
|
||||
testPerm({ 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!");
|
||||
Deno.copyFileSync(fromFilename, toFilename);
|
||||
// No change to original file
|
||||
assertEquals(readFileString(fromFilename), "Hello world!");
|
||||
// Original == Dest
|
||||
assertSameContent(fromFilename, toFilename);
|
||||
});
|
||||
|
||||
testPerm({ 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 {
|
||||
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!");
|
||||
Deno.copyFileSync(fromFilename, toFilename);
|
||||
} catch (e) {
|
||||
err = e;
|
||||
// No change to original file
|
||||
assertEquals(readFileString(fromFilename), "Hello world!");
|
||||
// Original == Dest
|
||||
assertSameContent(fromFilename, toFilename);
|
||||
}
|
||||
assert(!!err);
|
||||
assert(err instanceof Deno.errors.NotFound);
|
||||
});
|
||||
);
|
||||
|
||||
testPerm({ 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);
|
||||
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);
|
||||
}
|
||||
assert(caughtError);
|
||||
});
|
||||
);
|
||||
|
||||
testPerm({ 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);
|
||||
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);
|
||||
}
|
||||
assert(caughtError);
|
||||
});
|
||||
);
|
||||
|
||||
testPerm({ 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);
|
||||
});
|
||||
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);
|
||||
}
|
||||
);
|
||||
|
||||
testPerm({ 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!");
|
||||
await Deno.copyFile(fromFilename, toFilename);
|
||||
// No change to original file
|
||||
assertEquals(readFileString(fromFilename), "Hello world!");
|
||||
// Original == Dest
|
||||
assertSameContent(fromFilename, toFilename);
|
||||
});
|
||||
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);
|
||||
}
|
||||
);
|
||||
|
||||
testPerm({ 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 {
|
||||
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!");
|
||||
await Deno.copyFile(fromFilename, toFilename);
|
||||
} catch (e) {
|
||||
err = e;
|
||||
// No change to original file
|
||||
assertEquals(readFileString(fromFilename), "Hello world!");
|
||||
// Original == Dest
|
||||
assertSameContent(fromFilename, toFilename);
|
||||
}
|
||||
assert(!!err);
|
||||
assert(err instanceof Deno.errors.NotFound);
|
||||
});
|
||||
);
|
||||
|
||||
testPerm(
|
||||
{ read: true, write: true },
|
||||
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 } },
|
||||
async function copyFileOverwrite(): Promise<void> {
|
||||
const tempDir = Deno.makeTempDirSync();
|
||||
const fromFilename = tempDir + "/from.txt";
|
||||
|
@ -130,28 +147,30 @@ testPerm(
|
|||
}
|
||||
);
|
||||
|
||||
testPerm({ 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);
|
||||
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);
|
||||
}
|
||||
assert(caughtError);
|
||||
});
|
||||
);
|
||||
|
||||
testPerm({ 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);
|
||||
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);
|
||||
}
|
||||
assert(caughtError);
|
||||
});
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue