1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

feat: deno upgrade --output (#6352)

This commit is contained in:
crowlKats 2020-07-07 00:21:26 +02:00 committed by GitHub
parent 5c43131be1
commit 2b52e3daf1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 16 deletions

View file

@ -70,6 +70,7 @@ pub enum DenoSubcommand {
dry_run: bool, dry_run: bool,
force: bool, force: bool,
version: Option<String>, version: Option<String>,
output: Option<PathBuf>,
ca_file: Option<String>, ca_file: Option<String>,
}, },
} }
@ -569,11 +570,18 @@ fn upgrade_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
let dry_run = matches.is_present("dry-run"); let dry_run = matches.is_present("dry-run");
let force = matches.is_present("force"); let force = matches.is_present("force");
let version = matches.value_of("version").map(|s| s.to_string()); let version = matches.value_of("version").map(|s| s.to_string());
let output = if matches.is_present("output") {
let install_root = matches.value_of("output").unwrap();
Some(PathBuf::from(install_root))
} else {
None
};
let ca_file = matches.value_of("cert").map(|s| s.to_string()); let ca_file = matches.value_of("cert").map(|s| s.to_string());
flags.subcommand = DenoSubcommand::Upgrade { flags.subcommand = DenoSubcommand::Upgrade {
dry_run, dry_run,
force, force,
version, version,
output,
ca_file, ca_file,
}; };
} }
@ -848,7 +856,11 @@ Defaults to latest.
The version is downloaded from The version is downloaded from
https://github.com/denoland/deno/releases https://github.com/denoland/deno/releases
and is used to replace the current executable.", and is used to replace the current executable.
If you want to not replace the current Deno executable but instead download an
update to a different location, use the --output flag
deno upgrade --output $HOME/my_deno",
) )
.arg( .arg(
Arg::with_name("version") Arg::with_name("version")
@ -856,6 +868,12 @@ and is used to replace the current executable.",
.help("The version to upgrade to") .help("The version to upgrade to")
.takes_value(true), .takes_value(true),
) )
.arg(
Arg::with_name("output")
.long("output")
.help("The path to output the updated version to")
.takes_value(true),
)
.arg( .arg(
Arg::with_name("dry-run") Arg::with_name("dry-run")
.long("dry-run") .long("dry-run")
@ -1400,7 +1418,8 @@ mod tests {
force: true, force: true,
dry_run: true, dry_run: true,
version: None, version: None,
ca_file: None output: None,
ca_file: None,
}, },
..Flags::default() ..Flags::default()
} }
@ -2637,6 +2656,7 @@ mod tests {
force: false, force: false,
dry_run: false, dry_run: false,
version: None, version: None,
output: None,
ca_file: Some("example.crt".to_owned()), ca_file: Some("example.crt".to_owned()),
}, },
ca_file: Some("example.crt".to_owned()), ca_file: Some("example.crt".to_owned()),

View file

@ -733,8 +733,11 @@ pub fn main() {
force, force,
dry_run, dry_run,
version, version,
output,
ca_file, ca_file,
} => upgrade_command(dry_run, force, version, ca_file).boxed_local(), } => {
upgrade_command(dry_run, force, version, output, ca_file).boxed_local()
}
_ => unreachable!(), _ => unreachable!(),
}; };

View file

