Skip to content

Commit 0c45215

Browse files
Merge pull request #517 from drager/binary-install-tests
test(binaryinstall): Add initial tests
2 parents 70dba37 + ae836f3 commit 0c45215

File tree

8 files changed

+448
-15
lines changed

8 files changed

+448
-15
lines changed

.appveyor.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ build: false
1717
test_script:
1818
- cargo test --release --tests --locked
1919
- cargo test --release --doc
20+
- cd binary-install
21+
- cargo test
22+
- cd ..
2023

2124
before_deploy:
2225
- ps: |

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ matrix:
4747
install:
4848
- *INSTALL_NODE_VIA_NVM
4949
script:
50-
- cargo test --locked
50+
- cargo test --all --locked
5151
- rustup component add rustfmt-preview
5252
- cargo fmt --version
5353
- cargo fmt --all -- --check

binary-install/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ is_executable = "0.1.2"
1818
siphasher = "0.2.3"
1919
tar = "0.4.16"
2020
zip = "0.5.0"
21+
22+
[dev-dependencies]
23+
tempfile = "3.0.5"

binary-install/src/lib.rs

Lines changed: 88 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ use std::path::{Path, PathBuf};
2323

2424
/// Global cache for wasm-pack, currently containing binaries downloaded from
2525
/// urls like wasm-bindgen and such.
26+
#[derive(Debug)]
2627
pub struct Cache {
2728
destination: PathBuf,
2829
}
2930

3031
/// Representation of a downloaded tarball/zip
32+
#[derive(Debug)]
3133
pub struct Download {
3234
root: PathBuf,
3335
}
@@ -81,22 +83,10 @@ impl Cache {
8183
binaries: &[&str],
8284
url: &str,
8385
) -> Result<Option<Download>, Error> {
84-
let mut hasher = SipHasher13::new();
85-
url.hash(&mut hasher);
86-
let result = hasher.finish();
87-
let hex = hex::encode(&[
88-
(result >> 0) as u8,
89-
(result >> 8) as u8,
90-
(result >> 16) as u8,
91-
(result >> 24) as u8,
92-
(result >> 32) as u8,
93-
(result >> 40) as u8,
94-
(result >> 48) as u8,
95-
(result >> 56) as u8,
96-
]);
97-
let dirname = format!("{}-{}", name, hex);
86+
let dirname = hashed_dirname(url, name);
9887

9988
let destination = self.destination.join(&dirname);
89+
10090
if destination.exists() {
10191
return Ok(Some(Download { root: destination }));
10292
}
@@ -270,3 +260,87 @@ fn curl(url: &str) -> Result<Vec<u8>, Error> {
270260
)
271261
}
272262
}
263+
264+
fn hashed_dirname(url: &str, name: &str) -> String {
265+
let mut hasher = SipHasher13::new();
266+
url.hash(&mut hasher);
267+
let result = hasher.finish();
268+
let hex = hex::encode(&[
269+
(result >> 0) as u8,
270+
(result >> 8) as u8,
271+
(result >> 16) as u8,
272+
(result >> 24) as u8,
273+
(result >> 32) as u8,
274+
(result >> 40) as u8,
275+
(result >> 48) as u8,
276+
(result >> 56) as u8,
277+
]);
278+
format!("{}-{}", name, hex)
279+
}
280+
281+
#[cfg(test)]
282+
mod tests {
283+
use super::*;
284+
285+
#[test]
286+
fn it_returns_same_hash_for_same_name_and_url() {
287+
let name = "wasm-pack";
288+
let url = "http://localhost:7878/wasm-pack-v0.6.0.tar.gz";
289+
290+
let first = hashed_dirname(url, name);
291+
let second = hashed_dirname(url, name);
292+
293+
assert!(!first.is_empty());
294+
assert!(!second.is_empty());
295+
assert_eq!(first, second);
296+
}
297+
298+
#[test]
299+
fn it_returns_different_hashes_for_different_urls() {
300+
let name = "wasm-pack";
301+
let url = "http://localhost:7878/wasm-pack-v0.5.1.tar.gz";
302+
let second_url = "http://localhost:7878/wasm-pack-v0.6.0.tar.gz";
303+
304+
let first = hashed_dirname(url, name);
305+
let second = hashed_dirname(second_url, name);
306+
307+
assert_ne!(first, second);
308+
}
309+
310+
#[test]
311+
fn it_returns_cache_dir() {
312+
let name = "wasm-pack";
313+
let cache = Cache::new(name);
314+
315+
let expected = dirs::cache_dir()
316+
.unwrap()
317+
.join(PathBuf::from(".".to_owned() + name));
318+
319+
assert!(cache.is_ok());
320+
assert_eq!(cache.unwrap().destination, expected);
321+
}
322+
323+
#[test]
324+
fn it_returns_destination_if_binary_already_exists() {
325+
use std::fs;
326+
327+
let binary_name = "wasm-pack";
328+
let binaries = vec![binary_name];
329+
330+
let dir = tempfile::TempDir::new().unwrap();
331+
let cache = Cache::at(dir.path());
332+
let url = &format!("{}/{}.tar.gz", "http://localhost:7878", binary_name);
333+
334+
let dirname = hashed_dirname(&url, &binary_name);
335+
let full_path = dir.path().join(dirname);
336+
337+
// Create temporary directory and binary to simulate that
338+
// a cached binary already exists.
339+
fs::create_dir_all(full_path).unwrap();
340+
341+
let dl = cache.download(true, binary_name, &binaries, url);
342+
343+
assert!(dl.is_ok());
344+
assert!(dl.unwrap().is_some())
345+
}
346+
}

