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

Change way the project path is searched to work with upcoming forge #95

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading