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

Update contract collection docs #2656

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

cptartur
Copy link
Member

@cptartur cptartur commented Nov 8, 2024

Closes #2627

Introduced changes

  • Split contract collection docs into two parts: one showing new mechanism and one showing old
  • Moved current (old) contract collection docs to subpage
  • Added new contract collection docs subpage

Checklist

  • Linked relevant issue
  • Updated relevant documentation
  • Added relevant tests
  • Performed self-review of the code
  • Added changes to CHANGELOG.md

@cptartur cptartur marked this pull request as ready for review November 13, 2024 11:40
It makes Scarb build all contracts from your package and save them to the `target/{current_profile}` directory
(read more on [Scarb website](https://docs.swmansion.com/scarb/docs/extensions/starknet/contract-target.html)).
`snforge` supports two mechanism for collecting contracts used in tests.
The default depends on Scarb version used and can be controlled with `--no-optimization` flag.
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit:

Suggested change
The default depends on Scarb version used and can be controlled with `--no-optimization` flag.
The default one depends on Scarb version used and can be controlled with `--no-optimization` flag.

Comment on lines +6 to +9
- If using Scarb version greater or equal to
2.8.3, [optimized collection mechanism](contract-collection/new-mechanism.md) is used by default.
- If using Scarb version below 2.8.3 or using `--no-optimization` flag with
`snforge test` [old collection mechanism](contract-collection/old-mechanism.md) is used.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- If using Scarb version greater or equal to
2.8.3, [optimized collection mechanism](contract-collection/new-mechanism.md) is used by default.
- If using Scarb version below 2.8.3 or using `--no-optimization` flag with
`snforge test` [old collection mechanism](contract-collection/old-mechanism.md) is used.
- If using Scarb version >= 2.8.3, [optimized collection mechanism](contract-collection/new-mechanism.md) is used by default.
- If using Scarb version < 2.8.3 or running `snforge test` with `--no-optimization` flag, the [old collection mechanism](contract-collection/old-mechanism.md) is used.

Comment on lines +13 to +14
> When using Scarb versions older than 2.8.3 it is **not possible** to enable new mechanism.
> Migrating to new Scarb version is required.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe let's just say that new mechanism requires Scarb >= 2.8.3, e.g.

Suggested change
> When using Scarb versions older than 2.8.3 it is **not possible** to enable new mechanism.
> Migrating to new Scarb version is required.
> Enabling new mechanism **requires** Scarb version >= 2.8.3.

Comment on lines +18 to +25
| Feature | Old Mechanism | Optimised Mechanism |
|---------------------------------------------------------|---------------|---------------------|
| Using contracts from `/src` | ✅ | ✅ |
| Using contracts from `/tests` | ❌ | ✅ |
| Using contracts from modules marked with `#[cfg(test)]` | ❌ | ✅ |
| Using contracts from dependencies | ✅ | ✅ |
| Contracts more closely resemble ones from real network | ✅ | ❌ |
| Additional compilation step required (`scarb build`) | ✅ | ❌ |
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Just stylistic 😅

Suggested change
| Feature | Old Mechanism | Optimised Mechanism |
|---------------------------------------------------------|---------------|---------------------|
| Using contracts from `/src` | | |
| Using contracts from `/tests` | | |
| Using contracts from modules marked with `#[cfg(test)]` | | |
| Using contracts from dependencies | | |
| Contracts more closely resemble ones from real network | | |
| Additional compilation step required (`scarb build`) | | |
| Feature | Old Mechanism | Optimised Mechanism |
|---------------------------------------------------------|---------------|---------------------|
| Using contracts from `/src` |||
| Using contracts from `/tests` |||
| Using contracts from modules marked with `#[cfg(test)]` |||
| Using contracts from dependencies |||
| Contracts more closely resemble ones from real network |||
| Additional compilation step required (`scarb build`) |||

It makes Scarb build all contracts from your package and save them to the `target/{current_profile}` directory
(read more on [Scarb website](https://docs.swmansion.com/scarb/docs/extensions/starknet/contract-target.html)).

Then, `snforge` loads compiled contracts from the package your tests are in, allowing you to declare the contracts in
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Then, `snforge` loads compiled contracts from the package your tests are in, allowing you to declare the contracts in
Then, `snforge` loads compiled contracts from the package your tests are located, allowing you to declare the contracts in

## Using External Contracts In Tests

If you wish to use contracts from your dependencies inside your tests (e.g. an ERC20 token, an account contract),
you must first make Scarb build them. You can do that by using `build-external-contracts` property in `Scarb.toml`,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Key is the correct name per TOML spec.

Suggested change
you must first make Scarb build them. You can do that by using `build-external-contracts` property in `Scarb.toml`,
you must first make Scarb build them. You can do that by using `build-external-contracts` key in `Scarb.toml`,

Comment on lines +3 to +5
For the [`declare`](../../appendix/snforge-library/declare.md) to function, snforge must collect and call build on
contracts in the package. By default, if using Scarb version greater or equal to 2.8.3, snforge will combine test
collection and contract collection steps.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure if it should be:

  • ... function to work, snforge must collect...
  • ... function, snforge must collect...
Suggested change
For the [`declare`](../../appendix/snforge-library/declare.md) to function, snforge must collect and call build on
contracts in the package. By default, if using Scarb version greater or equal to 2.8.3, snforge will combine test
collection and contract collection steps.
For the [`declare`](../../appendix/snforge-library/declare.md) function to, snforge must collect and call build on
contracts in the package. By default, if using Scarb >= 2.8.3, snforge will combine test
collection and contract collection steps.

target. If `integration` is not present, snforge will first collect contracts from the first encountered `[[test]]`
target.

After collecting from initial `[[test]]` target, snforge will collect contracts from any other encountered contracts.
Copy link
Collaborator

@franciszekjob franciszekjob Nov 13, 2024

Choose a reason for hiding this comment

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

snforge will collect contracts from any other encountered contracts.

should this sound like this 😅 ?


## Using External Contracts in Tests

To use contract from dependencies in tests `Scarb.toml` must be updated to include these contracts under
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit:

Suggested change
To use contract from dependencies in tests `Scarb.toml` must be updated to include these contracts under
To use contract from dependencies in tests, `Scarb.toml` must be updated to include these contracts under

| Using contracts from modules marked with `#[cfg(test)]` | ❌ | ✅ |
| Using contracts from dependencies | ✅ | ✅ |
| Contracts more closely resemble ones from real network | ✅ | ❌ |
| Additional compilation step required (`scarb build`) | ✅ | ❌ |
Copy link
Contributor

Choose a reason for hiding this comment

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

With the current version it kind of suggests that having this additional compilation step is better. Wouldn't it be more appropriate to rename it to Less compilation steps required? Then the green mark would be on the optimized side.

When you call `snforge test`, one of the things that `snforge` does is that it calls Scarb, particularly `scarb build`.
It makes Scarb build all contracts from your package and save them to the `target/{current_profile}` directory
(read more on [Scarb website](https://docs.swmansion.com/scarb/docs/extensions/starknet/contract-target.html)).
`snforge` supports two mechanism for collecting contracts used in tests.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
`snforge` supports two mechanism for collecting contracts used in tests.
`snforge` supports two mechanisms for collecting contracts used in tests.

```toml
[[target.starknet-contract]]
build-external-contracts = ["path::to::Contract1", "other::path::to::Contract2"]
```
Copy link
Contributor

Choose a reason for hiding this comment

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

I think a note with the link to the Scarb documentation should be added, the same as here


Contracts are collected from both `src` and `tests` directory, including modules marked with `#[cfg(test)]`.
Internally, snforge collects contracts from all `[[test]]` targets compiled by Scarb.
You can read more about that in [test collection](../test-collection.md) documentation.
Copy link
Contributor

Choose a reason for hiding this comment

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

Not clear what this sentence means. It suggests that you can read more about collecting contracts in the test collection documentation, which is not the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update Contracts Collection docs
3 participants