0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

fix(cli): Handling of relative importmaps while using watch (#7950)

This commit is contained in:
Absebo 2020-10-19 23:53:39 +02:00 committed by GitHub
parent c488468b32
commit 1474d5d76d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 5 deletions

View file

@ -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

View file

@ -1177,6 +1177,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(