Skip to content

Commit ec8e610

Browse files
committed
tests: Improve testing infrastructure
Move common test code to a reusable module, and have many different test files use this common module. This then eliminates the need for many different cargo features, and makes the code much more readable. We also add testing for custom/cpurand, making sure it builds/links on our various x86 targets. Signed-off-by: Joe Richey <[email protected]>
1 parent 9f87c44 commit ec8e610

File tree

11 files changed

+50
-58
lines changed

11 files changed

+50
-58
lines changed

.travis.yml

+5-17
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ matrix:
7676
- cargo web test --target=wasm32-unknown-unknown
7777
# wasm-bindgen tests (Node, Firefox, Chrome)
7878
- cd ../wasm-bindgen
79-
- cargo test --target wasm32-unknown-unknown
80-
- GECKODRIVER=$HOME/geckodriver cargo test --target wasm32-unknown-unknown --features=test-in-browser
81-
- CHROMEDRIVER=$HOME/chromedriver cargo test --target wasm32-unknown-unknown --features=test-in-browser
79+
- cargo test --target wasm32-unknown-unknown --test node
80+
- GECKODRIVER=$HOME/geckodriver cargo test --target wasm32-unknown-unknown --test web
81+
- CHROMEDRIVER=$HOME/chromedriver cargo test --target wasm32-unknown-unknown --test web
8282

8383
- &nightly_and_docs
8484
name: "Linux, nightly, docs"
@@ -122,20 +122,8 @@ matrix:
122122
- rustup component add rust-src
123123
- cargo install cargo-xbuild || true
124124
script:
125-
- cargo build --target=x86_64-sun-solaris
126-
- cargo build --target=x86_64-unknown-cloudabi
127-
- cargo build --target=x86_64-unknown-freebsd
128-
- cargo build --target=x86_64-fuchsia
129-
- cargo build --target=x86_64-unknown-netbsd
130-
- cargo build --target=x86_64-unknown-redox
131-
- cargo build --target=x86_64-fortanix-unknown-sgx
132-
- cargo xbuild --target=x86_64-unknown-uefi
133-
- cargo xbuild --target=x86_64-unknown-hermit
134-
- cargo xbuild --target=x86_64-unknown-l4re-uclibc
135-
- cargo xbuild --target=x86_64-uwp-windows-gnu
136-
- cargo xbuild --target=x86_64-wrs-vxworks
137-
# also test minimum dependency versions are usable
138-
- cargo generate-lockfile -Z minimal-versions
125+
# We test that getrandom and cpurand build for all x86 targets
126+
- cd custom/cpurand
139127
- cargo build --target=x86_64-sun-solaris
140128
- cargo build --target=x86_64-unknown-cloudabi
141129
- cargo build --target=x86_64-unknown-freebsd

custom/cpurand/Cargo.toml

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ repository = "https://github.com/rust-random/getrandom"
1010
categories = ["hardware-support", "no-std"]
1111

1212
[dependencies]
13-
cfg-if = "0.1.2"
1413
getrandom = { path = "../..", version = "0.2", features = ["custom"] }
15-
16-
[[test]]
17-
name = "common"
18-
path = "../../tests/common.rs"
14+
cfg-if = "0.1.2"
1915

2016
[package.metadata.docs.rs]
2117
default-target = "x86_64-unknown-linux"

custom/cpurand/tests/cpurand.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Call cpurand so that it will be invoked on std targets (like Linux).
2+
use cpurand_getrandom::cpurand as getrandom;
3+
#[path = "../../../tests/common/mod.rs"]
4+
mod common;

custom/cpurand/tests/normal.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Explicitly use the Custom RNG crate to link it in.
2+
use cpurand_getrandom as _;
3+
4+
use getrandom::getrandom;
5+
#[path = "../../../tests/common/mod.rs"]
6+
mod common;

custom/stdweb/Cargo.toml

+1-10
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,5 @@ categories = ["wasm"]
1313
getrandom = { path = "../..", version = "0.2", features = ["custom"] }
1414
stdweb = "0.4.18"
1515

