Skip to content

Commit 2ba2880

Browse files
committed
Add configure_for_build_environment to the Solver trait
Fix commented out code in cmd_test for build stage tests and add a new test that checks the basic behavior of a build stage test that the test environment includes the build dependencies of the package being tested. Lift the old solver's configure_for_build_environment method to the Solver trait. Although the other test stages have code that only lives in the cmd_test crate to configure the solver properly, the build stage uses this method that was defined in the solver itself. While the logic of configure_for_build_environment could be duplicated in cmd_test, it is also reused by other code in the solver so it made sense to preserve the logic as a reusable solver method. Signed-off-by: J Robert Ray <jrray@jrray.org>
1 parent e2ce393 commit 2ba2880

File tree

5 files changed

+98
-20
lines changed

5 files changed

+98
-20
lines changed

crates/spk-cli/cmd-test/src/cmd_test_test.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,58 @@ tests:
541541
.await
542542
.expect_err("the test run should fail, otherwise the selectors aren't working properly");
543543
}
544+
545+
#[rstest]
546+
#[case::cli("cli")]
547+
#[case::checks("checks")]
548+
#[case::resolvo("resolvo")]
549+
#[tokio::test]
550+
async fn build_stage_test_env_includes_build_deps(
551+
tmpdir: tempfile::TempDir,
552+
#[case] solver_to_run: &str,
553+
) {
554+
let _rt = spfs_runtime().await;
555+
556+
let _ = build_package!(
557+
tmpdir,
558+
"base.spk.yaml",
559+
br#"
560+
pkg: base/1.0.0
561+
build:
562+
script:
563+
- touch "$PREFIX"/base
564+
"#,
565+
solver_to_run
566+
);
567+
568+
let filename_str = build_package!(
569+
tmpdir,
570+
"simple1.spk.yaml",
571+
br#"
572+
pkg: simple1/1.0.0
573+
build:
574+
options:
575+
- pkg: base
576+
script:
577+
- "true"
578+
579+
tests:
580+
- stage: build
581+
script:
582+
# simple's build options should exist in a build stage test environment.
583+
- test -f "$PREFIX"/base
584+
"#,
585+
solver_to_run
586+
);
587+
588+
let mut opt = TestOpt::try_parse_from([
589+
"test",
590+
// Don't exec a new process to move into a new runtime, this confuses
591+
// coverage testing.
592+
"--no-runtime",
593+
"--disable-repo=origin",
594+
filename_str,
595+
])
596+
.unwrap();
597+
opt.test.run().await.unwrap();
598+
}

crates/spk-cli/cmd-test/src/test/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ where
117117
for repo in self.repos.iter().cloned() {
118118
solver.add_repository(repo);
119119
}
120-
// TODO
121-
// solver.configure_for_build_environment(&self.recipe)?;
120+
// Configure solver for build environment.
121+
solver.configure_for_build_environment(&self.recipe)?;
122122
for request in self.additional_requirements.drain(..) {
123123
solver.add_request(request)
124124
}

crates/spk-solve/src/solver.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// https://github.com/spkenv/spk
44

5+
use std::borrow::Cow;
56
use std::sync::Arc;
67

78
use enum_dispatch::enum_dispatch;
89
use spk_schema::ident::{PkgRequest, VarRequest};
9-
use spk_schema::{OptionMap, Request};
10+
use spk_schema::{OptionMap, Recipe, Request};
1011
use spk_solve_solution::Solution;
1112
use spk_storage::RepositoryHandle;
1213
use variantly::Variantly;
@@ -26,6 +27,12 @@ pub enum SolverImpl {
2627
#[async_trait::async_trait]
2728
#[enum_dispatch]
2829
pub trait Solver {
30+
/// Return the options that the solver is currently configured with.
31+
///
32+
/// These are the options that have been set via
33+
/// [`SolverMut::update_options`].
34+
fn get_options(&self) -> Cow<'_, OptionMap>;
35+
2936
/// Return the PkgRequests added to the solver.
3037
fn get_pkg_requests(&self) -> Vec<PkgRequest>;
3138

@@ -38,10 +45,26 @@ pub trait Solver {
3845

3946
#[async_trait::async_trait]
4047
#[enum_dispatch]
41-
pub trait SolverMut {
48+
pub trait SolverMut: Solver {
4249
/// Add a request to this solver.
4350
fn add_request(&mut self, request: Request);
4451

52+
/// Adds requests for all build requirements of the given recipe.
53+
fn configure_for_build_environment<T: Recipe>(&mut self, recipe: &T) -> Result<()> {
54+
let options = self.get_options();
55+
56+
let build_options = recipe.resolve_options(&*options)?;
57+
for req in recipe
58+
.get_build_requirements(&build_options)?
59+
.iter()
60+
.cloned()
61+
{
62+
self.add_request(req)
63+
}
64+
65+
Ok(())
66+
}
67+
4568
/// Put this solver back into its default state
4669
fn reset(&mut self);
4770

@@ -86,6 +109,10 @@ impl<T> Solver for &T
86109
where
87110
T: Solver,
88111
{
112+
fn get_options(&self) -> Cow<'_, OptionMap> {
113+
T::get_options(self)
114+
}
115+
89116
fn get_pkg_requests(&self) -> Vec<PkgRequest> {
90117
T::get_pkg_requests(self)
91118
}
@@ -103,6 +130,10 @@ impl<T> Solver for &mut T
103130
where
104131
T: Solver,
105132
{
133+
fn get_options(&self) -> Cow<'_, OptionMap> {
134+
T::get_options(self)
135+
}
136+
106137
fn get_pkg_requests(&self) -> Vec<PkgRequest> {
107138
T::get_pkg_requests(self)
108139
}

crates/spk-solve/src/solvers/resolvo/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ impl Solver {
306306
}
307307

308308
impl SolverTrait for Solver {
309+
fn get_options(&self) -> Cow<'_, OptionMap> {
310+
Cow::Borrowed(&self.options)
311+
}
312+
309313
fn get_pkg_requests(&self) -> Vec<PkgRequest> {
310314
self.requests
311315
.iter()

crates/spk-solve/src/solvers/step/solver.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,22 +1044,6 @@ impl Solver {
10441044
|| self.impossible_checks.use_in_build_keys
10451045
}
10461046

1047-
/// Adds requests for all build requirements
1048-
pub fn configure_for_build_environment<T: Recipe>(&mut self, recipe: &T) -> Result<()> {
1049-
let state = self.get_initial_state();
1050-
1051-
let build_options = recipe.resolve_options(state.get_option_map())?;
1052-
for req in recipe
1053-
.get_build_requirements(&build_options)?
1054-
.iter()
1055-
.cloned()
1056-
{
1057-
self.add_request(req)
1058-
}
1059-
1060-
Ok(())
1061-
}
1062-
10631047
/// Adds requests for all build requirements and solves
10641048
pub async fn solve_build_environment(&mut self, recipe: &SpecRecipe) -> Result<Solution> {
10651049
self.configure_for_build_environment(recipe)?;
@@ -1098,6 +1082,10 @@ impl Solver {
10981082
}
10991083

11001084
impl SolverTrait for Solver {
1085+
fn get_options(&self) -> Cow<'_, OptionMap> {
1086+
Cow::Owned(self.get_initial_state().get_option_map().clone())
1087+
}
1088+
11011089
fn get_pkg_requests(&self) -> Vec<PkgRequest> {
11021090
self.get_initial_state()
11031091
.get_pkg_requests()

0 commit comments

Comments
 (0)