mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
Port permission_prompt_tests to Rust (#4129)
This commit is contained in:
parent
be787d09d5
commit
e53064c4f2
2 changed files with 120 additions and 26 deletions
|
@ -370,8 +370,11 @@ fn bundle_tla() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_console_log() {
|
fn repl_test_console_log() {
|
||||||
let (out, err, code) =
|
let (out, err, code) = util::run_and_collect_output(
|
||||||
util::repl_process(vec!["console.log('hello')", "'world'"], None);
|
"repl",
|
||||||
|
Some(vec!["console.log('hello')", "'world'"]),
|
||||||
|
None,
|
||||||
|
);
|
||||||
assert_eq!(out, "hello\nundefined\nworld\n");
|
assert_eq!(out, "hello\nundefined\nworld\n");
|
||||||
assert!(err.is_empty());
|
assert!(err.is_empty());
|
||||||
assert_eq!(code, 0);
|
assert_eq!(code, 0);
|
||||||
|
@ -379,8 +382,8 @@ fn repl_test_console_log() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_eof() {
|
fn repl_test_eof() {
|
||||||
// test_eof
|
let (out, err, code) =
|
||||||
let (out, err, code) = util::repl_process(vec!["1 + 2"], None);
|
util::run_and_collect_output("repl", Some(vec!["1 + 2"]), None);
|
||||||
assert_eq!(out, "3\n");
|
assert_eq!(out, "3\n");
|
||||||
assert!(err.is_empty());
|
assert!(err.is_empty());
|
||||||
assert_eq!(code, 0);
|
assert_eq!(code, 0);
|
||||||
|
@ -388,7 +391,8 @@ fn repl_test_eof() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_exit_command() {
|
fn repl_test_exit_command() {
|
||||||
let (out, err, code) = util::repl_process(vec!["exit", "'ignored'"], None);
|
let (out, err, code) =
|
||||||
|
util::run_and_collect_output("repl", Some(vec!["exit", "'ignored'"]), None);
|
||||||
assert!(out.is_empty());
|
assert!(out.is_empty());
|
||||||
assert!(err.is_empty());
|
assert!(err.is_empty());
|
||||||
assert_eq!(code, 0);
|
assert_eq!(code, 0);
|
||||||
|
@ -396,7 +400,8 @@ fn repl_test_exit_command() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_help_command() {
|
fn repl_test_help_command() {
|
||||||
let (out, err, code) = util::repl_process(vec!["help"], None);
|
let (out, err, code) =
|
||||||
|
util::run_and_collect_output("repl", Some(vec!["help"]), None);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
out,
|
out,
|
||||||
vec![
|
vec![
|
||||||
|
@ -414,7 +419,11 @@ fn repl_test_help_command() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_function() {
|
fn repl_test_function() {
|
||||||
let (out, err, code) = util::repl_process(vec!["Deno.writeFileSync"], None);
|
let (out, err, code) = util::run_and_collect_output(
|
||||||
|
"repl",
|
||||||
|
Some(vec!["Deno.writeFileSync"]),
|
||||||
|
None,
|
||||||
|
);
|
||||||
assert_eq!(out, "[Function: writeFileSync]\n");
|
assert_eq!(out, "[Function: writeFileSync]\n");
|
||||||
assert!(err.is_empty());
|
assert!(err.is_empty());
|
||||||
assert_eq!(code, 0);
|
assert_eq!(code, 0);
|
||||||
|
@ -422,7 +431,8 @@ fn repl_test_function() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_multiline() {
|
fn repl_test_multiline() {
|
||||||
let (out, err, code) = util::repl_process(vec!["(\n1 + 2\n)"], None);
|
let (out, err, code) =
|
||||||
|
util::run_and_collect_output("repl", Some(vec!["(\n1 + 2\n)"]), None);
|
||||||
assert_eq!(out, "3\n");
|
assert_eq!(out, "3\n");
|
||||||
assert!(err.is_empty());
|
assert!(err.is_empty());
|
||||||
assert_eq!(code, 0);
|
assert_eq!(code, 0);
|
||||||
|
@ -430,7 +440,8 @@ fn repl_test_multiline() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_eval_unterminated() {
|
fn repl_test_eval_unterminated() {
|
||||||
let (out, err, code) = util::repl_process(vec!["eval('{')"], None);
|
let (out, err, code) =
|
||||||
|
util::run_and_collect_output("repl", Some(vec!["eval('{')"]), None);
|
||||||
assert!(out.is_empty());
|
assert!(out.is_empty());
|
||||||
assert!(err.contains("Unexpected end of input"));
|
assert!(err.contains("Unexpected end of input"));
|
||||||
assert_eq!(code, 0);
|
assert_eq!(code, 0);
|
||||||
|
@ -438,7 +449,8 @@ fn repl_test_eval_unterminated() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_reference_error() {
|
fn repl_test_reference_error() {
|
||||||
let (out, err, code) = util::repl_process(vec!["not_a_variable"], None);
|
let (out, err, code) =
|
||||||
|
util::run_and_collect_output("repl", Some(vec!["not_a_variable"]), None);
|
||||||
assert!(out.is_empty());
|
assert!(out.is_empty());
|
||||||
assert!(err.contains("not_a_variable is not defined"));
|
assert!(err.contains("not_a_variable is not defined"));
|
||||||
assert_eq!(code, 0);
|
assert_eq!(code, 0);
|
||||||
|
@ -446,7 +458,8 @@ fn repl_test_reference_error() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_syntax_error() {
|
fn repl_test_syntax_error() {
|
||||||
let (out, err, code) = util::repl_process(vec!["syntax error"], None);
|
let (out, err, code) =
|
||||||
|
util::run_and_collect_output("repl", Some(vec!["syntax error"]), None);
|
||||||
assert!(out.is_empty());
|
assert!(out.is_empty());
|
||||||
assert!(err.contains("Unexpected identifier"));
|
assert!(err.contains("Unexpected identifier"));
|
||||||
assert_eq!(code, 0);
|
assert_eq!(code, 0);
|
||||||
|
@ -454,7 +467,8 @@ fn repl_test_syntax_error() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_type_error() {
|
fn repl_test_type_error() {
|
||||||
let (out, err, code) = util::repl_process(vec!["console()"], None);
|
let (out, err, code) =
|
||||||
|
util::run_and_collect_output("repl", Some(vec!["console()"]), None);
|
||||||
assert!(out.is_empty());
|
assert!(out.is_empty());
|
||||||
assert!(err.contains("console is not a function"));
|
assert!(err.contains("console is not a function"));
|
||||||
assert_eq!(code, 0);
|
assert_eq!(code, 0);
|
||||||
|
@ -462,7 +476,8 @@ fn repl_test_type_error() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_variable() {
|
fn repl_test_variable() {
|
||||||
let (out, err, code) = util::repl_process(vec!["var a = 123;", "a"], None);
|
let (out, err, code) =
|
||||||
|
util::run_and_collect_output("repl", Some(vec!["var a = 123;", "a"]), None);
|
||||||
assert_eq!(out, "undefined\n123\n");
|
assert_eq!(out, "undefined\n123\n");
|
||||||
assert!(err.is_empty());
|
assert!(err.is_empty());
|
||||||
assert_eq!(code, 0);
|
assert_eq!(code, 0);
|
||||||
|
@ -470,7 +485,8 @@ fn repl_test_variable() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_lexical_scoped_variable() {
|
fn repl_test_lexical_scoped_variable() {
|
||||||
let (out, err, code) = util::repl_process(vec!["let a = 123;", "a"], None);
|
let (out, err, code) =
|
||||||
|
util::run_and_collect_output("repl", Some(vec!["let a = 123;", "a"]), None);
|
||||||
assert_eq!(out, "undefined\n123\n");
|
assert_eq!(out, "undefined\n123\n");
|
||||||
assert!(err.is_empty());
|
assert!(err.is_empty());
|
||||||
assert_eq!(code, 0);
|
assert_eq!(code, 0);
|
||||||
|
@ -480,12 +496,15 @@ fn repl_test_lexical_scoped_variable() {
|
||||||
fn repl_test_missing_deno_dir() {
|
fn repl_test_missing_deno_dir() {
|
||||||
use std::fs::{read_dir, remove_dir_all};
|
use std::fs::{read_dir, remove_dir_all};
|
||||||
const DENO_DIR: &str = "nonexistent";
|
const DENO_DIR: &str = "nonexistent";
|
||||||
let (out, err, code) = util::repl_process(
|
let test_deno_dir =
|
||||||
vec!["1"],
|
util::root_path().join("cli").join("tests").join(DENO_DIR);
|
||||||
|
let (out, err, code) = util::run_and_collect_output(
|
||||||
|
"repl",
|
||||||
|
Some(vec!["1"]),
|
||||||
Some(vec![("DENO_DIR".to_owned(), DENO_DIR.to_owned())]),
|
Some(vec![("DENO_DIR".to_owned(), DENO_DIR.to_owned())]),
|
||||||
);
|
);
|
||||||
assert!(read_dir(DENO_DIR).is_ok());
|
assert!(read_dir(&test_deno_dir).is_ok());
|
||||||
remove_dir_all(DENO_DIR).unwrap();
|
remove_dir_all(&test_deno_dir).unwrap();
|
||||||
assert_eq!(out, "1\n");
|
assert_eq!(out, "1\n");
|
||||||
assert!(err.is_empty());
|
assert!(err.is_empty());
|
||||||
assert_eq!(code, 0);
|
assert_eq!(code, 0);
|
||||||
|
@ -493,7 +512,8 @@ fn repl_test_missing_deno_dir() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_save_last_eval() {
|
fn repl_test_save_last_eval() {
|
||||||
let (out, err, code) = util::repl_process(vec!["1", "_"], None);
|
let (out, err, code) =
|
||||||
|
util::run_and_collect_output("repl", Some(vec!["1", "_"]), None);
|
||||||
assert_eq!(out, "1\n1\n");
|
assert_eq!(out, "1\n1\n");
|
||||||
assert!(err.is_empty());
|
assert!(err.is_empty());
|
||||||
assert_eq!(code, 0);
|
assert_eq!(code, 0);
|
||||||
|
@ -501,7 +521,8 @@ fn repl_test_save_last_eval() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_save_last_thrown() {
|
fn repl_test_save_last_thrown() {
|
||||||
let (out, err, code) = util::repl_process(vec!["throw 1", "_error"], None);
|
let (out, err, code) =
|
||||||
|
util::run_and_collect_output("repl", Some(vec!["throw 1", "_error"]), None);
|
||||||
assert_eq!(out, "1\n");
|
assert_eq!(out, "1\n");
|
||||||
assert_eq!(err, "Thrown: 1\n");
|
assert_eq!(err, "Thrown: 1\n");
|
||||||
assert_eq!(code, 0);
|
assert_eq!(code, 0);
|
||||||
|
@ -509,7 +530,8 @@ fn repl_test_save_last_thrown() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_assign_underscore() {
|
fn repl_test_assign_underscore() {
|
||||||
let (out, err, code) = util::repl_process(vec!["_ = 1", "2", "_"], None);
|
let (out, err, code) =
|
||||||
|
util::run_and_collect_output("repl", Some(vec!["_ = 1", "2", "_"]), None);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
out,
|
out,
|
||||||
"Last evaluation result is no longer saved to _.\n1\n2\n1\n"
|
"Last evaluation result is no longer saved to _.\n1\n2\n1\n"
|
||||||
|
@ -520,8 +542,11 @@ fn repl_test_assign_underscore() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repl_test_assign_underscore_error() {
|
fn repl_test_assign_underscore_error() {
|
||||||
let (out, err, code) =
|
let (out, err, code) = util::run_and_collect_output(
|
||||||
util::repl_process(vec!["_error = 1", "throw 2", "_error"], None);
|
"repl",
|
||||||
|
Some(vec!["_error = 1", "throw 2", "_error"]),
|
||||||
|
None,
|
||||||
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
out,
|
out,
|
||||||
"Last thrown error is no longer saved to _error.\n1\n1\n"
|
"Last thrown error is no longer saved to _error.\n1\n1\n"
|
||||||
|
@ -1365,6 +1390,32 @@ fn cafile_bundle_remote_exports() {
|
||||||
drop(g)
|
drop(g)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_permissions_with_allow() {
|
||||||
|
for permission in &util::PERMISSION_VARIANTS {
|
||||||
|
let (_, err, code) = util::run_and_collect_output(
|
||||||
|
&format!("run --allow-{0} permission_test.ts {0}Required", permission),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
assert_eq!(code, 0);
|
||||||
|
assert!(!err.contains(util::PERMISSION_DENIED_PATTERN));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_permissions_without_allow() {
|
||||||
|
for permission in &util::PERMISSION_VARIANTS {
|
||||||
|
let (_, err, code) = util::run_and_collect_output(
|
||||||
|
&format!("run permission_test.ts {0}Required", permission),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
assert_eq!(code, 1);
|
||||||
|
assert!(err.contains(util::PERMISSION_DENIED_PATTERN));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mod util {
|
mod util {
|
||||||
use deno::colors::strip_ansi_codes;
|
use deno::colors::strip_ansi_codes;
|
||||||
pub use deno::test_util::*;
|
pub use deno::test_util::*;
|
||||||
|
@ -1376,16 +1427,25 @@ mod util {
|
||||||
use std::process::Stdio;
|
use std::process::Stdio;
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
|
pub const PERMISSION_VARIANTS: [&str; 5] =
|
||||||
|
["read", "write", "env", "net", "run"];
|
||||||
|
pub const PERMISSION_DENIED_PATTERN: &str = "PermissionDenied";
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref DENO_DIR: TempDir = { TempDir::new().expect("tempdir fail") };
|
static ref DENO_DIR: TempDir = { TempDir::new().expect("tempdir fail") };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn repl_process(
|
pub fn run_and_collect_output(
|
||||||
lines: Vec<&str>,
|
args: &str,
|
||||||
|
input: Option<Vec<&str>>,
|
||||||
envs: Option<Vec<(String, String)>>,
|
envs: Option<Vec<(String, String)>>,
|
||||||
) -> (String, String, i32) {
|
) -> (String, String, i32) {
|
||||||
|
let root = root_path();
|
||||||
|
let tests_dir = root.join("cli").join("tests");
|
||||||
let mut deno_process_builder = deno_cmd();
|
let mut deno_process_builder = deno_cmd();
|
||||||
|
deno_process_builder.args(args.split_whitespace());
|
||||||
deno_process_builder
|
deno_process_builder
|
||||||
|
.current_dir(&tests_dir)
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.stderr(Stdio::piped());
|
.stderr(Stdio::piped());
|
||||||
|
@ -1395,7 +1455,7 @@ mod util {
|
||||||
let mut deno = deno_process_builder
|
let mut deno = deno_process_builder
|
||||||
.spawn()
|
.spawn()
|
||||||
.expect("failed to spawn script");
|
.expect("failed to spawn script");
|
||||||
{
|
if let Some(lines) = input {
|
||||||
let stdin = deno.stdin.as_mut().expect("failed to get stdin");
|
let stdin = deno.stdin.as_mut().expect("failed to get stdin");
|
||||||
stdin
|
stdin
|
||||||
.write_all(lines.join("\n").as_bytes())
|
.write_all(lines.join("\n").as_bytes())
|
||||||
|
|
34
cli/tests/permission_test.ts
Normal file
34
cli/tests/permission_test.ts
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
|
const { args, listen, env, exit, makeTempDirSync, readFileSync, run } = Deno;
|
||||||
|
|
||||||
|
const name = args[0];
|
||||||
|
const test: { [key: string]: Function } = {
|
||||||
|
async readRequired(): Promise<void> {
|
||||||
|
readFileSync("README.md");
|
||||||
|
},
|
||||||
|
writeRequired(): void {
|
||||||
|
makeTempDirSync();
|
||||||
|
},
|
||||||
|
envRequired(): void {
|
||||||
|
env().home;
|
||||||
|
},
|
||||||
|
netRequired(): void {
|
||||||
|
listen({ transport: "tcp", port: 4541 });
|
||||||
|
},
|
||||||
|
runRequired(): void {
|
||||||
|
run({
|
||||||
|
args: [
|
||||||
|
"python",
|
||||||
|
"-c",
|
||||||
|
"import sys; sys.stdout.write('hello'); sys.stdout.flush()"
|
||||||
|
]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!test[name]) {
|
||||||
|
console.log("Unknown test:", name);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
test[name]();
|
Loading…
Add table
Add a link
Reference in a new issue