Skip to content

Commit

Permalink
alter tests for all read ops except op_open
Browse files Browse the repository at this point in the history
  • Loading branch information
fewf committed Feb 6, 2019
1 parent fc8c083 commit 2cf7909
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 47 deletions.
64 changes: 44 additions & 20 deletions js/copy_file_test.ts
Expand Up @@ -20,7 +20,7 @@ function assertSameContent(filename1: string, filename2: string) {
assertEqual(data1, data2);
}

testPerm({ write: true }, function copyFileSyncSuccess() {
testPerm({ write: true, read: true }, function copyFileSyncSuccess() {
const tempDir = deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
Expand All @@ -32,7 +32,7 @@ testPerm({ write: true }, function copyFileSyncSuccess() {
assertSameContent(fromFilename, toFilename);
});

testPerm({ write: true }, function copyFileSyncFailure() {
testPerm({ write: true, read: true }, function copyFileSyncFailure() {
const tempDir = deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
Expand All @@ -48,6 +48,30 @@ testPerm({ write: true }, function copyFileSyncFailure() {
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({ write: true }, function copyFileSyncOverwrite() {
const tempDir = deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
Expand All @@ -62,18 +86,6 @@ testPerm({ write: true }, function copyFileSyncOverwrite() {
assertSameContent(fromFilename, toFilename);
});

testPerm({ write: false }, function copyFileSyncPerm() {
let err;
try {
deno.copyFileSync("/from.txt", "/to.txt");
} catch (e) {
err = e;
}
assert(!!err);
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
});

testPerm({ write: true }, async function copyFileSuccess() {
const tempDir = deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
Expand Down Expand Up @@ -116,14 +128,26 @@ testPerm({ write: true }, async function copyFileOverwrite() {
assertSameContent(fromFilename, toFilename);
});

testPerm({ write: false }, async function copyFilePerm() {
let err;
testPerm({ write: true, read: false }, async function copyFilePerm1() {
let caughtError = false;
try {
await deno.copyFile("/from.txt", "/to.txt");
} catch (e) {
err = e;
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(!!err);
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
assert(caughtError);
});

testPerm({ write: false, read: true }, 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);
});
34 changes: 29 additions & 5 deletions js/read_dir_test.ts
@@ -1,5 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, testPerm, assert, assertEqual } from "./test_util.ts";
import { testPerm, assert, assertEqual } from "./test_util.ts";
import * as deno from "deno";
import { FileInfo } from "deno";

Expand All @@ -22,12 +22,24 @@ function assertSameContent(files: FileInfo[]) {
assertEqual(counter, 2);
}

testPerm({ write: true }, function readDirSyncSuccess() {
testPerm({ write: true, read: true }, function readDirSyncSuccess() {
const files = deno.readDirSync("tests/");
assertSameContent(files);
});

test(function readDirSyncNotDir() {
testPerm({ write: true, read: false }, function readDirSyncPerm() {
let caughtError = false;
try {
const files = deno.readDirSync("tests/");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});

testPerm({ read: true }, function readDirSyncNotDir() {
let caughtError = false;
let src;

Expand All @@ -41,7 +53,7 @@ test(function readDirSyncNotDir() {
assertEqual(src, undefined);
});

test(function readDirSyncNotFound() {
testPerm({ read: true }, function readDirSyncNotFound() {
let caughtError = false;
let src;

Expand All @@ -55,7 +67,19 @@ test(function readDirSyncNotFound() {
assertEqual(src, undefined);
});

testPerm({ write: true }, async function readDirSuccess() {
testPerm({ write: true, read: true }, async function readDirSuccess() {
const files = await deno.readDir("tests/");
assertSameContent(files);
});

testPerm({ write: true, read: false }, async function readDirPerm() {
let caughtError = false;
try {
const files = await deno.readDir("tests/");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});
32 changes: 28 additions & 4 deletions js/read_file_test.ts
@@ -1,8 +1,8 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, assert, assertEqual } from "./test_util.ts";
import { testPerm, assert, assertEqual } from "./test_util.ts";
import * as deno from "deno";

test(function readFileSyncSuccess() {
testPerm({read: true}, function readFileSyncSuccess() {
const data = deno.readFileSync("package.json");
assert(data.byteLength > 0);
const decoder = new TextDecoder("utf-8");
Expand All @@ -11,7 +11,19 @@ test(function readFileSyncSuccess() {
assertEqual(pkg.name, "deno");
});

test(function readFileSyncNotFound() {
testPerm({read: false}, function readFileSyncPerm() {
let caughtError = false;
try {
const data = deno.readFileSync("package.json");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});

testPerm({read: true}, function readFileSyncNotFound() {
let caughtError = false;
let data;
try {
Expand All @@ -24,11 +36,23 @@ test(function readFileSyncNotFound() {
assert(data === undefined);
});

test(async function readFileSuccess() {
testPerm({read: true}, async function readFileSuccess() {
const data = await deno.readFile("package.json");
assert(data.byteLength > 0);
const decoder = new TextDecoder("utf-8");
const json = decoder.decode(data);
const pkg = JSON.parse(json);
assertEqual(pkg.name, "deno");
});

testPerm({read: false}, async function readFilePerm() {
let caughtError = false;
try {
await deno.readFile("package.json");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});
32 changes: 28 additions & 4 deletions js/read_link_test.ts
@@ -1,8 +1,8 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, testPerm, assert, assertEqual } from "./test_util.ts";
import { testPerm, assert, assertEqual } from "./test_util.ts";
import * as deno from "deno";

testPerm({ write: true }, function readlinkSyncSuccess() {
testPerm({ write: true, read: true }, function readlinkSyncSuccess() {
const testDir = deno.makeTempDirSync();
const target = testDir + "/target";
const symlink = testDir + "/symln";
Expand All @@ -16,7 +16,19 @@ testPerm({ write: true }, function readlinkSyncSuccess() {
}
});

test(function readlinkSyncNotFound() {
testPerm({ read: false }, async function readlinkSyncPerm() {
let caughtError = false;
try {
deno.readlinkSync("/symlink");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});

testPerm({ read: true }, function readlinkSyncNotFound() {
let caughtError = false;
let data;
try {
Expand All @@ -29,7 +41,7 @@ test(function readlinkSyncNotFound() {
assertEqual(data, undefined);
});

testPerm({ write: true }, async function readlinkSuccess() {
testPerm({ write: true, read: true }, async function readlinkSuccess() {
const testDir = deno.makeTempDirSync();
const target = testDir + "/target";
const symlink = testDir + "/symln";
Expand All @@ -42,3 +54,15 @@ testPerm({ write: true }, async function readlinkSuccess() {
assertEqual(targetPath, target);
}
});

testPerm({ read: false }, async function readlinkPerm() {
let caughtError = false;
try {
await deno.readlink("/symlink");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});

0 comments on commit 2cf7909

Please sign in to comment.