Skip to content

Commit

Permalink
feat: first pass at "deno upgrade" (denoland#4328)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartossh committed Mar 23, 2020
1 parent a0ba476 commit ec07386
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 2 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ utime = "0.2.1"
webpki = "0.21.2"
webpki-roots = "0.19.0"
walkdir = "2.3.1"
semver-parser = "0.9.0"

[target.'cfg(windows)'.dependencies]
winapi = "0.3.8"
Expand Down
56 changes: 55 additions & 1 deletion cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ pub enum DenoSubcommand {
include: Option<Vec<String>>,
},
Types,
Upgrade {
dry_run: bool,
force: bool,
},
}

impl Default for DenoSubcommand {
Expand Down Expand Up @@ -250,6 +254,8 @@ pub fn flags_from_vec_safe(args: Vec<String>) -> clap::Result<Flags> {
completions_parse(&mut flags, m);
} else if let Some(m) = matches.subcommand_matches("test") {
test_parse(&mut flags, m);
} else if let Some(m) = matches.subcommand_matches("upgrade") {
upgrade_parse(&mut flags, m);
} else {
unimplemented!();
}
Expand Down Expand Up @@ -302,6 +308,7 @@ If the flag is set, restrict these messages to errors.",
.subcommand(run_subcommand())
.subcommand(test_subcommand())
.subcommand(types_subcommand())
.subcommand(upgrade_subcommand())
.long_about(DENO_HELP)
.after_help(ENV_VARIABLES_HELP)
}
Expand Down Expand Up @@ -534,6 +541,12 @@ fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
};
}

fn upgrade_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
let dry_run = matches.is_present("dry-run");
let force = matches.is_present("force");
flags.subcommand = DenoSubcommand::Upgrade { dry_run, force };
}

fn types_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("types")
.about("Print runtime TypeScript declarations")
Expand Down Expand Up @@ -731,6 +744,29 @@ Future runs of this module will trigger no downloads or compilation unless
)
}

fn upgrade_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("upgrade")
.about("Upgrade deno executable to newest version")
.long_about(
"Upgrade deno executable to newest available version.
The latest version is downloaded from
https://github.com/denoland/deno/releases
and is used to replace the current executable.",
)
.arg(
Arg::with_name("dry-run")
.long("dry-run")
.help("Perform all checks without replacing old exe"),
)
.arg(
Arg::with_name("force")
.long("force")
.short("f")
.help("Replace current exe even if not out-of-date"),
)
}

fn permission_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
app
.arg(
Expand Down Expand Up @@ -1142,7 +1178,8 @@ fn arg_hacks(mut args: Vec<String>) -> Vec<String> {
"types",
"install",
"help",
"version"
"version",
"upgrade"
];
let modifier_flags = sset!["-h", "--help", "-V", "--version"];
// deno [subcommand|behavior modifier flags] -> do nothing
Expand Down Expand Up @@ -1188,6 +1225,23 @@ mod tests {
assert_eq!(args4, ["deno", "run", "-A", "script.js", "-L=info"]);
}

#[test]
fn upgrade() {
let r =
flags_from_vec_safe(svec!["deno", "upgrade", "--dry-run", "--force"]);
let flags = r.unwrap();
assert_eq!(
flags,
Flags {
subcommand: DenoSubcommand::Upgrade {
force: true,
dry_run: true,
},
..Flags::default()
}
);
}

#[test]
fn version() {
let r = flags_from_vec_safe(svec!["deno", "--version"]);
Expand Down
7 changes: 7 additions & 0 deletions cli/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ extern crate indexmap;
#[cfg(unix)]
extern crate nix;
extern crate rand;
extern crate regex;
extern crate reqwest;
extern crate serde;
extern crate serde_derive;
extern crate tokio;
Expand Down Expand Up @@ -52,6 +54,7 @@ pub mod state;
mod test_runner;
pub mod test_util;
mod tokio_util;
mod upgrade;
pub mod version;
mod web_worker;
pub mod worker;
Expand All @@ -75,6 +78,7 @@ use log::Record;
use std::env;
use std::io::Write;
use std::path::PathBuf;
use upgrade::upgrade_command;
use url::Url;

static LOGGER: Logger = Logger;
Expand Down Expand Up @@ -487,6 +491,9 @@ pub fn main() {
let _r = std::io::stdout().write_all(types.as_bytes());
return;
}
DenoSubcommand::Upgrade { force, dry_run } => {
upgrade_command(dry_run, force).boxed_local()
}
_ => unreachable!(),
};

Expand Down
24 changes: 24 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@ fn fmt_stdin_error() {
assert!(!output.status.success());
}

// Warning: this test requires internet access.
#[test]
fn upgrade_in_tmpdir() {
let temp_dir = TempDir::new().unwrap();
let exe_path = if cfg!(windows) {
temp_dir.path().join("deno")
} else {
temp_dir.path().join("deno.exe")
};
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("--force")
.spawn()
.unwrap()
.wait()
.unwrap();
assert!(status.success());
let _mtime2 = std::fs::metadata(&exe_path).unwrap().modified().unwrap();
// TODO(ry) assert!(mtime1 < mtime2);
}

#[test]
fn installer_test_local_module_run() {
let temp_dir = TempDir::new().expect("tempdir fail");
Expand Down
Loading

0 comments on commit ec07386

Please sign in to comment.