xianyun_cli/buildinfo.rs
1//! Build-time version information, for `caproni version`'s output.
2//!
3//! Ported from `internal/buildinfo/buildinfo.go`. Go injects `version`,
4//! `commit`, and `date` via `goreleaser`'s `-ldflags`, defaulting to
5//! `"dev"`/`"unknown"`/`"unknown"` in a plain `go build`. This port uses
6//! `env!("CARGO_PKG_VERSION")` for `version()` (always accurate; Cargo
7//! always knows it, unlike Go's zero-value default), and `option_env!` for
8//! `commit()`/`date()`, which a release build sets via `XIANYUN_BUILD_COMMIT`/
9//! `XIANYUN_BUILD_DATE` environment variables at compile time, falling back
10//! to the same `"unknown"` Go defaults to otherwise.
11
12/// Returns the build version.
13///
14/// Ported from `buildinfo.Version`.
15#[must_use]
16pub fn version() -> &'static str {
17 env!("CARGO_PKG_VERSION")
18}
19
20/// Returns the git commit hash.
21///
22/// Ported from `buildinfo.Commit`.
23#[must_use]
24pub fn commit() -> &'static str {
25 option_env!("XIANYUN_BUILD_COMMIT").unwrap_or("unknown")
26}
27
28/// Returns the build date.
29///
30/// Ported from `buildinfo.Date`.
31#[must_use]
32pub fn date() -> &'static str {
33 option_env!("XIANYUN_BUILD_DATE").unwrap_or("unknown")
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39
40 #[test]
41 fn version_matches_cargo_package_version() {
42 assert_eq!(version(), env!("CARGO_PKG_VERSION"));
43 assert!(!version().is_empty());
44 }
45
46 #[test]
47 fn commit_defaults_to_unknown_without_ldflags_equivalent() {
48 // No XIANYUN_BUILD_COMMIT is set for a plain `cargo build`/`cargo
49 // test`, matching Go's zero-value default.
50 assert_eq!(commit(), "unknown");
51 }
52
53 #[test]
54 fn date_defaults_to_unknown_without_ldflags_equivalent() {
55 assert_eq!(date(), "unknown");
56 }
57}