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

feat(forge) - Test scaffolding #5495

Merged
merged 5 commits into from
Aug 1, 2023

Conversation

raxhvl
Copy link
Contributor

@raxhvl raxhvl commented Jul 28, 2023

Note

I have exactly 3 days of Rust experience. So I had to wing this PR. Please excuse if I have missed any obvious expectations. BTW rust blown away by the rust compiler 🤯

Motivation

Creating test file typically involves repeated, verbose steps. This PR introduces a new forge sub command to help scaffold generation of test file - one of many features discussed in detail in this issue: #5466

Solution

A new forge sub command has been introduced forge generate test --contract-name <CONTRACT_NAME>. For a given contract it does the following:

  • Creates the test file in tests directory.
  • Adds the necessary imports.
  • Adds setup code for the test contract.

A sub command called generate has intentionally been added to accommodate more potential scaffolds in the future - like generating Interface for a given contract which also is a cumbersome exercise.

cc: @mattsse @Evalir @0x-r4bbit

@raxhvl raxhvl changed the title feat: #5466 - Test scaffolding feat(forge) - Test scaffolding Jul 28, 2023
@raxhvl
Copy link
Contributor Author

raxhvl commented Jul 31, 2023

HI @Evalir - Let me know if anything else to be done. Thanks!

@Evalir Evalir requested review from mattsse and Evalir July 31, 2023 12:27
Copy link
Member

@Evalir Evalir left a comment

Choose a reason for hiding this comment

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

looks good! supportive—pretty harmless addition. wdyt @mattsse ?

Copy link
Member

@Evalir Evalir left a comment

Choose a reason for hiding this comment

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

just some quick nits, but lgtm!

.replace("{instance_name}", &instance_name);

// Create the test directory if it doesn't exist.
fs::create_dir_all("test").unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

Could we avoid unwrap here? We can just use ? and maybe map_err as well to make it a tad more descriptive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely! I looked at some other files and figured there is wrapper around fs foundry_common::fs so I have swapped the implementation.

let test_file_path = Path::new("test").join(format!("{}.t.sol", contract_name));

// Write the test content to the test file.
fs::write(&test_file_path, test_content).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

ditto on unwrap

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 too

Copy link
Member

@Evalir Evalir left a comment

Choose a reason for hiding this comment

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

amazing, thank you!

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

this looks pretty good, nicely done

@mattsse mattsse merged commit cf03bb6 into foundry-rs:master Aug 1, 2023
20 checks passed
@raxhvl raxhvl deleted the rahul/test-scaffolding branch August 1, 2023 19:02
@utx0
Copy link

utx0 commented Aug 2, 2023

I like the idea of using templates and code gen in my use of foundry. What would also be nice is if a local project specific template could get used if it say exists in ./.foundry/templates/TestTemplate.t.sol and then this overrides the default. Reasoning for this is, on my current project we have a pretty complex testing setup where we dont follow some of the standard foundry ways of writing tests. Over time I could see the generation feature extent to other areas of the foundry development process too.

@mattsse
Copy link
Member

mattsse commented Aug 2, 2023

that'd be a nice feature, please open a separate issue for this

aathan pushed a commit to aathan/foundry that referenced this pull request Aug 2, 2023
* feat: foundry-rs#5466 - Test scaffolding

* reafactor: removed return

* chore: fmt

* refactor: named imports

* refactor: std::fs -> foundry_common::fs

---------

Co-authored-by: Rahul Ravindran <ravindranrahul@users.noreply.github.com>
Co-authored-by: Enrique Ortiz <hi@enriqueortiz.dev>
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.

5 participants