Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to include special code only available for cli_examples #5519

Closed
wants to merge 24 commits into from

Conversation

crodas
Copy link
Contributor

@crodas crodas commented Jan 26, 2024

Description

The cli_examples are converted also to unit tests. These unit tests will compile a binary and execute it. The main goal is to enforce documentation correctness through the unit tests.

Sometimes a special case should be hard-coded (especially mocking user input) that should only be available for the binaries to be executed from the unit tests but should be excluded otherwise.

This PR adds the ability to include code through option_env! macro since the CLI_TEST should only be defined in for the binary to be executed from the unit tests.

This PR is a pre-requisite to solve FuelLabs/forc-wallet#157.

This PR also fixes #5482

This PR will include more examples as part of #5444.

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@crodas crodas self-assigned this Jan 26, 2024
@crodas crodas force-pushed the more_cli_examples branch 5 times, most recently from 768b36a to c856b15 Compare February 1, 2024 11:35
@crodas
Copy link
Contributor Author

crodas commented Feb 1, 2024

@FuelLabs/tooling any idea how to generate a source map so I can test forc addr2line ?

@crodas crodas marked this pull request as ready for review February 1, 2024 15:56
@crodas crodas force-pushed the more_cli_examples branch from 95d8e31 to ee24693 Compare February 1, 2024 16:23
@crodas crodas enabled auto-merge (squash) February 1, 2024 16:25
@crodas crodas requested a review from a team February 1, 2024 21:11
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "tests"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sample project should be under forc/tests/<project_name>

It should also have a more meaningful name, since all the projects under tests are used for testing.

The binary file should probably not be checked in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll move those files as part of the setup {} and teardown {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sdankel I removed all the fixtures from the source code, instead they are all generated inside setup {} and destroyed inside teardown {} or automatically since every test has its own folder space in /tmp,.

That said, tests now run much faster and run naturally in parallel instead of sequentially. /cc @FuelLabs/tooling

@crodas crodas force-pushed the more_cli_examples branch from 62a7151 to 4b1f987 Compare February 3, 2024 19:10
@crodas crodas requested review from sdankel and a team February 3, 2024 19:12
@crodas crodas force-pushed the more_cli_examples branch from 4b1f987 to 43320d7 Compare February 3, 2024 19:13
forc-util/src/cli.rs Outdated Show resolved Hide resolved
@JoshuaBatty JoshuaBatty requested a review from a team February 4, 2024 23:13
@crodas crodas force-pushed the more_cli_examples branch from 43320d7 to 60ee2f5 Compare February 5, 2024 13:46
@crodas crodas requested a review from JoshuaBatty February 5, 2024 13:47
Copy link
Member

@kayagokalp kayagokalp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, I haven't tested yet but left nitpicks and couple of questions to understand this better.

I see that there is test_project2 under forc-doc. Is that still necessary. Maybe we can rename that to be more meaningful

@@ -30,24 +33,28 @@ pub struct Command {
pub node: NodeTarget,
/// Optional 256-bit hexadecimal literal(s) to redeploy contracts.
///
/// For a single contract, use `--salt <SALT>`, eg.: forc deploy --salt 0x0000000000000000000000000000000000000000000000000000000000000001
/// For a single contract, use `--salt <SALT>`, eg.: forc deploy --salt
/// 0x0000000000000000000000000000000000000000000000000000000000000001
///
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we are adding these newlines? Is this how rustfmt formats these 🤔

Copy link
Member

@kayagokalp kayagokalp Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these are how these should be formatted I wonder if we can add a check for our doc comments formatting, in a later/seperate pr of course

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is done by rustfmt automatically. I'll see to revert it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 86d96f3

forc-util/src/cli.rs Outdated Show resolved Hide resolved
Comment on lines 67 to 70
if option_env!("CLI_TEST").is_some() {
return Ok(true);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required at all but might make sense to create a macro that expands to this, as this appears a lot. Would bring most of these checks to single line from 3-4 lines.

execute_if_cli_test!(return Ok(true)))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I am a little out of the loop so this might be already discussed. What was the reason we are doing these with if and not cfgs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if cfg can be used with ENV variables at compile time. Adding features is quite complicated as it needs changes in the Cargo.toml

execute_if_cli_test! {
   return Ok(true);
}

I like macros that can take a group of statements instead of a single statement. Thoughts @JoshuaBatty ? I can make it happen very quickly and that would abstract the usage of the ENV and it can be changed into the future to something like cfg

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in bce67d1

Copy link
Member

@kayagokalp kayagokalp Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arent' these only relevant while we are running cargo test? what would be stopping us from using something like cfg(test) instead of a separate env variable CLI_TESTS. Do we want to be able to run tests without cli tests or something?

The following code piece would only fail to compile if you execute cargo t and build correctly if you execute cargo b

fn test_me() {
    #[cfg(test)]
    {
    a = 10;
    }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can maybe make if_cli_tests { x } macro to expand into:

#[cfg(test)]
{
x
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh I see if we have other unit tests touching those functions things might get complicated, you may want to test the rest of the function but cli tests insertion would return early etc. A final thought is to make this into a separate flag:

fn test_me() {
    #[cfg(cli_test)]
    {
    a = 10;
    }
}

If you do not want to setup features in cargo tomls, you can set thorough env variables:

RUSTFLAGS="--cfg cli_test" cargo test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RUSTFLAGS is a great thing, I did not know about it, I'll give it a go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I like RUSTFLAGS and play with cfg, can we revisit that? I think the PR is ready as it is @kayagokalp .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool

@crodas crodas force-pushed the more_cli_examples branch from c9e694a to bce67d1 Compare February 5, 2024 22:27
@crodas crodas requested a review from kayagokalp February 5, 2024 22:28
forc-util/src/cli.rs Outdated Show resolved Hide resolved
forc-util/src/cli.rs Outdated Show resolved Hide resolved
forc-util/src/cli.rs Outdated Show resolved Hide resolved
@crodas crodas force-pushed the more_cli_examples branch from a163023 to c6d0943 Compare February 7, 2024 20:45
@crodas crodas requested review from JoshuaBatty and a team February 7, 2024 20:49
JoshuaBatty
JoshuaBatty previously approved these changes Feb 8, 2024
forc-plugins/forc-client/mint.json Outdated Show resolved Hide resolved
[ 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.*" ]
[ Deploy a single contract from a different path => deploy "bc09bfa7a11a04ce42b0a5abf04fd437387ee49bf4561d575177e2946468b408 --path {path}" => r#".*Error making HTTP request.*"# ]
[ Deploy to a custom network => deploy "--node-url https://beta-5.fuel.network/graphql" ]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be an error for this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for some reason, it used to fail with "account has no funds"

use fuel_core_client::client::{types::TransactionStatus, FuelClient};
use fuel_crypto::fuel_types::canonical::Deserialize;

/// A command for submitting transactions to a Fuel network.
pub async fn submit(cmd: cmd::Submit) -> anyhow::Result<()> {
let tx = read_tx(&cmd.tx_path)?;
let node_url = get_node_url(&cmd.network.node, &None)?;
if_cli_test! {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can do this without an env variable (link)

Suggested change
if_cli_test! {
#[cfg(test)]
{

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not really. Because this bit of code is executed from a binary that is unaware of the testing context.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if you pass cargo run -f test? That should make it aware of the feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sdankel -f does not work with cargo run. I had a few attempts with RUSTFLAGS. I will follow up with a ticket. Ideally, this should work with cfg(test). I'll keep trying to get it done, but if possible in a follow-up PR.

@@ -28,6 +28,32 @@ mod commands;
mod plugin;
pub mod shared;

/// Creates a new project and install their dependencies
#[cfg(any(feature = "test", test))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the same as:

Suggested change
#[cfg(any(feature = "test", test))]
#[cfg(test)]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, cfg(test) is a built-in thing, we also have a feature "test" whenever the test only function should be included.

@crodas crodas force-pushed the more_cli_examples branch from 6d65175 to 54dd5dc Compare February 8, 2024 23:01
@crodas crodas requested review from sdankel and JoshuaBatty February 8, 2024 23:01
@crodas crodas disabled auto-merge February 9, 2024 13:05
@crodas crodas marked this pull request as draft February 9, 2024 13:05
@crodas crodas mentioned this pull request Feb 9, 2024
7 tasks
crodas added a commit that referenced this pull request Feb 10, 2024
## 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.
@crodas crodas closed this Feb 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CLI tests should not be stateful
4 participants