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

internal: add tests to archiving #1281

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/archive/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ impl From<crate::http::Error> for Error {
}

pub trait Extract {
fn extract_into(self: Box<Self>, path: &Path) -> Result<(), Error>;
fn extract_into(self, path: &Path) -> Result<(), Error>;
}
14 changes: 7 additions & 7 deletions src/archive/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod extract;
#[cfg(any(unix, debug_assertions))]
pub mod tar;
#[cfg(any(windows, debug_assertions))]
pub mod zip;

use std::io::Read;
Expand All @@ -23,16 +25,14 @@ pub enum Archive {

impl Archive {
pub fn extract_archive_into(&self, path: &Path, response: impl Read) -> Result<(), Error> {
let extractor: Box<dyn Extract> = match self {
match self {
#[cfg(windows)]
Self::Zip => Box::new(Zip::new(response)),
Self::Zip => Zip::new(response).extract_into(path),
#[cfg(unix)]
Self::TarXz => Box::new(Tar::Xz(response)),
Self::TarXz => Tar::Xz(response).extract_into(path),
#[cfg(unix)]
Self::TarGz => Box::new(Tar::Gz(response)),
};
extractor.extract_into(path)?;
Ok(())
Self::TarGz => Tar::Gz(response).extract_into(path),
}
}

pub fn file_extension(&self) -> &'static str {
Expand Down
45 changes: 44 additions & 1 deletion src/archive/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,50 @@ impl<R: Read> Tar<R> {
}

impl<R: Read> Extract for Tar<R> {
fn extract_into(self: Box<Self>, path: &Path) -> Result<(), Error> {
fn extract_into(self, path: &Path) -> Result<(), Error> {
self.extract_into_impl(path)
}
}

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

#[test_log::test]
fn test_tar_gz_extraction() {
let temp_dir = &tempfile::tempdir().expect("Can't create a temp directory");
let response =
crate::http::get("https://nodejs.org/dist/v12.0.0/node-v12.0.0-darwin-x64.tar.gz")
.and_then(reqwest::blocking::Response::error_for_status)
.expect("Can't make request to Node v12.0.0 zip file");
Tar::Gz(response)
.extract_into(temp_dir.as_ref())
.expect("Can't unzip files");
dbg!(&temp_dir);
let node_file = temp_dir
.as_ref()
.join("node-v12.0.0-darwin-x64")
.join("bin")
.join("node");
assert!(node_file.exists());
}

#[test_log::test]
fn test_tar_xz_extraction() {
let temp_dir = &tempfile::tempdir().expect("Can't create a temp directory");
let response =
crate::http::get("https://nodejs.org/dist/v12.0.0/node-v12.0.0-darwin-x64.tar.xz")
.and_then(reqwest::blocking::Response::error_for_status)
.expect("Can't make request to Node v12.0.0 zip file");
Tar::Xz(response)
.extract_into(temp_dir.as_ref())
.expect("Can't unzip files");
dbg!(&temp_dir);
let node_file = temp_dir
.as_ref()
.join("node-v12.0.0-darwin-x64")
.join("bin")
.join("node");
assert!(node_file.exists());
}
}
4 changes: 2 additions & 2 deletions src/archive/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<R: Read> Zip<R> {
}

impl<R: Read> Extract for Zip<R> {
fn extract_into(mut self: Box<Self>, path: &Path) -> Result<(), Error> {
fn extract_into(mut self, path: &Path) -> Result<(), Error> {
let mut tmp_zip_file = tempfile().expect("Can't get a temporary file");

debug!("Created a temporary zip file");
Expand Down Expand Up @@ -88,7 +88,7 @@ mod tests {
let temp_dir = &tempfile::tempdir().expect("Can't create a temp directory");
let response = crate::http::get("https://nodejs.org/dist/v12.0.0/node-v12.0.0-win-x64.zip")
.expect("Can't make request to Node v12.0.0 zip file");
Box::new(Zip::new(response))
Zip::new(response)
.extract_into(temp_dir.as_ref())
.expect("Can't unzip files");
let node_file = temp_dir
Expand Down
Loading