16-
# Test-only features allowing us to reuse most of the code in common.rs
17-
[features]
18-
default = ["test-stdweb"]
19-
test-stdweb = []
20-
21-
[[test]]
22-
name = "common"
23-
path = "../../tests/common.rs"
24-
2516
[package.metadata.docs.rs]
26-
default-target = "wasm32-unknown-unknown"
17+
default-target = "wasm32-unknown-unknown"

custom/stdweb/tests/test.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Explicitly use the Custom RNG crate to link it in.
2+
use stdweb_getrandom as _;
3+
4+
use getrandom::getrandom;
5+
use test;
6+
#[path = "../../../tests/common/mod.rs"]
7+
mod common;

custom/wasm-bindgen/Cargo.toml

+3-13
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,10 @@ categories = ["wasm"]
1111

1212
[dependencies]
1313
getrandom = { path = "../..", version = "0.2", features = ["custom"] }
14-
wasm-bindgen = "0.2.29"
14+
wasm-bindgen = "0.2.46"
1515

1616
[dev-dependencies]
17-
wasm-bindgen-test = "0.2"
18-
19-
# Test-only features allowing us to reuse most of the code in common.rs
20-
[features]
21-
default = ["test-bindgen"]
22-
test-bindgen = []
23-
test-in-browser = ["test-bindgen"]
24-
25-
[[test]]
26-
name = "common"
27-
path = "../../tests/common.rs"
17+
wasm-bindgen-test = "0.2.46"
2818

2919
[package.metadata.docs.rs]
30-
default-target = "wasm32-unknown-unknown"
20+
default-target = "wasm32-unknown-unknown"

custom/wasm-bindgen/tests/node.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Explicitly use the Custom RNG crate to link it in.
2+
use wasm_bindgen_getrandom as _;
3+
4+
use getrandom::getrandom;
5+
use wasm_bindgen_test::wasm_bindgen_test as test;
6+
#[path = "../../../tests/common/mod.rs"]
7+
mod common;

custom/wasm-bindgen/tests/web.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Explicitly use the Custom RNG crate to link it in.
2+
use wasm_bindgen_getrandom as _;
3+
4+
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
5+
6+
use getrandom::getrandom;
7+
use wasm_bindgen_test::wasm_bindgen_test as test;
8+
#[path = "../../../tests/common/mod.rs"]
9+
mod common;

tests/common.rs tests/common/mod.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
// Explicitly use the Custom RNG crates to link them in.
22
#[cfg(feature = "test-stdweb")]
33
use stdweb_getrandom as _;
4-
#[cfg(feature = "test-bindgen")]
5-
use wasm_bindgen_getrandom as _;
64

7-
#[cfg(feature = "test-bindgen")]
8-
use wasm_bindgen_test::*;
5+
use super::getrandom;
6+
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
7+
use super::test;
98

10-
use getrandom::getrandom;
11-
12-
#[cfg(feature = "test-in-browser")]
13-
wasm_bindgen_test_configure!(run_in_browser);
14-
15-
#[cfg_attr(feature = "test-bindgen", wasm_bindgen_test)]
169
#[test]
1710
fn test_zero() {
1811
// Test that APIs are happy with zero-length requests
1912
getrandom(&mut [0u8; 0]).unwrap();
2013
}
2114

22-
#[cfg_attr(feature = "test-bindgen", wasm_bindgen_test)]
2315
#[test]
2416
fn test_diff() {
2517
let mut v1 = [0u8; 1000];
@@ -37,14 +29,14 @@ fn test_diff() {
3729
assert!(n_diff_bits >= v1.len() as u32);
3830
}
3931

40-
#[cfg_attr(feature = "test-bindgen", wasm_bindgen_test)]
4132
#[test]
4233
fn test_huge() {
4334
let mut huge = [0u8; 100_000];
4435
getrandom(&mut huge).unwrap();
4536
}
4637

47-
#[cfg(any(unix, windows, target_os = "redox", target_os = "fuchsia"))]
38+
// On WASM, the thread API always fails/panics
39+
#[cfg(all(feature = "std", not(target_arch = "wasm32")))]
4840
#[test]
4941
fn test_multithreading() {
5042
use std::sync::mpsc::channel;

tests/test.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
use getrandom::getrandom;
2+
mod common;

0 commit comments

Comments
 (0)