Skip to content

Commit

Permalink
Move shared tests for read_layer to shared module
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax committed Jun 12, 2024
1 parent 73529c5 commit 0a0c62b
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 187 deletions.
180 changes: 180 additions & 0 deletions libcnb/src/layer/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,183 @@ pub enum LayerError {
#[error("Unexpected missing layer")]
UnexpectedMissingLayer,
}

#[cfg(test)]
mod test {
use crate::layer::ReadLayerError;
use libcnb_data::generic::GenericMetadata;
use libcnb_data::layer_content_metadata::{LayerContentMetadata, LayerTypes};
use libcnb_data::layer_name;
use serde::Deserialize;
use std::fs;
use tempfile::tempdir;

#[test]
fn read_layer() {
#[derive(Deserialize, Debug, Eq, PartialEq)]
struct TestLayerMetadata {
version: String,
sha: String,
}

let layer_name = layer_name!("foo");
let temp_dir = tempdir().unwrap();
let layers_dir = temp_dir.path();
let layer_dir = layers_dir.join(layer_name.as_str());

fs::create_dir_all(&layer_dir).unwrap();
fs::write(
layers_dir.join(format!("{layer_name}.toml")),
r#"
[types]
launch = true
build = false
cache = true
[metadata]
version = "1.0"
sha = "2608a36467a6fec50be1672bfbf88b04b9ec8efaafa58c71d9edf73519ed8e2c"
"#,
)
.unwrap();

let layer_data = super::read_layer::<TestLayerMetadata, _>(layers_dir, &layer_name)
.unwrap()
.unwrap();

assert_eq!(layer_data.path, layer_dir);

assert_eq!(layer_data.name, layer_name);

assert_eq!(
layer_data.metadata.types,
Some(LayerTypes {
launch: true,
build: false,
cache: true
})
);

assert_eq!(
layer_data.metadata.metadata,
TestLayerMetadata {
version: String::from("1.0"),
sha: String::from(
"2608a36467a6fec50be1672bfbf88b04b9ec8efaafa58c71d9edf73519ed8e2c"
)
}
);
}

#[test]
fn read_malformed_toml_layer() {
let layer_name = layer_name!("foo");
let temp_dir = tempdir().unwrap();
let layers_dir = temp_dir.path();
let layer_dir = layers_dir.join(layer_name.as_str());

fs::create_dir_all(layer_dir).unwrap();
fs::write(
layers_dir.join(format!("{layer_name}.toml")),
r"
[types
build = true
launch = true
cache = true
",
)
.unwrap();

match super::read_layer::<GenericMetadata, _>(layers_dir, &layer_name) {
Err(ReadLayerError::LayerContentMetadataParseError(toml_error)) => {
assert_eq!(toml_error.span(), Some(19..20));
}
_ => panic!("Expected ReadLayerError::LayerContentMetadataParseError!"),
}
}

#[test]
fn read_incompatible_metadata_layer() {
#[derive(Deserialize, Debug, Eq, PartialEq)]
struct TestLayerMetadata {
version: String,
sha: String,
}

let layer_name = layer_name!("foo");
let temp_dir = tempdir().unwrap();
let layers_dir = temp_dir.path();
let layer_dir = layers_dir.join(layer_name.as_str());

fs::create_dir_all(layer_dir).unwrap();
fs::write(
layers_dir.join(format!("{layer_name}.toml")),
r#"
[types]
build = true
launch = true
cache = true
[metadata]
version = "1.0"
"#,
)
.unwrap();

match super::read_layer::<TestLayerMetadata, _>(layers_dir, &layer_name) {
Err(ReadLayerError::LayerContentMetadataParseError(toml_error)) => {
assert_eq!(toml_error.span(), Some(110..148));
}
_ => panic!("Expected ReadLayerError::LayerContentMetadataParseError!"),
}
}

#[test]
fn read_layer_without_layer_directory() {
let layer_name = layer_name!("foo");
let temp_dir = tempdir().unwrap();
let layers_dir = temp_dir.path();
let layer_dir = layers_dir.join(layer_name.as_str());

fs::create_dir_all(layer_dir).unwrap();

match super::read_layer::<GenericMetadata, _>(layers_dir, &layer_name) {
Ok(Some(layer_data)) => {
assert_eq!(
layer_data.metadata,
LayerContentMetadata {
types: None,
metadata: None
}
);
}
_ => panic!("Expected Ok(Some(_)!"),
}
}

#[test]
fn read_layer_without_layer_content_metadata() {
let layer_name = layer_name!("foo");
let temp_dir = tempdir().unwrap();
let layers_dir = temp_dir.path();

fs::write(layers_dir.join(format!("{layer_name}.toml")), "").unwrap();

match super::read_layer::<GenericMetadata, _>(layers_dir, &layer_name) {
Ok(None) => {}
_ => panic!("Expected Ok(None)!"),
}
}

#[test]
fn read_nonexistent_layer() {
let layer_name = layer_name!("foo");
let temp_dir = tempdir().unwrap();
let layers_dir = temp_dir.path();

match super::read_layer::<GenericMetadata, _>(layers_dir, &layer_name) {
Ok(None) => {}
_ => panic!("Expected Ok(None)!"),
}
}
}
187 changes: 0 additions & 187 deletions libcnb/src/layer/trait_api/handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,191 +687,4 @@ mod tests {

assert!(!layer_dir.join("exec.d").exists());
}

#[test]
fn read_layer() {
#[derive(Deserialize, Debug, Eq, PartialEq)]
struct TestLayerMetadata {
version: String,
sha: String,
}

let layer_name = layer_name!("foo");
let temp_dir = tempdir().unwrap();
let layers_dir = temp_dir.path();
let layer_dir = layers_dir.join(layer_name.as_str());

fs::create_dir_all(&layer_dir).unwrap();
fs::write(
layers_dir.join(format!("{layer_name}.toml")),
r#"
[types]
launch = true
build = false
cache = true
[metadata]
version = "1.0"
sha = "2608a36467a6fec50be1672bfbf88b04b9ec8efaafa58c71d9edf73519ed8e2c"
"#,
)
.unwrap();

// Add a bin directory to test if implicit entries are added to the LayerEnv
fs::create_dir_all(layer_dir.join("bin")).unwrap();

// Add a file to the env directory to test if explicit entries are added to the LayerEnv
fs::create_dir_all(layer_dir.join("env")).unwrap();
fs::write(layer_dir.join("env/CUSTOM_ENV"), "CUSTOM_ENV_VALUE").unwrap();

let layer_data = super::read_layer::<TestLayerMetadata, _>(layers_dir, &layer_name)
.unwrap()
.unwrap();

assert_eq!(layer_data.path, layer_dir);

assert_eq!(layer_data.name, layer_name);

assert_eq!(
layer_data.content_metadata.types,
Some(LayerTypes {
launch: true,
build: false,
cache: true
})
);

assert_eq!(
layer_data.content_metadata.metadata,
TestLayerMetadata {
version: String::from("1.0"),
sha: String::from(
"2608a36467a6fec50be1672bfbf88b04b9ec8efaafa58c71d9edf73519ed8e2c"
)
}
);

let applied_layer_env = layer_data.env.apply_to_empty(Scope::Build);
assert_eq!(
applied_layer_env.get("PATH").cloned(),
Some(layer_dir.join("bin").into())
);

assert_eq!(
applied_layer_env.get("CUSTOM_ENV"),
Some(&OsString::from("CUSTOM_ENV_VALUE"))
);
}

#[test]
fn read_malformed_toml_layer() {
let layer_name = layer_name!("foo");
let temp_dir = tempdir().unwrap();
let layers_dir = temp_dir.path();
let layer_dir = layers_dir.join(layer_name.as_str());

fs::create_dir_all(layer_dir).unwrap();
fs::write(
layers_dir.join(format!("{layer_name}.toml")),
r"
[types
build = true
launch = true
cache = true
",
)
.unwrap();

match super::read_layer::<GenericMetadata, _>(layers_dir, &layer_name) {
Err(ReadLayerError::LayerContentMetadataParseError(toml_error)) => {
assert_eq!(toml_error.span(), Some(19..20));
}
_ => panic!("Expected ReadLayerError::LayerContentMetadataParseError!"),
}
}

#[test]
fn read_incompatible_metadata_layer() {
#[derive(Deserialize, Debug, Eq, PartialEq)]
struct TestLayerMetadata {
version: String,
sha: String,
}

let layer_name = layer_name!("foo");
let temp_dir = tempdir().unwrap();
let layers_dir = temp_dir.path();
let layer_dir = layers_dir.join(layer_name.as_str());

fs::create_dir_all(layer_dir).unwrap();
fs::write(
layers_dir.join(format!("{layer_name}.toml")),
r#"
[types]
build = true
launch = true
cache = true
[metadata]
version = "1.0"
"#,
)
.unwrap();

match super::read_layer::<TestLayerMetadata, _>(layers_dir, &layer_name) {
Err(ReadLayerError::LayerContentMetadataParseError(toml_error)) => {
assert_eq!(toml_error.span(), Some(110..148));
}
_ => panic!("Expected ReadLayerError::LayerContentMetadataParseError!"),
}
}

#[test]
fn read_layer_without_layer_directory() {
let layer_name = layer_name!("foo");
let temp_dir = tempdir().unwrap();
let layers_dir = temp_dir.path();
let layer_dir = layers_dir.join(layer_name.as_str());

fs::create_dir_all(layer_dir).unwrap();

match super::read_layer::<GenericMetadata, _>(layers_dir, &layer_name) {
Ok(Some(layer_data)) => {
assert_eq!(
layer_data.content_metadata,
LayerContentMetadata {
types: None,
metadata: None
}
);
}
_ => panic!("Expected Ok(Some(_)!"),
}
}

#[test]
fn read_layer_without_layer_content_metadata() {
let layer_name = layer_name!("foo");
let temp_dir = tempdir().unwrap();
let layers_dir = temp_dir.path();

fs::write(layers_dir.join(format!("{layer_name}.toml")), "").unwrap();

match super::read_layer::<GenericMetadata, _>(layers_dir, &layer_name) {
Ok(None) => {}
_ => panic!("Expected Ok(None)!"),
}
}

#[test]
fn read_nonexistent_layer() {
let layer_name = layer_name!("foo");
let temp_dir = tempdir().unwrap();
let layers_dir = temp_dir.path();

match super::read_layer::<GenericMetadata, _>(layers_dir, &layer_name) {
Ok(None) => {}
_ => panic!("Expected Ok(None)!"),
}
}
}

0 comments on commit 0a0c62b

Please sign in to comment.