1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
use libcnb_data::buildpack::BuildpackId;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::rc::Rc;
pub use libcnb_package::CargoProfile;
/// Configuration for a test.
#[derive(Clone)]
pub struct BuildConfig {
pub(crate) app_dir: PathBuf,
pub(crate) cargo_profile: CargoProfile,
pub(crate) target_triple: String,
pub(crate) builder_name: String,
pub(crate) buildpacks: Vec<BuildpackReference>,
pub(crate) env: HashMap<String, String>,
pub(crate) app_dir_preprocessor: Option<Rc<dyn Fn(PathBuf)>>,
pub(crate) expected_pack_result: PackResult,
}
impl BuildConfig {
/// Creates a new build configuration.
///
/// If the `app_dir` parameter is a relative path, it is treated as relative to the Cargo
/// manifest directory ([`CARGO_MANIFEST_DIR`](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates)),
/// i.e. the package's root directory.
///
/// # Example
/// ```no_run
/// use libcnb_test::{BuildConfig, TestRunner};
///
/// TestRunner::default().build(
/// BuildConfig::new("heroku/builder:22", "tests/fixtures/app"),
/// |context| {
/// // ...
/// },
/// );
/// ```
pub fn new(builder_name: impl Into<String>, app_dir: impl AsRef<Path>) -> Self {
Self {
app_dir: PathBuf::from(app_dir.as_ref()),
cargo_profile: CargoProfile::Dev,
target_triple: String::from("x86_64-unknown-linux-musl"),
builder_name: builder_name.into(),
buildpacks: vec![BuildpackReference::CurrentCrate],
env: HashMap::new(),
app_dir_preprocessor: None,
expected_pack_result: PackResult::Success,
}
}
/// Sets the buildpacks (and their ordering) to use when building the app.
///
/// Defaults to [`BuildpackReference::CurrentCrate`].
///
/// # Example
/// ```no_run
/// use libcnb::data::buildpack_id;
/// use libcnb_test::{BuildConfig, BuildpackReference, TestRunner};
///
/// TestRunner::default().build(
/// BuildConfig::new("heroku/builder:22", "tests/fixtures/app").buildpacks([
/// BuildpackReference::CurrentCrate,
/// BuildpackReference::WorkspaceBuildpack(buildpack_id!("my-project/buildpack")),
/// BuildpackReference::Other(String::from("heroku/another-buildpack")),
/// ]),
/// |context| {
/// // ...
/// },
/// );
/// ```
pub fn buildpacks(&mut self, buildpacks: impl Into<Vec<BuildpackReference>>) -> &mut Self {
self.buildpacks = buildpacks.into();
self
}
/// Sets the Cargo profile used when compiling the buildpack.
///
/// Defaults to [`CargoProfile::Dev`].
///
/// # Example
/// ```no_run
/// use libcnb_test::{BuildConfig, CargoProfile, TestRunner};
///
/// TestRunner::default().build(
/// BuildConfig::new("heroku/builder:22", "tests/fixtures/app")
/// .cargo_profile(CargoProfile::Release),
/// |context| {
/// // ...
/// },
/// );
/// ```
pub fn cargo_profile(&mut self, cargo_profile: CargoProfile) -> &mut Self {
self.cargo_profile = cargo_profile;
self
}
/// Sets the target triple used when compiling the buildpack.
///
/// Defaults to `x86_64-unknown-linux-musl`.
///
/// # Example
/// ```no_run
/// use libcnb_test::{BuildConfig, TestRunner};
///
/// TestRunner::default().build(
/// BuildConfig::new("heroku/builder:22", "tests/fixtures/app")
/// .target_triple("x86_64-unknown-linux-musl"),
/// |context| {
/// // ...
/// },
/// );
/// ```
pub fn target_triple(&mut self, target_triple: impl Into<String>) -> &mut Self {
self.target_triple = target_triple.into();
self
}
/// Inserts or updates an environment variable mapping for the build process.
///
/// Note: This does not set this environment variable for running containers, it's only
/// available during the build.
///
/// # Example
/// ```no_run
/// use libcnb_test::{BuildConfig, TestRunner};
///
/// TestRunner::default().build(
/// BuildConfig::new("heroku/builder:22", "tests/fixtures/app")
/// .env("ENV_VAR_ONE", "VALUE ONE")
/// .env("ENV_VAR_TWO", "SOME OTHER VALUE"),
/// |context| {
/// // ...
/// },
/// );
/// ```
pub fn env(&mut self, k: impl Into<String>, v: impl Into<String>) -> &mut Self {
self.env.insert(k.into(), v.into());
self
}
/// Adds or updates multiple environment variable mappings for the build process.
///
/// Note: This does not set environment variables for running containers, they're only
/// available during the build.
///
/// # Example
/// ```no_run
/// use libcnb_test::{BuildConfig, TestRunner};
///
/// TestRunner::default().build(
/// BuildConfig::new("heroku/builder:22", "tests/fixtures/app").envs([
/// ("ENV_VAR_ONE", "VALUE ONE"),
/// ("ENV_VAR_TWO", "SOME OTHER VALUE"),
/// ]),
/// |context| {
/// // ...
/// },
/// );
/// ```
pub fn envs<K: Into<String>, V: Into<String>, I: IntoIterator<Item = (K, V)>>(
&mut self,
envs: I,
) -> &mut Self {
envs.into_iter().for_each(|(key, value)| {
self.env(key.into(), value.into());
});
self
}
/// Sets an app directory preprocessor function.
///
/// It will be run after the app directory has been copied for the current integration test run,
/// the changes will not affect other integration test runs.
///
/// Generally, we suggest using dedicated test fixtures. However, in some cases it is more
/// economical to slightly modify a fixture programmatically before a test instead.
///
/// # Example
/// ```no_run
/// use libcnb_test::{BuildConfig, TestRunner};
///
/// TestRunner::default().build(
/// BuildConfig::new("heroku/builder:22", "tests/fixtures/app").app_dir_preprocessor(
/// |app_dir| {
/// std::fs::remove_file(app_dir.join("Procfile")).unwrap();
/// },
/// ),
/// |context| {
/// // ...
/// },
/// );
/// ```
pub fn app_dir_preprocessor<F: 'static + Fn(PathBuf)>(&mut self, f: F) -> &mut Self {
self.app_dir_preprocessor = Some(Rc::new(f));
self
}
/// Sets the app directory.
///
/// The app directory is normally set in the [`BuildConfig::new`] call, but when sharing test
/// configuration, it might be necessary to change the app directory but keep everything else
/// the same.
///
/// # Example
/// ```no_run
/// use libcnb_test::{BuildConfig, TestRunner};
///
/// fn default_config() -> BuildConfig {
/// BuildConfig::new("heroku/builder:22", "tests/fixtures/app")
/// }
///
/// TestRunner::default().build(
/// default_config().app_dir("tests/fixtures/a-different-app"),
/// |context| {
/// // ...
/// },
/// );
/// ```
pub fn app_dir<P: Into<PathBuf>>(&mut self, path: P) -> &mut Self {
self.app_dir = path.into();
self
}
/// Set the expected `pack` command result.
///
/// In some cases, users might want to explicitly test that a build fails and assert against
/// error output. When passed [`PackResult::Failure`], the test will fail if the pack build
/// succeeds and vice-versa.
///
/// Defaults to [`PackResult::Success`].
///
/// # Example
/// ```no_run
/// use libcnb_test::{assert_contains, BuildConfig, PackResult, TestRunner};
///
/// TestRunner::default().build(
/// BuildConfig::new("heroku/builder:22", "tests/fixtures/app")
/// .expected_pack_result(PackResult::Failure),
/// |context| {
/// assert_contains!(context.pack_stderr, "ERROR: Invalid Procfile!");
/// },
/// );
/// ```
pub fn expected_pack_result(&mut self, pack_result: PackResult) -> &mut Self {
self.expected_pack_result = pack_result;
self
}
}
/// References a Cloud Native Buildpack.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum BuildpackReference {
/// References the buildpack in the Rust Crate currently being tested.
///
/// Is equivalent to `BuildpackReference::WorkspaceBuildpack(buildpack_id!("<buildpack ID of current crate"))`.
CurrentCrate,
/// References a libcnb.rs or composite buildpack within the Cargo workspace that needs to be packaged into a buildpack.
WorkspaceBuildpack(BuildpackId),
/// References another buildpack by id, local directory or tarball.
Other(String),
}
/// Result of a pack execution.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum PackResult {
/// Pack executed successfully.
Success,
/// Pack execution failed.
Failure,
}