Skip to content

Commit

Permalink
Reintroduce Stack, with weak validation (#789)
Browse files Browse the repository at this point in the history
* Reintroduce Stack (but with weaker validation)

* Reintroduce stacks into deserialization tests

* Add changelog entry for stack reintroduction

* Skip serializing empty mixins

Co-authored-by: Ed Morley <501702+edmorley@users.noreply.github.com>

---------

Co-authored-by: Ed Morley <501702+edmorley@users.noreply.github.com>
  • Loading branch information
joshwlewis and edmorley authored Feb 23, 2024
1 parent b89c4ae commit dc95de5
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
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 {
pub id: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
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

0 comments on commit dc95de5

Please sign in to comment.