Skip to content

Commit

Permalink
Merge branch 'main' into no-timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelzw authored Oct 25, 2024
2 parents 13f98a3 + 0dede4b commit d08feb2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
26 changes: 25 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ pub use util::{get_size, ProgressReporter};
pub const CHANNEL_DIRECTORY_NAME: &str = "channel";
pub const PIXI_PACK_METADATA_PATH: &str = "pixi-pack.json";
pub const DEFAULT_PIXI_PACK_VERSION: &str = "1";
pub const PIXI_PACK_VERSION: &str = env!("CARGO_PKG_VERSION");

/// The metadata for a "pixi-pack".
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct PixiPackMetadata {
/// The pack format version.
pub version: String,
/// The version of pixi-pack that created the pack.
pub pixi_pack_version: Option<String>,
/// The platform the pack was created for.
pub platform: Platform,
}
Expand All @@ -25,6 +29,7 @@ impl Default for PixiPackMetadata {
fn default() -> Self {
Self {
version: DEFAULT_PIXI_PACK_VERSION.to_string(),
pixi_pack_version: Some(PIXI_PACK_VERSION.to_string()),
platform: Platform::current(),
}
}
Expand All @@ -44,16 +49,35 @@ mod tests {
fn test_metadata_serialization() {
let metadata = PixiPackMetadata {
version: DEFAULT_PIXI_PACK_VERSION.to_string(),
pixi_pack_version: Some(PIXI_PACK_VERSION.to_string()),
platform: Platform::Linux64,
};
let result = json!(metadata).to_string();
assert_eq!(result, "{\"version\":\"1\",\"platform\":\"linux-64\"}");
assert_eq!(
result,
format!(
"{{\"version\":\"1\",\"pixi-pack-version\":\"{}\",\"platform\":\"linux-64\"}}",
PIXI_PACK_VERSION
)
);
assert_eq!(
serde_json::from_str::<PixiPackMetadata>(&result).unwrap(),
metadata
);
}

#[test]
fn test_metadata_serialization_no_pixi_pack_version() {
let metadata = serde_json::from_str::<PixiPackMetadata>(
&json!({"version": "1", "platform": "linux-64"}).to_string(),
);
assert!(metadata.is_ok());
let metadata = metadata.unwrap();
assert_eq!(metadata.version, "1");
assert!(metadata.pixi_pack_version.is_none());
assert_eq!(metadata.platform, Platform::Linux64);
}

#[rstest]
#[case(json!({"version": "1", "platform": "linux64"}))]
#[case(json!({"version": 1.0, "platform": "linux-64"}))]
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rattler_conda_types::Platform;
use anyhow::Result;
use pixi_pack::{
pack, unpack, PackOptions, PixiPackMetadata, UnpackOptions, DEFAULT_PIXI_PACK_VERSION,
PIXI_PACK_VERSION,
};
use rattler_shell::shell::ShellEnum;
use tracing_log::AsTrace;
Expand Down Expand Up @@ -117,6 +118,7 @@ async fn main() -> Result<()> {
manifest_path,
metadata: PixiPackMetadata {
version: DEFAULT_PIXI_PACK_VERSION.to_string(),
pixi_pack_version: Some(PIXI_PACK_VERSION.to_string()),
platform,
},
injected_packages: inject,
Expand Down
18 changes: 16 additions & 2 deletions src/unpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use url::Url;

use crate::{
PixiPackMetadata, ProgressReporter, CHANNEL_DIRECTORY_NAME, DEFAULT_PIXI_PACK_VERSION,
PIXI_PACK_METADATA_PATH,
PIXI_PACK_METADATA_PATH, PIXI_PACK_VERSION,
};

/// Options for unpacking a pixi environment.
Expand Down Expand Up @@ -117,6 +117,14 @@ async fn validate_metadata_file(metadata_file: PathBuf) -> Result<()> {
anyhow::bail!("The pack was created for a different platform");
}

tracing::debug!("pack metadata: {:?}", metadata);
if metadata.pixi_pack_version != Some(PIXI_PACK_VERSION.to_string()) {
tracing::warn!(
"The pack was created with a different version of pixi-pack: {:?}",
metadata.pixi_pack_version
);
}

Ok(())
}

Expand Down Expand Up @@ -274,6 +282,8 @@ async fn create_activation_script(

#[cfg(test)]
mod tests {
use crate::PIXI_PACK_VERSION;

use super::*;
use rstest::*;
use serde_json::json;
Expand All @@ -293,7 +303,11 @@ mod tests {
#[default(Platform::current())] platform: Platform,
) -> NamedTempFile {
let mut metadata_file = NamedTempFile::new().unwrap();
let metadata = PixiPackMetadata { version, platform };
let metadata = PixiPackMetadata {
version,
pixi_pack_version: Some(PIXI_PACK_VERSION.to_string()),
platform,
};
let buffer = metadata_file.as_file_mut();
buffer
.write_all(json!(metadata).to_string().as_bytes())
Expand Down

0 comments on commit d08feb2

Please sign in to comment.