binary-install/tests/all/cache.rs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
use binary_install::Cache;
2+
use std::path::Path;
3+
use utils;
4+
5+
#[test]
6+
fn it_returns_none_if_install_is_not_permitted() {
7+
let binary_name = "wasm-pack";
8+
let binaries = vec![binary_name];
9+
10+
let dir = tempfile::TempDir::new().unwrap();
11+
let cache = Cache::at(dir.path());
12+
13+
let dl = cache.download(
14+
false,
15+
binary_name,
16+
&binaries,
17+
&format!("{}/{}.tar.gz", "", binary_name),
18+
);
19+
20+
assert!(dl.is_ok());
21+
assert!(dl.unwrap().is_none())
22+
}
23+
24+
#[test]
25+
fn it_downloads_tarball() {
26+
let binary_name = "wasm-pack";
27+
let binaries = vec![binary_name];
28+
29+
// Create a temporary tarball.
30+
let tarball = utils::create_tarball(binary_name).ok();
31+
32+
// Spin up a local TcpListener.
33+
let server_port = utils::start_server(tarball, None).recv().unwrap();
34+
35+
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port);
36+
37+
let dir = tempfile::TempDir::new().unwrap();
38+
let cache = Cache::at(dir.path());
39+
40+
let dl = cache.download(
41+
true,
42+
binary_name,
43+
&binaries,
44+
&format!("{}/{}.tar.gz", &url, binary_name),
45+
);
46+
47+
assert!(dl.is_ok());
48+
assert!(dl.unwrap().is_some())
49+
}
50+
51+
#[test]
52+
fn it_returns_error_when_it_failed_to_download() {
53+
let server_port = 7881;
54+
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port);
55+
let binary_name = "wasm-pack";
56+
let binaries = vec![binary_name];
57+
58+
let dir = tempfile::TempDir::new().unwrap();
59+
let cache = Cache::at(dir.path());
60+
let full_url = &format!("{}/{}.tar.gz", &url, binary_name);
61+
62+
let dl = cache.download(true, binary_name, &binaries, full_url);
63+
64+
assert!(dl.is_err());
65+
assert_eq!(
66+
&format!("failed to download from {}", full_url),
67+
&format!("{}", dl.unwrap_err())
68+
);
69+
}
70+
71+
#[test]
72+
fn it_returns_error_when_it_failed_to_extract_tarball() {
73+
let binary_name = "wasm-pack";
74+
let binaries = vec![binary_name];
75+
76+
let dir = tempfile::TempDir::new().unwrap();
77+
let cache = Cache::at(dir.path());
78+
79+
// Spin up a local TcpListener.
80+
let server_port = utils::start_server(None, None).recv().unwrap();
81+
82+
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port);
83+
let full_url = &format!("{}/{}.tar.gz", &url, binary_name);
84+
85+
let dl = cache.download(true, binary_name, &binaries, full_url);
86+
87+
assert!(dl.is_err());
88+
assert_eq!(
89+
&format!("failed to extract tarball from {}", full_url),
90+
&format!("{}", dl.unwrap_err())
91+
);
92+
}
93+
94+
#[test]
95+
fn it_returns_error_when_it_failed_to_extract_zip() {
96+
let binary_name = "wasm-pack";
97+
let binaries = vec![binary_name];
98+
99+
let dir = tempfile::TempDir::new().unwrap();
100+
let cache = Cache::at(dir.path());
101+
102+
// Spin up a local TcpListener.
103+
let server_port = utils::start_server(None, None).recv().unwrap();
104+
105+
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port);
106+
let full_url = &format!("{}/{}.zip", &url, binary_name);
107+
108+
let dl = cache.download(true, binary_name, &binaries, full_url);
109+
110+
assert!(dl.is_err());
111+
assert_eq!(
112+
&format!("failed to extract zip from {}", full_url),
113+
&format!("{}", dl.unwrap_err())
114+
);
115+
}
116+
117+
#[test]
118+
#[should_panic(expected = "don't know how to extract http://localhost:7884/wasm-pack.bin")]
119+
fn it_panics_if_not_tarball_or_zip() {
120+
let server_port = 7884;
121+
let binary_name = "wasm-pack";
122+
let binaries = vec![binary_name];
123+
124+
let dir = tempfile::TempDir::new().unwrap();
125+
let cache = Cache::at(dir.path());
126+
127+
// Spin up a local TcpListener.
128+
utils::start_server(None, Some(server_port)).recv().unwrap();
129+
130+
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port);
131+
let full_url = &format!("{}/{}.bin", &url, binary_name);
132+
133+
let _ = cache.download(true, binary_name, &binaries, full_url);
134+
}
135+
136+
#[test]
137+
fn it_joins_path_with_destination() {
138+
let dir = tempfile::TempDir::new().unwrap();
139+
let cache = Cache::at(dir.path());
140+
141+
assert_eq!(dir.path().join("hello"), cache.join(Path::new("hello")));
142+
}

0 commit comments

Comments
 (0)