aKode fase 5: rinomina interna (crate akode-*, akode-rs/, akode-cli/, binario akode, AKODE_*, ~/.akode, snapshot)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
109
akode-rs/config/src/test_support.rs
Normal file
109
akode-rs/config/src/test_support.rs
Normal file
@@ -0,0 +1,109 @@
|
||||
//! Test-only helpers exposed for cross-crate integration tests.
|
||||
//!
|
||||
//! Production code should not depend on this module.
|
||||
|
||||
use crate::LoaderOverrides;
|
||||
use crate::merge_toml_values;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use toml::Value as TomlValue;
|
||||
|
||||
/// Directory, relative to the caller-provided root, that holds fixture files.
|
||||
const FIXTURE_DIR: &str = "managed-config-fixture";
|
||||
|
||||
/// Local managed configuration used by tests: requirements are written to a
|
||||
/// system `requirements.toml` and configuration to a system `config.toml`,
|
||||
/// which the loader picks up through [`LoaderOverrides`].
|
||||
///
|
||||
/// Fragments are listed from highest to lowest precedence; when more than one
|
||||
/// fragment is added they are merged into a single file so that values from
|
||||
/// earlier fragments win.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct ManagedConfigFixture {
|
||||
requirements: Vec<String>,
|
||||
config: Vec<String>,
|
||||
}
|
||||
|
||||
impl ManagedConfigFixture {
|
||||
pub fn requirements(contents: impl Into<String>) -> Self {
|
||||
Self::default().with_requirements(contents)
|
||||
}
|
||||
|
||||
pub fn config(contents: impl Into<String>) -> Self {
|
||||
Self::default().with_config(contents)
|
||||
}
|
||||
|
||||
pub fn with_requirements(mut self, contents: impl Into<String>) -> Self {
|
||||
self.requirements.push(contents.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_config(mut self, contents: impl Into<String>) -> Self {
|
||||
self.config.push(contents.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Returns the managed requirements document, if any requirements were added.
|
||||
pub fn requirements_toml(&self) -> io::Result<Option<String>> {
|
||||
merge_fragments(&self.requirements)
|
||||
}
|
||||
|
||||
/// Returns the managed config document, if any config was added.
|
||||
pub fn config_toml(&self) -> io::Result<Option<String>> {
|
||||
merge_fragments(&self.config)
|
||||
}
|
||||
|
||||
/// Writes the fixture below `root` and returns `base` with the system
|
||||
/// requirements and system config paths pointing at the written files.
|
||||
pub fn loader_overrides(
|
||||
&self,
|
||||
root: &Path,
|
||||
base: LoaderOverrides,
|
||||
) -> io::Result<LoaderOverrides> {
|
||||
let mut overrides = base;
|
||||
if self.requirements.is_empty() && self.config.is_empty() {
|
||||
return Ok(overrides);
|
||||
}
|
||||
let dir = root.join(FIXTURE_DIR);
|
||||
std::fs::create_dir_all(&dir)?;
|
||||
if let Some(requirements) = self.requirements_toml()? {
|
||||
let path = dir.join("requirements.toml");
|
||||
std::fs::write(&path, requirements)?;
|
||||
overrides.system_requirements_path = Some(path);
|
||||
}
|
||||
if let Some(config) = self.config_toml()? {
|
||||
let path = dir.join("config.toml");
|
||||
std::fs::write(&path, config)?;
|
||||
overrides.system_config_path = Some(path);
|
||||
}
|
||||
Ok(overrides)
|
||||
}
|
||||
|
||||
/// Like [`Self::loader_overrides`], starting from overrides that ignore
|
||||
/// host-managed configuration.
|
||||
pub fn loader_overrides_for_tests(&self, root: &Path) -> io::Result<LoaderOverrides> {
|
||||
self.loader_overrides(root, LoaderOverrides::without_managed_config_for_tests())
|
||||
}
|
||||
}
|
||||
|
||||
fn merge_fragments(fragments: &[String]) -> io::Result<Option<String>> {
|
||||
match fragments {
|
||||
[] => Ok(None),
|
||||
[single] => Ok(Some(single.clone())),
|
||||
fragments => {
|
||||
let mut merged = TomlValue::Table(toml::map::Map::new());
|
||||
for fragment in fragments.iter().rev() {
|
||||
let value = toml::from_str::<TomlValue>(fragment)
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
|
||||
merge_toml_values(&mut merged, &value);
|
||||
}
|
||||
toml::to_string(&merged)
|
||||
.map(Some)
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "test_support_tests.rs"]
|
||||
mod tests;
|
||||
Reference in New Issue
Block a user