Skip to content

Commit

Permalink
fix: add test for tar source and fix filename for single file source (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv authored Dec 12, 2023
1 parent 9c84e3d commit 4e554cc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
9 changes: 9 additions & 0 deletions rust-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,13 @@ mod tests {
assert!(rattler_build.is_ok());
assert!(rattler_build.unwrap().status.success());
}

#[test]
fn test_tar_source() {
let tmp = tmp("test_tar_source");
let rattler_build =
rattler().build::<_, _, &str>(recipes().join("tar-source"), tmp.as_dir(), None);
assert!(rattler_build.is_ok());
assert!(rattler_build.unwrap().status.success());
}
}
22 changes: 14 additions & 8 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub enum SourceError {
#[error("Failed to download source from url: {0}")]
Url(#[from] reqwest::Error),

#[error("Url does not point to a file: {0}")]
UrlNotFile(url::Url),

#[error("WalkDir Error: {0}")]
WalkDir(#[from] walkdir::Error),

Expand Down Expand Up @@ -100,6 +103,13 @@ pub async fn fetch_sources(
}
Source::Url(src) => {
tracing::info!("Fetching source from URL: {}", src.url());

let file_name_from_url = src
.url()
.path_segments()
.and_then(|segments| segments.last().map(|last| last.to_string()))
.ok_or_else(|| SourceError::UrlNotFile(src.url().clone()))?;

let res = url_source::url_src(src, &cache_src).await?;
let mut dest_dir = if let Some(folder) = src.folder() {
work_dir.join(folder)
Expand All @@ -126,12 +136,7 @@ pub async fn fetch_sources(
if let Some(file_name) = src.file_name() {
dest_dir = dest_dir.join(file_name);
} else {
dest_dir = dest_dir.join(res.file_name().ok_or_else(|| {
SourceError::UnknownError(format!(
"Failed to get filename for `{}`",
res.display()
))
})?);
dest_dir = dest_dir.join(file_name_from_url);
}
fs::copy(&res, &dest_dir)?;
tracing::info!("Downloaded to {:?}", dest_dir);
Expand Down Expand Up @@ -192,7 +197,7 @@ pub async fn fetch_sources(
fn extract(archive: &Path, target_directory: &Path) -> Result<std::process::Output, SourceError> {
let tar_exe = which::which("tar").map_err(|_| SourceError::TarNotFound)?;

let output = Command::new(tar_exe)
let output = Command::new(&tar_exe)
.arg("-xf")
.arg(archive.as_os_str())
.arg("--preserve-permissions")
Expand All @@ -203,7 +208,8 @@ fn extract(archive: &Path, target_directory: &Path) -> Result<std::process::Outp

if !output.status.success() {
return Err(SourceError::ExtractionError(format!(
"Failed to extract archive: {}.\nStdout: {}\nStderr: {}",
"Failed to extract archive with {:?}: {}.\nStdout: {}\nStderr: {}",
tar_exe,
archive.display(),
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
Expand Down
21 changes: 21 additions & 0 deletions test-data/recipes/tar-source/recipe.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
context:
name: polarify
version: 0.1.3

package:
name: ${{ name }}
version: ${{ version }}

source:
- url: https://github.com/quantco/polarify/archive/refs/tags/v${{ version }}.tar.gz
sha256: 93441164c23b764d72c8a66d14b11d5bbd353ed6112ccf3b35efda2a98f9df02
- url: https://repo1.maven.org/maven2/net/java/dev/jna/jna/5.8.0/jna-5.8.0.jar
sha256: 930273cc1c492f25661ea62413a6da3fd7f6e01bf1c4dcc0817fc8696a7b07ac

build:
script:
- if: unix
then:
- test -f jna-5.8.0.jar
else:
- if not exist jna-5.8.0.jar exit 1

0 comments on commit 4e554cc

Please sign in to comment.