Skip to content

Commit

Permalink
Introducing an automatic way to test examples
Browse files Browse the repository at this point in the history
  • Loading branch information
crodas committed Dec 15, 2023
1 parent 21ab78f commit 542fc12
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 111 deletions.
2 changes: 2 additions & 0 deletions 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 forc-plugins/forc-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ anyhow = "1.0.75"
async-trait = "0.1.58"
atty = "0.2.14"
clap = { version = "3", features = ["derive", "env"] }
forc = { version = "0.48.1", path = "../../forc" }
forc-tracing = { version = "0.48.1", path = "../../forc-tracing" }
fuel-core-types = { workspace = true }
fuel-crypto = { workspace = true, features = ["random"] }
Expand Down
24 changes: 2 additions & 22 deletions forc-plugins/forc-crypto/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,8 @@ use fuels_core::types::bech32::Bech32Address;
use serde_json::json;
use std::str::{from_utf8, FromStr};

fn help() -> &'static str {
Box::leak(
format!(
r#"
EXAMPLES:
{}
"#,
examples()
)
.into_boxed_str(),
)
}

pub(crate) fn examples() -> &'static str {
r#"# Convert address formats
## Converts address from one format to another
forc crypto address 0x565e671a359a43e90c4c99761a02ddc448c0fae749a3e45ffeb7d275318a7589
## Converts address from one format to another
forc crypto address fuel12e0xwx34nfp7jrzvn9mp5qkac3yvp7h8fx37ghl7klf82vv2wkys6wd523
"#
forc::examples! {
[ Convert any valid address to all other supported formats => crypto "address" "fuel12e0xwx34nfp7jrzvn9mp5qkac3yvp7h8fx37ghl7klf82vv2wkys6wd523" ]
}

#[derive(Debug, clap::Args)]
Expand Down
38 changes: 6 additions & 32 deletions forc-plugins/forc-crypto/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,19 @@ use std::{
path::Path,
};

pub(crate) fn examples(sub_command: &str, example: &str) -> String {
format!(
r#"# Hashes a given data with the {sub_command} algorithm
## Hashes passing the data from the argument
forc crypto {sub_command} "{example}"
## Hashes a data through unix pipes
echo {example} | forc crypto {sub_command}
## Hashes passing the data through the stdin
forc crypto {sub_command}
{example}^D
## Hashes a file
forc crypto {sub_command} /path/to/file.txt
"#
)
}

fn after_long_help() -> &'static str {
Box::leak(
format!(
r#"EXAMPLES:
{}
{}"#,
examples("keccack256", "fuel"),
examples("sha256", "fuel")
)
.into_boxed_str(),
)
forc::examples! {
[ Hashes data passing as an argument with SHA256 => crypto "sha256" "test" ]
[ Hashes data passing as an argument with Keccak256 => crypto "keccak256" "test" ]
[ Hashes a file path as an argument with Sha256 => crypto "sha256" "src/args.rs" ]
[ Hashes a file path as an argument with Keccak256 => crypto "keccak256" "src/args.rs" ]
}

