Base: codex-main originale (prima del fork aKode)
This commit is contained in:
197
codex-rs/build-info/src/build_info_tests.rs
Normal file
197
codex-rs/build-info/src/build_info_tests.rs
Normal file
@@ -0,0 +1,197 @@
|
||||
use std::fs;
|
||||
|
||||
use codex_install_context::InstallContext;
|
||||
use pretty_assertions::assert_eq;
|
||||
use semver::Version;
|
||||
use tempfile::tempdir;
|
||||
|
||||
use crate::BuildInfo;
|
||||
use crate::build_id;
|
||||
|
||||
const BUILD_COMMIT: &str = "0123456789abcdef0123456789abcdef01234567";
|
||||
|
||||
/// A packaged runtime takes its release identity from its package manifest.
|
||||
#[test]
|
||||
fn packaged_runtime_uses_manifest_version() {
|
||||
let package = tempdir().expect("create runtime package");
|
||||
let bin_dir = package.path().join("bin");
|
||||
fs::create_dir(&bin_dir).expect("create runtime binary directory");
|
||||
let executable = bin_dir.join("codex");
|
||||
fs::write(&executable, b"").expect("create runtime binary");
|
||||
fs::write(
|
||||
package.path().join("codex-package.json"),
|
||||
r#"{"version":"1.2.3-alpha.4"}"#,
|
||||
)
|
||||
.expect("create runtime package manifest");
|
||||
|
||||
let context = InstallContext::from_exe(
|
||||
cfg!(target_os = "macos"),
|
||||
Some(&executable),
|
||||
/*method_override*/ None,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
BuildInfo::resolve(&context, BUILD_COMMIT),
|
||||
BuildInfo {
|
||||
version: Version::parse("1.2.3-alpha.4").expect("valid release version"),
|
||||
build_commit: BUILD_COMMIT.to_string(),
|
||||
target: Some(env!("CODEX_BUILD_TARGET").to_string()),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Unpackaged builds expose their stamped commit and structured source version.
|
||||
#[test]
|
||||
fn unpackaged_runtime_uses_build_commit() {
|
||||
let context = InstallContext::from_exe(
|
||||
cfg!(target_os = "macos"),
|
||||
/*current_exe*/ None,
|
||||
/*method_override*/ None,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
BuildInfo::resolve(&context, BUILD_COMMIT),
|
||||
BuildInfo {
|
||||
version: Version::new(0, 0, 0),
|
||||
build_commit: BUILD_COMMIT.to_string(),
|
||||
target: Some(env!("CODEX_BUILD_TARGET").to_string()),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Older package layouts without release metadata retain their build identity.
|
||||
#[test]
|
||||
fn legacy_package_without_version_uses_build_commit() {
|
||||
let package = tempdir().expect("create runtime package");
|
||||
let bin_dir = package.path().join("bin");
|
||||
fs::create_dir(&bin_dir).expect("create runtime binary directory");
|
||||
let executable = bin_dir.join("codex");
|
||||
fs::write(&executable, b"").expect("create runtime binary");
|
||||
fs::write(package.path().join("codex-package.json"), "{}")
|
||||
.expect("create legacy runtime package manifest");
|
||||
|
||||
let context = InstallContext::from_exe(
|
||||
cfg!(target_os = "macos"),
|
||||
Some(&executable),
|
||||
/*method_override*/ None,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
BuildInfo::resolve(&context, BUILD_COMMIT),
|
||||
BuildInfo {
|
||||
version: Version::new(0, 0, 0),
|
||||
build_commit: BUILD_COMMIT.to_string(),
|
||||
target: Some(env!("CODEX_BUILD_TARGET").to_string()),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Invalid package versions cannot override the executable's stamped commit.
|
||||
#[test]
|
||||
fn invalid_package_version_uses_build_commit() {
|
||||
let package = tempdir().expect("create runtime package");
|
||||
let bin_dir = package.path().join("bin");
|
||||
fs::create_dir(&bin_dir).expect("create runtime binary directory");
|
||||
let executable = bin_dir.join("codex");
|
||||
fs::write(&executable, b"").expect("create runtime binary");
|
||||
fs::write(
|
||||
package.path().join("codex-package.json"),
|
||||
r#"{"version":"not-a-release-version"}"#,
|
||||
)
|
||||
.expect("create runtime package manifest");
|
||||
|
||||
let context = InstallContext::from_exe(
|
||||
cfg!(target_os = "macos"),
|
||||
Some(&executable),
|
||||
/*method_override*/ None,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
BuildInfo::resolve(&context, BUILD_COMMIT),
|
||||
BuildInfo {
|
||||
version: Version::new(0, 0, 0),
|
||||
build_commit: BUILD_COMMIT.to_string(),
|
||||
target: Some(env!("CODEX_BUILD_TARGET").to_string()),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Serializing build information preserves release version, commit, and target.
|
||||
#[test]
|
||||
fn build_info_serialization_preserves_build_provenance() {
|
||||
let build_info = BuildInfo {
|
||||
version: Version::parse("1.2.3-alpha.4").expect("valid release version"),
|
||||
build_commit: BUILD_COMMIT.to_string(),
|
||||
target: Some("x86_64-pc-windows-msvc".to_string()),
|
||||
};
|
||||
let serialized = serde_json::json!({
|
||||
"version": "1.2.3-alpha.4",
|
||||
"build_commit": BUILD_COMMIT,
|
||||
"target": "x86_64-pc-windows-msvc",
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
serde_json::to_value(&build_info).expect("serialize build information"),
|
||||
serialized,
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_value::<BuildInfo>(serialized).expect("deserialize build information"),
|
||||
build_info,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn historical_build_info_does_not_infer_the_current_target() {
|
||||
let legacy = serde_json::json!({ "version": "1.2.3", "build_commit": BUILD_COMMIT });
|
||||
for info in [
|
||||
serde_json::from_value::<BuildInfo>(legacy).expect("deserialize legacy build information"),
|
||||
BuildInfo::from_version("1.2.3"),
|
||||
BuildInfo::from_version(BUILD_COMMIT),
|
||||
] {
|
||||
assert_eq!(info.target(), None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_id_is_stable_and_uses_the_supplied_commit_and_target() {
|
||||
// These vectors also cover targets different from the machine running CI.
|
||||
for (target, digest) in [
|
||||
(
|
||||
"aarch64-apple-darwin",
|
||||
"90138d7ee35f3f61cd61eb55e8d64105f856055641af175388b102dffa772594",
|
||||
),
|
||||
(
|
||||
"x86_64-unknown-linux-gnu",
|
||||
"89f3a373036537bde64861b3ad8b1c9494924b0174622e47acda695619630d09",
|
||||
),
|
||||
(
|
||||
"x86_64-unknown-linux-musl",
|
||||
"fb4f62da3e84f6864dcec8ede7bc66f1c96ecaeaf55f8a786b85df994057c8ac",
|
||||
),
|
||||
] {
|
||||
let expected = Some(format!("sha256:{digest}"));
|
||||
assert_eq!(build_id(BUILD_COMMIT, target), expected);
|
||||
assert_eq!(
|
||||
build_id(&BUILD_COMMIT.to_ascii_uppercase(), target),
|
||||
expected
|
||||
);
|
||||
assert_ne!(
|
||||
build_id("1123456789abcdef0123456789abcdef01234567", target),
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_id_requires_a_valid_stamp_and_target() {
|
||||
for commit in [
|
||||
"",
|
||||
"dev",
|
||||
"unknown",
|
||||
"0123456",
|
||||
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
|
||||
] {
|
||||
assert_eq!(build_id(commit, "x86_64-unknown-linux-musl"), None);
|
||||
}
|
||||
assert_eq!(build_id(BUILD_COMMIT, ""), None);
|
||||
}
|
||||
155
codex-rs/build-info/src/lib.rs
Normal file
155
codex-rs/build-info/src/lib.rs
Normal file
@@ -0,0 +1,155 @@
|
||||
//! Resolve the packaged release version, compiled commit, and target of the current runtime.
|
||||
|
||||
use std::fmt;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use codex_install_context::InstallContext;
|
||||
use semver::Version;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use sha2::Digest;
|
||||
use sha2::Sha256;
|
||||
|
||||
static BUILD_INFO: OnceLock<BuildInfo> = OnceLock::new();
|
||||
|
||||
/// Initialize build information from the commit stamped into the calling executable.
|
||||
///
|
||||
/// The environment lookup intentionally expands at the macro call site so Git
|
||||
/// changes invalidate only final binary actions, not this shared library.
|
||||
/// Cargo builds must supply STABLE_GIT_COMMIT in the build environment.
|
||||
#[macro_export]
|
||||
macro_rules! initialize {
|
||||
() => {
|
||||
$crate::BuildInfo::initialize(option_env!("STABLE_GIT_COMMIT").unwrap_or("dev"));
|
||||
};
|
||||
}
|
||||
|
||||
/// The packaged release version and build provenance for the current runtime.
|
||||
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||
pub struct BuildInfo {
|
||||
version: Version,
|
||||
build_commit: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
target: Option<String>,
|
||||
}
|
||||
|
||||
impl BuildInfo {
|
||||
/// Return build information for the current Codex runtime.
|
||||
pub fn get() -> Self {
|
||||
BUILD_INFO
|
||||
.get_or_init(|| Self::resolve(InstallContext::current(), "dev"))
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// Initialize build information using the final executable's stamped commit.
|
||||
///
|
||||
/// Keeping the stamp in the executable prevents Git operations from
|
||||
/// invalidating the shared Rust library graph.
|
||||
#[doc(hidden)]
|
||||
pub fn initialize(build_commit: &'static str) {
|
||||
let _ = BUILD_INFO.get_or_init(|| Self::resolve(InstallContext::current(), build_commit));
|
||||
}
|
||||
|
||||
/// Recover structured release information from a persisted version string.
|
||||
pub fn from_version(version: impl Into<String>) -> Self {
|
||||
let version = version.into();
|
||||
match Version::parse(&version) {
|
||||
Ok(parsed_version) => Self {
|
||||
build_commit: if parsed_version.major == 0
|
||||
&& parsed_version.minor == 0
|
||||
&& parsed_version.patch == 0
|
||||
{
|
||||
version
|
||||
} else {
|
||||
"unknown".to_string()
|
||||
},
|
||||
version: parsed_version,
|
||||
target: None,
|
||||
},
|
||||
Err(_) => Self {
|
||||
version: Version::new(0, 0, 0),
|
||||
build_commit: version,
|
||||
target: None,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the parsed package version, or `0.0.0` for a source build.
|
||||
pub fn version(&self) -> &Version {
|
||||
&self.version
|
||||
}
|
||||
|
||||
/// Format the version for a user-facing Codex header.
|
||||
pub fn display_version(&self) -> String {
|
||||
if self.build_commit == "dev" {
|
||||
"dev".to_string()
|
||||
} else if self.is_source_build() {
|
||||
format!("v{}", self.build_commit)
|
||||
} else {
|
||||
format!("v{}", self.version)
|
||||
}
|
||||
}
|
||||
|
||||
/// Identify source builds without parsing their displayed Git commit.
|
||||
pub fn is_source_build(&self) -> bool {
|
||||
self.version.major == 0 && self.version.minor == 0 && self.version.patch == 0
|
||||
}
|
||||
|
||||
/// Return the Git commit stamped into the final executable.
|
||||
pub fn build_commit(&self) -> &str {
|
||||
&self.build_commit
|
||||
}
|
||||
|
||||
/// Return the compiler target triple, or `None` for historical metadata without a target.
|
||||
pub fn target(&self) -> Option<&str> {
|
||||
self.target.as_deref()
|
||||
}
|
||||
|
||||
fn resolve(install_context: &InstallContext, build_commit: &'static str) -> Self {
|
||||
if let Some(manifest) = install_context.package_manifest() {
|
||||
return Self {
|
||||
version: manifest.version,
|
||||
build_commit: build_commit.to_owned(),
|
||||
target: Some(env!("CODEX_BUILD_TARGET").to_owned()),
|
||||
};
|
||||
}
|
||||
|
||||
Self {
|
||||
version: Version::new(0, 0, 0),
|
||||
build_commit: build_commit.to_owned(),
|
||||
target: Some(env!("CODEX_BUILD_TARGET").to_owned()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Derive a standard build's opaque ID from its stamped commit and compiler target.
|
||||
///
|
||||
/// Explicit inputs let CI identify cross-compiled builds without running them.
|
||||
/// Runtime callers must use the running executable's stamp and target. The stable
|
||||
/// encoding is SHA-256 of `git:<full lowercase commit>:<target>`, excluding version.
|
||||
/// This identifies a standard build configuration, not exact executable bytes.
|
||||
pub fn build_id(build_commit: &str, target: &str) -> Option<String> {
|
||||
if build_commit.len() != 40
|
||||
|| !build_commit.bytes().all(|byte| byte.is_ascii_hexdigit())
|
||||
|| target.is_empty()
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let identity = format!("git:{}:{target}", build_commit.to_ascii_lowercase());
|
||||
let digest = Sha256::digest(identity.as_bytes());
|
||||
Some(format!("sha256:{digest:x}"))
|
||||
}
|
||||
|
||||
impl fmt::Display for BuildInfo {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if self.is_source_build() {
|
||||
formatter.write_str(&self.build_commit)
|
||||
} else {
|
||||
self.version.fmt(formatter)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "build_info_tests.rs"]
|
||||
mod tests;
|
||||
Reference in New Issue
Block a user