use std::path::{Path, PathBuf};
use fallow_config::{ConfigLoadOptions, FallowConfig};
use crate::EngineError;
#[derive(Debug, Clone, Copy, Default)]
pub struct ConfigFileOptions {
pub allow_remote_extends: bool,
}
#[derive(Debug)]
pub struct ConfigFile {
pub config: FallowConfig,
pub path: Option<PathBuf>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ConfigLoadError {
Parse {
path: Option<PathBuf>,
message: String,
chain: String,
},
}
impl ConfigLoadError {
#[must_use]
pub fn message(&self) -> &str {
match self {
Self::Parse { message, .. } => message,
}
}
#[must_use]
pub fn chain(&self) -> &str {
match self {
Self::Parse { chain, .. } => chain,
}
}
}
impl From<ConfigLoadError> for EngineError {
fn from(err: ConfigLoadError) -> Self {
Self::new(format!("invalid config: {}", err.chain()))
}
}
pub fn load_config_file(
root: &Path,
config_path: Option<&Path>,
options: ConfigFileOptions,
) -> Result<ConfigFile, ConfigLoadError> {
let load_options = ConfigLoadOptions {
allow_remote_extends: options.allow_remote_extends,
};
if let Some(path) = config_path {
return FallowConfig::load_with_options(path, load_options)
.map(|config| ConfigFile {
config,
path: Some(path.to_path_buf()),
})
.map_err(|err| explicit_load_error(path, &err));
}
match FallowConfig::find_and_load_with_options(root, load_options) {
Ok(Some((config, path))) => Ok(ConfigFile {
config,
path: Some(path),
}),
Ok(None) => Ok(ConfigFile {
config: FallowConfig::default(),
path: None,
}),
Err(message) => Err(ConfigLoadError::Parse {
path: None,
chain: message.clone(),
message,
}),
}
}
fn explicit_load_error(path: &Path, err: &impl std::fmt::Display) -> ConfigLoadError {
ConfigLoadError::Parse {
path: Some(path.to_path_buf()),
message: err.to_string(),
chain: format!("{err:#}"),
}
}
#[cfg(test)]
mod tests {
use super::*;
const UNREACHABLE_REMOTE_EXTENDS: &str = r#"{ "extends": "https://127.0.0.1:1/fallow.json" }"#;
fn project(config: &str) -> (tempfile::TempDir, PathBuf) {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path().to_path_buf();
std::fs::create_dir(root.join(".git")).expect("git marker");
std::fs::write(root.join(".fallowrc.json"), config).expect("config");
(dir, root)
}
#[test]
fn explicit_error_keeps_the_path_and_the_cause_chain() {
let (_dir, root) = project(r#"{ "production": "yes" }"#);
let path = root.join(".fallowrc.json");
let cause = FallowConfig::load(&path).expect_err("wrong type");
let err = load_config_file(&root, Some(&path), ConfigFileOptions::default())
.expect_err("wrong type");
assert_eq!(
err,
ConfigLoadError::Parse {
path: Some(path),
message: cause.to_string(),
chain: format!("{cause:#}"),
}
);
}
struct WrappedError;
impl std::fmt::Display for WrappedError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Failed to read config file")?;
if f.alternate() {
f.write_str(": no such file")?;
}
Ok(())
}
}
#[test]
fn explicit_error_chain_adds_each_cause() {
let err = explicit_load_error(Path::new("fallow.json"), &WrappedError);
assert_eq!(
err,
ConfigLoadError::Parse {
path: Some(PathBuf::from("fallow.json")),
message: "Failed to read config file".to_owned(),
chain: "Failed to read config file: no such file".to_owned(),
}
);
}
#[test]
fn remote_extends_is_fetched_with_the_opt_in() {
let (_dir, root) = project(UNREACHABLE_REMOTE_EXTENDS);
let options = ConfigFileOptions {
allow_remote_extends: true,
};
for config_path in [None, Some(root.join(".fallowrc.json"))] {
let err =
load_config_file(&root, config_path.as_deref(), options).expect_err("fetch fails");
assert!(
err.chain().contains("Failed to fetch remote config"),
"{err:?}"
);
}
}
#[test]
fn discovered_error_has_no_path_and_one_text() {
let (_dir, root) = project(r#"{ "rules": "#);
let cause = FallowConfig::find_and_load(&root).expect_err("syntax error");
let err =
load_config_file(&root, None, ConfigFileOptions::default()).expect_err("syntax error");
assert_eq!(
err,
ConfigLoadError::Parse {
path: None,
message: cause.clone(),
chain: cause,
}
);
}
#[test]
fn remote_extends_is_refused_by_default() {
let (_dir, root) = project(r#"{ "extends": "https://example.com/fallow.json" }"#);
let err = load_config_file(&root, None, ConfigFileOptions::default()).expect_err("refused");
assert!(err.chain().contains("Remote config extends"), "{err:?}");
}
#[test]
fn a_project_without_a_file_gets_the_default_config() {
let dir = tempfile::tempdir().expect("tempdir");
std::fs::create_dir(dir.path().join(".git")).expect("git marker");
let file = load_config_file(dir.path(), None, ConfigFileOptions::default())
.expect("defaults load");
assert_eq!(file.path, None);
assert_eq!(
file.config.production,
fallow_config::ProductionConfig::default()
);
}
}