diff options
Diffstat (limited to 'cli/tests')
14 files changed, 215 insertions, 47 deletions
diff --git a/cli/tests/integration/bundle_tests.rs b/cli/tests/integration/bundle_tests.rs index 08665091e..5a86a3bb9 100644 --- a/cli/tests/integration/bundle_tests.rs +++ b/cli/tests/integration/bundle_tests.rs @@ -466,6 +466,16 @@ itest!(check_local_by_default_type_error { exit_code: 1, }); +itest!(ts_without_extension { + args: "bundle --ext ts file_extensions/ts_without_extension", + output: "bundle/file_extensions/ts_without_extension.out", +}); + +itest!(js_without_extension { + args: "bundle --ext js file_extensions/js_without_extension", + output: "bundle/file_extensions/js_without_extension.out", +}); + itest!(bundle_shebang_file { args: "bundle subdir/shebang_file.js", output: "bundle/shebang_file.bundle.out", diff --git a/cli/tests/integration/compile_tests.rs b/cli/tests/integration/compile_tests.rs index 810cf5f80..957beed30 100644 --- a/cli/tests/integration/compile_tests.rs +++ b/cli/tests/integration/compile_tests.rs @@ -412,6 +412,82 @@ fn standalone_runtime_flags() { } #[test] +fn standalone_ext_flag_ts() { + let dir = TempDir::new(); + let exe = if cfg!(windows) { + dir.path().join("ext_flag_ts.exe") + } else { + dir.path().join("ext_flag_ts") + }; + let output = util::deno_cmd() + .current_dir(util::testdata_path()) + .arg("compile") + .arg("--unstable") + .arg("--ext") + .arg("ts") + .arg("--output") + .arg(&exe) + .arg("./file_extensions/ts_without_extension") + .stdout(std::process::Stdio::piped()) + .spawn() + .unwrap() + .wait_with_output() + .unwrap(); + assert!(output.status.success()); + let output = Command::new(exe) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap() + .wait_with_output() + .unwrap(); + assert!(output.status.success()); + let stdout_str = String::from_utf8(output.stdout).unwrap(); + assert_eq!( + util::strip_ansi_codes(&stdout_str), + "executing typescript with no extension\n" + ); +} + +#[test] +fn standalone_ext_flag_js() { + let dir = TempDir::new(); + let exe = if cfg!(windows) { + dir.path().join("ext_flag_js.exe") + } else { + dir.path().join("ext_flag_js") + }; + let output = util::deno_cmd() + .current_dir(util::testdata_path()) + .arg("compile") + .arg("--unstable") + .arg("--ext") + .arg("js") + .arg("--output") + .arg(&exe) + .arg("./file_extensions/js_without_extension") + .stdout(std::process::Stdio::piped()) + .spawn() + .unwrap() + .wait_with_output() + .unwrap(); + assert!(output.status.success()); + let output = Command::new(exe) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap() + .wait_with_output() + .unwrap(); + assert!(output.status.success()); + let stdout_str = String::from_utf8(output.stdout).unwrap(); + assert_eq!( + util::strip_ansi_codes(&stdout_str), + "executing javascript with no extension\n" + ); +} + +#[test] fn standalone_import_map() { let dir = TempDir::new(); let exe = if cfg!(windows) { diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index 4c78a5cf5..e4d670935 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -3823,6 +3823,30 @@ itest!(error_cause_recursive { exit_code: 1, }); +itest!(default_file_extension_is_js { + args: "run --check file_extensions/js_without_extension", + output: "file_extensions/js_without_extension.out", + exit_code: 0, +}); + +itest!(js_without_extension { + args: "run --ext js --check file_extensions/js_without_extension", + output: "file_extensions/js_without_extension.out", + exit_code: 0, +}); + +itest!(ts_without_extension { + args: "run --ext ts file_extensions/ts_without_extension", + output: "file_extensions/ts_without_extension.out", + exit_code: 0, +}); + +itest!(ext_flag_takes_precedence_over_extension { + args: "run --ext ts file_extensions/ts_with_js_extension.js", + output: "file_extensions/ts_with_extension.out", + exit_code: 0, +}); + #[test] fn websocket() { let _g = util::http_server(); diff --git a/cli/tests/integration/watcher_tests.rs b/cli/tests/integration/watcher_tests.rs index 0c9b8c29f..e50ac04e7 100644 --- a/cli/tests/integration/watcher_tests.rs +++ b/cli/tests/integration/watcher_tests.rs @@ -58,6 +58,24 @@ fn wait_contains(s: &str, lines: &mut impl Iterator<Item = String>) { wait_for(|msg| msg.contains(s), lines) } +/// Before test cases touch files, they need to wait for the watcher to be +/// ready. Waiting for subcommand output is insufficient. +/// The file watcher takes a moment to start watching files due to +/// asynchronicity. It is possible for the watched subcommand to finish before +/// any files are being watched. +/// deno must be running with --log-level=debug +/// file_name should be the file name and, optionally, extension. file_name +/// may not be a full path, as it is not portable. +fn wait_for_watcher( + file_name: &str, + stderr_lines: &mut impl Iterator<Item = String>, +) { + wait_for( + |m| m.contains("Watching paths") && m.contains(file_name), + stderr_lines, + ); +} + fn read_line(s: &str, lines: &mut impl Iterator<Item = String>) -> String { lines.find(|m| m.contains(s)).unwrap() } @@ -508,20 +526,14 @@ fn run_watch_no_dynamic() { let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child); wait_contains("Hello world", &mut stdout_lines); - wait_for( - |m| m.contains("Watching paths") && m.contains("file_to_watch.js"), - &mut stderr_lines, - ); + wait_for_watcher("file_to_watch.js", &mut stderr_lines); // Change content of the file write(&file_to_watch, "console.log('Hello world2');").unwrap(); wait_contains("Restarting", &mut stderr_lines); wait_contains("Hello world2", &mut stdout_lines); - wait_for( - |m| m.contains("Watching paths") && m.contains("file_to_watch.js"), - &mut stderr_lines, - ); + wait_for_watcher("file_to_watch.js", &mut stderr_lines); // Add dependency let another_file = t.path().join("another_file.js"); @@ -534,30 +546,21 @@ fn run_watch_no_dynamic() { wait_contains("Restarting", &mut stderr_lines); wait_contains("0", &mut stdout_lines); - wait_for( - |m| m.contains("Watching paths") && m.contains("another_file.js"), - &mut stderr_lines, - ); + wait_for_watcher("another_file.js", &mut stderr_lines); // Confirm that restarting occurs when a new file is updated write(&another_file, "export const foo = 42;").unwrap(); wait_contains("Restarting", &mut stderr_lines); wait_contains("42", &mut stdout_lines); - wait_for( - |m| m.contains("Watching paths") && m.contains("file_to_watch.js"), - &mut stderr_lines, - ); + wait_for_watcher("file_to_watch.js", &mut stderr_lines); // Confirm that the watcher keeps on working even if the file is updated and has invalid syntax write(&file_to_watch, "syntax error ^^").unwrap(); wait_contains("Restarting", &mut stderr_lines); wait_contains("error:", &mut stderr_lines); - wait_for( - |m| m.contains("Watching paths") && m.contains("file_to_watch.js"), - &mut stderr_lines, - ); + wait_for_watcher("file_to_watch.js", &mut stderr_lines); // Then restore the file write( @@ -568,20 +571,14 @@ fn run_watch_no_dynamic() { wait_contains("Restarting", &mut stderr_lines); wait_contains("42", &mut stdout_lines); - wait_for( - |m| m.contains("Watching paths") && m.contains("another_file.js"), - &mut stderr_lines, - ); + wait_for_watcher("another_file.js", &mut stderr_lines); // Update the content of the imported file with invalid syntax write(&another_file, "syntax error ^^").unwrap(); wait_contains("Restarting", &mut stderr_lines); wait_contains("error:", &mut stderr_lines); - wait_for( - |m| m.contains("Watching paths") && m.contains("another_file.js"), - &mut stderr_lines, - ); + wait_for_watcher("another_file.js", &mut stderr_lines); // Modify the imported file and make sure that restarting occurs write(&another_file, "export const foo = 'modified!';").unwrap(); @@ -629,12 +626,7 @@ fn run_watch_external_watch_files() { let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child); wait_contains("Process started", &mut stderr_lines); wait_contains("Hello world", &mut stdout_lines); - wait_for( - |m| { - m.contains("Watching paths") && m.contains("external_file_to_watch.txt") - }, - &mut stderr_lines, - ); + wait_for_watcher("external_file_to_watch.txt", &mut stderr_lines); // Change content of the external file write(&external_file_to_watch, "Hello world2").unwrap(); @@ -685,10 +677,7 @@ fn run_watch_load_unload_events() { // Wait for the first load event to fire wait_contains("load", &mut stdout_lines); - wait_for( - |m| m.contains("Watching paths") && m.contains("file_to_watch.js"), - &mut stderr_lines, - ); + wait_for_watcher("file_to_watch.js", &mut stderr_lines); // Change content of the file, this time without an interval to keep it alive. write( @@ -743,10 +732,7 @@ fn run_watch_not_exit() { wait_contains("Process started", &mut stderr_lines); wait_contains("error:", &mut stderr_lines); - wait_for( - |m| m.contains("Watching paths") && m.contains("file_to_watch.js"), - &mut stderr_lines, - ); + wait_for_watcher("file_to_watch.js", &mut stderr_lines); // Make sure the watcher actually restarts and works fine with the proper syntax write(&file_to_watch, "console.log(42);").unwrap(); @@ -808,6 +794,47 @@ fn run_watch_with_import_map_and_relative_paths() { } #[test] +fn run_watch_with_ext_flag() { + let t = TempDir::new(); + let file_to_watch = t.path().join("file_to_watch"); + write(&file_to_watch, "interface I{}; console.log(42);").unwrap(); + + let mut child = util::deno_cmd() + .current_dir(util::testdata_path()) + .arg("run") + .arg("--watch") + .arg("--log-level") + .arg("debug") + .arg("--ext") + .arg("ts") + .arg(&file_to_watch) + .env("NO_COLOR", "1") + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child); + + wait_contains("42", &mut stdout_lines); + + // Make sure the watcher actually restarts and works fine with the proper language + wait_for_watcher("file_to_watch", &mut stderr_lines); + wait_contains("Process finished", &mut stderr_lines); + + write( + &file_to_watch, + "type Bear = 'polar' | 'grizzly'; console.log(123);", + ) + .unwrap(); + + wait_contains("Restarting!", &mut stderr_lines); + wait_contains("123", &mut stdout_lines); + wait_contains("Process finished", &mut stderr_lines); + + check_alive_then_kill(child); +} + +#[test] fn run_watch_error_messages() { let t = TempDir::new(); let file_to_watch = t.path().join("file_to_watch.js"); @@ -1193,10 +1220,7 @@ fn run_watch_dynamic_imports() { &mut stdout_lines, ); - wait_for( - |m| m.contains("Watching paths") && m.contains("imported2.js"), - &mut stderr_lines, - ); + wait_for_watcher("imported2.js", &mut stderr_lines); wait_contains("finished", &mut stderr_lines); write( diff --git a/cli/tests/testdata/bundle/file_extensions/js_without_extension.out b/cli/tests/testdata/bundle/file_extensions/js_without_extension.out new file mode 100644 index 000000000..0273e6207 --- /dev/null +++ b/cli/tests/testdata/bundle/file_extensions/js_without_extension.out @@ -0,0 +1,8 @@ +[WILDCARD] +// deno-fmt-ignore-file +// deno-lint-ignore-file +// This code was bundled using `deno bundle` and it's not recommended to edit it manually + +"hello"; +console.log("executing javascript with no extension"); + diff --git a/cli/tests/testdata/bundle/file_extensions/ts_without_extension.out b/cli/tests/testdata/bundle/file_extensions/ts_without_extension.out new file mode 100644 index 000000000..39e355d14 --- /dev/null +++ b/cli/tests/testdata/bundle/file_extensions/ts_without_extension.out @@ -0,0 +1,7 @@ +[WILDCARD] +// deno-fmt-ignore-file +// deno-lint-ignore-file +// This code was bundled using `deno bundle` and it's not recommended to edit it manually + +console.log("executing typescript with no extension"); + diff --git a/cli/tests/testdata/file_extensions/js_without_extension b/cli/tests/testdata/file_extensions/js_without_extension new file mode 100644 index 000000000..4774be326 --- /dev/null +++ b/cli/tests/testdata/file_extensions/js_without_extension @@ -0,0 +1,3 @@ +let i = 123; +i = "hello" +console.log("executing javascript with no extension"); diff --git a/cli/tests/testdata/file_extensions/js_without_extension.out b/cli/tests/testdata/file_extensions/js_without_extension.out new file mode 100644 index 000000000..1236c1e53 --- /dev/null +++ b/cli/tests/testdata/file_extensions/js_without_extension.out @@ -0,0 +1 @@ +executing javascript with no extension diff --git a/cli/tests/testdata/file_extensions/ts_with_extension.out b/cli/tests/testdata/file_extensions/ts_with_extension.out new file mode 100644 index 000000000..181959ee2 --- /dev/null +++ b/cli/tests/testdata/file_extensions/ts_with_extension.out @@ -0,0 +1 @@ +executing typescript with extension diff --git a/cli/tests/testdata/file_extensions/ts_with_extension.ts b/cli/tests/testdata/file_extensions/ts_with_extension.ts new file mode 100644 index 000000000..3c49f7484 --- /dev/null +++ b/cli/tests/testdata/file_extensions/ts_with_extension.ts @@ -0,0 +1,5 @@ +interface Lollipop { + _: number; +} + +console.log("executing typescript with extension"); diff --git a/cli/tests/testdata/file_extensions/ts_with_js_extension.js b/cli/tests/testdata/file_extensions/ts_with_js_extension.js new file mode 100644 index 000000000..3c49f7484 --- /dev/null +++ b/cli/tests/testdata/file_extensions/ts_with_js_extension.js @@ -0,0 +1,5 @@ +interface Lollipop { + _: number; +} + +console.log("executing typescript with extension"); diff --git a/cli/tests/testdata/file_extensions/ts_without_extension b/cli/tests/testdata/file_extensions/ts_without_extension new file mode 100644 index 000000000..f10891d7a --- /dev/null +++ b/cli/tests/testdata/file_extensions/ts_without_extension @@ -0,0 +1,3 @@ +interface Lollipop {} + +console.log("executing typescript with no extension"); diff --git a/cli/tests/testdata/file_extensions/ts_without_extension.out b/cli/tests/testdata/file_extensions/ts_without_extension.out new file mode 100644 index 000000000..b15c063c8 --- /dev/null +++ b/cli/tests/testdata/file_extensions/ts_without_extension.out @@ -0,0 +1 @@ +executing typescript with no extension diff --git a/cli/tests/unit/process_test.ts b/cli/tests/unit/process_test.ts index 1799a0190..e6c4bfe59 100644 --- a/cli/tests/unit/process_test.ts +++ b/cli/tests/unit/process_test.ts @@ -658,6 +658,6 @@ Deno.test( p.close(); p.stdout.close(); assertStrictEquals(code, 1); - assertStringIncludes(stderr, "No such file or directory"); + assertStringIncludes(stderr, "Failed getting cwd."); }, ); |