use std::path::{Path, PathBuf};
use std::time::Duration;
use fallow_config::{ConfigLoadOptions, EnvSnapshot, FallowConfig};
use crate::EngineError;
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct ConfigFileOptions {
pub allow_remote_extends: bool,
pub extends_timeout: Duration,
}
impl ConfigFileOptions {
#[must_use]
pub fn new(allow_remote_extends: bool, env: &EnvSnapshot) -> Self {
Self {
allow_remote_extends,
extends_timeout: env.extends_timeout(),
}
}
}
impl Default for ConfigFileOptions {
fn default() -> Self {
let load = ConfigLoadOptions::default();
Self {
allow_remote_extends: load.allow_remote_extends,
extends_timeout: load.extends_timeout,
}
}
}
#[derive(Debug)]
pub struct ConfigFile {
pub config: FallowConfig,
pub path: Option<PathBuf>,
pub chain: ConfigChainSnapshot,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ConfigChainSnapshot {
files: Vec<(PathBuf, Option<Vec<u8>>)>,
changed_during_load: bool,
}
impl ConfigChainSnapshot {
fn read(path: Option<&Path>) -> Self {
let files = path
.into_iter()
.flat_map(FallowConfig::local_source_files)
.map(|file| {
let bytes = std::fs::read(&file).ok();
(file, bytes)
})
.collect();
Self {
files,
changed_during_load: false,
}
}
fn around_load(before: &Self, after: Self) -> Self {
Self {
changed_during_load: *before != after,
..after
}
}
#[must_use]
pub fn changed(&self) -> bool {
self.changed_during_load
|| self
.files
.iter()
.any(|(path, bytes)| std::fs::read(path).ok() != *bytes)
}
}
#[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()))
}
}
#[must_use]
pub fn find_config_file(root: &Path) -> Option<PathBuf> {
FallowConfig::find_config_path(root)
}
pub fn load_config_file(
root: &Path,
config_path: Option<&Path>,
options: ConfigFileOptions,
) -> Result<ConfigFile, ConfigLoadError> {
#[cfg(any(test, feature = "testing"))]
load_count::record(root);
let load_options = ConfigLoadOptions::new(options.allow_remote_extends)
.with_extends_timeout(options.extends_timeout);
if let Some(path) = config_path {
let before = ConfigChainSnapshot::read(Some(path));
return FallowConfig::load_with_options(path, load_options)
.map(|config| ConfigFile {
config,
path: Some(path.to_path_buf()),
chain: ConfigChainSnapshot::around_load(
&before,
ConfigChainSnapshot::read(Some(path)),
),
})
.map_err(|err| explicit_load_error(path, &err));
}
let before = ConfigChainSnapshot::read(find_config_file(root).as_deref());
match FallowConfig::find_and_load_with_options(root, load_options) {
Ok(Some((config, path))) => Ok(ConfigFile {
config,
chain: ConfigChainSnapshot::around_load(
&before,
ConfigChainSnapshot::read(Some(&path)),
),
path: Some(path),
}),
Ok(None) => Ok(ConfigFile {
config: FallowConfig::default(),
path: None,
chain: ConfigChainSnapshot::around_load(&before, ConfigChainSnapshot::default()),
}),
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(any(test, feature = "testing"))]
pub mod load_count {
use std::path::{Path, PathBuf};
use std::sync::{Mutex, PoisonError};
use rustc_hash::FxHashMap;
static LOADS: Mutex<Option<FxHashMap<PathBuf, usize>>> = Mutex::new(None);
pub(super) fn record(root: &Path) {
let mut loads = LOADS.lock().unwrap_or_else(PoisonError::into_inner);
*loads
.get_or_insert_with(FxHashMap::default)
.entry(root.to_path_buf())
.or_default() += 1;
}
#[must_use]
pub fn config_loads(root: &Path) -> usize {
LOADS
.lock()
.unwrap_or_else(PoisonError::into_inner)
.as_ref()
.and_then(|loads| loads.get(root).copied())
.unwrap_or(0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn each_load_counts_for_its_root() {
let dir = tempfile::tempdir().expect("tempdir");
std::fs::create_dir(dir.path().join(".git")).expect("git marker");
assert_eq!(load_count::config_loads(dir.path()), 0);
for _ in 0..2 {
load_config_file(dir.path(), None, ConfigFileOptions::default()).expect("loads");
}
assert_eq!(load_count::config_loads(dir.path()), 2);
}
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 file_options_take_the_extends_timeout_of_the_snapshot() {
let env = EnvSnapshot::from_pairs(&[("FALLOW_EXTENDS_TIMEOUT_SECS", "9")], None);
assert_eq!(
ConfigFileOptions::new(true, &env).extends_timeout,
Duration::from_secs(9)
);
assert_eq!(
ConfigFileOptions::default().extends_timeout,
Duration::from_secs(5)
);
}
#[test]
fn remote_extends_is_fetched_with_the_opt_in() {
let (_dir, root) = project(UNREACHABLE_REMOTE_EXTENDS);
let options = ConfigFileOptions::new(true, &EnvSnapshot::none());
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 the_chain_snapshot_sees_an_edit_of_each_local_file() {
let (_dir, root) = project(r#"{ "extends": "./base.json" }"#);
let base = root.join("base.json");
std::fs::write(&base, r#"{ "entry": ["a.ts"] }"#).expect("base");
let config = root.join(".fallowrc.json");
for config_path in [None, Some(config.as_path())] {
let file =
load_config_file(&root, config_path, ConfigFileOptions::default()).expect("loads");
assert!(!file.chain.changed());
std::fs::write(&base, r#"{ "entry": ["b.ts"] }"#).expect("same-size edit");
assert!(file.chain.changed());
std::fs::write(&base, r#"{ "entry": ["a.ts"] }"#).expect("restore");
}
}
#[test]
fn a_chain_that_differs_around_the_parse_counts_as_changed() {
let (_dir, root) = project("{}");
let config = root.join(".fallowrc.json");
let before = ConfigChainSnapshot::read(Some(&config));
std::fs::write(&config, r#"{ "entry": [] }"#).expect("edit during the parse");
let after = ConfigChainSnapshot::read(Some(&config));
assert!(ConfigChainSnapshot::around_load(&before, after.clone()).changed());
assert!(!ConfigChainSnapshot::around_load(&after.clone(), after).changed());
assert!(
ConfigChainSnapshot::around_load(&before, ConfigChainSnapshot::default()).changed(),
"a discovered file that is gone at the parse counts as changed"
);
}
#[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()
);
}
}