1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

Remove conditionals from installer (#3909)

This commit is contained in:
Ryan Dahl 2020-02-07 03:31:19 -05:00 committed by GitHub
parent 98fc7db47d
commit 99186dbaa2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 188 deletions

View file

@ -48,30 +48,17 @@ fn validate_exec_name(exec_name: &str) -> Result<(), Error> {
} }
#[cfg(windows)] #[cfg(windows)]
/// On Windows if user is using Powershell .cmd extension is need to run the
/// installed module.
/// Generate batch script to satisfy that.
fn generate_executable_file( fn generate_executable_file(
file_path: PathBuf, file_path: PathBuf,
commands: Vec<String>, args: Vec<String>,
) -> Result<(), Error> { ) -> Result<(), Error> {
// On Windows if user is using Powershell .cmd extension is need to run the let args: Vec<String> = args.iter().map(|c| format!("\"{}\"", c)).collect();
// installed module.
// Generate batch script to satisfy that.
let template_header = "This executable is generated by Deno. Please don't modify it unless you know what it means.";
let commands: Vec<String> =
commands.iter().map(|c| format!("\"{}\"", c)).collect();
// TODO: remove conditionals in generated scripts
let template = format!( let template = format!(
r#"% {} % "% generated by deno install %\ndeno.exe {} %*\n",
@IF EXIST "%~dp0\deno.exe" ( args.join(" ")
"%~dp0\deno.exe" {} %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.TS;=;%
{} %*
)
"#,
template_header,
commands[1..].join(" "),
commands.join(" ")
); );
let file_path = file_path.with_extension(".cmd"); let file_path = file_path.with_extension(".cmd");
let mut file = File::create(&file_path)?; let mut file = File::create(&file_path)?;
@ -82,37 +69,18 @@ fn generate_executable_file(
#[cfg(not(windows))] #[cfg(not(windows))]
fn generate_executable_file( fn generate_executable_file(
file_path: PathBuf, file_path: PathBuf,
commands: Vec<String>, args: Vec<String>,
) -> Result<(), Error> { ) -> Result<(), Error> {
// On Windows if user is using Powershell .cmd extension is need to run the // On Windows if user is using Powershell .cmd extension is need to run the
// installed module. // installed module.
// Generate batch script to satisfy that. // Generate batch script to satisfy that.
let template_header = "This executable is generated by Deno. Please don't modify it unless you know what it means."; let args: Vec<String> = args.iter().map(|c| format!("\"{}\"", c)).collect();
let commands: Vec<String> =
commands.iter().map(|c| format!("\"{}\"", c)).collect();
// TODO: remove conditionals in generated scripts
let template = format!( let template = format!(
r#"#!/bin/sh r#"#!/bin/sh
# {} # generated by deno install
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')") deno {} "$@"
case \`uname\` in
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
esac
if [ -x "$basedir/deno" ]; then
"$basedir/deno" {} "$@"
ret=$?
else
{} "$@"
ret=$?
fi
exit $ret
"#, "#,
template_header, args.join(" "),
commands[1..].join(" "),
commands.join(" ")
); );
let mut file = File::create(&file_path)?; let mut file = File::create(&file_path)?;
file.write_all(template.as_bytes())?; file.write_all(template.as_bytes())?;
@ -197,7 +165,7 @@ pub fn install(
}; };
}; };
let mut executable_args = vec!["deno".to_string(), "run".to_string()]; let mut executable_args = vec!["run".to_string()];
executable_args.extend_from_slice(&flags.to_permission_args()); executable_args.extend_from_slice(&flags.to_permission_args());
executable_args.push(module_url.to_string()); executable_args.push(module_url.to_string());
executable_args.extend_from_slice(&args); executable_args.extend_from_slice(&args);
@ -270,39 +238,11 @@ mod tests {
assert!(file_path.exists()); assert!(file_path.exists());
let content = fs::read_to_string(file_path).unwrap(); let content = fs::read_to_string(file_path).unwrap();
// It's annoying when shell scripts don't have NL at the end.
assert_eq!(content.chars().last().unwrap(), '\n');
let expected_content = if cfg!(windows) { assert!(content
r#"% This executable is generated by Deno. Please don't modify it unless you know what it means. % .contains(r#""run" "http://localhost:4545/cli/tests/echo_server.ts""#));
@IF EXIST "%~dp0\deno.exe" (
"%~dp0\deno.exe" "run" "http://localhost:4545/cli/tests/echo_server.ts" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.TS;=;%
"deno" "run" "http://localhost:4545/cli/tests/echo_server.ts" %*
)
"#
} else {
r#"#!/bin/sh
# This executable is generated by Deno. Please don't modify it unless you know what it means.
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
case \`uname\` in
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
esac
if [ -x "$basedir/deno" ]; then
"$basedir/deno" "run" "http://localhost:4545/cli/tests/echo_server.ts" "$@"
ret=$?
else
"deno" "run" "http://localhost:4545/cli/tests/echo_server.ts" "$@"
ret=$?
fi
exit $ret
"#
};
assert_eq!(content, expected_content.to_string());
if let Some(home) = original_home { if let Some(home) = original_home {
env::set_var("HOME", home); env::set_var("HOME", home);
} }
@ -314,7 +254,6 @@ exit $ret
#[test] #[test]
fn install_custom_dir() { fn install_custom_dir() {
let temp_dir = TempDir::new().expect("tempdir fail"); let temp_dir = TempDir::new().expect("tempdir fail");
install( install(
DenoFlags::default(), DenoFlags::default(),
Some(temp_dir.path().to_string_lossy().to_string()), Some(temp_dir.path().to_string_lossy().to_string()),
@ -331,38 +270,8 @@ exit $ret
assert!(file_path.exists()); assert!(file_path.exists());
let content = fs::read_to_string(file_path).unwrap(); let content = fs::read_to_string(file_path).unwrap();
assert!(content
let expected_content = if cfg!(windows) { .contains(r#""run" "http://localhost:4545/cli/tests/echo_server.ts""#));
r#"% This executable is generated by Deno. Please don't modify it unless you know what it means. %
@IF EXIST "%~dp0\deno.exe" (
"%~dp0\deno.exe" "run" "http://localhost:4545/cli/tests/echo_server.ts" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.TS;=;%
"deno" "run" "http://localhost:4545/cli/tests/echo_server.ts" %*
)
"#
} else {
r#"#!/bin/sh
# This executable is generated by Deno. Please don't modify it unless you know what it means.
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
case \`uname\` in
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
esac
if [ -x "$basedir/deno" ]; then
"$basedir/deno" "run" "http://localhost:4545/cli/tests/echo_server.ts" "$@"
ret=$?
else
"deno" "run" "http://localhost:4545/cli/tests/echo_server.ts" "$@"
ret=$?
fi
exit $ret
"#
};
assert_eq!(content, expected_content.to_string());
} }
#[test] #[test]
@ -389,38 +298,7 @@ exit $ret
assert!(file_path.exists()); assert!(file_path.exists());
let content = fs::read_to_string(file_path).unwrap(); let content = fs::read_to_string(file_path).unwrap();
assert!(content.contains(r#""run" "--allow-read" "--allow-net" "http://localhost:4545/cli/tests/echo_server.ts" "--foobar""#));
let expected_content = if cfg!(windows) {
r#"% This executable is generated by Deno. Please don't modify it unless you know what it means. %
@IF EXIST "%~dp0\deno.exe" (
"%~dp0\deno.exe" "run" "--allow-read" "--allow-net" "http://localhost:4545/cli/tests/echo_server.ts" "--foobar" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.TS;=;%
"deno" "run" "--allow-read" "--allow-net" "http://localhost:4545/cli/tests/echo_server.ts" "--foobar" %*
)
"#
} else {
r#"#!/bin/sh
# This executable is generated by Deno. Please don't modify it unless you know what it means.
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
case \`uname\` in
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
esac
if [ -x "$basedir/deno" ]; then
"$basedir/deno" "run" "--allow-read" "--allow-net" "http://localhost:4545/cli/tests/echo_server.ts" "--foobar" "$@"
ret=$?
else
"deno" "run" "--allow-read" "--allow-net" "http://localhost:4545/cli/tests/echo_server.ts" "--foobar" "$@"
ret=$?
fi
exit $ret
"#
};
assert_eq!(content, expected_content.to_string());
} }
#[test] #[test]
@ -446,45 +324,6 @@ exit $ret
assert!(file_path.exists()); assert!(file_path.exists());
let content = fs::read_to_string(file_path).unwrap(); let content = fs::read_to_string(file_path).unwrap();
assert!(content.contains(&local_module_url.to_string()));
let expected_content = if cfg!(windows) {
format!(
r#"% This executable is generated by Deno. Please don't modify it unless you know what it means. %
@IF EXIST "%~dp0\deno.exe" (
"%~dp0\deno.exe" "run" "{}" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.TS;=;%
"deno" "run" "{}" %*
)
"#,
local_module_url.to_string(),
local_module_url.to_string()
)
} else {
format!(
r#"#!/bin/sh
# This executable is generated by Deno. Please don't modify it unless you know what it means.
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
case \`uname\` in
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
esac
if [ -x "$basedir/deno" ]; then
"$basedir/deno" "run" "{}" "$@"
ret=$?
else
"deno" "run" "{}" "$@"
ret=$?
fi
exit $ret
"#,
local_module_url.to_string(),
local_module_url.to_string()
)
};
assert_eq!(content, expected_content);
} }
} }

View file

@ -138,7 +138,7 @@ fn installer_test_local_module_run() {
let stderr_str = std::str::from_utf8(&output.stderr).unwrap().trim(); let stderr_str = std::str::from_utf8(&output.stderr).unwrap().trim();
println!("Got stdout: {:?}", stdout_str); println!("Got stdout: {:?}", stdout_str);
println!("Got stderr: {:?}", stderr_str); println!("Got stderr: {:?}", stderr_str);
assert_eq!(stdout_str, "hello, foo"); assert!(stdout_str.ends_with("hello, foo"));
drop(temp_dir); drop(temp_dir);
} }
@ -180,10 +180,10 @@ fn installer_test_remote_module_run() {
.env(path_var_name, path_var_value) .env(path_var_name, path_var_value)
.output() .output()
.expect("failed to spawn script"); .expect("failed to spawn script");
assert_eq!( assert!(std::str::from_utf8(&output.stdout)
std::str::from_utf8(&output.stdout).unwrap().trim(), .unwrap()
"hello, foo" .trim()
); .ends_with("hello, foo"));
drop(temp_dir); drop(temp_dir);
drop(g) drop(g)
} }
@ -243,7 +243,10 @@ fn bundle_exports() {
.output() .output()
.expect("failed to spawn script"); .expect("failed to spawn script");
// check the output of the test.ts program. // check the output of the test.ts program.
assert_eq!(std::str::from_utf8(&output.stdout).unwrap().trim(), "Hello"); assert!(std::str::from_utf8(&output.stdout)
.unwrap()
.trim()
.ends_with("Hello"));
assert_eq!(output.stderr, b""); assert_eq!(output.stderr, b"");
} }