Skip to content

Commit

Permalink
WIP: Fix regression introduced in #61
Browse files Browse the repository at this point in the history
  • Loading branch information
chevdor committed May 9, 2023
1 parent f412927 commit 5d77a9b
Show file tree
Hide file tree
Showing 19 changed files with 264 additions and 199 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

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

13 changes: 13 additions & 0 deletions TODO.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
== TODOS:

- [ ] byte arrays should be smartly displayed: Short ones can be shown as such but long ones should be encoded to hex and truncated. The user can still use the json output to get the full data
- [ ] Show extrinsics: see `// TODO: deal with extrinsic as well`
- [x] load wasm file
- [x] add ws support (= switch to substrate-api-client ?)
- [x] compute section hashes
- [ ] show sections summary as diff
- [ ] fix subwasm meta | head => piping fails
- [ ] add --chain to meta command
The main command loads ONE wasm and analyses it (meta + version).
- [ ] Add option to SHOW a runtime as a ReducedRuntime
- [ ] hunt down expects and unwrap
- [ ] remove those --chain-a --chain-b and use ONE enum that can handle 3 sources: file / url / alias
- [ ] integrate with subrpc
13 changes: 6 additions & 7 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
mod opts;

use std::{env, io::Write};

use clap::{crate_name, crate_version, Parser};
use env_logger::Env;
use log::info;
use opts::*;
use serde_json::json;
use std::{env, io::Write};
use subwasmlib::*;
use text_style::{AnsiColor, StyledStr};

Expand Down Expand Up @@ -102,7 +101,7 @@ fn main() -> color_eyre::Result<()> {

match diff_opts.method {
DiffMethod::Reduced => {
let diff_result = reduced_diff(src_a, src_b);
let diff_result = reduced_diff(src_a, src_b).expect("Reduced diff failed");
if opts.json {
let s = serde_json::to_string_pretty(&diff_result).unwrap();
println!("{s}");
Expand All @@ -121,8 +120,7 @@ fn main() -> color_eyre::Result<()> {
println!("{diff_result}");
text_style::termion::render(std::io::stdout(), &warning).expect("Could not render line");
}
}
DiffMethod::Summary => todo!(),
} // DiffMethod::Summary => todo!(),
}
}

