diff options
Diffstat (limited to 'cli/tests/integration_tests.rs')
-rw-r--r-- | cli/tests/integration_tests.rs | 196 |
1 files changed, 193 insertions, 3 deletions
diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 5429170c7..1767be7c2 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -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] @@ -1164,6 +1320,7 @@ 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; @@ -1171,6 +1328,39 @@ mod util { 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()); |