@ -399,11 +399,7 @@ fn fmt_stdin_error() {
#[test] #[test]
fn upgrade_in_tmpdir() { fn upgrade_in_tmpdir() {
let temp_dir = TempDir::new().unwrap(); let temp_dir = TempDir::new().unwrap();
let exe_path = if cfg!(windows) { let exe_path = temp_dir.path().join("deno");
temp_dir.path().join("deno")
} else {
temp_dir.path().join("deno.exe")
};
let _ = std::fs::copy(util::deno_exe_path(), &exe_path).unwrap(); let _ = std::fs::copy(util::deno_exe_path(), &exe_path).unwrap();
assert!(exe_path.exists()); assert!(exe_path.exists());
let _mtime1 = std::fs::metadata(&exe_path).unwrap().modified().unwrap(); let _mtime1 = std::fs::metadata(&exe_path).unwrap().modified().unwrap();
@ -444,11 +440,7 @@ fn upgrade_with_space_in_path() {
#[test] #[test]
fn upgrade_with_version_in_tmpdir() { fn upgrade_with_version_in_tmpdir() {
let temp_dir = TempDir::new().unwrap(); let temp_dir = TempDir::new().unwrap();
let exe_path = if cfg!(windows) { let exe_path = temp_dir.path().join("deno");
temp_dir.path().join("deno")
} else {
temp_dir.path().join("deno.exe")
};
let _ = std::fs::copy(util::deno_exe_path(), &exe_path).unwrap(); let _ = std::fs::copy(util::deno_exe_path(), &exe_path).unwrap();
assert!(exe_path.exists()); assert!(exe_path.exists());
let _mtime1 = std::fs::metadata(&exe_path).unwrap().modified().unwrap(); let _mtime1 = std::fs::metadata(&exe_path).unwrap().modified().unwrap();
@ -471,6 +463,41 @@ fn upgrade_with_version_in_tmpdir() {
// TODO(ry) assert!(mtime1 < mtime2); // TODO(ry) assert!(mtime1 < mtime2);
} }
// Warning: this test requires internet access.
#[test]
fn upgrade_with_out_in_tmpdir() {
let temp_dir = TempDir::new().unwrap();
let exe_path = temp_dir.path().join("deno");
let new_exe_path = temp_dir.path().join("foo");
let _ = std::fs::copy(util::deno_exe_path(), &exe_path).unwrap();
assert!(exe_path.exists());
let mtime1 = std::fs::metadata(&exe_path).unwrap().modified().unwrap();
let status = Command::new(&exe_path)
.arg("upgrade")
.arg("--version")
.arg("1.0.2")
.arg("--output")
.arg(&new_exe_path.to_str().unwrap())
.spawn()
.unwrap()
.wait()
.unwrap();
assert!(status.success());
assert!(new_exe_path.exists());
let mtime2 = std::fs::metadata(&exe_path).unwrap().modified().unwrap();
assert_eq!(mtime1, mtime2); // Original exe_path was not changed.
let v = String::from_utf8(
Command::new(&new_exe_path)
.arg("-V")
.output()
.unwrap()
.stdout,
)
.unwrap();
assert!(v.contains("1.0.2"));
}
#[test] #[test]
fn installer_test_local_module_run() { fn installer_test_local_module_run() {
let temp_dir = TempDir::new().expect("tempdir fail"); let temp_dir = TempDir::new().expect("tempdir fail");

View file

@ -56,6 +56,7 @@ pub async fn upgrade_command(
dry_run: bool, dry_run: bool,
force: bool, force: bool,
version: Option<String>, version: Option<String>,
output: Option<PathBuf>,
ca_file: Option<String>, ca_file: Option<String>,
) -> Result<(), ErrBox> { ) -> Result<(), ErrBox> {
let mut client_builder = Client::builder().redirect(Policy::none()); let mut client_builder = Client::builder().redirect(Policy::none());
@ -76,7 +77,7 @@ pub async fn upgrade_command(
Ok(ver) => { Ok(ver) => {
if !force && current_version == ver { if !force && current_version == ver {
println!("Version {} is already installed", &ver); println!("Version {} is already installed", &ver);
std::process::exit(1) return Ok(());
} else { } else {
ver ver
} }
@ -94,7 +95,7 @@ pub async fn upgrade_command(
"Local deno version {} is the most recent release", "Local deno version {} is the most recent release",
&crate::version::DENO &crate::version::DENO
); );
std::process::exit(1) return Ok(());
} else { } else {
latest_version latest_version
} }
@ -114,7 +115,13 @@ pub async fn upgrade_command(
check_exe(&new_exe_path, &install_version)?; check_exe(&new_exe_path, &install_version)?;
if !dry_run { if !dry_run {
replace_exe(&new_exe_path, &old_exe_path)?; match output {
Some(path) => {
fs::rename(&new_exe_path, &path)
.or_else(|_| fs::copy(&new_exe_path, &path).map(|_| ()))?;
}
None => replace_exe(&new_exe_path, &old_exe_path)?,
}
} }
println!("Upgrade done successfully"); println!("Upgrade done successfully");