Skip to content

Commit

Permalink
Change way the project path is searched to work with upcoming forge (#95
Browse files Browse the repository at this point in the history
)

<!-- Reference any GitHub issues resolved by this PR -->

Related foundry-rs/starknet-foundry#2637

## Introduced changes

<!-- A brief description of the changes -->

- Allowed raw sierra to be in `target/dev` directory
- Updated error messages

## Checklist

<!-- Make sure all of these are complete -->

- [x] Linked relevant issue
- [ ] Updated relevant documentation
- [ ] Added relevant tests
- [x] Performed self-review of the code
- [x] Added changes to `CHANGELOG.md`
  • Loading branch information
ksew1 authored Nov 18, 2024
1 parent 9805cce commit d2ad357
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.0] - 2024-09-17
## [Unreleased]

#### Fixed

- Fixed project inference to work with upcoming `snforge` `0.34.0`

## [0.2.0] - 2024-09-17

Expand Down
39 changes: 22 additions & 17 deletions crates/cairo-coverage/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ fn get_project_path(
} else {
find_user_project_path(source_sierra_path).context(indoc! {
r"Inference of project path failed.
Please provide the project path explicitly using the --project-path flag.
If you are using snforge, it is not possible to use cairo-coverage flags.
You need to run cairo-coverage directly."
Please provide the project path explicitly using the --project-path flag."
})
}
}
Expand All @@ -79,29 +77,36 @@ fn find_user_project_path(source_sierra_path: &Utf8PathBuf) -> Result<Utf8PathBu

match source_sierra_path.with_extension("").extension() {
Some("sierra") => {
source_sierra_path
.parent()
.filter(|parent| parent.file_name() == Some(SNFORGE_SIERRA_DIR))
.and_then(|parent| parent.parent())
.map(Utf8PathBuf::from)
navigate_and_check(source_sierra_path, &["target", "dev"])
.or_else(|| navigate_and_check(source_sierra_path, &[SNFORGE_SIERRA_DIR]))
.context(format!(
"Source sierra path should be in the format: <project_root>/{SNFORGE_SIERRA_DIR}/<file>.sierra.json, got: {source_sierra_path}"
"Source sierra path should be in one of the formats: \
<project_root>/{SNFORGE_SIERRA_DIR}/<file>.sierra.json \
or <project_root>/target/dev/<file>.sierra.json, got: {source_sierra_path}"
))
}
Some("contract_class") => {
source_sierra_path
.parent()
.filter(|parent| parent.file_name() == Some("dev"))
.and_then(|parent| parent.parent())
.filter(|parent| parent.file_name() == Some("target"))
.and_then(|parent| parent.parent())
.map(Utf8PathBuf::from)
navigate_and_check(source_sierra_path, &["target", "dev"])
.context(format!(
"Source sierra path should be in the format: <project_root>/target/dev/<file>.contract_class.json, got: {source_sierra_path}"
"Source sierra path should be in the format: \
<project_root>/target/dev/<file>.contract_class.json, got: {source_sierra_path}"
))
}
_ => bail!(
"Source sierra path should have a .sierra or .contract_class extension, got: {source_sierra_path}"
),
}
}

fn navigate_and_check(path: &Utf8PathBuf, folders: &[&str]) -> Option<Utf8PathBuf> {
folders
.iter()
.rev()
.try_fold(path.parent()?, |current, &folder| {
current
.file_name()
.filter(|name| *name == folder)
.map(|_| current.parent())?
})
.map(Utf8PathBuf::from)
}

0 comments on commit d2ad357

Please sign in to comment.