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

Inject CONTRACT_ID automatically while testing contracts #3673

Closed
kayagokalp opened this issue Jan 2, 2023 · 0 comments · Fixed by #3729
Closed

Inject CONTRACT_ID automatically while testing contracts #3673

kayagokalp opened this issue Jan 2, 2023 · 0 comments · Fixed by #3729
Assignees
Labels
enhancement New feature or request forc forc-test Everything related to the `forc-test` lib and `forc test` command.

Comments

@kayagokalp
Copy link
Member

blocked by #3622.

After #3672, we will have the ability to call into contract abis but to do that we need to provide the contract id of the contract we are testing at that moment. Since we already know the contract id we can inject it automatically like we do for contract dependencies as mentioned by @mitchmindtree in the original issue (#3262). This will make the following code piece usable:

let caller = abi(MyContract, CONTRACT_ID);
let result = caller.test_function {}();
assert(result == true)

With #3672 we require the following manual entry:

let contract_id = 0xa8f18533afc18453323bdf17c83750c556916ab183daacf46d7a8d3c633a40ee;
let caller = abi(MyContract, contract_id);
let result = caller.test_function {}();
assert(result == true)
@kayagokalp kayagokalp added enhancement New feature or request forc-test Everything related to the `forc-test` lib and `forc test` command. labels Jan 2, 2023
@kayagokalp kayagokalp self-assigned this Jan 2, 2023
@kayagokalp kayagokalp added the forc label Jan 2, 2023
kayagokalp added a commit that referenced this issue Jan 4, 2023
closes #3262.

# About this PR

For unit tests in contracts, we are now deploying the contract before
executing the tests so that we can support calling into contract ABI.

With this PR testing a contract looks like:

1. Compile the contract with tests
2. Compile the contract without the tests
3. Deploy the contract (the one that we compiled without the tests)
4. Run each test as a script

Currently there is no way to obtain the contract id without deploying
the contract (related #3444) and automatic `CONTRACT_ID` injection is
not available as we cannot inject it until we solve #3622.

~I am planning to tackle #3622 and add the documentation after that as
this is not directly usable by our users until we have `CONTRACT_ID`
injection or #3444.~
I changed my mind and decided to print the contract id for now so that
this will be useable and because of that I will be adding the
documentation with this PR.

For now it the testing flow will be rough as users will need to first
run `forc test` and get the contract id and manually edit their tests
while setting up the tests (writing the `abi` casts). I will add some
explanation of the process and point to #3673 since it will make the
testing flow seamless.

## TODO

- [x] Add tests
- [x]  Add documentation
kayagokalp added a commit that referenced this issue Jan 5, 2023
…3684)

closes #3622.
closes #3586.

unblocks #3673.

## About this PR

After parsing, I added a check to remove tests from `AstTree` if tests
are not enabled. Since all the other checks are done after this
exclusion of tests, this PR also prevents warnings and errors coming
from test functions to be emitted while using `forc build`. They are
still visible with `forc test` as they should be.
kayagokalp added a commit that referenced this issue Jan 24, 2023
…3729)

closes #3673.

## About this PR

This PR adds `CONTRACT_ID` injection while building contracts with `forc
build --test`. `CONTRACT_ID` is the id of the contract without the tests
added. To find out that id, we first compile the contract without tests
enabled.

The rough outline of stuff happening here:

1. We iterate over `BuildPlan` members to find out contracts and collect
their contract id into an injection map. In this step only the contracts
are built. To determine contract id we need to compile the contract
without tests. Since we also need the bytecode of the contract without
tests, we are collecting them as we come across them while iterating
over members.
2. With the injection map build and execute all the tests, so basically
after first step we are just doing the old `forc-test` behaviour.

So basically I added a pre-processing step for contract id collection
for those members that require it (contracts).

This enables the following test structure:

```rust
let caller = abi(MyContract, CONTRACT_ID);
let result = caller.test_function {}();
assert(result == true)
```
sdankel pushed a commit that referenced this issue Jan 25, 2023
…3729)

closes #3673.

## About this PR

This PR adds `CONTRACT_ID` injection while building contracts with `forc
build --test`. `CONTRACT_ID` is the id of the contract without the tests
added. To find out that id, we first compile the contract without tests
enabled.

The rough outline of stuff happening here:

1. We iterate over `BuildPlan` members to find out contracts and collect
their contract id into an injection map. In this step only the contracts
are built. To determine contract id we need to compile the contract
without tests. Since we also need the bytecode of the contract without
tests, we are collecting them as we come across them while iterating
over members.
2. With the injection map build and execute all the tests, so basically
after first step we are just doing the old `forc-test` behaviour.

So basically I added a pre-processing step for contract id collection
for those members that require it (contracts).

This enables the following test structure:

```rust
let caller = abi(MyContract, CONTRACT_ID);
let result = caller.test_function {}();
assert(result == true)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request forc forc-test Everything related to the `forc-test` lib and `forc test` command.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant