Skip to content

Commit

Permalink
Add unpack tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Jan 24, 2024
1 parent 9c0cf57 commit 56ac648
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 17 deletions.
4 changes: 2 additions & 2 deletions crates/download/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ extern "ExtismHost" {

#[derive(Args)]
pub struct DownloadExtensionArgs {
#[arg(long, required = true)]
#[arg(long, short = 'u', required = true)]
pub url: String,

#[arg(long)]
#[arg(long, short = 'd')]
pub dest: Option<String>,

#[arg(long)]
Expand Down
27 changes: 14 additions & 13 deletions crates/unpack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub struct UnpackExtensionArgs {
#[arg(long, short = 's', required = true)]
pub src: String,

#[arg(long, short = 'd', required = true)]
pub dest: String,
#[arg(long, short = 'd')]
pub dest: Option<String>,

#[arg(long)]
pub prefix: Option<String>,
Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn execute_extension(Json(input): Json<ExecuteExtensionInput>) -> FnResult<(
.is_some_and(|ext| ext == "tar" || ext == "tgz" || ext == "gz" || ext == "zip")
{
return Err(plugin_err!(
"Invalid source, only <file>tar (gz)</file> and <file>zip</file> archives are supported."
"Invalid source, only <file>.tar</file>, <file>.tar.gz</file>, and <file>.zip</file> archives are supported."
));
}

Expand All @@ -67,7 +67,12 @@ pub fn execute_extension(Json(input): Json<ExecuteExtensionInput>) -> FnResult<(
);

// Convert the provided output into a virtual file path.
let dest_dir = virtual_path!(buf, input.context.get_absolute_path(args.dest));
let dest_dir = virtual_path!(
buf,
input
.context
.get_absolute_path(args.dest.as_deref().unwrap_or_default())
);

if dest_dir.exists() && dest_dir.is_file() {
return Err(plugin_err!(
Expand All @@ -76,12 +81,6 @@ pub fn execute_extension(Json(input): Json<ExecuteExtensionInput>) -> FnResult<(
));
}

if src_file == dest_dir {
return Err(plugin_err!(
"Source and destination cannot point to the same location."
));
}

fs::create_dir_all(&dest_dir)?;

host_log!(
Expand All @@ -102,9 +101,11 @@ pub fn execute_extension(Json(input): Json<ExecuteExtensionInput>) -> FnResult<(
}

// Unpack the files
archive
.unpack_from_ext()
.map_err(|error| anyhow!("{error}"))?;
if let Err(error) = archive.unpack_from_ext() {
host_log!(stdout, "{}", error.to_string());

return Err(plugin_err!("{error}"));
};

host_log!(stdout, "Unpacked archive!");

Expand Down
Binary file added crates/unpack/tests/__fixtures__/tar/archive.tar
Binary file not shown.
Binary file added crates/unpack/tests/__fixtures__/tar/archive.tar.gz
Binary file not shown.
Binary file added crates/unpack/tests/__fixtures__/zip/archive.zip
Binary file not shown.
143 changes: 141 additions & 2 deletions crates/unpack/tests/unpack_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use moon_pdk_test_utils::{create_extension, ExecuteExtensionInput};
use starbase_sandbox::create_empty_sandbox;
use std::fs;
use starbase_sandbox::{create_empty_sandbox, create_sandbox};

mod unpack {
use super::*;
Expand All @@ -16,4 +15,144 @@ mod unpack {
context: plugin.create_context(sandbox.path()),
});
}

#[test]
#[should_panic(
expected = "Invalid source, only .tar, .tar.gz, and .zip archives are supported."
)]
fn errors_if_unsupported_ext() {
let sandbox = create_empty_sandbox();
let plugin = create_extension("test", sandbox.path());

plugin.execute_extension(ExecuteExtensionInput {
args: vec![
"--src".into(),
"https://raw.githubusercontent.com/moonrepo/moon/master/README.md".into(),
],
context: plugin.create_context(sandbox.path()),
});
}

#[test]
#[should_panic(expected = "must be a valid file")]
fn errors_if_src_file_missing() {
let sandbox = create_empty_sandbox();
let plugin = create_extension("test", sandbox.path());

plugin.execute_extension(ExecuteExtensionInput {
args: vec!["--src".into(), "./some/archive.zip".into()],
context: plugin.create_context(sandbox.path()),
});
}

#[test]
#[should_panic(expected = "must be a directory, found a file")]
fn errors_if_dest_is_a_file() {
let sandbox = create_empty_sandbox();
let plugin = create_extension("test", sandbox.path());

sandbox.create_file("dest", "file");

plugin.execute_extension(ExecuteExtensionInput {
args: vec![
"--src".into(),
"https://github.com/moonrepo/moon/archive/refs/tags/v1.0.0.zip".into(),
"--dest".into(),
"./dest".into(),
],
context: plugin.create_context(sandbox.path()),
});
}

#[test]
fn unpacks_tar() {
let sandbox = create_sandbox("tar");
let plugin = create_extension("test", sandbox.path());

plugin.execute_extension(ExecuteExtensionInput {
args: vec![
"--src".into(),
"./archive.tar".into(),
"--dest".into(),
"./out".into(),
],
context: plugin.create_context(sandbox.path()),
});

assert!(sandbox.path().join("out/file.txt").exists());
}

#[test]
fn unpacks_tar_gz() {
let sandbox = create_sandbox("tar");
let plugin = create_extension("test", sandbox.path());

plugin.execute_extension(ExecuteExtensionInput {
args: vec![
"--src".into(),
"./archive.tar.gz".into(),
"--dest".into(),
"./out".into(),
],
context: plugin.create_context(sandbox.path()),
});

assert!(sandbox.path().join("out/file.txt").exists());
}

#[test]
fn unpacks_zip() {
let sandbox = create_sandbox("zip");
let plugin = create_extension("test", sandbox.path());

plugin.execute_extension(ExecuteExtensionInput {
args: vec![
"--src".into(),
"./archive.zip".into(),
"--dest".into(),
"./out".into(),
],
context: plugin.create_context(sandbox.path()),
});

assert!(sandbox.path().join("out/file.txt").exists());
}

// #[test]
// fn downloads_and_unpacks_tar() {
// let sandbox = create_empty_sandbox();
// let plugin = create_extension("test", sandbox.path());

// plugin.execute_extension(ExecuteExtensionInput {
// args: vec![
// "--src".into(),
// "https://github.com/moonrepo/moon/archive/refs/tags/v1.0.0.tar.gz".into(),
// "--dest".into(),
// "./out".into(),
// ],
// context: plugin.create_context(sandbox.path()),
// });

// assert!(sandbox.path().join(".moon/temp/v1.0.0.zip").exists());
// assert!(sandbox.path().join("out/README.md").exists());
// }

// #[test]
// fn downloads_and_unpacks_zip() {
// let sandbox = create_empty_sandbox();
// let plugin = create_extension("test", sandbox.path());

// plugin.execute_extension(ExecuteExtensionInput {
// args: vec![
// "--src".into(),
// "https://github.com/moonrepo/moon/archive/refs/tags/v1.0.0.zip".into(),
// "--dest".into(),
// "./out".into(),
// ],
// context: plugin.create_context(sandbox.path()),
// });

// assert!(sandbox.path().join(".moon/temp/v1.0.0.zip").exists());
// assert!(sandbox.path().join("out/README.md").exists());
// }
}

0 comments on commit 56ac648

Please sign in to comment.