forked from denoland/deno
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to unit test by permissions.
- Loading branch information
Showing
7 changed files
with
113 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2018 the Deno authors. All rights reserved. MIT license. | ||
// | ||
// We want to test many ops in deno which have different behavior depending on | ||
// the permissions set. These tests can specify which permissions they expect, | ||
// which appends a special string like "permW1N0" to the end of the test name. | ||
// Here we run several copies of deno with different permissions, filtering the | ||
// tests by the special string. permW0N0 means allow-write but not allow-net. | ||
// See tools/unit_tests.py for more details. | ||
|
||
import * as deno from "deno"; | ||
import * as testing from "./testing/testing.ts"; | ||
export { assert, assertEqual } from "./testing/testing.ts"; | ||
|
||
// testing.setFilter must be run before any tests are defined. | ||
const permFilter = deno.argv[1]; | ||
permFromString(permFilter); | ||
testing.setFilter(permFilter); | ||
|
||
interface DenoPermissions { | ||
write?: boolean; | ||
net?: boolean; | ||
} | ||
|
||
function permToString(perms: DenoPermissions): string { | ||
const w = perms.write ? 1 : 0; | ||
const n = perms.net ? 1 : 0; | ||
return `permW${w}N${n}`; | ||
} | ||
|
||
function permFromString(s: string): DenoPermissions { | ||
const re = /^permW([01])N([01])$/; | ||
const found = s.match(re); | ||
if (!found) { | ||
throw Error("Not a permission string"); | ||
} | ||
return { | ||
write: Boolean(Number(found[1])), | ||
net: Boolean(Number(found[2])) | ||
}; | ||
} | ||
|
||
export function testPerm(perms: DenoPermissions, fn: testing.TestFunction) { | ||
const name = `${fn.name}_${permToString(perms)}`; | ||
testing.test({ fn, name }); | ||
} | ||
|
||
export function test(fn: testing.TestFunction) { | ||
testPerm({ write: false, net: false }, fn); | ||
} | ||
|
||
test(function permSerialization() { | ||
for (let write of [true, false]) { | ||
for (let net of [true, false]) { | ||
let perms: DenoPermissions = { write, net }; | ||
testing.assertEqual(perms, permFromString(permToString(perms))); | ||
} | ||
} | ||
}); | ||
|
||
// To better catch internal errors, permFromString should throw if it gets an | ||
// invalid permission string. | ||
test(function permFromStringThrows() { | ||
let threw = false; | ||
try { | ||
permFromString("bad"); | ||
} catch (e) { | ||
threw = true; | ||
} | ||
testing.assert(threw); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env python | ||
from util import run | ||
import sys | ||
|
||
|
||
# We want to test many ops in deno which have different behavior depending on | ||
# the permissions set. These tests can specify which permissions they expect, | ||
# which appends a special string like "permW1N0" to the end of the test name. | ||
# Here we run several copies of deno with different permissions, filtering the | ||
# tests by the special string. permW0N0 means allow-write but not allow-net. | ||
# See js/test_util.ts for more details. | ||
def unit_tests(deno_exe): | ||
run([deno_exe, "js/unit_tests.ts", "permW0N0"]) | ||
run([deno_exe, "js/unit_tests.ts", "permW1N0", "--allow-write"]) | ||
run([deno_exe, "js/unit_tests.ts", "permW0N1", "--allow-net"]) | ||
run([ | ||
deno_exe, "js/unit_tests.ts", "permW1N1", "--allow-write", | ||
"--allow-net" | ||
]) | ||
|
||
|
||
if __name__ == '__main__': | ||
if len(sys.argv) < 2: | ||
print "Usage ./tools/unit_tests.py out/debug/deno" | ||
sys.exit(1) | ||
unit_tests(sys.argv[1]) |