|
| 1 | +use std::io::{self, Write}; |
| 2 | + |
| 3 | +use crate::error::Result; |
| 4 | + |
| 5 | +/// Arguments for the version command. |
| 6 | +#[derive(clap::Args)] |
| 7 | +pub struct VersionArgs { |
| 8 | + #[arg( |
| 9 | + long = "verbose", |
| 10 | + help = "Includes detailed module version info and supported protocols." |
| 11 | + )] |
| 12 | + pub verbose: bool, |
| 13 | +} |
| 14 | + |
| 15 | +/// Runs the version command. |
| 16 | +pub fn run(args: VersionArgs) -> Result<()> { |
| 17 | + run_with_writer(args, &mut io::stdout()) |
| 18 | +} |
| 19 | + |
| 20 | +/// Runs the version command with a custom writer (used for testing). |
| 21 | +fn run_with_writer<W: Write>(args: VersionArgs, writer: &mut W) -> Result<()> { |
| 22 | + let (hash, timestamp) = charon_core::version::git_commit(); |
| 23 | + writeln!( |
| 24 | + writer, |
| 25 | + "{} [git_commit_hash={},git_commit_time={}]", |
| 26 | + *charon_core::version::VERSION, |
| 27 | + hash, |
| 28 | + timestamp |
| 29 | + )?; |
| 30 | + |
| 31 | + if !args.verbose { |
| 32 | + return Ok(()); |
| 33 | + } |
| 34 | + |
| 35 | + writeln!(writer, "Package: {}", env!("CARGO_PKG_NAME"))?; |
| 36 | + writeln!(writer, "Dependencies:")?; |
| 37 | + |
| 38 | + for dependency in charon_core::version::dependencies() { |
| 39 | + writeln!(writer, "\t{dependency}")?; |
| 40 | + } |
| 41 | + |
| 42 | + writeln!(writer, "Consensus protocols:")?; |
| 43 | + for protocol in charon_core::consensus::protocols::protocols() { |
| 44 | + writeln!(writer, "\t{}", protocol)?; |
| 45 | + } |
| 46 | + |
| 47 | + Ok(()) |
| 48 | +} |
| 49 | + |
| 50 | +#[cfg(test)] |
| 51 | +mod tests { |
| 52 | + use super::*; |
| 53 | + |
| 54 | + #[test] |
| 55 | + fn test_run_version_cmd_default() { |
| 56 | + let mut buf = Vec::new(); |
| 57 | + let args = VersionArgs { verbose: false }; |
| 58 | + |
| 59 | + let result = run_with_writer(args, &mut buf); |
| 60 | + assert!(result.is_ok()); |
| 61 | + |
| 62 | + let output = String::from_utf8(buf).expect("valid UTF-8 output"); |
| 63 | + |
| 64 | + // Check that output contains git info |
| 65 | + assert!( |
| 66 | + output.contains("git_commit_hash"), |
| 67 | + "Output should contain git_commit_hash" |
| 68 | + ); |
| 69 | + assert!( |
| 70 | + output.contains("git_commit_time"), |
| 71 | + "Output should contain git_commit_time" |
| 72 | + ); |
| 73 | + |
| 74 | + // Check that verbose-only content is NOT present |
| 75 | + assert!( |
| 76 | + !output.contains("Package:"), |
| 77 | + "Default output should not contain Package:" |
| 78 | + ); |
| 79 | + |
| 80 | + // Parse the version from output |
| 81 | + // Format: "v1.7.1 |
| 82 | + // [git_commit_hash=abc1234,git_commit_time=2024-01-01T00:00:00Z]\n" |
| 83 | + let parts: Vec<&str> = output.split_whitespace().collect(); |
| 84 | + assert_eq!( |
| 85 | + parts.len(), |
| 86 | + 2, |
| 87 | + "Default output should have exactly 2 space-separated parts" |
| 88 | + ); |
| 89 | + |
| 90 | + // Parse the version string |
| 91 | + let version_str = parts[0]; |
| 92 | + let parsed_version = |
| 93 | + charon_core::version::SemVer::parse(version_str).expect("valid semver"); |
| 94 | + assert_eq!( |
| 95 | + parsed_version, |
| 96 | + *charon_core::version::VERSION, |
| 97 | + "Parsed version should match VERSION constant" |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_run_version_cmd_verbose() { |
| 103 | + let mut buf = Vec::new(); |
| 104 | + let args = VersionArgs { verbose: true }; |
| 105 | + |
| 106 | + let result = run_with_writer(args, &mut buf); |
| 107 | + assert!(result.is_ok()); |
| 108 | + |
| 109 | + let output = String::from_utf8(buf).expect("valid UTF-8 output"); |
| 110 | + |
| 111 | + // Check that output contains git info |
| 112 | + assert!( |
| 113 | + output.contains("git_commit_hash"), |
| 114 | + "Output should contain git_commit_hash" |
| 115 | + ); |
| 116 | + assert!( |
| 117 | + output.contains("git_commit_time"), |
| 118 | + "Output should contain git_commit_time" |
| 119 | + ); |
| 120 | + |
| 121 | + // Check that verbose content is present |
| 122 | + assert!( |
| 123 | + output.contains("Package:"), |
| 124 | + "Verbose output should contain Package:" |
| 125 | + ); |
| 126 | + assert!( |
| 127 | + output.contains("Dependencies:"), |
| 128 | + "Verbose output should contain Dependencies:" |
| 129 | + ); |
| 130 | + assert!( |
| 131 | + output.contains("Consensus protocols:"), |
| 132 | + "Verbose output should contain Consensus protocols:" |
| 133 | + ); |
| 134 | + |
| 135 | + // Check that the first protocol is listed |
| 136 | + let protocols = charon_core::consensus::protocols::protocols(); |
| 137 | + assert!(!protocols.is_empty(), "Should have at least one protocol"); |
| 138 | + let first_protocol = protocols[0].to_string(); |
| 139 | + assert!( |
| 140 | + output.contains(&first_protocol), |
| 141 | + "Verbose output should contain the first protocol: {}", |
| 142 | + first_protocol |
| 143 | + ); |
| 144 | + } |
| 145 | +} |
0 commit comments