From a9a57dcabbd3112cafe22d9863930eb0ead8d6f9 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 6 Apr 2021 04:58:11 +0200 Subject: [PATCH 1/5] Fix `wasm-opt --version` parsing --- src/cmd/build.rs | 93 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 25 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 05d0b285..db48198f 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -493,7 +493,7 @@ fn check_wasm_opt_version_compatibility(wasm_opt_path: &Path) -> Result<()> { let version_stdout = str::from_utf8(&cmd.stdout) .expect("Cannot convert stdout output of wasm-opt to string") .trim(); - let re = Regex::new(r"wasm-opt version (\d+)\s+").unwrap(); + let re = Regex::new(r"wasm-opt version (\d+)").unwrap(); let captures = re.captures(version_stdout).ok_or_else(|| { anyhow::anyhow!( "Unable to extract version information from {}.\n\ @@ -516,7 +516,11 @@ fn check_wasm_opt_version_compatibility(wasm_opt_path: &Path) -> Result<()> { ) })?; - log::info!("The wasm-opt version is \"{}\"", version_stdout); + log::info!( + "The wasm-opt version output is \"{}\", which was parsed to \"{}\"", + version_stdout, + version_number + ); if version_number < 99 { anyhow::bail!( "Your wasm-opt version is {}, but we require a version >= 99.", @@ -642,7 +646,11 @@ mod tests_ci_only { BuildArtifacts, ManifestPath, OptimizationPasses, UnstableFlags, UnstableOptions, Verbosity, VerbosityFlags, }; - use std::{io::Write, os::unix::fs::PermissionsExt, path::PathBuf}; + use std::{ + io::Write, + os::unix::fs::PermissionsExt, + path::{Path, PathBuf}, + }; /// Modifies the `Cargo.toml` under the supplied `cargo_toml_path` by /// setting `optimization-passes` in `[package.metadata.contract]` to `passes`. @@ -661,6 +669,23 @@ mod tests_ci_only { .expect("writing manifest failed"); } + /// Creates an executable `wasm-opt-mocked` file which outputs + /// "wasm-opt version `version`". + /// + /// Returns the path to this file. + fn mock_wasm_opt_version(tmp_dir: &Path, version: &str) -> PathBuf { + let path = tmp_dir.join("wasm-opt-mocked"); + { + let mut file = std::fs::File::create(&path).unwrap(); + let version = format!("#!/bin/sh\necho \"wasm-opt version {}\"", version); + file.write_all(version.as_bytes()) + .expect("writing wasm-opt-mocked failed"); + } + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o777)) + .expect("setting permissions failed"); + path + } + #[test] fn build_code_only() { with_tmp_dir(|path| { @@ -855,19 +880,10 @@ mod tests_ci_only { } #[test] - fn incompatible_wasm_opt_version_must_be_detected() { + fn incompatible_wasm_opt_version_must_be_detected_if_built_from_repo() { with_tmp_dir(|path| { // given - let path = path.join("wasm-opt-mocked"); - { - let mut file = std::fs::File::create(&path).unwrap(); - file.write_all( - b"#!/bin/sh\necho \"wasm-opt version 98 (version_13-79-gc12cc3f50)\"", - ) - .expect("writing wasm-opt-mocked failed"); - } - std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o777)) - .expect("setting permissions failed"); + let path = mock_wasm_opt_version(path, "98 (version_13-79-gc12cc3f50)"); // when let res = check_wasm_opt_version_compatibility(&path); @@ -884,19 +900,46 @@ mod tests_ci_only { } #[test] - fn compatible_wasm_opt_version_must_be_detected() { + fn compatible_wasm_opt_version_must_be_detected_if_built_from_repo() { with_tmp_dir(|path| { // given - let path = path.join("wasm-opt-mocked"); - { - let mut file = std::fs::File::create(&path).unwrap(); - file.write_all( - b"#!/bin/sh\necho \"wasm-opt version 99 (version_99-79-gc12cc3f50)\"", - ) - .expect("writing wasm-opt-mocked failed"); - } - std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o777)) - .expect("setting permissions failed"); + let path = mock_wasm_opt_version(path, "99 (version_99-79-gc12cc3f50"); + + // when + let res = check_wasm_opt_version_compatibility(&path); + + // then + assert!(res.is_ok()); + + Ok(()) + }) + } + + #[test] + fn incompatible_wasm_opt_version_must_be_detected_if_installed_as_package() { + with_tmp_dir(|path| { + // given + let path = mock_wasm_opt_version(path, "98"); + + // when + let res = check_wasm_opt_version_compatibility(&path); + + // then + assert!(res.is_err()); + assert_eq!( + format!("{:?}", res), + "Err(Your wasm-opt version is 98, but we require a version >= 99.)" + ); + + Ok(()) + }) + } + + #[test] + fn compatible_wasm_opt_version_must_be_detected_if_installed_as_package() { + with_tmp_dir(|path| { + // given + let path = mock_wasm_opt_version(path, "99"); // when let res = check_wasm_opt_version_compatibility(&path); -- GitLab From dd4c4f9021c9e32e49bab448513cf25c5c968c6e Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 6 Apr 2021 05:13:54 +0200 Subject: [PATCH 2/5] Mark stdout output and error clearer --- src/cmd/build.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index db48198f..69c5e73a 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -496,7 +496,7 @@ fn check_wasm_opt_version_compatibility(wasm_opt_path: &Path) -> Result<()> { let re = Regex::new(r"wasm-opt version (\d+)").unwrap(); let captures = re.captures(version_stdout).ok_or_else(|| { anyhow::anyhow!( - "Unable to extract version information from {}.\n\ + "Unable to extract version information from \"{}\".\n\ Your wasm-opt version is most probably too old. Make sure you use a version >= 99.", version_stdout ) @@ -504,13 +504,16 @@ fn check_wasm_opt_version_compatibility(wasm_opt_path: &Path) -> Result<()> { let version_number: u32 = captures .get(1) // first capture group is at index 1 .ok_or_else(|| { - anyhow::anyhow!("Unable to extract version number from {:?}", version_stdout) + anyhow::anyhow!( + "Unable to extract version number from \"{:?}\"", + version_stdout + ) })? .as_str() .parse() .map_err(|err| { anyhow::anyhow!( - "Parsing version number failed with {:?} for {:?}", + "Parsing version number failed with \"{:?}\" for \"{:?}\"", err, version_stdout ) -- GitLab From 4d0206fdfed3be2176630e2210c262328d0da867 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 6 Apr 2021 11:01:27 +0200 Subject: [PATCH 3/5] Implement comments --- src/cmd/build.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 69c5e73a..5ae8c8dc 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -493,10 +493,10 @@ fn check_wasm_opt_version_compatibility(wasm_opt_path: &Path) -> Result<()> { let version_stdout = str::from_utf8(&cmd.stdout) .expect("Cannot convert stdout output of wasm-opt to string") .trim(); - let re = Regex::new(r"wasm-opt version (\d+)").unwrap(); + let re = Regex::new(r"wasm-opt version (\d+)").expect("invalid regex"); let captures = re.captures(version_stdout).ok_or_else(|| { anyhow::anyhow!( - "Unable to extract version information from \"{}\".\n\ + "Unable to extract version information from '{}'.\n\ Your wasm-opt version is most probably too old. Make sure you use a version >= 99.", version_stdout ) @@ -505,7 +505,7 @@ fn check_wasm_opt_version_compatibility(wasm_opt_path: &Path) -> Result<()> { .get(1) // first capture group is at index 1 .ok_or_else(|| { anyhow::anyhow!( - "Unable to extract version number from \"{:?}\"", + "Unable to extract version number from '{:?}'", version_stdout ) })? @@ -513,14 +513,14 @@ fn check_wasm_opt_version_compatibility(wasm_opt_path: &Path) -> Result<()> { .parse() .map_err(|err| { anyhow::anyhow!( - "Parsing version number failed with \"{:?}\" for \"{:?}\"", + "Parsing version number failed with '{:?}' for '{:?}'", err, version_stdout ) })?; log::info!( - "The wasm-opt version output is \"{}\", which was parsed to \"{}\"", + "The wasm-opt version output is '{}', which was parsed to '{}'", version_stdout, version_number ); -- GitLab From d29308e98676130321fa540f6fef4b2a9ca80034 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 6 Apr 2021 11:03:31 +0200 Subject: [PATCH 4/5] Update readme --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 09020aa5..340d3679 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,8 @@ We optimize the resulting contract Wasm using `binaryen`. You have two options f ## Usage ``` -cargo-contract 0.8.0 -Utilities to develop Wasm smart contracts. +cargo-contract 0.11.0-4d0206f-x86_64-linux-gnu +Utilities to develop Wasm smart contracts USAGE: cargo contract @@ -42,8 +42,11 @@ OPTIONS: SUBCOMMANDS: new Setup and create a new smart contract project - build Compiles the contract, generates metadata, bundles both together in a '.contract' file - check Check that the code builds as Wasm; does not output any build artifact to the top level `target/` directory + build Compiles the contract, generates metadata, bundles + both together in a `.contract` file + generate-metadata Command has been deprecated, use `cargo contract build` instead + check Check that the code builds as Wasm; does not output any + `.contract` artifact to the `target/` directory test Test the smart contract off-chain deploy Upload the smart contract code to the chain instantiate Instantiate a deployed smart contract -- GitLab From be3f55ce755c36e9c133f12361c3d4b40399adf8 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 6 Apr 2021 11:05:16 +0200 Subject: [PATCH 5/5] Update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 340d3679..ce3512f3 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ We optimize the resulting contract Wasm using `binaryen`. You have two options f ## Usage ``` -cargo-contract 0.11.0-4d0206f-x86_64-linux-gnu +cargo-contract 0.11.0 Utilities to develop Wasm smart contracts USAGE: -- GitLab