Skip to content

Commit 8bcd163

Browse files
authored
Change spk ls/search filters to use host var compatibility rules (#1169)
Signed-off-by: David Gilligan-Cook <[email protected]>
1 parent 9c856ab commit 8bcd163

File tree

4 files changed

+87
-14
lines changed

4 files changed

+87
-14
lines changed

Diff for: crates/spk-cli/group2/src/cmd_ls_test.rs

+74-1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ async fn test_ls_shows_remote_packages_with_host_default_filter() {
118118
remote_repo.publish_recipe(&recipe).await.unwrap();
119119
let host_options = HOST_OPTIONS.get().unwrap();
120120
let os_id = host_options.get(OptName::distro()).unwrap();
121+
122+
// Build with all matching host options
121123
let spec = spec!({"pkg": "my-pkg/1.0.0/BGSHW3CN",
122124
"build": {
123125
"options":
@@ -140,7 +142,78 @@ async fn test_ls_shows_remote_packages_with_host_default_filter() {
140142

141143
let mut opt = Opt::try_parse_from(["ls", "--host"]).unwrap();
142144
opt.ls.run().await.unwrap();
143-
assert_ne!(opt.ls.output.vec.len(), 0);
145+
assert_eq!(opt.ls.output.vec.len(), 1);
146+
}
147+
148+
/// `spk ls` is expected to list packages in the configured remote
149+
/// repositories that match the default filter for the current host
150+
#[tokio::test]
151+
async fn test_ls_shows_remote_packages_with_host_default_filter_multiple_builds() {
152+
let mut rt = spfs_runtime().await;
153+
let remote_repo = spfsrepo().await;
154+
155+
// Populate the "origin" repo with one package.
156+
// The "local" repo is empty.
157+
158+
rt.add_remote_repo(
159+
"origin",
160+
Remote::Address(RemoteAddress {
161+
address: remote_repo.address().clone(),
162+
}),
163+
)
164+
.unwrap();
165+
166+
let recipe = recipe!({"pkg": "my-pkg/1.0.0"});
167+
remote_repo.publish_recipe(&recipe).await.unwrap();
168+
let host_options = HOST_OPTIONS.get().unwrap();
169+
let os_id = host_options.get(OptName::distro()).unwrap();
170+
171+
// Build with all matching host options
172+
let spec = spec!({"pkg": "my-pkg/1.0.0/BGSHW3CN",
173+
"build": {
174+
"options":
175+
[
176+
{"var": format!("{}/{}", OptName::distro(), host_options.get(OptName::distro()).unwrap()) },
177+
{"var": format!("{}/{}", OptName::os(), host_options.get(OptName::os()).unwrap()) },
178+
{"var": format!("{}/{}", OptName::arch(), host_options.get(OptName::arch()).unwrap()) },
179+
{"var": format!("{}/{}", os_id, host_options.get(os_id).unwrap()) }
180+
]
181+
}});
182+
remote_repo
183+
.publish_package(
184+
&spec,
185+
&vec![(Component::Run, empty_layer_digest())]
186+
.into_iter()
187+
.collect(),
188+
)
189+
.await
190+
.unwrap();
191+
192+
// Build with for another distro in its host options
193+
let spec = spec!({"pkg": "my-pkg/1.0.0/2RGMWL2B",
194+
"build": {
195+
"options":
196+
[
197+
{"var": format!("{}/{}", OptName::distro(), "test_distro") },
198+
{"var": format!("{}/{}", OptName::os(), host_options.get(OptName::os()).unwrap()) },
199+
{"var": format!("{}/{}", OptName::arch(), host_options.get(OptName::arch()).unwrap()) },
200+
{"var": format!("{}/{}", os_id, host_options.get(os_id).unwrap()) }
201+
]
202+
}});
203+
remote_repo
204+
.publish_package(
205+
&spec,
206+
&vec![(Component::Run, empty_layer_digest())]
207+
.into_iter()
208+
.collect(),
209+
)
210+
.await
211+
.unwrap();
212+
213+
let mut opt = Opt::try_parse_from(["ls", "--host"]).unwrap();
214+
opt.ls.run().await.unwrap();
215+
println!("Output: {:?}", opt.ls.output.vec);
216+
assert_eq!(opt.ls.output.vec.len(), 1);
144217
}
145218

146219
/// `spk ls` is expected to list packages in both the local and the configured

Diff for: crates/spk-schema/crates/foundation/src/option_map/filters.rs

-12
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ pub struct OptFilter {
1212
pub value: String,
1313
}
1414

15-
impl OptFilter {
16-
pub fn matches(&self, options: &OptionMap) -> bool {
17-
if let Some(v) = options.get(&self.name) {
18-
self.value == *v
19-
} else {
20-
// Not having an option with the filter's name is
21-
// considered a match.
22-
true
23-
}
24-
}
25-
}
26-
2715
/// Constructs a list of filters from the current host's host options,
2816
/// if any.
2917
pub fn get_host_options_filters() -> Option<Vec<OptFilter>> {

Diff for: crates/spk-schema/src/v0/spec.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,19 @@ impl Package for Spec<BuildIdent> {
257257
fn matches_all_filters(&self, filter_by: &Option<Vec<OptFilter>>) -> bool {
258258
if let Some(filters) = filter_by {
259259
let settings = self.option_values();
260+
260261
for filter in filters {
261-
if !filter.matches(&settings) {
262+
if !settings.contains_key(&filter.name) {
263+
// Not having an option with the filter's name is
264+
// considered a match.
265+
continue;
266+
}
267+
268+
let var_request =
269+
VarRequest::new_with_value(filter.name.clone(), filter.value.clone());
270+
271+
let compat = self.check_satisfies_request(&var_request);
272+
if !compat.is_ok() {
262273
return false;
263274
}
264275
}

Diff for: cspell.json

+1
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@
592592
"respecifying",
593593
"retpoline",
594594
"retryable",
595+
"RGMWL",
595596
"rhelversion",
596597
"robinmap",
597598
"rootdata",

0 commit comments

Comments
 (0)