Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pins in components not being rendered #886

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion crates/spk-schema/src/install_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ impl InstallSpec {
options: &OptionMap,
resolved: impl Iterator<Item = &'a BuildIdent>,
) -> Result<()> {
self.requirements.render_all_pins(options, resolved)
let resolved_by_name = resolved.map(|x| (x.name(), x)).collect();
self.requirements
.render_all_pins(options, &resolved_by_name)?;
for component in self.components.iter_mut() {
component
.requirements
.render_all_pins(options, &resolved_by_name)?;
}
Ok(())
}
}

Expand Down
61 changes: 61 additions & 0 deletions crates/spk-schema/src/install_spec_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,64 @@
// Copyright (c) Sony Pictures Imageworks, et al.
// SPDX-License-Identifier: Apache-2.0
// https://github.com/imageworks/spk

use rstest::rstest;
use spk_schema_foundation::ident_component::Component;
use spk_schema_foundation::option_map::OptionMap;
use spk_schema_foundation::version::BINARY_STR;
use spk_schema_ident::{parse_ident_range, PkgRequest, Request, RequestedBy};

use crate::{InstallSpec, RequirementsList};

#[rstest]
fn test_render_all_pins_renders_requirements_in_components() {
let mut install_spec = InstallSpec::default();
let mut requirements = RequirementsList::default();
requirements.insert_or_replace({
Request::Pkg(
PkgRequest::new(
parse_ident_range("test").unwrap(),
RequestedBy::SpkInternalTest,
)
.with_pin(Some(BINARY_STR.to_string())),
)
});
install_spec
.components
.iter_mut()
.find(|c| c.name == Component::Run)
.unwrap()
.requirements = requirements;

// Expected value before pinning.
let Request::Pkg(req) = &install_spec
.components
.iter()
.find(|c| c.name == Component::Run)
.unwrap()
.requirements[0]
else {
panic!("Expected a Pkg request");
};
assert_eq!(req.to_string(), "test");

install_spec
.render_all_pins(
&OptionMap::default(),
["test/1.2.3/GMTG3CXY".parse().unwrap()].iter(),
)
.unwrap();

// Now the install requirement inside the run component should be pinned to
// version 1.2.3.
let Request::Pkg(req) = &install_spec
.components
.iter()
.find(|c| c.name == Component::Run)
.unwrap()
.requirements[0]
else {
panic!("Expected a Pkg request");
};
assert_eq!(req.to_string(), "test/Binary:1.2.3");
}
11 changes: 4 additions & 7 deletions crates/spk-schema/src/requirements_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::collections::HashSet;
use std::fmt::Write;

use serde::{Deserialize, Serialize};
use spk_schema_foundation::name::PkgName;
use spk_schema_foundation::version::Compatibility;
use spk_schema_ident::{BuildIdent, PinPolicy};

Expand Down Expand Up @@ -116,19 +117,15 @@ impl RequirementsList {
}

/// Render all requests with a package pin using the given resolved packages.
pub fn render_all_pins<'a>(
pub fn render_all_pins(
&mut self,
options: &OptionMap,
resolved: impl Iterator<Item = &'a BuildIdent>,
resolved_by_name: &std::collections::HashMap<&PkgName, &BuildIdent>,
) -> Result<()> {
let mut by_name = std::collections::HashMap::new();
for pkg in resolved {
by_name.insert(pkg.name(), pkg);
}
self.0 = std::mem::take(&mut self.0).into_iter().filter_map(|request| {
match &request {
Request::Pkg(pkg_request) => {
match by_name.get(pkg_request.pkg.name()) {
match resolved_by_name.get(pkg_request.pkg.name()) {
None if pkg_request.pin.is_none() && pkg_request.pin_policy == PinPolicy::IfPresentInBuildEnv => {
None
}
Expand Down