Skip to content

Commit

Permalink
tests: Improve testing infrastructure
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
josephlr committed Feb 13, 2020
1 parent 9f87c44 commit ec8e610
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 58 deletions.
22 changes: 5 additions & 17 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ matrix:
- cargo web test --target=wasm32-unknown-unknown
# wasm-bindgen tests (Node, Firefox, Chrome)
- cd ../wasm-bindgen
- cargo test --target wasm32-unknown-unknown
- GECKODRIVER=$HOME/geckodriver cargo test --target wasm32-unknown-unknown --features=test-in-browser
- CHROMEDRIVER=$HOME/chromedriver cargo test --target wasm32-unknown-unknown --features=test-in-browser
- cargo test --target wasm32-unknown-unknown --test node
- GECKODRIVER=$HOME/geckodriver cargo test --target wasm32-unknown-unknown --test web
- CHROMEDRIVER=$HOME/chromedriver cargo test --target wasm32-unknown-unknown --test web

- &nightly_and_docs
name: "Linux, nightly, docs"
Expand Down Expand Up @@ -122,20 +122,8 @@ matrix:
- rustup component add rust-src
- cargo install cargo-xbuild || true
script:
- cargo build --target=x86_64-sun-solaris
- cargo build --target=x86_64-unknown-cloudabi
- cargo build --target=x86_64-unknown-freebsd
- cargo build --target=x86_64-fuchsia
- cargo build --target=x86_64-unknown-netbsd
- cargo build --target=x86_64-unknown-redox
- cargo build --target=x86_64-fortanix-unknown-sgx
- cargo xbuild --target=x86_64-unknown-uefi
- cargo xbuild --target=x86_64-unknown-hermit
- cargo xbuild --target=x86_64-unknown-l4re-uclibc
- cargo xbuild --target=x86_64-uwp-windows-gnu
- cargo xbuild --target=x86_64-wrs-vxworks
# also test minimum dependency versions are usable
- cargo generate-lockfile -Z minimal-versions
# We test that getrandom and cpurand build for all x86 targets
- cd custom/cpurand
- cargo build --target=x86_64-sun-solaris
- cargo build --target=x86_64-unknown-cloudabi
- cargo build --target=x86_64-unknown-freebsd
Expand Down
6 changes: 1 addition & 5 deletions custom/cpurand/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ repository = "https://github.com/rust-random/getrandom"
categories = ["hardware-support", "no-std"]

[dependencies]
cfg-if = "0.1.2"
getrandom = { path = "../..", version = "0.2", features = ["custom"] }

[[test]]
name = "common"
path = "../../tests/common.rs"
cfg-if = "0.1.2"

[package.metadata.docs.rs]
default-target = "x86_64-unknown-linux"
4 changes: 4 additions & 0 deletions custom/cpurand/tests/cpurand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Call cpurand so that it will be invoked on std targets (like Linux).
use cpurand_getrandom::cpurand as getrandom;
#[path = "../../../tests/common/mod.rs"]
mod common;
6 changes: 6 additions & 0 deletions custom/cpurand/tests/normal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Explicitly use the Custom RNG crate to link it in.
use cpurand_getrandom as _;

use getrandom::getrandom;
#[path = "../../../tests/common/mod.rs"]
mod common;
11 changes: 1 addition & 10 deletions custom/stdweb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,5 @@ categories = ["wasm"]
getrandom = { path = "../..", version = "0.2", features = ["custom"] }
stdweb = "0.4.18"

# Test-only features allowing us to reuse most of the code in common.rs
[features]
default = ["test-stdweb"]
test-stdweb = []

[[test]]
name = "common"
path = "../../tests/common.rs"

[package.metadata.docs.rs]
default-target = "wasm32-unknown-unknown"
default-target = "wasm32-unknown-unknown"
7 changes: 7 additions & 0 deletions custom/stdweb/tests/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Explicitly use the Custom RNG crate to link it in.
use stdweb_getrandom as _;

use getrandom::getrandom;
use test;
#[path = "../../../tests/common/mod.rs"]
mod common;
16 changes: 3 additions & 13 deletions custom/wasm-bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,10 @@ categories = ["wasm"]

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

[dev-dependencies]
wasm-bindgen-test = "0.2"

# Test-only features allowing us to reuse most of the code in common.rs
[features]
default = ["test-bindgen"]
test-bindgen = []
test-in-browser = ["test-bindgen"]

[[test]]
name = "common"
path = "../../tests/common.rs"
wasm-bindgen-test = "0.2.46"

[package.metadata.docs.rs]
default-target = "wasm32-unknown-unknown"
default-target = "wasm32-unknown-unknown"
7 changes: 7 additions & 0 deletions custom/wasm-bindgen/tests/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Explicitly use the Custom RNG crate to link it in.
use wasm_bindgen_getrandom as _;

use getrandom::getrandom;
use wasm_bindgen_test::wasm_bindgen_test as test;
#[path = "../../../tests/common/mod.rs"]
mod common;
9 changes: 9 additions & 0 deletions custom/wasm-bindgen/tests/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Explicitly use the Custom RNG crate to link it in.
use wasm_bindgen_getrandom as _;

wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

use getrandom::getrandom;
use wasm_bindgen_test::wasm_bindgen_test as test;
#[path = "../../../tests/common/mod.rs"]
mod common;
18 changes: 5 additions & 13 deletions tests/common.rs → tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
// Explicitly use the Custom RNG crates to link them in.
#[cfg(feature = "test-stdweb")]
use stdweb_getrandom as _;
#[cfg(feature = "test-bindgen")]
use wasm_bindgen_getrandom as _;

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

use getrandom::getrandom;

#[cfg(feature = "test-in-browser")]
wasm_bindgen_test_configure!(run_in_browser);

#[cfg_attr(feature = "test-bindgen", wasm_bindgen_test)]
#[test]
fn test_zero() {
// Test that APIs are happy with zero-length requests
getrandom(&mut [0u8; 0]).unwrap();
}

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

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

#[cfg(any(unix, windows, target_os = "redox", target_os = "fuchsia"))]
// On WASM, the thread API always fails/panics
#[cfg(all(feature = "std", not(target_arch = "wasm32")))]
#[test]
fn test_multithreading() {
use std::sync::mpsc::channel;
Expand Down
2 changes: 2 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
use getrandom::getrandom;
mod common;

0 comments on commit ec8e610

Please sign in to comment.