Skip to content
Open
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
11 changes: 4 additions & 7 deletions crates/cli/src/opts/build/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,11 @@ impl BuildOpts {
impl<'a> From<&'a BuildOpts> for Figment {
fn from(args: &'a BuildOpts) -> Self {
let root = if let Some(config_path) = &args.project_paths.config_path {
if !config_path.exists() {
panic!("error: config-path `{}` does not exist", config_path.display())
}
if !config_path.ends_with(Config::FILE_NAME) {
panic!("error: the config-path must be a path to a foundry.toml file")
}
let config_path = canonicalized(config_path);
config_path.parent().unwrap().to_path_buf()
config_path
.parent()
.map(|path| path.to_path_buf())
.unwrap_or_else(|| args.project_paths.project_root())
} else {
args.project_paths.project_root()
};
Expand Down
45 changes: 43 additions & 2 deletions crates/cli/src/opts/build/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use foundry_config::{
find_project_root, remappings_from_env_var,
};
use serde::Serialize;
use std::path::PathBuf;
use std::{ffi::OsStr, path::PathBuf};

/// Common arguments for a project's paths.
#[derive(Clone, Debug, Default, Serialize, Parser)]
Expand Down Expand Up @@ -58,7 +58,12 @@ pub struct ProjectPathOpts {
pub hardhat: bool,

/// Path to the config file.
#[arg(long, value_hint = ValueHint::FilePath, value_name = "FILE")]
#[arg(
long,
value_hint = ValueHint::FilePath,
value_name = "FILE",
value_parser = parse_config_path
)]
#[serde(skip)]
pub config_path: Option<PathBuf>,
}
Expand Down Expand Up @@ -90,6 +95,42 @@ impl ProjectPathOpts {
}
}

/// Parses and validates `--config-path`.
fn parse_config_path(path: &str) -> Result<PathBuf, String> {
let path = PathBuf::from(path);
if !path.exists() {
return Err(format!("config-path `{}` does not exist", path.display()));
}
if path.file_name() != Some(OsStr::new(Config::FILE_NAME)) {
return Err("the config-path must be a path to a foundry.toml file".to_string());
}
Ok(path)
}

#[cfg(test)]
mod tests {
use super::parse_config_path;
use foundry_config::Config;
use std::path::PathBuf;

#[test]
fn parse_config_path_rejects_nonexistent_path() {
let path = PathBuf::from("/definitely/nonexistent/path/foundry.toml");
let err = parse_config_path(path.to_str().expect("utf8 path")).unwrap_err();
assert!(err.contains("does not exist"), "unexpected error: {err}");
}

#[test]
fn parse_config_path_rejects_non_foundry_toml_file() {
let tmp = tempfile::NamedTempFile::new().unwrap();
let path = tmp.path().with_file_name("not-foundry.toml");
std::fs::write(&path, "").unwrap();

let err = parse_config_path(path.to_str().expect("utf8 path")).unwrap_err();
assert!(err.contains(Config::FILE_NAME), "error should mention required file name: {err}");
}
}

foundry_config::impl_figment_convert!(ProjectPathOpts);

// Make this args a `figment::Provider` so that it can be merged into the `Config`
Expand Down
Loading