-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Conversation
768b36a
to
c856b15
Compare
@FuelLabs/tooling any idea how to generate a source map so I can test |
95d8e31
to
ee24693
Compare
forc/tests/Forc.toml
Outdated
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "tests" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 {}
There was a problem hiding this comment.
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
62a7151
to
4b1f987
Compare
4b1f987
to
43320d7
Compare
43320d7
to
60ee2f5
Compare
There was a problem hiding this 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 | |||
/// |
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 86d96f3
if option_env!("CLI_TEST").is_some() { | ||
return Ok(true); | ||
} |
There was a problem hiding this comment.
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)))
There was a problem hiding this comment.
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 cfg
s?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in bce67d1
There was a problem hiding this comment.
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;
}
}
There was a problem hiding this comment.
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
}
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool
c9e694a
to
bce67d1
Compare
a163023
to
c6d0943
Compare
[ 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" ] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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! { |
There was a problem hiding this comment.
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)
if_cli_test! { | |
#[cfg(test)] | |
{ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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))] |
There was a problem hiding this comment.
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:
#[cfg(any(feature = "test", test))] | |
#[cfg(test)] |
There was a problem hiding this comment.
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.
The setup is a codeblock that will be called before any test. This was needed in order to easily test things like `forc new`
After a test runs the teardown {} section allows to reset any state for other tests
Updated the macro so every instance have their own CWD so the binaries can be called in parallel
Co-authored-by: Kaya Gökalp <[email protected]>
6d65175
to
54dd5dc
Compare
## 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.
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 theCLI_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
Breaking*
orNew Feature
labels where relevant.