Expand Down Expand Up @@ -165,9 +163,10 @@ fn main() -> color_eyre::Result<()> {
let subwasm = Subwasm::new(&source);

if let Some(pallet) = sr_opts.pallet {
subwasm.display_reduced_pallet(&pallet, opts.json)
// todo: fix previous branches and DO return an anyhow:Result<()> instead of ignoring the output
let _ = subwasm.display_reduced_pallet(&pallet, opts.json);
} else {
subwasm.display_reduced_runtime(opts.json)
let _ = subwasm.display_reduced_runtime(opts.json);
}
}
};
Expand Down
33 changes: 33 additions & 0 deletions cli/tests/compress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#[cfg(test)]
mod cli_tests {
#[cfg(test)]
mod cli_compress {
use assert_cmd::Command;

#[test]
fn it_does_basic_compress_decompress() {
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
let assert = cmd.args(["get", "wss://rpc.polkadot.io:443", "--output", "compressed.wasm"]).assert();
assert.success().code(0);

let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
cmd.args(["decompress", "compressed.wasm", "decompressed.wasm"]).assert().success().code(0);

let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
cmd.args(["compress", "decompressed.wasm", "new_compressed.wasm"]).assert().success().code(0);
}

#[test]
fn it_does_decompress_on_already() {
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
let assert = cmd.args(["get", "wss://rpc.polkadot.io:443", "--output", "compressed.wasm"]).assert();
assert.success().code(0);

let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
cmd.args(["decompress", "compressed.wasm", "decompressed.wasm"]).assert().success().code(0);

let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
cmd.args(["decompress", "decompressed.wasm", "new_decompressed.wasm"]).assert().success().code(0);
}
}
}
18 changes: 18 additions & 0 deletions cli/tests/diff.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[cfg(test)]
mod cli_tests {
#[cfg(test)]
mod diff {
use assert_cmd::Command;

#[test]
fn it_shows_metadata() {
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
let assert = cmd.args(["get", "wss://rpc.polkadot.io:443", "--output", "runtime.wasm"]).assert();
assert.success().code(0);

let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
let assert = cmd.args(["meta", "runtime.wasm"]).assert();
assert.success().code(0);
}
}
}
24 changes: 24 additions & 0 deletions cli/tests/get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#[cfg(test)]
mod cli_tests {
#[cfg(test)]
mod get {
use assert_cmd::Command;
use std::path::Path;
#[test]
fn it_gets_a_runtime() {
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();

let assert = cmd.args(["get", "--output", "runtime.wasm", "wss://rpc.polkadot.io:443"]).assert();
assert.success().code(0);
assert!(Path::new("runtime.wasm").exists());
}

#[test]
fn it_fails_on_bad_chain() {
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();

let assert = cmd.args(["get", "--chain", "foobar"]).assert();
assert.failure().code(101);
}
}
}
13 changes: 13 additions & 0 deletions cli/tests/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[cfg(test)]
mod cli_tests {
#[cfg(test)]
mod info {
use assert_cmd::Command;
#[test]
fn it_fails_without_source() {
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
let assert = cmd.arg("info tcp://foo.bar").assert();
assert.failure().code(2);
}
}
}
48 changes: 48 additions & 0 deletions cli/tests/meta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#[cfg(test)]
mod cli_tests {
#[cfg(test)]
mod meta {
use assert_cmd::Command as AssertCommand;
use std::process::{Command, Stdio};

#[test]
fn it_shows_metadata() {
let mut cmd = AssertCommand::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
let assert = cmd.args(["get", "wss://rpc.polkadot.io:443", "--output", "runtime.wasm"]).assert();
assert.success().code(0);

let mut cmd = AssertCommand::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
let assert = cmd.args(["meta", "runtime.wasm"]).assert();
assert.success().code(0);
}

// todo: test/fix following
// c run -- meta --chain wnd -f json | head # OK
// c run -- meta --chain wnd -f scale | head # FAIL
// c run -- meta --chain wnd -f hex+scale | head # FIXED
// c run -- meta --chain wnd -f json+scale | head # FIXED
// #[test]
// fn it_shows_when_piped() {
// // let mut cmd = AssertCommand::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
// // let assert = cmd.args(["get", "wss://rpc.polkadot.io:443", "--output", "runtime.wasm"]).assert();
// // assert.success().code(0);

// let subwasm_process = Command::new(env!("CARGO_PKG_NAME"))
// .args(["meta", "wss://rpc.polkadot.io:443", "-f", "json"])
// .stdout(Stdio::piped())
// .spawn()
// .unwrap();
// let head_process = Command::new("head")
// .stdin(Stdio::from(subwasm_process.stdout.unwrap())) // Pipe through.
// .stdout(Stdio::piped())
// .spawn()
// .unwrap();
// let output = head_process.wait_with_output().unwrap();

// let s = String::from_utf8_lossy(&output.stdout);
// println!("s = {:?}", s);
// println!("s = {:?}", s.len());
// assert!(s.len() > 100)
// }
}
}
15 changes: 15 additions & 0 deletions cli/tests/misc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[cfg(test)]
mod cli_tests {

#[cfg(test)]
mod help {
use assert_cmd::Command;

#[test]
fn it_shows_help() {
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
let assert = cmd.arg("--help").assert();
assert.success().code(0);
}
}
}
95 changes: 0 additions & 95 deletions cli/tests/test.rs

This file was deleted.

2 changes: 2 additions & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ wasm-loader = { version = "1.0.0-alpha6", path = "../libs/wasm-loader" }
wasm-testbed = { version = "1.0.0-alpha6", path = "../libs/wasm-testbed" }
sp-version = { tag = "monthly-2023-05", git = "https://github.com/paritytech/substrate" }
hex = "0.4"
anyhow = "1.0"
calm_io = "0.1"
Loading

0 comments on commit 5d77a9b

Please sign in to comment.