pub mod clap_based_cli;
pub mod common_args;
pub mod common_subcommands;
pub mod error;
pub mod logging;
pub mod prelude {
pub use crate::clap_based_cli::{
ClapBasedCli, CliPort, GlobalOptions, ParsedCli, ParsedInvocation,
};
pub use crate::common_args::{ConfigArg, OutputFormat, Verbosity};
pub use crate::common_subcommands::{InitCmd, ValidateCmd, VersionCmd};
pub use crate::error::{CliError, CliResult};
pub use crate::logging::setup_tracing;
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use crate::prelude::*;
#[test]
fn prelude_smoke_test() {
let _cfg: ConfigArg = ConfigArg {
config: Some(PathBuf::from("/tmp/c.yaml")),
};
let _v: Verbosity = Verbosity {
verbose: 1,
quiet: false,
};
let _o: OutputFormat = OutputFormat::Json;
let _i: InitCmd = InitCmd {
path: PathBuf::from("."),
force: false,
template: "default".to_string(),
};
let _va: ValidateCmd = ValidateCmd {
path: PathBuf::from("/x"),
strict: false,
};
let _ve: VersionCmd = VersionCmd {};
let _e: CliError = CliError::Config("cfg".into());
let _r: CliResult<u8> = Ok(1);
let port: Box<dyn CliPort> = Box::new(ClapBasedCli::new("p", "a", "0.0.0"));
let _ = port.name();
}
#[test]
fn fr001_verbosity_to_filter_quiet_overrides_verbose() {
use tracing_subscriber::filter::LevelFilter;
let q = Verbosity {
verbose: 2,
quiet: true,
};
assert_eq!(q.to_filter(), LevelFilter::ERROR);
}
#[test]
fn fr002_common_commands_enum_parses_three_variants() {
use clap::Parser;
use crate::common_subcommands::CommonCommands;
#[derive(Debug, Parser)]
struct W {
#[command(subcommand)]
cmd: CommonCommands,
}
let w = W::parse_from(["t", "init", "/tmp/p", "-f", "-t", "rust"]);
match w.cmd {
CommonCommands::Init(c) => {
assert_eq!(c.path, PathBuf::from("/tmp/p"));
assert!(c.force);
assert_eq!(c.template, "rust");
}
_ => panic!("expected Init"),
}
let w = W::parse_from(["t", "validate", "/x", "--strict"]);
match w.cmd {
CommonCommands::Validate(c) => {
assert_eq!(c.path, PathBuf::from("/x"));
assert!(c.strict);
}
_ => panic!("expected Validate"),
}
let w = W::parse_from(["t", "version"]);
assert!(matches!(w.cmd, CommonCommands::Version(_)));
}
#[test]
fn fr003_question_mark_propagates_io_error_via_from() {
fn read_missing() -> CliResult<String> {
let s = std::fs::read_to_string("/this/path/does/not/exist/q9w8e7")?;
Ok(s)
}
let err = read_missing().unwrap_err();
assert!(matches!(err, CliError::Io(_)));
}
#[test]
fn fr004_setup_tracing_idempotent() {
use tracing_subscriber::filter::LevelFilter;
setup_tracing(LevelFilter::INFO);
setup_tracing(LevelFilter::DEBUG);
}
#[test]
fn fr005_cli_port_parse_init_round_trips_globals() {
let cli = ClapBasedCli::new("mycli", "demo", "1.0.0");
let inv = cli
.parse(&["-vv", "-c", "/etc/c.yaml", "--output", "json", "version"])
.expect("parse should succeed");
assert_eq!(inv.globals.verbose, 2);
assert_eq!(inv.globals.config, Some(PathBuf::from("/etc/c.yaml")));
assert!(!inv.globals.quiet);
assert_eq!(inv.globals.output, OutputFormat::Json);
assert_eq!(inv.command, ParsedCli::Version);
}
}