Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reintroduce Stack, with weak validation #789

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `libcnb-data':
- Reintroduced support for deserializing `[[stacks]]` in `buildpack.toml`.
([#789](https://github.com/heroku/libcnb.rs/pull/789))
- `libherokubuildpack`:
- Added `buildpack_output` module. This will help buildpack authors provide consistent and delightful output to their buildpack users ([#721](https://github.com/heroku/libcnb.rs/pull/721))

Expand Down
40 changes: 40 additions & 0 deletions libcnb-data/src/buildpack/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod api;
mod id;
mod stack;
mod target;
mod version;

Expand All @@ -8,6 +9,7 @@ use crate::sbom::SbomFormat;
pub use api::*;
pub use id::*;
use serde::Deserialize;
pub use stack::*;
use std::collections::HashSet;
pub use target::*;
pub use version::*;
Expand Down Expand Up @@ -120,6 +122,8 @@ pub struct ComponentBuildpackDescriptor<BM = GenericMetadata> {
pub api: BuildpackApi,
pub buildpack: Buildpack,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub stacks: Vec<Stack>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub targets: Vec<Target>,
pub metadata: BM,
// As of 2024-02-09, the CNB spec does not forbid component buildpacks
Expand Down Expand Up @@ -257,6 +261,20 @@ uri = "https://example.tld/my-license"
[[buildpack.licenses]]
uri = "https://example.tld/my-license"
[[stacks]]
id = "heroku-20"
[[stacks]]
id = "io.buildpacks.stacks.bionic"
mixins = []
[[stacks]]
id = "io.buildpacks.stacks.focal"
mixins = ["build:jq", "wget"]
[[stacks]]
id = "*"
[[targets]]
os = "linux"
arch = "amd64"
Expand Down Expand Up @@ -339,6 +357,27 @@ checksum = "abc123"
SbomFormat::SpdxJson
])
);
assert_eq!(
buildpack_descriptor.stacks,
[
Stack {
id: String::from("heroku-20"),
mixins: Vec::new(),
},
Stack {
id: String::from("io.buildpacks.stacks.bionic"),
mixins: Vec::new(),
},
Stack {
id: String::from("io.buildpacks.stacks.focal"),
mixins: vec![String::from("build:jq"), String::from("wget")]
},
Stack {
id: String::from("*"),
mixins: Vec::new()
}
]
);
assert_eq!(
buildpack_descriptor.targets,
[
Expand Down Expand Up @@ -529,6 +568,7 @@ version = "0.0.1"
);
assert_eq!(buildpack_descriptor.buildpack.licenses, Vec::new());
assert_eq!(buildpack_descriptor.buildpack.sbom_formats, HashSet::new());
assert_eq!(buildpack_descriptor.stacks, []);
assert_eq!(buildpack_descriptor.targets, []);
assert_eq!(buildpack_descriptor.metadata, None);
}
Expand Down
60 changes: 60 additions & 0 deletions libcnb-data/src/buildpack/stack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use serde::Deserialize;

// Stacks are deprecated in Buildpack API 0.10, and libcnb.rs effectively
// ignores them. However, they are still supported by the Buildpack API, so
// libcnb should continue to allow them to exist in buildpack.toml.
#[derive(Deserialize, Debug, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Stack {
joshwlewis marked this conversation as resolved.
Show resolved Hide resolved
pub id: String,
#[serde(default)]
joshwlewis marked this conversation as resolved.
Show resolved Hide resolved
pub mixins: Vec<String>,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn deserialize_specific_stack_without_mixins() {
let toml_str = r#"
id = "heroku-20"
"#;
assert_eq!(
toml::from_str::<Stack>(toml_str),
Ok(Stack {
id: String::from("heroku-20"),
mixins: Vec::new()
}),
);
}

#[test]
fn deserialize_specific_stack_with_mixins() {
let toml_str = r#"
id = "io.buildpacks.stacks.focal"
mixins = ["build:jq", "wget"]
"#;
assert_eq!(
toml::from_str::<Stack>(toml_str),
Ok(Stack {
id: String::from("io.buildpacks.stacks.focal"),
mixins: vec![String::from("build:jq"), String::from("wget")]
}),
);
}

#[test]
fn deserialize_any_stack() {
let toml_str = r#"
id = "*"
"#;
assert_eq!(
toml::from_str::<Stack>(toml_str),
Ok(Stack {
id: String::from("*"),
mixins: vec![],
}),
);
}
}
1 change: 1 addition & 0 deletions libcnb/src/layer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ fn build_context(temp_dir: &TempDir) -> BuildContext<TestBuildpack> {
licenses: Vec::new(),
sbom_formats: HashSet::new(),
},
stacks: vec![],
targets: vec![Target {
os: Some(String::from("linux")),
arch: Some(String::from("amd64")),
Expand Down