use std::{
collections::BTreeMap,
fs,
path::{Path, PathBuf},
};
use anyhow::{Context as _, Result, bail};
use serde::Deserialize;
pub const CONFIG_FILE_NAME: &str = ".sephera.toml";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DeprecatedKey {
pub section: &'static str,
pub old: &'static str,
pub current: &'static str,
}
pub const DEPRECATED_KEYS: &[DeprecatedKey] = &[];
fn known_sections() -> BTreeMap<&'static str, &'static [&'static str]> {
let mut sections: BTreeMap<&'static str, &'static [&'static str]> =
BTreeMap::new();
sections.insert("project", PROJECT_KEYS);
sections.insert("loc", LOC_KEYS);
sections.insert("symbols", SYMBOLS_KEYS);
sections.insert("graph", GRAPH_KEYS);
sections.insert("impact", IMPACT_KEYS);
sections.insert("context", CONTEXT_KEYS);
sections.insert("watch", WATCH_KEYS);
sections
}
const PROJECT_KEYS: &[&str] = &[
"ignore",
"no_gitignore",
"progress",
"format",
"output",
"path",
"url",
"ref",
];
const LOC_KEYS: &[&str] = &["format", "output"];
const SYMBOLS_KEYS: &[&str] = &["format", "output", "detail", "by_file"];
const GRAPH_KEYS: &[&str] = &[
"format",
"output",
"depth",
"focus",
"exclude_types",
"fail_on_cycles",
"fail_on_unresolved",
"what_depends_on",
"diff",
];
const IMPACT_KEYS: &[&str] = &[
"format",
"output",
"depth",
"focus",
"exclude_types",
"fail_on",
];
const CONTEXT_KEYS: &[&str] = &[
"ignore", "focus", "diff", "budget", "compress", "format", "output",
];
const WATCH_KEYS: &[&str] = &["on", "once"];
#[derive(Debug, Clone)]
pub struct LoadedConfig {
pub source_path: PathBuf,
pub parsed: SepheraToml,
}
impl LoadedConfig {
#[must_use]
pub const fn section_for(&self, command: ConfigCommand) -> Section<'_> {
match command {
ConfigCommand::Loc => Section::Command(&self.parsed.loc.command),
ConfigCommand::Symbols => {
Section::Command(&self.parsed.symbols.command)
}
ConfigCommand::Graph => {
Section::Command(&self.parsed.graph.command)
}
ConfigCommand::Impact => {
Section::Command(&self.parsed.impact.command)
}
ConfigCommand::Context => {
Section::Context(&self.parsed.context.command)
}
ConfigCommand::Watch => {
Section::Command(&self.parsed.watch.command)
}
}
}
#[must_use]
pub fn profile(&self, name: &str) -> Option<&ProfileToml> {
self.parsed.profiles.get(name)
}
#[must_use]
pub fn alias(&self, name: &str) -> Option<&AliasToml> {
self.parsed.aliases.get(name)
}
#[must_use]
pub fn alias_names(&self) -> Vec<&str> {
self.parsed.aliases.keys().map(String::as_str).collect()
}
#[must_use]
pub fn profile_names(&self) -> Vec<&str> {
self.parsed.profiles.keys().map(String::as_str).collect()
}
}
#[derive(Debug, Clone, Copy)]
pub enum Section<'config> {
Command(&'config CommandToml),
Context(&'config ContextCommandToml),
}
impl Section<'_> {
#[must_use]
pub fn as_command(&self) -> CommandToml {
match self {
Self::Command(command) => (*command).clone(),
Self::Context(context) => context.as_command(),
}
}
#[must_use]
pub const fn budget(&self) -> Option<&TokenBudgetValue> {
match self {
Self::Command(_) => None,
Self::Context(context) => context.budget.as_ref(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigCommand {
Loc,
Symbols,
Graph,
Impact,
Context,
Watch,
}
impl ConfigCommand {
pub const ALL: [Self; 6] = [
Self::Loc,
Self::Symbols,
Self::Graph,
Self::Impact,
Self::Context,
Self::Watch,
];
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Loc => "loc",
Self::Symbols => "symbols",
Self::Graph => "graph",
Self::Impact => "impact",
Self::Context => "context",
Self::Watch => "watch",
}
}
#[must_use]
pub const fn keys(self) -> &'static [&'static str] {
match self {
Self::Loc => LOC_KEYS,
Self::Symbols => SYMBOLS_KEYS,
Self::Graph => GRAPH_KEYS,
Self::Impact => IMPACT_KEYS,
Self::Context => CONTEXT_KEYS,
Self::Watch => WATCH_KEYS,
}
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
Self::ALL.into_iter().find(|command| command.name() == name)
}
}
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ProjectToml {
#[serde(default)]
pub ignore: Vec<String>,
#[serde(default)]
pub no_gitignore: bool,
pub progress: Option<String>,
pub format: Option<String>,
pub output: Option<PathBuf>,
pub path: Option<String>,
pub url: Option<String>,
#[serde(rename = "ref")]
pub git_ref: Option<String>,
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct ProjectSource {
pub path: Option<String>,
pub url: Option<String>,
pub git_ref: Option<String>,
}
impl ProjectToml {
#[must_use]
pub fn source(&self) -> ProjectSource {
ProjectSource {
path: self.path.clone(),
url: self.url.clone(),
git_ref: self.git_ref.clone(),
}
}
}
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct LocToml {
#[serde(flatten)]
pub command: CommandToml,
}
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct SymbolsToml {
#[serde(flatten)]
pub command: CommandToml,
}
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct GraphToml {
#[serde(flatten)]
pub command: CommandToml,
}
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ImpactToml {
#[serde(flatten)]
pub command: CommandToml,
}
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ContextToml {
#[serde(flatten)]
pub command: ContextCommandToml,
}
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct WatchToml {
#[serde(flatten)]
pub command: CommandToml,
}
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct CommandToml {
pub format: Option<String>,
pub output: Option<PathBuf>,
pub depth: Option<u32>,
pub focus: Option<Vec<PathBuf>>,
pub exclude_types: Option<bool>,
pub fail_on_cycles: Option<bool>,
pub fail_on_unresolved: Option<bool>,
pub what_depends_on: Option<String>,
pub diff: Option<String>,
pub fail_on: Option<u64>,
pub detail: Option<bool>,
pub by_file: Option<bool>,
pub on: Option<Vec<String>>,
pub once: Option<bool>,
}
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ContextCommandToml {
#[serde(default)]
pub ignore: Vec<String>,
#[serde(default)]
pub focus: Vec<PathBuf>,
pub diff: Option<String>,
pub budget: Option<TokenBudgetValue>,
pub compress: Option<String>,
pub format: Option<String>,
pub output: Option<PathBuf>,
}
impl ContextCommandToml {
#[must_use]
pub fn as_command(&self) -> CommandToml {
CommandToml {
format: self.format.clone(),
output: self.output.clone(),
diff: self.diff.clone(),
focus: (!self.focus.is_empty()).then(|| self.focus.clone()),
..CommandToml::default()
}
}
}
impl CommandToml {
#[must_use]
pub fn to_args(&self, config_directory: &Path) -> Vec<String> {
let mut args: Vec<String> = Vec::new();
let mut value = |flag: &str, value: Option<String>| {
if let Some(value) = value {
args.push(format!("--{flag}"));
args.push(value);
}
};
value("format", self.format.clone());
value(
"output",
self.output
.as_ref()
.map(|path| config_path(path, config_directory)),
);
value("depth", self.depth.map(|depth| depth.to_string()));
value("what-depends-on", self.what_depends_on.clone());
value("diff", self.diff.clone());
value("fail-on", self.fail_on.map(|limit| limit.to_string()));
for path in self.focus.iter().flatten() {
value("focus", Some(config_path(path, config_directory)));
}
for event in self.on.iter().flatten() {
value("on", Some(event.clone()));
}
for (flag, set) in [
("exclude-types", self.exclude_types),
("fail-on-cycles", self.fail_on_cycles),
("fail-on-unresolved", self.fail_on_unresolved),
("detail", self.detail),
("by-file", self.by_file),
("once", self.once),
] {
if set == Some(true) {
args.push(format!("--{flag}"));
}
}
args
}
}
impl ProjectToml {
#[must_use]
pub fn to_args(&self, config_directory: &Path) -> Vec<String> {
let mut args: Vec<String> = Vec::new();
let mut value = |flag: &str, value: Option<String>| {
if let Some(value) = value {
args.push(format!("--{flag}"));
args.push(value);
}
};
for pattern in &self.ignore {
value("ignore", Some(pattern.clone()));
}
value(
"path",
self.path
.as_ref()
.map(|path| config_path(Path::new(path), config_directory)),
);
value("url", self.url.clone());
value("ref", self.git_ref.clone());
value("progress", self.progress.clone());
value("format", self.format.clone());
value(
"output",
self.output
.as_ref()
.map(|path| config_path(path, config_directory)),
);
if self.no_gitignore {
args.push("--no-gitignore".to_owned());
}
args
}
}
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ProfileToml {
#[serde(default)]
pub project: ProjectToml,
#[serde(default)]
pub loc: LocToml,
#[serde(default)]
pub symbols: SymbolsToml,
#[serde(default)]
pub graph: GraphToml,
#[serde(default)]
pub impact: ImpactToml,
#[serde(default)]
pub context: ContextToml,
#[serde(default)]
pub watch: WatchToml,
}
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct AliasToml {
pub command: String,
pub profile: Option<String>,
#[serde(flatten)]
pub settings: CommandToml,
}
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum TokenBudgetValue {
Integer(u64),
String(String),
}
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct SepheraToml {
#[serde(default)]
pub project: ProjectToml,
#[serde(default)]
pub loc: LocToml,
#[serde(default)]
pub symbols: SymbolsToml,
#[serde(default)]
pub graph: GraphToml,
#[serde(default)]
pub impact: ImpactToml,
#[serde(default)]
pub context: ContextToml,
#[serde(default)]
pub watch: WatchToml,
#[serde(default)]
pub profiles: BTreeMap<String, ProfileToml>,
#[serde(default)]
pub aliases: BTreeMap<String, AliasToml>,
}
pub fn load_config_file(config_path: &Path) -> Result<LoadedConfig> {
let raw = fs::read_to_string(config_path).with_context(|| {
format!("failed to read config file `{}`", config_path.display())
})?;
let mut table: toml::Table = toml::from_str(&raw).with_context(|| {
format!("failed to parse config file `{}`", config_path.display())
})?;
rewrite_deprecated_keys(config_path, &mut table);
reject_unknown_keys(config_path, &table)?;
let parsed = toml::Value::Table(table)
.try_into::<SepheraToml>()
.with_context(|| {
format!("failed to read config file `{}`", config_path.display())
})?;
validate_aliases(config_path, &parsed)?;
Ok(LoadedConfig {
source_path: config_path.to_path_buf(),
parsed,
})
}
fn rewrite_deprecated_keys(config_path: &Path, table: &mut toml::Table) {
rewrite_deprecated_with(table, DEPRECATED_KEYS);
let _ = config_path;
}
fn rewrite_deprecated_with(
table: &mut toml::Table,
deprecated_keys: &[DeprecatedKey],
) {
for deprecated in deprecated_keys {
let Some(section) = section_mut(table, deprecated.section) else {
continue;
};
let Some(value) = section.remove(deprecated.old) else {
continue;
};
eprintln!(
"warning: `{}` in `{}` is deprecated; use `{}` instead",
deprecated.old, deprecated.section, deprecated.current
);
section
.entry(deprecated.current.to_owned())
.or_insert(value);
}
}
fn section_mut<'table>(
table: &'table mut toml::Table,
section: &str,
) -> Option<&'table mut toml::Table> {
table.get_mut(section)?.as_table_mut()
}
fn reject_unknown_keys(config_path: &Path, table: &toml::Table) -> Result<()> {
let sections = known_sections();
for (name, value) in table {
if matches!(name.as_str(), "profiles" | "aliases") {
let Some(section) = value.as_table() else {
bail!(
"`{name}` in `{}` must be a table",
config_path.display()
);
};
reject_unknown_named_keys(config_path, name, section)?;
continue;
}
let Some(accepted) = sections.get(name.as_str()) else {
bail!(
"unknown section `{name}` in `{}`; expected one of: {}",
config_path.display(),
join_names(§ions.keys().copied().collect::<Vec<_>>(),),
);
};
let Some(section) = value.as_table() else {
bail!("`{name}` in `{}` must be a table", config_path.display());
};
for key in section.keys() {
if accepted.contains(&key.as_str()) {
continue;
}
bail!(
"unknown key `{key}` in `{name}` of `{}`; {}",
config_path.display(),
did_you_mean(key, accepted),
);
}
}
Ok(())
}
fn reject_unknown_named_keys(
config_path: &Path,
container: &str,
section: &toml::Table,
) -> Result<()> {
for (name, value) in section {
let Some(inner) = value.as_table() else {
continue;
};
let path = format!("{container}.{name}");
if container == "profiles" {
reject_profile_keys(config_path, &path, inner)?;
continue;
}
reject_alias_keys(config_path, &path, inner)?;
}
Ok(())
}
fn reject_profile_keys(
config_path: &Path,
path: &str,
inner: &toml::Table,
) -> Result<()> {
for (key, value) in inner {
let Some(command_table) = value.as_table() else {
bail!(
"`{path}.{key}` in `{}` must be a table",
config_path.display()
);
};
let command_section = format!("{path}.{key}");
if key == "project" {
reject_keys_in(
config_path,
&command_section,
command_table,
PROJECT_KEYS,
)?;
continue;
}
let Some(command) = ConfigCommand::from_name(key) else {
bail!(
"unknown section `{command_section}` in `{}`; expected one of: project, {}",
config_path.display(),
join_names(
&ConfigCommand::ALL
.iter()
.map(|command| command.name())
.collect::<Vec<_>>()
),
);
};
reject_keys_in(
config_path,
&command_section,
command_table,
command.keys(),
)?;
}
Ok(())
}
fn reject_alias_keys(
config_path: &Path,
path: &str,
inner: &toml::Table,
) -> Result<()> {
let Some(command) = inner.get("command").and_then(toml::Value::as_str)
else {
bail!(
"alias `{path}` in `{}` must name a `command`",
config_path.display()
);
};
let Some(resolved) = ConfigCommand::from_name(command) else {
return Ok(());
};
for key in inner.keys() {
if key == "command" || key == "profile" {
continue;
}
if !resolved.keys().contains(&key.as_str()) {
bail!(
"unknown key `{key}` in `{path}` of `{}`; the alias runs `{}`, and {}",
config_path.display(),
resolved.name(),
did_you_mean(key, resolved.keys()),
);
}
}
Ok(())
}
fn reject_keys_in(
config_path: &Path,
path: &str,
section: &toml::Table,
accepted: &[&str],
) -> Result<()> {
for key in section.keys() {
if accepted.contains(&key.as_str()) {
continue;
}
bail!(
"unknown key `{key}` in `{path}` of `{}`; {}",
config_path.display(),
did_you_mean(key, accepted),
);
}
Ok(())
}
fn did_you_mean(key: &str, accepted: &[&str]) -> String {
let lowered = key.to_lowercase();
if let Some(candidate) = accepted
.iter()
.find(|accepted| accepted.eq_ignore_ascii_case(&lowered))
{
return format!("did you mean `{candidate}`?");
}
if let Some(candidate) = accepted
.iter()
.find(|accepted| edit_distance_one(&lowered, &accepted.to_lowercase()))
{
return format!("did you mean `{candidate}`?");
}
format!("accepted keys are: {}", join_names(accepted))
}
fn join_names(names: &[&str]) -> String {
names.join(", ")
}
fn edit_distance_one(left: &str, right: &str) -> bool {
if left == right {
return false;
}
bounded_edit_distance(left, right, 1) <= 1
}
fn bounded_edit_distance(left: &str, right: &str, limit: usize) -> usize {
let left: Vec<char> = left.chars().collect();
let right: Vec<char> = right.chars().collect();
let unreachable = limit + 1;
if left.len().abs_diff(right.len()) > limit {
return unreachable;
}
let mut rows: [Vec<usize>; 3] =
std::array::from_fn(|_| vec![unreachable; right.len() + 1]);
for (column, cell) in rows[1].iter_mut().enumerate() {
*cell = column.min(unreachable);
}
for i in 1..=left.len() {
rows[2][0] = i.min(unreachable);
for j in 1..=right.len() {
let substitution =
rows[1][j - 1] + usize::from(left[i - 1] != right[j - 1]);
let insertion = rows[2][j - 1] + 1;
let deletion = rows[1][j] + 1;
let mut cost = substitution.min(insertion).min(deletion);
if i > 1
&& j > 1
&& left[i - 1] == right[j - 2]
&& left[i - 2] == right[j - 1]
{
cost = cost.min(rows[0][j - 2] + 1);
}
rows[2][j] = cost.min(unreachable);
}
rows.swap(0, 1);
rows.swap(1, 2);
}
rows[1][right.len()]
}
fn validate_aliases(config_path: &Path, config: &SepheraToml) -> Result<()> {
for (name, alias) in &config.aliases {
if ConfigCommand::from_name(&alias.command).is_none() {
bail!(
"alias `{name}` in `{}` names command `{}`, which does not exist; expected one of: {}",
config_path.display(),
alias.command,
join_names(
&ConfigCommand::ALL
.iter()
.map(|command| command.name())
.collect::<Vec<_>>(),
)
);
}
if let Some(profile) = &alias.profile
&& !config.profiles.contains_key(profile)
{
bail!(
"alias `{name}` in `{}` selects profile `{profile}`, which is not defined",
config_path.display()
);
}
}
Ok(())
}
fn config_path(value: &Path, config_directory: &Path) -> String {
if value.is_absolute() {
return value.display().to_string();
}
config_directory.join(value).display().to_string()
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write as _;
fn load(contents: &str) -> Result<LoadedConfig> {
use std::sync::atomic::{AtomicU64, Ordering};
static COUNTER: AtomicU64 = AtomicU64::new(0);
let mut path = std::env::temp_dir();
path.push(format!(
"sephera-settings-{}-{}.toml",
std::process::id(),
COUNTER.fetch_add(1, Ordering::Relaxed)
));
let mut file =
fs::File::create(&path).expect("the temp file is writable");
file.write_all(contents.as_bytes())
.expect("the temp file is writable");
drop(file);
let loaded = load_config_file(&path);
let _ = fs::remove_file(&path);
loaded
}
fn error_text(contents: &str) -> String {
load(contents)
.expect_err("the config should have been rejected")
.to_string()
}
#[test]
fn a_full_file_reads_every_section() {
let config = load(
r#"
[project]
ignore = ["vendor"]
no_gitignore = true
progress = "never"
format = "markdown"
output = "reports/all.md"
path = "."
url = "https://github.com/Reim-developer/Sephera"
ref = "master"
[loc]
format = "json"
[symbols]
by_file = true
detail = true
[graph]
depth = 2
exclude_types = true
what_depends_on = "crates/sephera_core/src/lib.rs"
[impact]
fail_on = 40
[watch]
once = true
on = ["save"]
[profiles.review.graph]
format = "markdown"
[aliases.audit]
command = "graph"
what_depends_on = "src/main.rs"
"#,
)
.expect("a complete config is valid");
assert!(config.parsed.project.no_gitignore);
assert_eq!(config.parsed.project.progress.as_deref(), Some("never"));
assert_eq!(config.parsed.project.git_ref.as_deref(), Some("master"));
assert_eq!(config.parsed.loc.command.format.as_deref(), Some("json"));
assert_eq!(config.parsed.symbols.command.by_file, Some(true));
assert_eq!(config.parsed.graph.command.depth, Some(2));
assert_eq!(config.parsed.impact.command.fail_on, Some(40));
assert_eq!(config.parsed.watch.command.once, Some(true));
assert!(config.parsed.profiles.contains_key("review"));
assert_eq!(config.parsed.aliases["audit"].command, "graph");
}
#[test]
fn a_boolean_key_becomes_a_bare_flag_and_never_a_pair() {
let config =
load("[symbols]\ndetail = true\nby_file = true\n").unwrap();
let args = config.parsed.symbols.command.to_args(Path::new("."));
assert!(args.contains(&"--detail".to_owned()), "{args:?}");
assert!(args.contains(&"--by-file".to_owned()), "{args:?}");
let mut expect_value = false;
for argument in &args {
if expect_value {
assert!(
!argument.starts_with('-'),
"`{argument:?}` is a value, not a flag: {args:?}"
);
expect_value = false;
} else if argument.starts_with('-') {
expect_value =
!matches!(argument.as_str(), "--detail" | "--by-file");
}
}
}
#[test]
fn a_typo_in_a_key_is_an_error_naming_the_real_one() {
let message = error_text("[project]\nignroe = [\"vendor\"]\n");
assert!(
message.contains("unknown key `ignroe`"),
"the key must be named: {message}"
);
assert!(
message.contains("did you mean `ignore`"),
"and the correction offered: {message}"
);
}
#[test]
fn a_typo_in_a_section_is_an_error_naming_the_sections() {
let message = error_text("[lok]\nformat = \"json\"\n");
assert!(
message.contains("unknown section `lok`"),
"the section must be named: {message}"
);
assert!(
message.contains("loc"),
"and the real sections listed: {message}"
);
}
#[test]
fn a_typo_in_a_key_names_the_command_that_owns_it() {
let message = error_text("[graph]\ndepht = 2\n");
assert!(
message.contains("unknown key `depht` in `graph`"),
"{message}"
);
assert!(message.contains("did you mean `depth`"), "{message}");
}
#[test]
fn a_key_from_another_command_is_not_silently_accepted() {
let message = error_text("[loc]\nbudget = \"32k\"\n");
assert!(
message.contains("unknown key `budget` in `loc`"),
"{message}"
);
}
#[test]
fn an_alias_may_only_carry_its_own_commands_keys() {
let message = error_text(
"[aliases.audit]\ncommand = \"loc\"\nfocus = [\"crates\"]\n",
);
assert!(
message.contains("alias runs `loc`"),
"the command must be named: {message}"
);
}
#[test]
fn an_alias_naming_a_command_that_does_not_exist_is_an_error() {
let message = error_text("[aliases.audit]\ncommand = \"grpah\"\n");
assert!(message.contains("which does not exist"), "{message}");
}
#[test]
fn an_alias_must_say_which_command_it_runs() {
let message = error_text("[aliases.audit]\nformat = \"json\"\n");
assert!(message.contains("must name a `command`"), "{message}");
}
#[test]
fn an_alias_selecting_an_undefined_profile_is_an_error() {
let message = error_text(
"[aliases.audit]\ncommand = \"graph\"\nprofile = \"nope\"\n",
);
assert!(
message.contains("selects profile `nope`, which is not defined"),
"{message}"
);
}
#[test]
fn an_unknown_section_inside_a_profile_is_an_error() {
let message = error_text("[profiles.review]\nlocc = {}\n");
assert!(
message.contains("unknown section `profiles.review.locc`"),
"{message}"
);
}
#[test]
fn an_unknown_key_inside_a_profile_is_still_checked() {
let message = error_text("[profiles.review.graph]\ndepht = 2\n");
assert!(
message.contains("unknown key `depht`"),
"a profile is not a loophole: {message}"
);
}
#[test]
fn a_value_of_the_wrong_shape_is_reported_by_its_key() {
let message = error_text("[graph]\ndepth = \"two\"\n");
assert!(!message.is_empty(), "a wrong type must be an error");
}
#[test]
fn one_edit_typos_are_corrected() {
assert!(edit_distance_one("ignor", "ignore"));
assert!(edit_distance_one("ignore", "ignoree"));
assert!(edit_distance_one("ignore", "ignroe"));
assert!(!edit_distance_one("ignore", "ignore"));
assert!(!edit_distance_one("depth", "focus"));
}
#[test]
fn a_deprecated_key_warns_and_is_still_read() {
let mut table: toml::Table =
toml::from_str("[project]\nignore = [\"vendor\"]\n")
.expect("the fixture is valid TOML");
let deprecated = DeprecatedKey {
section: "project",
old: "ignores",
current: "ignore",
};
let mut registry = vec![deprecated];
rewrite_deprecated_with(&mut table, ®istry);
let project = table
.get("project")
.and_then(toml::Value::as_table)
.expect("the section survives");
assert_eq!(
project.get("ignore").and_then(toml::Value::as_str),
None,
"the value is a list, so this checks the key moved"
);
assert!(
project.contains_key("ignore"),
"the current key must hold the value: {project:?}"
);
assert!(
!project.contains_key("ignores"),
"the old spelling must be gone: {project:?}"
);
registry.clear();
assert!(registry.is_empty(), "the scratch registry is drained");
}
#[test]
fn aliases_and_profiles_can_be_listed_for_help() {
let config = load(
"[profiles.review.graph]\nformat = \"markdown\"\n\n[profiles.ci.graph]\nformat = \"json\"\n\n[aliases.audit]\ncommand = \"graph\"\n\n[aliases.deps]\ncommand = \"graph\"\n",
)
.expect("a complete config is valid");
assert_eq!(config.profile_names(), vec!["ci", "review"]);
assert_eq!(config.alias_names(), vec!["audit", "deps"]);
}
}