diff options
author | Absebo <abseboco@gmail.com> | 2020-10-19 23:53:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-19 23:53:39 +0200 |
commit | 1474d5d76d38c094c6c551bcd23a61f8328bb258 (patch) | |
tree | 4204448f1a03aefc776c2041f630479e0bbae7cc | |
parent | c488468b32ef3869a233190437641e5ec8ef11b6 (diff) |
fix(cli): Handling of relative importmaps while using watch (#7950)
-rw-r--r-- | cli/main.rs | 7 | ||||
-rw-r--r-- | cli/tests/integration_tests.rs | 60 |
2 files changed, 62 insertions, 5 deletions
diff --git a/cli/main.rs b/cli/main.rs index ecf8d5fc7..48e76d9ab 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -520,11 +520,8 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> { .collect(); if let Some(import_map) = program_state.flags.import_map_path.clone() { - paths_to_watch.push( - Url::parse(&format!("file://{}", &import_map))? - .to_file_path() - .unwrap(), - ); + paths_to_watch + .push(fs::resolve_from_cwd(std::path::Path::new(&import_map)).unwrap()); } // FIXME(bartlomieju): new file watcher is created on after each restart diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index cbe24625e..51eb922d9 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -1178,6 +1178,66 @@ fn repl_test_pty_multiline() { } #[test] +fn run_watch_with_importmap_and_relative_paths() { + fn create_relative_tmp_file( + directory: &TempDir, + filename: &'static str, + filecontent: &'static str, + ) -> std::path::PathBuf { + let absolute_path = directory.path().join(filename); + std::fs::write(&absolute_path, filecontent).expect("error writing file"); + let relative_path = absolute_path + .strip_prefix(util::root_path()) + .expect("unable to create relative temporary file") + .to_owned(); + assert!(relative_path.is_relative()); + relative_path + } + let temp_directory = + TempDir::new_in(util::root_path()).expect("tempdir fail"); + let file_to_watch = create_relative_tmp_file( + &temp_directory, + "file_to_watch.js", + "console.log('Hello world');", + ); + let import_map_path = create_relative_tmp_file( + &temp_directory, + "import_map.json", + "{\"imports\": {}}", + ); + + let mut child = util::deno_cmd() + .current_dir(util::root_path()) + .arg("run") + .arg("--unstable") + .arg("--watch") + .arg("--importmap") + .arg(&import_map_path) + .arg(&file_to_watch) + .env("NO_COLOR", "1") + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .expect("failed to spawn script"); + + let stdout = child.stdout.as_mut().unwrap(); + let mut stdout_lines = + std::io::BufReader::new(stdout).lines().map(|r| r.unwrap()); + let stderr = child.stderr.as_mut().unwrap(); + let mut stderr_lines = + std::io::BufReader::new(stderr).lines().map(|r| r.unwrap()); + + assert!(stderr_lines.next().unwrap().contains("Process terminated")); + assert!(stdout_lines.next().unwrap().contains("Hello world")); + + child.kill().unwrap(); + + drop(file_to_watch); + drop(import_map_path); + temp_directory.close().unwrap(); +} + +#[test] fn repl_test_console_log() { let (out, err) = util::run_and_collect_output( true, |