Skip to content

Commit

Permalink
refactor: move e2e tests into separate crate (#1374)
Browse files Browse the repository at this point in the history
A different crate forces `e2e` tests to only use what was exported
through `fuels`.

Part of an ongoing effort to stabilize the sdk

---------

Co-authored-by: Ahmed Sagdati <[email protected]>
Co-authored-by: hal3e <[email protected]>
  • Loading branch information
3 people authored May 14, 2024
1 parent 49c25d4 commit b9421db
Show file tree
Hide file tree
Showing 285 changed files with 616 additions and 610 deletions.
38 changes: 11 additions & 27 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,45 +59,28 @@ jobs:
fi
- name: Check format of Sway test projects
run: forc fmt --check
working-directory: packages/fuels
run: forc fmt --check --path e2e

- name: Build Sway test projects
run: forc build --release --terse --error-on-warnings
working-directory: packages/fuels
run: forc build --release --terse --error-on-warnings --path e2e

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
retention-days: 2
name: sway-examples
# cache only the sway build artifacts, skip all src files
path: |
packages/fuels/tests
!packages/fuels/tests/*.rs
!packages/fuels/tests/**/*.rs
!packages/fuels/tests/**/*.sw
!packages/fuels/tests/**/Forc.toml
!packages/fuels/tests/**/Forc.lock
!packages/fuels/tests/.gitignore
e2e/sway/**/out/*
# TODO: To be removed once https://github.com/FuelLabs/fuels-rs/issues/881 is unblocked.
- name: Build Sway test projects w type paths
run: forc build --release --terse --error-on-warnings --json-abi-with-callpaths
working-directory: packages/fuels
run: forc build --release --terse --error-on-warnings --json-abi-with-callpaths --path e2e

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
retention-days: 2
name: sway-examples-w-type-paths
# cache only the sway build artifacts, skip all src files
path: |
packages/fuels/tests
!packages/fuels/tests/*.rs
!packages/fuels/tests/**/*.rs
!packages/fuels/tests/**/*.sw
!packages/fuels/tests/**/Forc.toml
!packages/fuels/tests/**/Forc.lock
!packages/fuels/tests/.gitignore
e2e/sway/**/out/*
get-workspace-members:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -217,10 +200,11 @@ jobs:
- name: Download sway example artifacts
if: ${{ matrix.download_sway_artifacts }}
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: ${{ matrix.download_sway_artifacts }}
path: packages/fuels/tests/
# Needed because `upload-artifact` will remove 'e2e/sway' because it is shared between all matched files
path: e2e/sway/

- name: Install nextest
if: ${{ matrix.cargo_command == 'nextest' }}
Expand Down Expand Up @@ -249,7 +233,7 @@ jobs:
run: |
rustup target add wasm32-unknown-unknown
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
cd packages/wasm-tests
cd wasm-tests
wasm-pack test --node
- name: Check for invalid documentation anchors
Expand Down
18 changes: 5 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by Cargo
# will have compiled files and executables
/target/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Expand All @@ -12,17 +12,9 @@ Cargo.lock
# Don't add the generated MDBook artifacts
docs/book/

# Don't add lock files in the fuels tests directory.
packages/fuels/tests/**/**/Forc.lock
# Don't add Forc lock files
**/Forc.lock

# Don't add out/ files from test Sway projects.
packages/fuels/tests/**/**/out/

# Don't add target/ files from test Sway projects.
packages/fuels/tests/**/**/target/

# Don't add .gitignore files from test Sway projects.
packages/fuels/tests/**/**/.gitignore

# Don't add lock files in the fuels directory.
packages/fuels/Forc.lock
e2e/sway/**/out/
e2e/sway/**/.gitignore
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# https://doc.rust-lang.org/edition-guide/rust-2021/default-cargo-resolver.html#details
resolver = "2"
members = [
"e2e",
"examples/codec",
"examples/contracts",
"examples/cookbook",
Expand All @@ -23,9 +24,9 @@ members = [
"packages/fuels-macros",
"packages/fuels-programs",
"packages/fuels-test-helpers",
"packages/wasm-tests",
"scripts/check-docs",
"scripts/versions-replacer",
"wasm-tests",
]

[workspace.package]
Expand Down Expand Up @@ -92,7 +93,7 @@ fuel-types = { version = "0.49.0" }
fuel-vm = { version = "0.49.0" }

# Workspace projects
fuels = { version = "0.61.0", path = "./packages/fuels" }
fuels = { version = "0.61.0", path = "./packages/fuels", default-features = false }
fuels-accounts = { version = "0.61.0", path = "./packages/fuels-accounts", default-features = false }
fuels-code-gen = { version = "0.61.0", path = "./packages/fuels-code-gen", default-features = false }
fuels-core = { version = "0.61.0", path = "./packages/fuels-core", default-features = false }
Expand Down
20 changes: 10 additions & 10 deletions ci_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
# Note, if you need a custom branch, you can replace `--tag {RELEASE}` with the `--branch {BRANCH_NAME}`.

cargo fmt --all -- --check &&
forc fmt --check --path packages/fuels &&
forc build --release --terse --path packages/fuels &&
forc build --release --terse --json-abi-with-callpaths --path packages/fuels &&
cargo clippy --all-targets &&
cargo clippy --all-targets --all-features &&
cargo test --all-targets --all-features &&
cargo test --all-targets --all-features --workspace &&
cargo test --all-targets --workspace &&
cargo run --bin check-docs &&
$(cargo doc |& grep -A 6 "warning: unresolved link to")
forc fmt --check &&
forc build --release --terse &&
cargo clippy --all-targets &&
forc build --release --terse --json-abi-with-callpaths &&
cargo clippy --all-targets --all-features &&
cargo test --all-targets --all-features &&
cargo test --all-targets --all-features --workspace &&
cargo test --all-targets --workspace &&
cargo run --bin check-docs &&
$(cargo doc |& grep -A 6 "warning: unresolved link to")
4 changes: 2 additions & 2 deletions docs/src/calling-contracts/call-params.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ You can use these to forward coins to a contract. You can configure these parame
For instance, suppose the following contract that uses Sway's `msg_amount()` to return the amount sent in that transaction.

```rust,ignore
{{#include ../../../packages/fuels/tests/contracts/contract_test/src/main.sw:msg_amount}}
{{#include ../../../e2e/sway/contracts/contract_test/src/main.sw:msg_amount}}
```

Then, in Rust, after setting up and deploying the above contract, you can configure the amount being sent in the transaction like this:
Expand All @@ -31,7 +31,7 @@ Then, in Rust, after setting up and deploying the above contract, you can config
In the following example, we try to forward an amount of `100` of the base asset to `non_payable`. As its name suggests, `non_payable` isn't annotated with `#[payable]` in the contract code. Passing `CallParameters` with an amount other than `0` leads to an error:

```rust,ignore
{{#include ../../../packages/fuels/tests/contracts.rs:non_payable_params}}
{{#include ../../../e2e/tests/contracts.rs:non_payable_params}}
```

> **Note:** forwarding gas to a contract call is always possible, regardless of the contract method being non-payable.
Expand Down
6 changes: 3 additions & 3 deletions docs/src/calling-contracts/logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ Whenever you log a value within a contract method, the resulting log entry is ad
Consider the following contract method:

```rust,ignore
{{#include ../../../packages/fuels/tests/logs/contract_logs/src/main.sw:produce_logs}}
{{#include ../../../e2e/sway/logs/contract_logs/src/main.sw:produce_logs}}
```

You can access the logged values in Rust by calling `decode_logs_with_type::<T>` from a `FuelCallResponse`, where `T` is the type of the logged variables you want to retrieve. The result will be a `Vec<T>`:

```rust,ignore
{{#include ../../../packages/fuels/tests/logs.rs:produce_logs}}
{{#include ../../../e2e/tests/logs.rs:produce_logs}}
```

You can use the `decode_logs()` function to retrieve a `LogResult` struct containing a `results` field that is a vector of `Result<String>` values representing the success or failure of decoding each log.

```rust, ignore
{{#include ../../../packages/fuels/tests/logs.rs:decode_logs}}
{{#include ../../../e2e/tests/logs.rs:decode_logs}}
```

Due to possible performance hits, it is not recommended to use `decode_logs()` outside of a debugging scenario.
Expand Down
4 changes: 2 additions & 2 deletions docs/src/calling-contracts/low-level-calls.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ Your caller contract should call `std::low_level_call::call_with_function_select
- `std::low_level_call::CallParams`

```rust,ignore
{{#include ../../../packages/fuels/tests/contracts/low_level_caller/src/main.sw:low_level_call_contract}}
{{#include ../../../e2e/sway/contracts/low_level_caller/src/main.sw:low_level_call_contract}}
```

On the SDK side, you can construct an encoded function selector using `fuels::core::encode_fn_selector`, and encoded calldata using the `fuels::core::calldata!` macro.

E.g. to call the following function on the target contract:

```rust,ignore
{{#include ../../../packages/fuels/tests/contracts/contract_test/src/main.sw:low_level_call}}
{{#include ../../../e2e/sway/contracts/contract_test/src/main.sw:low_level_call}}
```

you would construct the function selector and the calldata as such, and provide them to the caller contract (like the one above):
Expand Down
4 changes: 2 additions & 2 deletions docs/src/calling-contracts/other-contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ If your contract method is calling other contracts you will have to add the appr
`with_contracts(&[&contract_instance, ...])` requires contract instances that were created using the `abigen` macro. When setting the external contracts with this method, logs and require revert errors originating from the external contract can be propagated and decoded by the calling contract.

```rust,ignore
{{#include ../../../packages/fuels/tests/contracts.rs:external_contract}}
{{#include ../../../e2e/tests/contracts.rs:external_contract}}
```

If however, you do not need do decode logs or you do not have a contract instance that was generated using the `abigen` macro you can use `with_contract_ids(&[&contract_id, ...])` and provide the required contract ids.

```rust,ignore
{{#include ../../../packages/fuels/tests/contracts.rs:external_contract_ids}}
{{#include ../../../e2e/tests/contracts.rs:external_contract_ids}}
```
2 changes: 1 addition & 1 deletion docs/src/calling-contracts/variable-outputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Sometimes, the contract you call might transfer funds to a specific address, dep
Let's say you deployed a contract with the following method:

```rust,ignore
{{#include ../../../packages/fuels/tests/contracts/token_ops/src/main.sw:variable_outputs}}
{{#include ../../../e2e/sway/contracts/token_ops/src/main.sw:variable_outputs}}
```

When calling `transfer_coins_to_output` with the SDK, you can specify the number of variable outputs by chaining `append_variable_outputs(amount)` to your call. Like this:
Expand Down
2 changes: 1 addition & 1 deletion docs/src/cookbook/deposit-and-withdraw.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Consider the following contract:

```rust,ignore
{{#include ../../../packages/fuels/tests/contracts/liquidity_pool/src/main.sw}}
{{#include ../../../e2e/sway/contracts/liquidity_pool/src/main.sw}}
```

As its name suggests, it represents a simplified example of a liquidity pool contract. The method `deposit()` expects you to supply an arbitrary amount of the `BASE_TOKEN`. As a result, it mints double the amount of the liquidity asset to the calling address. Analogously, if you call `withdraw()` supplying it with the liquidity asset, it will transfer half that amount of the `BASE_TOKEN` back to the calling address except for deducting it from the contract balance instead of minting it.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/custom-transactions/custom-calls.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ When preparing a contract call via `ContractCallHandler` or a script call via `S
## Custom script call

```rust,ignore
{{#include ../../../packages/fuels/tests/scripts.rs:script_call_tb}}
{{#include ../../../e2e/tests/scripts.rs:script_call_tb}}
```
2 changes: 1 addition & 1 deletion docs/src/custom-transactions/transaction-builders.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Finally, we verify the transaction succeeded and that the cold storage indeed ho
If you need to build the transaction without signatures, which is useful when estimating transaction costs or simulations, you can use the `build_without_signatures(&provider)` method and later sign the built transaction.

```rust,ignore
{{#include ../../../packages/fuels/tests/contracts.rs:tb_build_without_signatures}}
{{#include ../../../e2e/tests/contracts.rs:tb_build_without_signatures}}
```

> **Note** In contrast to adding signers to a transaction builder, when signing a built transaction, you must ensure that the order of signatures matches the order of signed inputs. Multiple signed inputs with the same owner will have the same witness index.
4 changes: 2 additions & 2 deletions docs/src/deploying/configurable-constants.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
In Sway, you can define `configurable` constants which can be changed during the contract deployment in the SDK. Here is an example how the constants are defined.

```rust,ignore
{{#include ../../../packages/fuels/tests/contracts/configurables/src/main.sw}}
{{#include ../../../e2e/sway/contracts/configurables/src/main.sw}}
```

Each of the configurable constants will get a dedicated `with` method in the SDK. For example, the constant `STR_4` will get the `with_STR_4` method which accepts the same type as defined in the contract code. Below is an example where we chain several `with` methods and deploy the contract with the new constants.

```rust,ignore
{{#include ../../../packages/fuels/tests/configurables.rs:contract_configurables}}
{{#include ../../../e2e/tests/configurables.rs:contract_configurables}}
```
2 changes: 1 addition & 1 deletion docs/src/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ use fuels::prelude::*;

## The Fuel Rust SDK source code

Another way to experience the SDK is to look at the source code. The `packages/fuels/tests/` folder is full of integration tests that go through almost all aspects of the SDK.
Another way to experience the SDK is to look at the source code. The `e2e/tests/` folder is full of integration tests that go through almost all aspects of the SDK.

> **Note** Before running the tests, we need to build all the Sway test projects. The file `packages/fuels/Forc.toml` contains a `[workspace], which members are the paths to all integration tests.
> To build these tests, run the following command:
Expand Down
6 changes: 3 additions & 3 deletions docs/src/predicates/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Predicates, in Sway, are programs that return a Boolean value and do not have an
Let's consider the following predicate example:

```rust,ignore
{{#include ../../../packages/fuels/tests/predicates/basic_predicate/src/main.sw}}
{{#include ../../../e2e/sway/predicates/basic_predicate/src/main.sw}}
```

We will look at a complete example of using the SDK to send and receive funds from a predicate.
Expand Down Expand Up @@ -46,10 +46,10 @@ Then we can transfer assets owned by the predicate via the [Account](../accounts
Same as contracts and scripts, you can define configurable constants in `predicates`, which can be changed during the predicate execution. Here is an example of how the constants are defined.

```rust,ignore
{{#include ../../../packages/fuels/tests/predicates/predicate_configurables/src/main.sw:predicate_configurables}}
{{#include ../../../e2e/sway/predicates/predicate_configurables/src/main.sw:predicate_configurables}}
```

Each configurable constant will get a dedicated `with` method in the SDK. For example, the constant `U8` will get the `with_U8` method which accepts the same type defined in sway. Below is an example where we chain several `with` methods and update the predicate with the new constants.

```rust,ignore
{{#include ../../../packages/fuels/tests/predicates.rs:predicate_configurables}}
{{#include ../../../e2e/tests/predicates.rs:predicate_configurables}}
2 changes: 1 addition & 1 deletion docs/src/predicates/send-spend-predicate.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This is a more involved example where the predicate accepts three signatures and matches them to three predefined public keys. The `ec_recover_address` function is used to recover the public key from the signatures. If two of the three extracted public keys match the predefined public keys, the funds can be spent. Note that the signature order has to match the order of the predefined public keys.

```rust,ignore
{{#include ../../../packages/fuels/tests/predicates/signatures/src/main.sw}}
{{#include ../../../e2e/sway/predicates/signatures/src/main.sw}}
```

Let's use the SDK to interact with the predicate. First, let's create three wallets with specific keys. Their hashed public keys are already hard-coded in the predicate. Then we create the receiver wallet, which we will use to spend the predicate funds.
Expand Down
16 changes: 8 additions & 8 deletions docs/src/running-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
You can run a script using its JSON-ABI and the path to its binary file. You can run the scripts with arguments. For this, you have to use the `abigen!` macro seen [previously](./abigen/the-abigen-macro.md).

````rust,ignore
{{#include ../../packages/fuels/tests/scripts.rs:script_with_arguments}}
{{#include ../../e2e/tests/scripts.rs:script_with_arguments}}
````

Furthermore, if you need to separate submission from value retrieval for any reason, you can do so as follows:

```rust,ignore
{{#include ../../packages/fuels/tests/scripts.rs:submit_response_script}}
{{#include ../../e2e/tests/scripts.rs:submit_response_script}}
```

## Running scripts with transaction policies

The method for passing transaction policies is the same as [with contracts](./calling-contracts/tx-policies.md). As a reminder, the workflow would look like this:

```rust,ignore
{{#include ../../packages/fuels/tests/scripts.rs:script_with_tx_policies}}
{{#include ../../e2e/tests/scripts.rs:script_with_tx_policies}}
```

## Logs

Script calls provide the same logging functions, `decode_logs()` and `decode_logs_with_type<T>()`, as contract calls. As a reminder, the workflow looks like this:

```rust,ignore
{{#include ../../packages/fuels/tests/logs.rs:script_logs}}
{{#include ../../e2e/tests/logs.rs:script_logs}}
```

## Calling contracts from scripts
Expand All @@ -35,24 +35,24 @@ Scripts use the same interfaces for setting external contracts as [contract meth
Below is an example that uses `with_contracts(&[&contract_instance, ...])`.

```rust,ignore
{{#include ../../packages/fuels/tests/logs.rs:external_contract}}
{{#include ../../e2e/tests/logs.rs:external_contract}}
```

And this is an example that uses `with_contract_ids(&[&contract_id, ...])`.

```rust,ignore
{{#include ../../packages/fuels/tests/logs.rs:external_contract_ids}}
{{#include ../../e2e/tests/logs.rs:external_contract_ids}}
```

## Configurable constants

Same as contracts, you can define `configurable` constants in `scripts` which can be changed during the script execution. Here is an example how the constants are defined.

```rust,ignore
{{#include ../../packages/fuels/tests/scripts/script_configurables/src/main.sw}}
{{#include ../../e2e/sway/scripts/script_configurables/src/main.sw}}
```

Each configurable constant will get a dedicated `with` method in the SDK. For example, the constant `STR_4` will get the `with_STR_4` method which accepts the same type defined in sway. Below is an example where we chain several `with` methods and execute the script with the new constants.

```rust,ignore
{{#include ../../packages/fuels/tests/configurables.rs:script_configurables}}
{{#include ../../e2e/tests/configurables.rs:script_configurables}}
4 changes: 2 additions & 2 deletions docs/src/testing/chains.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ You can use `produce_blocks` to help achieve an arbitrary block height; this is
> **Note**: For the `produce_blocks` API to work, it is imperative to have `manual_blocks_enabled = true` in the config for the running node. See example below.
````rust,ignore
{{#include ../../../packages/fuels/tests/providers.rs:use_produce_blocks_to_increase_block_height}}
{{#include ../../../e2e/tests/providers.rs:use_produce_blocks_to_increase_block_height}}
````

You can also set a custom block time as the second, optional argument. Here is an example:

````rust,ignore
{{#include ../../../packages/fuels/tests/providers.rs:use_produce_blocks_custom_time}}
{{#include ../../../e2e/tests/providers.rs:use_produce_blocks_custom_time}}
````
Loading

0 comments on commit b9421db

Please sign in to comment.