Skip to content

Commit

Permalink
added code to set GIT_EXEC_PATH while installing packages. Added a Fr…
Browse files Browse the repository at this point in the history
…omStr implementation to debian::MultiarchName in order to implement a unit test
  • Loading branch information
tlhmerry0098 committed Dec 23, 2024
1 parent 8949e5d commit 5791821
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 1 deletion.
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.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ libcnb-test = "=0.26.0"
regex = "1"
strip-ansi-escapes = "0.2"
tempfile = "3"
buildpacks-deb-packages = { path = "." }

[lints.rust]
unreachable_pub = "warn"
Expand Down
7 changes: 7 additions & 0 deletions project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[_]
schema-version = "0.2"

[com.heroku.buildpacks.deb-packages]
install = [
"git"
]
25 changes: 25 additions & 0 deletions src/debian/multiarch_name.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::fmt::{Display, Formatter};

use crate::debian::ArchitectureName;
use std::str::FromStr;
// use std::fmt::{Display, Formatter};

#[derive(Debug, PartialEq, Clone)]
#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -28,9 +30,32 @@ impl Display for MultiarchName {
}
}

impl FromStr for MultiarchName {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"x86_64-linux-gnu" => Ok(MultiarchName::X86_64_LINUX_GNU),
"aarch64-linux-gnu" => Ok(MultiarchName::AARCH_64_LINUX_GNU),
_ => Err(()),
}
}
}

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

#[test]
fn test_multiarch_name_from_str() {
// Test valid strings
assert_eq!(MultiarchName::from_str("x86_64-linux-gnu").unwrap(), MultiarchName::X86_64_LINUX_GNU);
assert_eq!(MultiarchName::from_str("aarch64-linux-gnu").unwrap(), MultiarchName::AARCH_64_LINUX_GNU);

// Test invalid string
assert!(MultiarchName::from_str("invalid-arch").is_err());
}

#[test]
fn converting_architecture_name_to_multiarch_name() {
Expand Down
34 changes: 33 additions & 1 deletion src/install_packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub(crate) async fn install_packages(
}
}

// Configure the environment variables for the installed layer
let layer_env = configure_layer_environment(
&install_layer.path(),
&MultiarchName::from(&distro.architecture),
Expand Down Expand Up @@ -313,6 +314,7 @@ async fn extract(download_path: PathBuf, output_dir: PathBuf) -> BuildpackResult
Ok(())
}

// Modified function to include setting GIT_EXEC_PATH
fn configure_layer_environment(install_path: &Path, multiarch_name: &MultiarchName) -> LayerEnv {
let mut layer_env = LayerEnv::new();

Expand All @@ -323,8 +325,16 @@ fn configure_layer_environment(install_path: &Path, multiarch_name: &MultiarchNa
];
prepend_to_env_var(&mut layer_env, "PATH", &bin_paths);

// Set GIT_EXEC_PATH
let git_exec_path = install_path.join("usr/lib/git-core");
layer_env.insert(
Scope::All,
ModificationBehavior::Override,
"GIT_EXEC_PATH",
git_exec_path.to_string_lossy().to_string(),
);

// support multi-arch and legacy filesystem layouts for debian packages
// https://wiki.ubuntu.com/MultiarchSpec
let library_paths = [
install_path.join(format!("usr/lib/{multiarch_name}")),
install_path.join("usr/lib"),
Expand Down Expand Up @@ -602,3 +612,25 @@ mod test {
.unwrap_or_default()
}
}

#[cfg(test)]
mod unit_tests {
use std::path::PathBuf;
use libcnb::layer_env::Scope;
use crate::install_packages::configure_layer_environment;
use crate::debian::MultiarchName;
use std::str::FromStr;

#[test]
fn test_configure_layer_environment_sets_git_exec_path() {
let install_path = PathBuf::from("/mock/install/path");
let multiarch_name = MultiarchName::from_str("x86_64-linux-gnu").unwrap();
let layer_env = configure_layer_environment(&install_path, &multiarch_name);

assert_eq!(
layer_env.apply_to_empty(Scope::All).get("GIT_EXEC_PATH").map(|s| s.to_string_lossy().into_owned()),
// layer_env.apply_to_empty(Scope::All).get("GIT_EXEC_PATH"),
Some("/mock/install/path/usr/lib/git-core".to_string())
);
}
}
44 changes: 44 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,47 @@ fn update_project_toml(app_dir: &Path, update_fn: impl FnOnce(&mut DocumentMut))
update_fn(&mut doc);
std::fs::write(&project_toml, doc.to_string()).unwrap();
}

#[test]
fn test_get_integration_test_builder() {
// Set environment variable
std::env::set_var("INTEGRATION_TEST_CNB_BUILDER", "heroku/builder:24");
assert_eq!(get_integration_test_builder(), "heroku/builder:24");

// Unset environment variable
std::env::remove_var("INTEGRATION_TEST_CNB_BUILDER");
assert_eq!(get_integration_test_builder(), DEFAULT_BUILDER);
}

#[test]
fn test_get_integration_test_arch() {
// Set environment variable
std::env::set_var("INTEGRATION_TEST_CNB_ARCH", "arm64");
assert_eq!(get_integration_test_arch(), "arm64");

// Unset environment variable
std::env::remove_var("INTEGRATION_TEST_CNB_ARCH");
assert_eq!(get_integration_test_arch(), DEFAULT_ARCH);
}

#[test]
fn test_panic_unsupported_test_configuration() {
// This test should panic
let result = std::panic::catch_unwind(|| {
panic_unsupported_test_configuration();
});
assert!(result.is_err());
}

#[test]
fn test_set_install_config() {
let temp_dir = tempfile::tempdir().unwrap();
let app_dir = temp_dir.path();
std::fs::write(app_dir.join("project.toml"), "[com.heroku.buildpacks.deb-packages]").unwrap();

set_install_config(app_dir, [requested_package_config("ffmpeg", true)]);

let contents = std::fs::read_to_string(app_dir.join("project.toml")).unwrap();
assert!(contents.contains("ffmpeg"));
assert!(contents.contains("skip_dependencies = true"));
}

0 comments on commit 5791821

Please sign in to comment.