Skip to content

Commit

Permalink
Improve cli_examples macro (#5589)
Browse files Browse the repository at this point in the history
## Description

Add macro to test argument parsing rather than testing the external
command through building and spawning a separated process. This is an
improved version of #5519

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
crodas authored Feb 10, 2024
1 parent 6fa3878 commit 7bcac37
Show file tree
Hide file tree
Showing 36 changed files with 237 additions and 292 deletions.
8 changes: 5 additions & 3 deletions forc-plugins/forc-client/src/cmd/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ pub use forc_util::tx_utils::Salt;
use crate::NodeTarget;

forc_util::cli_examples! {
[ Deploy a single contract => deploy "bc09bfa7a11a04ce42b0a5abf04fd437387ee49bf4561d575177e2946468b408" => r#".*Error making HTTP request.*"# ]
[ Deploy a single contract from a different path => deploy "bc09bfa7a11a04ce42b0a5abf04fd437387ee49bf4561d575177e2946468b408 --path ../tests/" => r#".*Error making HTTP request.*"# ]
[ Deploy to a custom network => deploy "--node-url https://beta-5.fuel.network/graphql" => ".*Refused to create a new wallet.*" ]
super::Command {
[ Deploy a single contract => "forc deploy bc09bfa7a11a04ce42b0a5abf04fd437387ee49bf4561d575177e2946468b408" ]
[ Deploy a single contract from a different path => "forc deploy bc09bfa7a11a04ce42b0a5abf04fd437387ee49bf4561d575177e2946468b408 --path {path}" ]
[ Deploy to a custom network => "forc deploy --node-url https://beta-5.fuel.network/graphql" ]
}
}

#[derive(Debug, Default, Parser)]
Expand Down
12 changes: 7 additions & 5 deletions forc-plugins/forc-client/src/cmd/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use devault::Devault;
use std::path::PathBuf;

forc_util::cli_examples! {
[ Submit a transaction from a json file => submit "./mint.json" => "Submission of tx or awaiting commit failed" ]
[ Submit a transaction from a json file and wait for confirmation => submit "./mint.json --await true" => "Submission of tx or awaiting commit failed" ]
[ Submit a transaction from a json file and get output in json => submit "./mint.json --tx-status-json true" => "Submission of tx or awaiting commit failed" ]
[ Submit a transaction from a json file to testnet => submit "./mint.json --testnet" => "Submission of tx or awaiting commit failed" ]
[ Submit a transaction from a json file to a local net => submit "./mint.json --target local" => "Submission of tx or awaiting commit failed" ]
super::Command {
[ Submit a transaction from a json file => "forc submit {path}/mint.json" ]
[ Submit a transaction from a json file and wait for confirmation => "forc submit {path}/mint.json --await true" ]
[ Submit a transaction from a json file and get output in json => "forc submit {path}/mint.json --tx-status-json true" ]
[ Submit a transaction from a json file to testnet => "forc submit {path}/mint.json --testnet" ]
[ Submit a transaction from a json file to a local net => "forc submit {path}/mint.json --target local" ]
}
}

/// Submit a transaction to the specified fuel node.
Expand Down
2 changes: 0 additions & 2 deletions forc-plugins/forc-client/tests/.gitignore

This file was deleted.

13 changes: 0 additions & 13 deletions forc-plugins/forc-client/tests/Forc.lock

This file was deleted.

8 changes: 0 additions & 8 deletions forc-plugins/forc-client/tests/Forc.toml

This file was deleted.

29 changes: 0 additions & 29 deletions forc-plugins/forc-client/tests/mint.json

This file was deleted.

11 changes: 0 additions & 11 deletions forc-plugins/forc-client/tests/src/main.sw

This file was deleted.

4 changes: 3 additions & 1 deletion forc-plugins/forc-crypto/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use serde_json::json;
use std::str::{from_utf8, FromStr};

forc_util::cli_examples! {
[ Convert an address to another format => crypto "address fuel12e0xwx34nfp7jrzvn9mp5qkac3yvp7h8fx37ghl7klf82vv2wkys6wd523" ]
crate::Command {
[ Convert an address to another format => "forc crypto address fuel12e0xwx34nfp7jrzvn9mp5qkac3yvp7h8fx37ghl7klf82vv2wkys6wd523" ]
}
}

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

forc_util::cli_examples! {
[ Hashes an argument with SHA256 => crypto "sha256 test" ]
[ Hashes an argument with Keccak256 => crypto "keccak256 test" ]
[ Hashes a file path with SHA256 => crypto "sha256 src/args.rs" ]
[ Hashes a file path with Keccak256 => crypto "keccak256 src/args.rs" ]
crate::Command {
[ Hashes an argument with SHA256 => "forc crypto sha256 test" ]
[ Hashes an argument with Keccak256 => "forc crypto keccak256 test" ]
[ Hashes a file path with SHA256 => "forc crypto sha256 {file}" ]
[ Hashes a file path with Keccak256 => "forc crypto keccak256 {file}" ]
}
}

#[derive(Debug, Clone, clap::Args)]
Expand Down
8 changes: 5 additions & 3 deletions forc-plugins/forc-crypto/src/keys/get_public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use fuels_core::types::bech32::Bech32Address;
use serde_json::json;

forc_util::cli_examples! {
[ Get the public key from a message and its signature => crypto r#"get-public-key \
0x1eff08081394b72239a0cf7ff6b499213dcb7a338bedbd75d072d504588ef27a1f74d5ceb2f111ec02ede097fb09ed00aa9867922ed39299dae0b1afc0fa8661 \
"This is a message that is signed""# ]
crate::Command {
[ Get the public key from a message and its signature => r#"forc crypto get-public-key \
0x1eff08081394b72239a0cf7ff6b499213dcb7a338bedbd75d072d504588ef27a1f74d5ceb2f111ec02ede097fb09ed00aa9867922ed39299dae0b1afc0fa8661 \
"This is a message that is signed""# ]
}
}

/// Parse a secret key to view the associated public key
Expand Down
8 changes: 5 additions & 3 deletions forc-plugins/forc-crypto/src/keys/new_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ use std::ops::Deref;
const ABOUT: &str = "Creates a new key for use with fuel-core";

forc_util::cli_examples! {
[ Creates a new key default for block production => 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 block-production" ]
crate::Command {
[ Creates a new key default for block production => "forc crypto new-key" ]
[ Creates a new key for peering => "forc crypto new-key -k peering" ]
[ Creates a new key for block production => "forc crypto new-key -k block-production" ]
}
}

/// Generate a random new secret & public key in the format expected by fuel-core
Expand Down
6 changes: 4 additions & 2 deletions forc-plugins/forc-crypto/src/keys/parse_secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use std::{ops::Deref, str::FromStr};
const ABOUT: &str = "Parses a private key to view the associated public key";

forc_util::cli_examples! {
[ Parses the secret of a block production => crypto "parse-secret \"f5204427d0ab9a311266c96a377f7c329cb8a41b9088225b6fcf40eefb423e28\"" ]
[ Parses the secret of a peering => crypto "parse-secret -k peering \"f5204427d0ab9a311266c96a377f7c329cb8a41b9088225b6fcf40eefb423e28\"" ]
crate::Command {
[ Parses the secret of a block production => "forc crypto parse-secret \"f5204427d0ab9a311266c96a377f7c329cb8a41b9088225b6fcf40eefb423e28\"" ]
[ Parses the secret of a peering => "forc crypto parse-secret -k peering \"f5204427d0ab9a311266c96a377f7c329cb8a41b9088225b6fcf40eefb423e28\"" ]
}
}

/// Parse a secret key to view the associated public key
Expand Down
12 changes: 7 additions & 5 deletions forc-plugins/forc-doc/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ use forc_pkg::source::IPFSNode;
const ABOUT: &str = "Forc plugin for building a Sway package's documentation";

forc_util::cli_examples! {
[ Build the docs for a project in the current path => doc ""]
[ Build the docs for a project in the current path and open it in the browser => doc "--open" ]
[ Build the docs for a project located in another path => doc "--manifest-path ../tests_project2" ]
[ Build the docs for the current project exporting private types => doc "--document-private-items" ]
[ Build the docs offline without downloading any dependency from the network => doc "--offline" ]
crate::Command {
[ Build the docs for a project in the current path => "forc doc"]
[ Build the docs for a project in the current path and open it in the browser => "forc doc --open" ]
[ Build the docs for a project located in another path => "forc doc --manifest-path {path}" ]
[ Build the docs for the current project exporting private types => "forc doc --document-private-items" ]
[ Build the docs offline without downloading any dependency from the network => "forc doc --offline" ]
}
}

#[derive(Debug, Parser, Default)]
Expand Down
2 changes: 0 additions & 2 deletions forc-plugins/forc-doc/tests/.gitignore

This file was deleted.

13 changes: 0 additions & 13 deletions forc-plugins/forc-doc/tests/Forc.lock

This file was deleted.

8 changes: 0 additions & 8 deletions forc-plugins/forc-doc/tests/Forc.toml

This file was deleted.

11 changes: 0 additions & 11 deletions forc-plugins/forc-doc/tests/src/main.sw

This file was deleted.

14 changes: 8 additions & 6 deletions forc-plugins/forc-fmt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ use sway_utils::{constants, find_parent_manifest_dir, get_sway_files, is_sway_fi
use swayfmt::Formatter;

forc_util::cli_examples! {
[ Run the formatter in check mode on the current directory => fmt "--check"]
[ Run the formatter in check mode on the current directory with short format => fmt "-c"]
[ Run formatter against a given file => fmt "--file src/main.sw"]
[ Run formatter against a given file with short format => fmt "-f src/main.sw"]
[ Run formatter against a given dir => fmt "--path ../tests/"]
[ Run formatter against a given dir with short format => fmt "-p ../tests"]
crate::App {
[ Run the formatter in check mode on the current directory => "forc fmt --check"]
[ Run the formatter in check mode on the current directory with short format => "forc fmt -c"]
[ Run formatter against a given file => "forc fmt --file {path}/src/main.sw"]
[ Run formatter against a given file with short format => "forc fmt -f {path}/src/main.sw"]
[ Run formatter against a given dir => "forc fmt --path {path}"]
[ Run formatter against a given dir with short format => "forc fmt -p {path}"]
}
}

#[derive(Debug, Parser)]
Expand Down
13 changes: 0 additions & 13 deletions forc-plugins/forc-fmt/tests/Forc.lock

This file was deleted.

8 changes: 0 additions & 8 deletions forc-plugins/forc-fmt/tests/Forc.toml

This file was deleted.

11 changes: 0 additions & 11 deletions forc-plugins/forc-fmt/tests/src/main.sw

This file was deleted.

25 changes: 15 additions & 10 deletions forc-plugins/forc-tx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ use std::path::PathBuf;
use thiserror::Error;

forc_util::cli_examples! {
[ Script example => tx r#"script --bytecode "out/debug/tests.bin" --data "data.bin" \
{
// This parser has a custom parser
super::Command::try_parse_from_args
} {
[ Script example => r#"forc tx script --bytecode "{path}/out/debug/name.bin" --data "{path}/data.bin" \
--receipts-root 0x2222222222222222222222222222222222222222222222222222222222222222"# ]
[ Multiple inputs => tx r#"create --bytecode "out/debug/tests.bin"
--storage-slots out/debug/tests-storage_slots.json
[ Multiple inputs => r#"forc tx create --bytecode "{name}/out/debug/name.bin"
--storage-slots "{path}/out/debug/name-storage_slots.json"
--script-gas-limit 100 \
--gas-price 0 \
--maturity 0 \
Expand Down Expand Up @@ -59,9 +63,9 @@ forc_util::cli_examples! {
--state-root 0x0000000000000000000000000000000000000000000000000000000000000000
"#
]
[ An example constructing a create transaction => tx "create \
--bytecode ./my-contract/out/debug/my-contract.bin \
--storage-slots out/debug/tests-storage_slots.json
[ An example constructing a create transaction => r#"forc tx create \
--bytecode {path}/out/debug/name.bin \
--storage-slots {path}/out/debug/name-storage_slots.json \
--script-gas-limit 100 \
--gas-price 0 \
--maturity 0 \
Expand All @@ -88,9 +92,9 @@ forc_util::cli_examples! {
--recipient 0x2222222222222222222222222222222222222222222222222222222222222222 \
--amount 1 \
--nonce 0xBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB \
--msg-data ./message.dat \
--predicate ./my-predicate2.bin \
--predicate-data ./my-predicate2.dat \
--msg-data {path}/message.dat \
--predicate {path}/my-predicate2.bin \
--predicate-data {path}/my-predicate2.dat \
output coin \
--to 0x2222222222222222222222222222222222222222222222222222222222222222 \
--amount 100 \
Expand All @@ -109,8 +113,9 @@ forc_util::cli_examples! {
--asset-id 0x0000000000000000000000000000000000000000000000000000000000000000 \
output contract-created \
--contract-id 0xCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC \
--state-root 0x0000000000000000000000000000000000000000000000000000000000000000"
--state-root 0x0000000000000000000000000000000000000000000000000000000000000000"#
]
}
}

/// The top-level `forc tx` command.
Expand Down
Loading

0 comments on commit 7bcac37

Please sign in to comment.