Skip to content

Commit 7ae948e

Browse files
committed
Add configure_for_build_environment to AbstractSolver
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 AbstractSolver. 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 <[email protected]>
1 parent 76a188c commit 7ae948e

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
@@ -3,11 +3,12 @@
33
// SPDX-License-Identifier: Apache-2.0
44
// https://github.com/spkenv/spk
55

6+
use std::borrow::Cow;
67
use std::sync::Arc;
78

89
use enum_dispatch::enum_dispatch;
910
use spk_schema::ident::{PkgRequest, VarRequest};
10-
use spk_schema::{OptionMap, Request};
11+
use spk_schema::{OptionMap, Recipe, Request};
1112
use spk_solve_solution::Solution;
1213
use spk_storage::RepositoryHandle;
1314
use variantly::Variantly;
@@ -29,6 +30,12 @@ pub enum SolverImpl {
2930
pub trait Solver {
3031
fn as_any(&self) -> &dyn std::any::Any;
3132

33+
/// Return the options that the solver is currently configured with.
34+
///
35+
/// These are the options that have been set via
36+
/// [`AbstractSolverMut::update_options`].
37+
fn get_options(&self) -> Cow<'_, OptionMap>;
38+
3239
/// Return the PkgRequests added to the solver.
3340
fn get_pkg_requests(&self) -> Vec<PkgRequest>;
3441

@@ -41,10 +48,26 @@ pub trait Solver {
4148

4249
#[async_trait::async_trait]
4350
#[enum_dispatch]
44-
pub trait SolverMut {
51+
pub trait SolverMut: Solver {
4552
/// Add a request to this solver.
4653
fn add_request(&mut self, request: Request);
4754

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

@@ -93,6 +116,10 @@ where
93116
T::as_any(self)
94117
}
95118

119+
fn get_options(&self) -> Cow<'_, OptionMap> {
120+
T::get_options(self)
121+
}
122+
96123
fn get_pkg_requests(&self) -> Vec<PkgRequest> {
97124
T::get_pkg_requests(self)
98125
}
@@ -114,6 +141,10 @@ where
114141
T::as_any(self)
115142
}
116143

144+
fn get_options(&self) -> Cow<'_, OptionMap> {
145+
T::get_options(self)
146+
}
147+
117148
fn get_pkg_requests(&self) -> Vec<PkgRequest> {
118149
T::get_pkg_requests(self)
119150
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ impl SolverTrait for Solver {
299299
self
300300
}
301301

302+
fn get_options(&self) -> Cow<'_, OptionMap> {
303+
Cow::Borrowed(&self.options)
304+
}
305+
302306
fn get_pkg_requests(&self) -> Vec<PkgRequest> {
303307
self.requests
304308
.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)?;
@@ -1102,6 +1086,10 @@ impl SolverTrait for Solver {
11021086
self
11031087
}
11041088

1089+
fn get_options(&self) -> Cow<'_, OptionMap> {
1090+
Cow::Owned(self.get_initial_state().get_option_map().clone())
1091+
}
1092+
11051093
fn get_pkg_requests(&self) -> Vec<PkgRequest> {
11061094
self.get_initial_state()
11071095
.get_pkg_requests()

0 commit comments

Comments
 (0)