Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix binary tool version check #1330

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions Cargo.lock

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

10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
name = "wasm-pack"
description = "📦✨ your favorite rust -> wasm workflow tool!"
version = "0.12.1"
authors = ["Ashley Williams <ashley666ashley@gmail.com>", "Jesper Håkansson <jesper@jesperh.se>"]
authors = [
"Ashley Williams <ashley666ashley@gmail.com>",
"Jesper Håkansson <jesper@jesperh.se>",
]
repository = "https://github.com/rustwasm/wasm-pack.git"
license = "MIT OR Apache-2.0"
edition = "2021"
Expand All @@ -16,21 +19,23 @@ atty = "0.2.14"
binary-install = "0.2.0"
cargo_metadata = "0.15.2"
chrono = "0.4.23"
clap = { version = "4.2.5", features = ["derive"] }
console = "0.15.5"
dialoguer = "0.10.3"
env_logger = { version = "0.10.0", default-features = false }
glob = "0.3.1"
human-panic = "1.0.3"
lazy_static = "1.4.0"
log = "0.4.17"
parking_lot = "0.12.1"
regex = "1.9.5"
semver = "1.0.16"
serde = "1.0.152"
serde_derive = "1.0.152"
serde_ignored = "0.1.7"
serde_json = "1.0.91"
siphasher = "0.3.10"
strsim = "0.10.0"
clap = { version = "4.2.5", features = ["derive"] }
toml = "0.7.3"
ureq = { version = "2.6.2", features = ["json"] }
walkdir = "2.3.2"
Expand All @@ -42,4 +47,3 @@ lazy_static = "1.4.0"
predicates = "3.0.3"
serial_test = "2.0.0"
tempfile = "3.3.0"

5 changes: 5 additions & 0 deletions src/install/krate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::install::Tool;
use anyhow::Result;
use log::debug;
use serde::Deserialize;
const VERSION: Option<&str> = option_env!("CARGO_PKG_VERSION");

Expand All @@ -25,6 +26,10 @@ impl Krate {
.call()?;

let kr: KrateResponse = res.into_json()?;
debug!(
"Latest `{name}` version on crates.io is {}",
kr.krate.max_version
);
Ok(kr.krate)
}
}
19 changes: 16 additions & 3 deletions src/install/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use crate::install;
use crate::PBAR;
use anyhow::{anyhow, bail, Context, Result};
use binary_install::{Cache, Download};
use lazy_static::lazy_static;
use log::debug;
use log::{info, warn};
use regex::Regex;
use std::env;
use std::fs;
use std::path::Path;
Expand Down Expand Up @@ -99,7 +101,7 @@ pub fn check_version(tool: &Tool, path: &Path, expected_version: &str) -> Result

let v = get_cli_version(tool, path)?;
info!(
"Checking installed `{}` version == expected version: {} == {}",
r#"Checking installed `{}` version == expected version: "{}" == "{}""#,
tool, v, &expected_version
);
Ok(v == expected_version)
Expand All @@ -110,13 +112,24 @@ pub fn get_cli_version(tool: &Tool, path: &Path) -> Result<String> {
let mut cmd = Command::new(path);
cmd.arg("--version");
let stdout = child::run_capture_stdout(cmd, tool)?;
let version = stdout.trim().split_whitespace().nth(1);
match version {

match extract_version(&stdout) {
Some(v) => Ok(v.to_string()),
None => bail!("Something went wrong! We couldn't determine your version of the wasm-bindgen CLI. We were supposed to set that up for you, so it's likely not your fault! You should file an issue: https://github.com/rustwasm/wasm-pack/issues/new?template=bug_report.md.")
}
}

/// Extract the version number from the version string printed by a binary.
pub fn extract_version(raw: &str) -> Option<&str> {
lazy_static! {
static ref VERSION_EXTRACTOR: Regex = Regex::new(r"(^|\s)(\d+(\.\d+)*)($|\s)").unwrap();
}
VERSION_EXTRACTOR
.captures(raw)
.and_then(|captures| captures.get(2))
.map(|m| m.as_str())
}

/// Downloads a precompiled copy of the tool, if available.
pub fn download_prebuilt(
tool: &Tool,
Expand Down
21 changes: 20 additions & 1 deletion tests/all/download.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
use wasm_pack::install::{self, Arch, Os, Tool};
use wasm_pack::install::{self, extract_version, Arch, Os, Tool};

#[test]
fn can_extract_cli_version() {
let tests = [
("cargo-generate 0.18.4", Some("0.18.4")),
("wasm-bindgen 0.2.87", Some("0.2.87")),
("wasm-opt version 116", Some("116")),
("cargo-generate 1", Some("1")), // missing minor & patch version
("cargo-generate 0.18", Some("0.18")), // missing patch version
("cargo generate 0.18.4", Some("0.18.4")), // space-separated subcommand
("wasm-bindgen 0.2.87 (deadbeef)", Some("0.2.87")), // with commit hash
("69.420", Some("69.420")), // raw version
("wasm-opt version", None), // version missing
];

for (i, o) in tests {
assert_eq!(extract_version(i), o);
}
}

#[test]
#[cfg(any(
Expand Down
Loading