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

Adding host_compat setting to package BuildSpec #922

Merged
merged 18 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/spk-build/src/build/binary_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async fn test_empty_var_option_is_not_a_request() {
r#"{
pkg: mypackage/1.0.0,
build: {
host_compat: Any,
options: [
{var: something}
]
Expand Down Expand Up @@ -831,11 +832,13 @@ async fn test_default_build_component() {
"pkg": "mypkg/1.0.0",
"sources": [],
"build": {
"host_compat": "Any",
"options": [{"pkg": "somepkg/1.0.0"}],
"script": "echo building...",
},
}
);

let requirements = spec.get_build_requirements(&option_map! {}).unwrap();
assert_eq!(requirements.len(), 1, "should have one build requirement");
let req = requirements.get(0).unwrap();
Expand Down
57 changes: 46 additions & 11 deletions crates/spk-cli/cmd-du/src/cmd_du_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ async fn test_du_trivially_works() {
.unwrap();

let spec = recipe!(
{"pkg": "my-pkg/1.0.0", "build": {"script": "echo Hello World!"}}
{ "pkg": "my-pkg/1.0.0",
"build": {
"auto_host_vars": "None",
"script": "echo Hello World!"
}
}
);

rt.tmprepo.publish_recipe(&spec).await.unwrap();
Expand All @@ -72,7 +77,7 @@ async fn test_du_trivially_works() {

let mut expected_output = vec![
"2local/my-pkg/1.0.0/3I42H3S6/:build/spk/pkg/my-pkg/1.0.0/3I42H3S6/options.json",
"160local/my-pkg/1.0.0/3I42H3S6/:build/spk/pkg/my-pkg/1.0.0/3I42H3S6/spec.yaml",
"180local/my-pkg/1.0.0/3I42H3S6/:build/spk/pkg/my-pkg/1.0.0/3I42H3S6/spec.yaml",
"0local/my-pkg/1.0.0/3I42H3S6/:build/spk/pkg/my-pkg/1.0.0/3I42H3S6/build.cmpt",
"17local/my-pkg/1.0.0/3I42H3S6/:build/spk/pkg/my-pkg/1.0.0/3I42H3S6/build.sh",
"0local/my-pkg/1.0.0/3I42H3S6/:run/spk/pkg/my-pkg/1.0.0/3I42H3S6/options.json",
Expand Down Expand Up @@ -204,7 +209,12 @@ async fn test_du_is_not_counting_links() {
.unwrap();

let spec = recipe!(
{"pkg": "my-pkg/1.0.0", "build": {"script": "echo Hello World!"}}
{ "pkg": "my-pkg/1.0.0",
"build": {
"auto_host_vars": "None",
"script": "echo Hello World!"
}
}
);

rt.tmprepo.publish_recipe(&spec).await.unwrap();
Expand Down Expand Up @@ -242,7 +252,12 @@ async fn test_du_is_counting_links() {
.unwrap();

let spec = recipe!(
{"pkg": "my-pkg/1.0.0", "build": {"script": "echo Hello World!"}}
{ "pkg": "my-pkg/1.0.0",
"build": {
"auto_host_vars": "None",
"script": "echo Hello World!"
}
}
);

rt.tmprepo.publish_recipe(&spec).await.unwrap();
Expand Down Expand Up @@ -328,7 +343,12 @@ async fn test_du_summarize_output_enabled() {
.unwrap();

let spec = recipe!(
{"pkg": "my-pkg/1.0.0", "build": {"script": "echo Hello World!"}}
{ "pkg": "my-pkg/1.0.0",
"build": {
"auto_host_vars": "None",
"script": "echo Hello World!"
}
}
);

rt.tmprepo.publish_recipe(&spec).await.unwrap();
Expand All @@ -343,7 +363,7 @@ async fn test_du_summarize_output_enabled() {
let mut opt = Opt::try_parse_from(["du", "local/my-pkg", "-s"]).unwrap();
opt.du.run().await.unwrap();

let expected_output = format!("179local/my-pkg/{}", "".red());
let expected_output = format!("199local/my-pkg/{}", "".red());
let mut generated_output = opt.du.output.vec.lock().unwrap()[0].clone();
generated_output.retain(|c| !c.is_whitespace());

Expand All @@ -363,7 +383,12 @@ async fn test_du_summarize_output_is_not_enabled() {
.unwrap();

let spec = recipe!(
{"pkg": "my-pkg/1.0.0", "build": {"script": "echo Hello World!"}}
{ "pkg": "my-pkg/1.0.0",
"build": {
"auto_host_vars": "None",
"script": "echo Hello World!"
}
}
);

rt.tmprepo.publish_recipe(&spec).await.unwrap();
Expand All @@ -382,7 +407,7 @@ async fn test_du_summarize_output_is_not_enabled() {
"2local/my-pkg/1.0.0/3I42H3S6/:build/spk/pkg/my-pkg/1.0.0/3I42H3S6/options.json",
"0local/my-pkg/1.0.0/3I42H3S6/:build/spk/pkg/my-pkg/1.0.0/3I42H3S6/build.cmpt",
"17local/my-pkg/1.0.0/3I42H3S6/:build/spk/pkg/my-pkg/1.0.0/3I42H3S6/build.sh",
"160local/my-pkg/1.0.0/3I42H3S6/:build/spk/pkg/my-pkg/1.0.0/3I42H3S6/spec.yaml",
"180local/my-pkg/1.0.0/3I42H3S6/:build/spk/pkg/my-pkg/1.0.0/3I42H3S6/spec.yaml",
"0local/my-pkg/1.0.0/3I42H3S6/:run/spk/pkg/my-pkg/1.0.0/3I42H3S6/spec.yaml",
"0local/my-pkg/1.0.0/3I42H3S6/:run/spk/pkg/my-pkg/1.0.0/3I42H3S6/options.json",
"0local/my-pkg/1.0.0/3I42H3S6/:run/spk/pkg/my-pkg/1.0.0/3I42H3S6/run.cmpt",
Expand Down Expand Up @@ -413,7 +438,12 @@ async fn test_deprecate_flag() {
.unwrap();

let spec = recipe!(
{"pkg": "my-pkg/1.0.0", "build": {"script": "echo Hello World!"}, "deprecated": true}
{"pkg": "my-pkg/1.0.0",
"build": {
"auto_host_vars": "None",
"script": "echo Hello World!"
},
"deprecated": true}
);

rt.tmprepo.publish_recipe(&spec).await.unwrap();
Expand All @@ -440,7 +470,7 @@ async fn test_deprecate_flag() {

let mut opt_with_deprecate_flag = Opt::try_parse_from(["du", "local/my-pkg", "-ds"]).unwrap();
opt_with_deprecate_flag.du.run().await.unwrap();
let expected_output = format!("196local/my-pkg/{}", "DEPRECATED".red());
let expected_output = format!("216local/my-pkg/{}", "DEPRECATED".red());
let mut generated_output = opt_with_deprecate_flag.du.output.vec.lock().unwrap()[0].clone();
generated_output.retain(|c| !c.is_whitespace());
assert_eq!(expected_output, generated_output);
Expand All @@ -459,7 +489,12 @@ async fn test_human_readable_flag() {
.unwrap();

let spec = recipe!(
{"pkg": "my-pkg/1.0.0", "build": {"script": "echo Hello World!"}}
{ "pkg": "my-pkg/1.0.0",
"build": {
"auto_host_vars": "None",
"script": "echo Hello World!"
}
}
);

rt.tmprepo.publish_recipe(&spec).await.unwrap();
Expand Down
15 changes: 12 additions & 3 deletions crates/spk-cli/group2/src/cmd_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,23 @@ fn get_stub(name: &PkgNameBuf) -> String {

build:

# set which host related vars are added automatically to the built package:
# - Distro: adds 'distro', 'arch', 'os' and '<distroname>' vars, so the package
# can only be used on the same OS, CPU, and OS distribution version
# (e.g. linux distro). This is the default.
# - Arch: adds 'arch' and 'os' vars, so the package can be used anywhere that
# has the same OS and CPU architecture (x86_64, i386)
# - Os: adds 'os' var, so the package can be used anywhere that has the same
# OS type (mac, linux, windows)
# - None: adds no host vars, so package can be used on any OS and any architecture
auto_host_vars: Distro

# options are all the inputs to the package build process, including
# build-time dependencies
options:
# var options define environment/string values that affect the build.
# The value is defined in the build environment as SPK_OPT_{{name}}
- var: arch # rebuild if the arch changes
- var: os # rebuild if the os changes
- var: centos # rebuild if centos version changes
# - var: somename/somvevalue

# pkg options request packages that need to be present
# in the build environment. You can specify a version number
Expand Down
12 changes: 7 additions & 5 deletions crates/spk-cli/group3/src/cmd_import_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ async fn test_archive_io() {
.build_and_publish(option_map! {}, &*rt.tmprepo)
.await
.unwrap();
let digest = spec.ident().build();

let filename = rt.tmpdir.path().join("archive.spk");
filename.ensure();
spk_storage::export_package(spec.ident().to_any(), &filename)
Expand Down Expand Up @@ -49,15 +51,15 @@ async fn test_archive_io() {
"tags/spk/pkg".to_string(),
"tags/spk/pkg/spk-archive-test".to_string(),
"tags/spk/pkg/spk-archive-test/0.0.1".to_string(),
"tags/spk/pkg/spk-archive-test/0.0.1/3I42H3S6".to_string(),
"tags/spk/pkg/spk-archive-test/0.0.1/3I42H3S6.tag".to_string(),
"tags/spk/pkg/spk-archive-test/0.0.1/3I42H3S6/build.tag".to_string(),
"tags/spk/pkg/spk-archive-test/0.0.1/3I42H3S6/run.tag".to_string(),
format!("tags/spk/pkg/spk-archive-test/0.0.1/{digest}"),
format!("tags/spk/pkg/spk-archive-test/0.0.1/{digest}.tag"),
format!("tags/spk/pkg/spk-archive-test/0.0.1/{digest}/build.tag"),
format!("tags/spk/pkg/spk-archive-test/0.0.1/{digest}/run.tag"),
"tags/spk/spec".to_string(),
"tags/spk/spec/spk-archive-test".to_string(),
"tags/spk/spec/spk-archive-test/0.0.1".to_string(),
"tags/spk/spec/spk-archive-test/0.0.1.tag".to_string(),
"tags/spk/spec/spk-archive-test/0.0.1/3I42H3S6.tag".to_string(),
format!("tags/spk/spec/spk-archive-test/0.0.1/{digest}.tag"),
]
);
let result = super::Import {
Expand Down
1 change: 1 addition & 0 deletions crates/spk-schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ spk-schema-foundation = { path = "./crates/foundation" }
spk-schema-ident = { path = "./crates/ident" }
spk-schema-validators = { path = "./crates/validators" }
spk-schema-liquid = { path = "./crates/liquid" }
strum = { workspace = true }
sys-info = "0.9.0"
tempfile = { workspace = true }
thiserror = { workspace = true }
Expand Down
34 changes: 34 additions & 0 deletions crates/spk-schema/crates/foundation/src/name/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ impl OptName {
unsafe { Self::from_str("distro") }
}

/// Fallback option used to identify an os with no distro name
pub const fn unknown_distro() -> &'static Self {
// Safety: from_str skips validation, but this is a known good value
unsafe { Self::from_str("unknown_distro") }
}

/// Validate the given string as an option name
pub fn validate<S: AsRef<str> + ?Sized>(s: &S) -> Result<()> {
validate_opt_name(s)
Expand Down Expand Up @@ -230,6 +236,34 @@ impl OptName {
}
}

impl OptNameBuf {
/// Construct a valid OptNameBuf, invalid characters will be
/// removed, the length will be padded or truncated, and uppercase
/// letters will be lowercased to make a valid option name.
pub fn new_lossy<S: AsRef<str>>(s: &S) -> OptNameBuf {
if validate_opt_base_name(s).is_ok() {
return unsafe { OptNameBuf::from_string(s.as_ref().to_string()) };
}

let mut new_name: String = s
.as_ref()
.to_lowercase()
.chars()
.filter(|c| is_valid_opt_name_char(*c))
.collect();

let length = new_name.len();
if length < OptName::MIN_LEN {
let difference = OptName::MIN_LEN - length;
new_name += &"_".repeat(difference)
} else if length > OptName::MAX_LEN {
new_name.truncate(OptName::MAX_LEN)
}

unsafe { OptNameBuf::from_string(new_name) }
}
}

/// Ensure that the provided string is a valid option name.
///
/// This is for checking option names with or without any leading
Expand Down
17 changes: 17 additions & 0 deletions crates/spk-schema/crates/foundation/src/name/name_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ fn test_opt_name_validation(#[case] input: &str) {
super::validate_opt_name(input).unwrap();
}

#[rstest]
#[case("lowercase", "lowercase")]
#[case("with-dashes", "with-dashes")]
#[case("with_underscores", "with_underscores")]
#[case("num000", "num000")]
#[case("000-000", "000-000")]
#[case("000_000", "000_000")]
#[case("-----", "-----")]
#[case("_____", "_____")]
#[case("upperCase", "uppercase")]
#[case("name!!", "name")]
#[case("a", "a_")]
fn test_opt_name_buf_new_lossy(#[case] input: &str, #[case] result: &str) {
let opt_name = super::OptNameBuf::new_lossy(&String::from(input));
assert_eq!(opt_name.base_name(), result);
}

#[rstest]
#[case("my_opt", None, "my_opt")]
#[case("my-pkg.my_opt", Some("my-pkg"), "my_opt")]
Expand Down
Loading