fix(cli/install): escape % symbols in windows batch files (#9133)

Fixes #9096.
This commit is contained in:
Liam Murphy 2021-01-19 14:34:37 +11:00 committed by GitHub
parent 9ff468df73
commit 3505823e20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -54,7 +54,11 @@ fn generate_executable_file(
let args: Vec<String> = args.iter().map(|c| format!("\"{}\"", c)).collect();
let template = format!(
"% generated by deno install %\n@deno.exe {} %*\n",
args.join(" ")
args
.iter()
.map(|arg| arg.replace("%", "%%"))
.collect::<Vec<_>>()
.join(" ")
);
let mut file = File::create(&file_path)?;
file.write_all(template.as_bytes())?;
@ -328,6 +332,7 @@ fn is_in_path(dir: &PathBuf) -> bool {
#[cfg(test)]
mod tests {
use super::*;
use std::process::Command;
use std::sync::Mutex;
use tempfile::TempDir;
@ -832,4 +837,35 @@ mod tests {
));
}
}
#[test]
fn install_unicode() {
let temp_dir = TempDir::new().expect("tempdir fail");
let bin_dir = temp_dir.path().join("bin");
std::fs::create_dir(&bin_dir).unwrap();
let unicode_dir = temp_dir.path().join("Magnús");
std::fs::create_dir(&unicode_dir).unwrap();
let local_module = unicode_dir.join("echo_server.ts");
let local_module_str = local_module.to_string_lossy();
std::fs::write(&local_module, "// Some JavaScript I guess").unwrap();
install(
Flags::default(),
&local_module_str,
vec![],
Some("echo_test".to_string()),
Some(temp_dir.path().to_path_buf()),
false,
)
.expect("Install failed");
let mut file_path = bin_dir.join("echo_test");
if cfg!(windows) {
file_path = file_path.with_extension("cmd");
}
// We need to actually run it to make sure the URL is interpreted correctly
let status = Command::new(file_path).spawn().unwrap().wait().unwrap();
assert!(status.success());
}
}