#[derive(Debug, Clone, clap::Args)]
#[clap(
author,
version,
about = "Hashes the argument or file with this hash",
after_long_help = after_long_help(),
after_long_help = help(),
)]
pub struct HashArgs {
/// This argument is optional, it can be either:
Expand Down
29 changes: 5 additions & 24 deletions forc-plugins/forc-crypto/src/keygen/new_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,15 @@ use std::ops::Deref;

const ABOUT: &str = "Creates a new key for use with fuel-core";

pub(crate) fn examples() -> String {
format!(
r#"# {}
## Creates a new key for block-production
forc crypto new-key
## Creates a new key for
forc crypto new-key -k peering
"#,
ABOUT
)
}

fn after_long_help() -> &'static str {
Box::leak(
format!(
r#"EXAMPLES:
{}"#,
examples()
)
.into_boxed_str(),
)
forc::examples! {
[ Creates a new key for block production by default => crypto "new-key" ]
[ Creates a new key for peering => crypto "new-key" "-k" "peering" ]
[ Creates a new key for block production => crypto "new-key" "-k" "peering" ]
}

/// Generate a random new secret & public key in the format expected by fuel-core
#[derive(Debug, clap::Args)]
#[clap(author, version, about = ABOUT, after_long_help = after_long_help())]
#[clap(author, version, about = ABOUT, after_long_help = help())]
pub struct Arg {
/// Key type to generate. It can either be `block-production` or `peering`.
#[clap(
Expand Down
31 changes: 7 additions & 24 deletions forc-plugins/forc-crypto/src/keygen/parse_secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,13 @@ use std::{ops::Deref, str::FromStr};

const ABOUT: &str = "Parses a private key to view the associated public key";

pub(crate) fn examples() -> String {
let secret = "{secret}";
format!(
r#"# {}
#[cfg(test)]
#[allow(non_upper_case_globals)]
const PrivateKey: &str = "f5204427d0ab9a311266c96a377f7c329cb8a41b9088225b6fcf40eefb423e28";

## Parses the secret for a block-production secret
forc crypto parse-secret "{secret}"
## Parses the secret for a block-production secret
forc crypto parse-secret "{secret}" -k peering
"#,
ABOUT
)
}

fn after_long_help() -> &'static str {
Box::leak(
format!(
r#"EXAMPLES:
{}"#,
examples()
)
.into_boxed_str(),
)
forc::examples! {
[ Parses the secret of a block production => crypto "parse-secret" PrivateKey ]
[ Parses the secret of a peering => crypto "parse-secret" "-k" "peering" PrivateKey ]
}

/// Parse a secret key to view the associated public key
Expand All @@ -41,7 +24,7 @@ fn after_long_help() -> &'static str {
author,
version,
about = ABOUT,
after_long_help = after_long_help(),
after_long_help = help(),
)]
pub struct Arg {
/// A private key in hex format
Expand Down
11 changes: 2 additions & 9 deletions forc-plugins/forc-crypto/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,8 @@ const ABOUT: &str = "Forc plugin for hashing arbitrary data.";
fn after_long_help() -> &'static str {
Box::leak(
format!(
r#"EXAMPLES:
{}
{}
{}
{}
{}
"#,
args::examples("keccack256", "fuel"),
args::examples("sha256", "fuel"),
"EXAMPLES:\n{}{}{}{}",
args::examples(),
address::examples(),
keygen::new_key::examples(),
keygen::parse_secret::examples(),
Expand Down
1 change: 1 addition & 0 deletions forc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ forc-util = { version = "0.48.1", path = "../forc-util" }
fs_extra = "1.2"
fuel-asm = { workspace = true }
hex = "0.4.3"
paste = "1.0.14"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.73"
sway-core = { version = "0.48.1", path = "../sway-core" }
Expand Down
38 changes: 38 additions & 0 deletions forc/src/cli/help.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#[macro_export]
macro_rules! examples {
($( [ $($description:ident)* => $command:tt $($arg:expr)* ] )*) => {
$(
$crate::paste::paste! {
#[cfg(test)]
#[test]
fn [<$($description:lower _)*:snake example>] () {

let mut proc = std::process::Command::new("cargo");
proc.arg("run");
proc.arg("--bin");
proc.arg(format!("forc-{}", stringify!($command)));
proc.arg("--");
$(
proc.arg($arg);
)*
let output = proc.output().expect(stringify!($command));
assert!(output.status.success(), "{}: {:?}", stringify!($($description)*), output);
}
}
)*

fn help() -> &'static str {
Box::leak(format!("EXAMPLES:\n{}", examples()).into_boxed_str())
}

pub fn examples() -> &'static str {
Box::leak( [
$(
$crate::paste::paste! {
format!("\t#{}\n\tforc {} {}\n\n", stringify!($($description)*), stringify!($command), stringify!($($arg)*) )
},
)*
].concat().into_boxed_str())
}
};
}
2 changes: 2 additions & 0 deletions forc/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub use test::Command as TestCommand;
use tracing::metadata::LevelFilter;
pub use update::Command as UpdateCommand;

#[macro_use]
pub mod help;
mod commands;
mod plugin;
pub mod shared;
Expand Down
2 changes: 2 additions & 0 deletions forc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ pub mod test {
pub mod util {
pub use sway_utils::constants;
}

pub extern crate paste;

0 comments on commit 542fc12

Please sign in to comment.