1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -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)]
/// 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(
file_path: PathBuf,
commands: Vec<String>,
args: Vec<String>,
) -> Result<(), Error> {
// On Windows if user is using Powershell .cmd extension is need to run the
// 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 args: Vec<String> = args.iter().map(|c| format!("\"{}\"", c)).collect();
let template = format!(
r#"% {} %
@IF EXIST "%~dp0\deno.exe" (
"%~dp0\deno.exe" {} %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.TS;=;%
{} %*
)
"#,
template_header,
commands[1..].join(" "),
commands.join(" ")
"% generated by deno install %\ndeno.exe {} %*\n",
args.join(" ")
);
let file_path = file_path.with_extension(".cmd");
let mut file = File::create(&file_path)?;
@ -82,37 +69,18 @@ fn generate_executable_file(
#[cfg(not(windows))]
fn generate_executable_file(
file_path: PathBuf,
commands: Vec<String>,
args: Vec<String>,
) -> Result<(), Error> {
// On Windows if user is using Powershell .cmd extension is need to run the
// 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 args: Vec<String> = args.iter().map(|c| format!("\"{}\"", c)).collect();
let template = format!(
r#"#!/bin/sh
# {}
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
case \`uname\` in
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
esac
if [ -x "$basedir/deno" ]; then
"$basedir/deno" {} "$@"
ret=$?
else
{} "$@"
ret=$?
fi
exit $ret
# generated by deno install
deno {} "$@"
"#,
template_header,
commands[1..].join(" "),
commands.join(" ")
args.join(" "),
);
let mut file = File::create(&file_path)?;
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.push(module_url.to_string());
executable_args.extend_from_slice(&args);
@ -270,39 +238,11 @@ mod tests {
assert!(file_path.exists());
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) {
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());
assert!(content
.contains(r#""run" "http://localhost:4545/cli/tests/echo_server.ts""#));
if let Some(home) = original_home {
env::set_var("HOME", home);
}
@ -314,7 +254,6 @@ exit $ret
#[test]
fn install_custom_dir() {
let temp_dir = TempDir::new().expect("tempdir fail");
install(
DenoFlags::default(),
Some(temp_dir.path().to_string_lossy().to_string()),
@ -331,38 +270,8 @@ exit $ret
assert!(file_path.exists());
let content = fs::read_to_string(file_path).unwrap();
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" "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());
assert!(content
.contains(r#""run" "http://localhost:4545/cli/tests/echo_server.ts""#));
}
#[test]
@ -389,38 +298,7 @@ exit $ret
assert!(file_path.exists());
let content = fs::read_to_string(file_path).unwrap();
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());
assert!(content.contains(r#""run" "--allow-read" "--allow-net" "http://localhost:4545/cli/tests/echo_server.ts" "--foobar""#));
}
#[test]
@ -446,45 +324,6 @@ exit $ret
assert!(file_path.exists());
let content = fs::read_to_string(file_path).unwrap();
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);
assert!(content.contains(&local_module_url.to_string()));
}
}

View file

@ -138,7 +138,7 @@ fn installer_test_local_module_run() {
let stderr_str = std::str::from_utf8(&output.stderr).unwrap().trim();
println!("Got stdout: {:?}", stdout_str);
println!("Got stderr: {:?}", stderr_str);
assert_eq!(stdout_str, "hello, foo");
assert!(stdout_str.ends_with("hello, foo"));
drop(temp_dir);
}
@ -180,10 +180,10 @@ fn installer_test_remote_module_run() {
.env(path_var_name, path_var_value)
.output()
.expect("failed to spawn script");
assert_eq!(
std::str::from_utf8(&output.stdout).unwrap().trim(),
"hello, foo"
);
assert!(std::str::from_utf8(&output.stdout)
.unwrap()
.trim()
.ends_with("hello, foo"));
drop(temp_dir);
drop(g)
}
@ -243,7 +243,10 @@ fn bundle_exports() {
.output()
.expect("failed to spawn script");
// 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"");
}