Skip to content

Commit

Permalink
port tools/repl_test.py to Rust (#4096)
Browse files Browse the repository at this point in the history
ref #2988
  • Loading branch information
jsouto18 authored Feb 24, 2020
1 parent 4005174 commit ac933dd
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 190 deletions.
196 changes: 193 additions & 3 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,166 @@ fn bundle_single_module() {
assert_eq!(output.stderr, b"");
}

// TODO(#2933): Rewrite this test in rust.
#[test]
fn repl_test() {
util::run_python_script("tools/repl_test.py")
fn repl_test_console_log() {
let (out, err, code) =
util::repl_process(vec!["console.log('hello')", "'world'"], None);
assert_eq!(out, "hello\nundefined\nworld\n");
assert!(err.is_empty());
assert_eq!(code, 0);
}

#[test]
fn repl_test_eof() {
// test_eof
let (out, err, code) = util::repl_process(vec!["1 + 2"], None);
assert_eq!(out, "3\n");
assert!(err.is_empty());
assert_eq!(code, 0);
}

#[test]
fn repl_test_exit_command() {
let (out, err, code) = util::repl_process(vec!["exit", "'ignored'"], None);
assert!(out.is_empty());
assert!(err.is_empty());
assert_eq!(code, 0);
}

#[test]
fn repl_test_help_command() {
let (out, err, code) = util::repl_process(vec!["help"], None);
assert_eq!(
out,
vec![
"_ Get last evaluation result",
"_error Get last thrown error",
"exit Exit the REPL",
"help Print this help message",
"",
]
.join("\n")
);
assert!(err.is_empty());
assert_eq!(code, 0);
}

#[test]
fn repl_test_function() {
let (out, err, code) = util::repl_process(vec!["Deno.writeFileSync"], None);
assert_eq!(out, "[Function: writeFileSync]\n");
assert!(err.is_empty());
assert_eq!(code, 0);
}

#[test]
fn repl_test_multiline() {
let (out, err, code) = util::repl_process(vec!["(\n1 + 2\n)"], None);
assert_eq!(out, "3\n");
assert!(err.is_empty());
assert_eq!(code, 0);
}

#[test]
fn repl_test_eval_unterminated() {
let (out, err, code) = util::repl_process(vec!["eval('{')"], None);
assert!(out.is_empty());
assert!(err.contains("Unexpected end of input"));
assert_eq!(code, 0);
}

#[test]
fn repl_test_reference_error() {
let (out, err, code) = util::repl_process(vec!["not_a_variable"], None);
assert!(out.is_empty());
assert!(err.contains("not_a_variable is not defined"));
assert_eq!(code, 0);
}

#[test]
fn repl_test_syntax_error() {
let (out, err, code) = util::repl_process(vec!["syntax error"], None);
assert!(out.is_empty());
assert!(err.contains("Unexpected identifier"));
assert_eq!(code, 0);
}

#[test]
fn repl_test_type_error() {
let (out, err, code) = util::repl_process(vec!["console()"], None);
assert!(out.is_empty());
assert!(err.contains("console is not a function"));
assert_eq!(code, 0);
}

#[test]
fn repl_test_variable() {
let (out, err, code) = util::repl_process(vec!["var a = 123;", "a"], None);
assert_eq!(out, "undefined\n123\n");
assert!(err.is_empty());
assert_eq!(code, 0);
}

#[test]
fn repl_test_lexical_scoped_variable() {
let (out, err, code) = util::repl_process(vec!["let a = 123;", "a"], None);
assert_eq!(out, "undefined\n123\n");
assert!(err.is_empty());
assert_eq!(code, 0);
}

#[test]
fn repl_test_missing_deno_dir() {
use std::fs::{read_dir, remove_dir_all};
const DENO_DIR: &str = "nonexistent";
let (out, err, code) = util::repl_process(
vec!["1"],
Some(vec![("DENO_DIR".to_owned(), DENO_DIR.to_owned())]),
);
assert!(read_dir(DENO_DIR).is_ok());
remove_dir_all(DENO_DIR).unwrap();
assert_eq!(out, "1\n");
assert!(err.is_empty());
assert_eq!(code, 0);
}

#[test]
fn repl_test_save_last_eval() {
let (out, err, code) = util::repl_process(vec!["1", "_"], None);
assert_eq!(out, "1\n1\n");
assert!(err.is_empty());
assert_eq!(code, 0);
}

#[test]
fn repl_test_save_last_thrown() {
let (out, err, code) = util::repl_process(vec!["throw 1", "_error"], None);
assert_eq!(out, "1\n");
assert_eq!(err, "Thrown: 1\n");
assert_eq!(code, 0);
}

#[test]
fn repl_test_assign_underscore() {
let (out, err, code) = util::repl_process(vec!["_ = 1", "2", "_"], None);
assert_eq!(
out,
"Last evaluation result is no longer saved to _.\n1\n2\n1\n"
);
assert!(err.is_empty());
assert_eq!(code, 0);
}

#[test]
fn repl_test_assign_underscore_error() {
let (out, err, code) =
util::repl_process(vec!["_error = 1", "throw 2", "_error"], None);
assert_eq!(
out,
"Last thrown error is no longer saved to _error.\n1\n1\n"
);
assert_eq!(err, "Thrown: 2\n");
assert_eq!(code, 0);
}

#[test]
Expand Down Expand Up @@ -1164,13 +1320,47 @@ mod util {
use std::io::Read;
use std::io::Write;
use std::process::Command;
use std::process::Output;
use std::process::Stdio;
use tempfile::TempDir;

lazy_static! {
static ref DENO_DIR: TempDir = { TempDir::new().expect("tempdir fail") };
}

pub fn repl_process(
lines: Vec<&str>,
envs: Option<Vec<(String, String)>>,
) -> (String, String, i32) {
let mut deno_process_builder = deno_cmd();
deno_process_builder
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
if let Some(envs) = envs {
deno_process_builder.envs(envs);
}
let mut deno = deno_process_builder
.spawn()
.expect("failed to spawn script");
{
let stdin = deno.stdin.as_mut().expect("failed to get stdin");
stdin
.write_all(lines.join("\n").as_bytes())
.expect("failed to write to stdin");
}
let Output {
stdout,
stderr,
status,
} = deno.wait_with_output().expect("failed to wait on child");
(
String::from_utf8(stdout).unwrap(),
String::from_utf8(stderr).unwrap(),
status.code().unwrap(),
)
}

pub fn deno_cmd() -> Command {
let mut c = Command::new(deno_exe_path());
c.env("DENO_DIR", DENO_DIR.path());
Expand Down
Loading

0 comments on commit ac933dd

Please sign in to comment.