-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support local buildpacks and meta-buildpacks in libcnb-test (#666)
* Support local buildpacks and meta-buildpacks in libcnb-test The test runner has been modified to support a `BuildpackReference::LibCnbRs(BuildpackId)` variant which represents a buildpack (and any of it's dependencies) within the workspace that needs to be compiled for testing. This is similar to `BuildpackReference::Crate` but also supports meta-buildpacks. --------- Co-authored-by: Ed Morley <[email protected]>
- Loading branch information
1 parent
31ec5af
commit b59a9ce
Showing
17 changed files
with
274 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
Cargo.lock | ||
**/fixtures/*/target/ | ||
**/fixtures/*/packaged/ | ||
**/test-fixtures/buildpacks/*/target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,116 @@ | ||
use cargo_metadata::MetadataCommand; | ||
use libcnb_package::build::{build_buildpack_binaries, BuildBinariesError}; | ||
use libcnb_common::toml_file::{read_toml_file, TomlFileError}; | ||
use libcnb_data::buildpack::{BuildpackDescriptor, BuildpackId}; | ||
use libcnb_package::buildpack_dependency_graph::{ | ||
build_libcnb_buildpacks_dependency_graph, BuildBuildpackDependencyGraphError, | ||
}; | ||
use libcnb_package::cross_compile::{cross_compile_assistance, CrossCompileAssistance}; | ||
use libcnb_package::{assemble_buildpack_directory, CargoProfile}; | ||
use std::path::PathBuf; | ||
use tempfile::{tempdir, TempDir}; | ||
use libcnb_package::dependency_graph::{get_dependencies, GetDependenciesError}; | ||
use libcnb_package::output::create_packaged_buildpack_dir_resolver; | ||
use libcnb_package::{find_cargo_workspace_root_dir, CargoProfile, FindCargoWorkspaceRootError}; | ||
use std::collections::BTreeMap; | ||
use std::fs; | ||
use std::path::{Path, PathBuf}; | ||
|
||
/// Packages the current crate as a buildpack into a temporary directory. | ||
pub(crate) fn package_crate_buildpack( | ||
cargo_profile: CargoProfile, | ||
target_triple: impl AsRef<str>, | ||
) -> Result<TempDir, PackageCrateBuildpackError> { | ||
let cargo_manifest_dir = std::env::var("CARGO_MANIFEST_DIR") | ||
.map(PathBuf::from) | ||
.map_err(PackageCrateBuildpackError::CannotDetermineCrateDirectory)?; | ||
cargo_manifest_dir: &Path, | ||
target_buildpack_dir: &Path, | ||
) -> Result<PathBuf, PackageBuildpackError> { | ||
let buildpack_toml = cargo_manifest_dir.join("buildpack.toml"); | ||
|
||
let cargo_metadata = MetadataCommand::new() | ||
.manifest_path(&cargo_manifest_dir.join("Cargo.toml")) | ||
.exec() | ||
.map_err(PackageCrateBuildpackError::CargoMetadataError)?; | ||
assert!( | ||
buildpack_toml.exists(), | ||
"Could not package directory as buildpack! No `buildpack.toml` file exists at {}", | ||
cargo_manifest_dir.display() | ||
); | ||
|
||
let cargo_env = match cross_compile_assistance(target_triple.as_ref()) { | ||
let buildpack_descriptor: BuildpackDescriptor = read_toml_file(buildpack_toml) | ||
.map_err(PackageBuildpackError::CannotReadBuildpackDescriptor)?; | ||
|
||
package_buildpack( | ||
&buildpack_descriptor.buildpack().id, | ||
cargo_profile, | ||
target_triple, | ||
cargo_manifest_dir, | ||
target_buildpack_dir, | ||
) | ||
} | ||
|
||
pub(crate) fn package_buildpack( | ||
buildpack_id: &BuildpackId, | ||
cargo_profile: CargoProfile, | ||
target_triple: impl AsRef<str>, | ||
cargo_manifest_dir: &Path, | ||
target_buildpack_dir: &Path, | ||
) -> Result<PathBuf, PackageBuildpackError> { | ||
let cargo_build_env = match cross_compile_assistance(target_triple.as_ref()) { | ||
CrossCompileAssistance::HelpText(help_text) => { | ||
return Err(PackageCrateBuildpackError::CrossCompileConfigurationError( | ||
return Err(PackageBuildpackError::CrossCompileConfigurationError( | ||
help_text, | ||
)); | ||
} | ||
CrossCompileAssistance::NoAssistance => Vec::new(), | ||
CrossCompileAssistance::Configuration { cargo_env } => cargo_env, | ||
}; | ||
|
||
let buildpack_dir = | ||
tempdir().map_err(PackageCrateBuildpackError::CannotCreateBuildpackTempDirectory)?; | ||
let workspace_root_path = find_cargo_workspace_root_dir(cargo_manifest_dir) | ||
.map_err(PackageBuildpackError::FindCargoWorkspaceRoot)?; | ||
|
||
let buildpack_binaries = build_buildpack_binaries( | ||
&cargo_manifest_dir, | ||
&cargo_metadata, | ||
let buildpack_dir_resolver = create_packaged_buildpack_dir_resolver( | ||
target_buildpack_dir, | ||
cargo_profile, | ||
&cargo_env, | ||
target_triple.as_ref(), | ||
) | ||
.map_err(PackageCrateBuildpackError::BuildBinariesError)?; | ||
); | ||
|
||
assemble_buildpack_directory( | ||
buildpack_dir.path(), | ||
cargo_manifest_dir.join("buildpack.toml"), | ||
&buildpack_binaries, | ||
let buildpack_dependency_graph = build_libcnb_buildpacks_dependency_graph(&workspace_root_path) | ||
.map_err(PackageBuildpackError::BuildBuildpackDependencyGraph)?; | ||
|
||
let root_node = buildpack_dependency_graph | ||
.node_weights() | ||
.find(|node| node.buildpack_id == buildpack_id.clone()); | ||
|
||
assert!( | ||
root_node.is_some(), | ||
"Could not package directory as buildpack! No buildpack with id `{buildpack_id}` exists in the workspace at {}", | ||
workspace_root_path.display() | ||
); | ||
|
||
let build_order = get_dependencies( | ||
&buildpack_dependency_graph, | ||
&[root_node.expect("The root node should exist")], | ||
) | ||
.map_err(PackageCrateBuildpackError::CannotAssembleBuildpackDirectory)?; | ||
.map_err(PackageBuildpackError::GetDependencies)?; | ||
|
||
let mut packaged_buildpack_dirs = BTreeMap::new(); | ||
for node in &build_order { | ||
let buildpack_destination_dir = buildpack_dir_resolver(&node.buildpack_id); | ||
|
||
fs::create_dir_all(&buildpack_destination_dir).unwrap(); | ||
|
||
libcnb_package::package::package_buildpack( | ||
&node.path, | ||
cargo_profile, | ||
target_triple.as_ref(), | ||
&cargo_build_env, | ||
&buildpack_destination_dir, | ||
&packaged_buildpack_dirs, | ||
) | ||
.map_err(PackageBuildpackError::PackageBuildpack)?; | ||
|
||
packaged_buildpack_dirs.insert(node.buildpack_id.clone(), buildpack_destination_dir); | ||
} | ||
|
||
Ok(buildpack_dir) | ||
Ok(buildpack_dir_resolver(buildpack_id)) | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) enum PackageCrateBuildpackError { | ||
BuildBinariesError(BuildBinariesError), | ||
CannotAssembleBuildpackDirectory(std::io::Error), | ||
CannotCreateBuildpackTempDirectory(std::io::Error), | ||
CannotDetermineCrateDirectory(std::env::VarError), | ||
CargoMetadataError(cargo_metadata::Error), | ||
pub(crate) enum PackageBuildpackError { | ||
CannotReadBuildpackDescriptor(TomlFileError), | ||
BuildBuildpackDependencyGraph(BuildBuildpackDependencyGraphError), | ||
CrossCompileConfigurationError(String), | ||
FindCargoWorkspaceRoot(FindCargoWorkspaceRootError), | ||
GetDependencies(GetDependenciesError<BuildpackId>), | ||
PackageBuildpack(libcnb_package::package::PackageBuildpackError), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
libcnb-test/test-fixtures/buildpacks/libcnb-test-a/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
name = "one" | ||
version = "0.0.0" | ||
|
||
[workspace] |
8 changes: 8 additions & 0 deletions
8
libcnb-test/test-fixtures/buildpacks/libcnb-test-a/buildpack.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
api = "0.8" | ||
|
||
[buildpack] | ||
id = "libcnb-test/a" | ||
version = "0.0.0" | ||
|
||
[[stacks]] | ||
id = "*" |
3 changes: 3 additions & 0 deletions
3
libcnb-test/test-fixtures/buildpacks/libcnb-test-a/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Buildpack A"); | ||
} |
5 changes: 5 additions & 0 deletions
5
libcnb-test/test-fixtures/buildpacks/libcnb-test-b/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
name = "two" | ||
version = "0.0.0" | ||
|
||
[workspace] |
8 changes: 8 additions & 0 deletions
8
libcnb-test/test-fixtures/buildpacks/libcnb-test-b/buildpack.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
api = "0.8" | ||
|
||
[buildpack] | ||
id = "libcnb-test/b" | ||
version = "0.0.0" | ||
|
||
[[stacks]] | ||
id = "*" |
3 changes: 3 additions & 0 deletions
3
libcnb-test/test-fixtures/buildpacks/libcnb-test-b/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Buildpack B"); | ||
} |
22 changes: 22 additions & 0 deletions
22
libcnb-test/test-fixtures/buildpacks/libcnb-test-meta/buildpack.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
api = "0.8" | ||
|
||
[buildpack] | ||
id = "libcnb-test/meta" | ||
name = "Meta-buildpack Test" | ||
version = "0.0.0" | ||
homepage = "https://example.com" | ||
description = "Official test example" | ||
keywords = ["test"] | ||
|
||
[[buildpack.licenses]] | ||
type = "BSD-3-Clause" | ||
|
||
[[order]] | ||
|
||
[[order.group]] | ||
id = "libcnb-test/a" | ||
version = "0.0.0" | ||
|
||
[[order.group]] | ||
id = "libcnb-test/b" | ||
version = "0.0.0" |
Oops, something went wrong.