Cli tool tests (#977)

* add tests and fix some minor things

* implement output writer and some tests

* formatting

* adjust tests and formatting

* clippy fix

* cargo fmt

* revert change in substrate runner

* clippy suggestions

* mode make_type to test mod
This commit is contained in:
Tadeo Hepperle
2023-06-14 10:52:31 +02:00
committed by GitHub
parent a62d6a400e
commit 3cfe91e3be
11 changed files with 228 additions and 116 deletions
+6 -5
View File
@@ -7,7 +7,7 @@ use clap::Parser as ClapParser;
use codec::{Decode, Encode};
use color_eyre::eyre::{self, bail};
use frame_metadata::{v15::RuntimeMetadataV15, RuntimeMetadata, RuntimeMetadataPrefixed};
use std::io::{self, Write};
use std::io::Write;
use subxt_metadata::Metadata;
/// Download metadata from a substrate node, for use with `subxt` codegen.
@@ -34,7 +34,7 @@ pub struct Opts {
runtime_apis: Option<Vec<String>>,
}
pub async fn run(opts: Opts) -> color_eyre::Result<()> {
pub async fn run(opts: Opts, output: &mut impl Write) -> color_eyre::Result<()> {
let bytes = opts.file_or_url.fetch().await?;
let mut metadata = RuntimeMetadataPrefixed::decode(&mut &bytes[..])?;
@@ -72,17 +72,18 @@ pub async fn run(opts: Opts) -> color_eyre::Result<()> {
match opts.format.as_str() {
"json" => {
let json = serde_json::to_string_pretty(&metadata)?;
println!("{json}");
write!(output, "{json}")?;
Ok(())
}
"hex" => {
let hex_data = format!("0x{:?}", hex::encode(metadata.encode()));
println!("{hex_data}");
write!(output, "{hex_data}")?;
Ok(())
}
"bytes" => {
let bytes = metadata.encode();
Ok(io::stdout().write_all(&bytes)?)
output.write_all(&bytes)?;
Ok(())
}
_ => Err(eyre::eyre!(
"Unsupported format `{}`, expected `json`, `hex` or `bytes`",