-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(binaryinstall): Add initial tests
- Loading branch information
Showing
6 changed files
with
381 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,6 @@ is_executable = "0.1.2" | |
siphasher = "0.2.3" | ||
tar = "0.4.16" | ||
zip = "0.5.0" | ||
|
||
[dev-dependencies] | ||
tempfile = "3.0.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
use binary_install::Cache; | ||
use std::path::Path; | ||
use utils; | ||
|
||
#[test] | ||
fn it_returns_none_if_install_is_not_permitted() { | ||
let binary_name = "wasm-pack"; | ||
let binaries = vec![binary_name]; | ||
|
||
let dir = tempfile::TempDir::new().unwrap(); | ||
let cache = Cache::at(dir.path()); | ||
|
||
let dl = cache.download( | ||
false, | ||
binary_name, | ||
&binaries, | ||
&format!("{}/{}.tar.gz", "", binary_name), | ||
); | ||
|
||
assert!(dl.is_ok()); | ||
assert!(dl.unwrap().is_none()) | ||
} | ||
|
||
#[test] | ||
fn it_downloads_tarball() { | ||
let server_port = 7880; | ||
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port); | ||
let binary_name = "wasm-pack"; | ||
let binaries = vec![binary_name]; | ||
|
||
// Create a temporary tarball. | ||
let tarball = utils::create_tarball(binary_name).ok(); | ||
|
||
// Spin up a local TcpListener. | ||
utils::start_server(server_port, tarball); | ||
|
||
let dir = tempfile::TempDir::new().unwrap(); | ||
let cache = Cache::at(dir.path()); | ||
|
||
let dl = cache.download( | ||
true, | ||
binary_name, | ||
&binaries, | ||
&format!("{}/{}.tar.gz", &url, binary_name), | ||
); | ||
|
||
assert!(dl.is_ok()); | ||
assert!(dl.unwrap().is_some()) | ||
} | ||
|
||
#[test] | ||
fn it_returns_error_when_it_failed_to_download() { | ||
let server_port = 7881; | ||
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port); | ||
let binary_name = "wasm-pack"; | ||
let binaries = vec![binary_name]; | ||
|
||
let dir = tempfile::TempDir::new().unwrap(); | ||
let cache = Cache::at(dir.path()); | ||
let full_url = &format!("{}/{}.tar.gz", &url, binary_name); | ||
|
||
let dl = cache.download(true, binary_name, &binaries, full_url); | ||
|
||
assert!(dl.is_err()); | ||
assert_eq!( | ||
&format!("failed to download from {}", full_url), | ||
&format!("{}", dl.unwrap_err()) | ||
); | ||
} | ||
|
||
#[test] | ||
fn it_returns_error_when_it_failed_to_extract_tarball() { | ||
let server_port = 7882; | ||
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port); | ||
let binary_name = "wasm-pack"; | ||
let binaries = vec![binary_name]; | ||
|
||
let dir = tempfile::TempDir::new().unwrap(); | ||
let cache = Cache::at(dir.path()); | ||
let full_url = &format!("{}/{}.tar.gz", &url, binary_name); | ||
|
||
// Spin up a local TcpListener. | ||
utils::start_server(server_port, None); | ||
|
||
let dl = cache.download(true, binary_name, &binaries, full_url); | ||
|
||
assert!(dl.is_err()); | ||
assert_eq!( | ||
&format!("failed to extract tarball from {}", full_url), | ||
&format!("{}", dl.unwrap_err()) | ||
); | ||
} | ||
|
||
#[test] | ||
fn it_returns_error_when_it_failed_to_extract_zip() { | ||
let server_port = 7883; | ||
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port); | ||
let binary_name = "wasm-pack"; | ||
let binaries = vec![binary_name]; | ||
|
||
let dir = tempfile::TempDir::new().unwrap(); | ||
let cache = Cache::at(dir.path()); | ||
let full_url = &format!("{}/{}.zip", &url, binary_name); | ||
|
||
// Spin up a local TcpListener. | ||
utils::start_server(server_port, None); | ||
|
||
let dl = cache.download(true, binary_name, &binaries, full_url); | ||
|
||
assert!(dl.is_err()); | ||
assert_eq!( | ||
&format!("failed to extract zip from {}", full_url), | ||
&format!("{}", dl.unwrap_err()) | ||
); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "don't know how to extract http://localhost:7884/wasm-pack.bin")] | ||
fn it_panics_if_not_tarball_or_zip() { | ||
let server_port = 7884; | ||
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port); | ||
let binary_name = "wasm-pack"; | ||
let binaries = vec![binary_name]; | ||
|
||
let dir = tempfile::TempDir::new().unwrap(); | ||
let cache = Cache::at(dir.path()); | ||
let full_url = &format!("{}/{}.bin", &url, binary_name); | ||
|
||
// Spin up a local TcpListener. | ||
utils::start_server(server_port, None); | ||
|
||
let _ = cache.download(true, binary_name, &binaries, full_url); | ||
} | ||
|
||
#[test] | ||
fn it_joins_path_with_destination() { | ||
let dir = tempfile::TempDir::new().unwrap(); | ||
let cache = Cache::at(dir.path()); | ||
|
||
assert_eq!(dir.path().join("hello"), cache.join(Path::new("hello"))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use binary_install::Download; | ||
use std::fs::OpenOptions; | ||
use std::os::unix::fs::OpenOptionsExt; | ||
|
||
#[test] | ||
#[cfg(unix)] | ||
fn it_returns_binary_name() { | ||
let binary_name = "wasm-pack"; | ||
|
||
let dir = tempfile::TempDir::new().unwrap(); | ||
let download = Download::at(dir.path()); | ||
|
||
let full_path = dir.path().join(binary_name); | ||
|
||
let mut options = OpenOptions::new(); | ||
options.create(true); | ||
options.write(true); | ||
|
||
// Make the "binary" an executable. | ||
options.mode(0o755); | ||
|
||
options.open(&full_path).unwrap(); | ||
|
||
let binary = download.binary(binary_name); | ||
|
||
assert!(binary.is_ok()); | ||
assert_eq!(full_path, binary.unwrap()); | ||
} | ||
|
||
#[test] | ||
fn it_bails_if_not_file() { | ||
let binary_name = "wasm-pack"; | ||
|
||
let dir = tempfile::TempDir::new().unwrap(); | ||
let download = Download::at(dir.path()); | ||
|
||
let full_path = dir.path().join(binary_name); | ||
|
||
let mut options = OpenOptions::new(); | ||
options.create(true); | ||
options.write(true); | ||
|
||
let binary = download.binary(binary_name); | ||
|
||
assert!(binary.is_err()); | ||
assert_eq!( | ||
format!("{} binary does not exist", full_path.to_str().unwrap()), | ||
binary.unwrap_err().to_string() | ||
); | ||
} | ||
|
||
#[test] | ||
fn it_bails_if_not_executable() { | ||
let binary_name = "wasm-pack"; | ||
|
||
let dir = tempfile::TempDir::new().unwrap(); | ||
let download = Download::at(dir.path()); | ||
|
||
let full_path = dir.path().join(binary_name); | ||
|
||
let mut options = OpenOptions::new(); | ||
options.create(true); | ||
options.write(true); | ||
|
||
options.open(&full_path).unwrap(); | ||
|
||
let binary = download.binary(binary_name); | ||
|
||
assert!(binary.is_err()); | ||
assert_eq!( | ||
format!("{} is not executable", full_path.to_str().unwrap()), | ||
binary.unwrap_err().to_string() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
extern crate binary_install; | ||
extern crate flate2; | ||
extern crate tar; | ||
|
||
mod cache; | ||
mod download; | ||
mod utils; |
Oops, something went wrong.