Skip to content

Commit

Permalink
Fix wasm testing
Browse files Browse the repository at this point in the history
wasm-bindgen-test and wasm-bindgen need to be different versions (0.3 and 0.2,
respectively), otherwise no tests are found([1], [2]);

This also fixes the test to and ci runner to just test in the current env/tz,
thereby requiring that js_sys returns the same timezone as the host system
thinks it is, but not otherwise trying to set the TZ.

[1]: https://users.rust-lang.org/t/wasm-bindgen-test-and-no-tests-to-run/43444
[2]: rustwasm/wasm-bindgen#2123
  • Loading branch information
quodlibetor committed Jul 5, 2020
1 parent 545fd9f commit b6d7bac
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ criterion = { version = "0.3" }
doc-comment = { version = "0.3" }

[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dev-dependencies]
wasm-bindgen-test = "0.2"
wasm-bindgen-test = "0.3"

[package.metadata.docs.rs]
features = ["serde"]
Expand Down
5 changes: 2 additions & 3 deletions ci/github.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ test_wasm() {

test_wasm_simple() {
now=$(date +%s)
for tz in "${TEST_TZS[@]}"; do
runt env TZ="$tz" NOW="$now" wasm-pack test --node -- --features wasmbind
done
tz=$(date +%z)
runt env TZ="$tz" NOW="$now" wasm-pack test --node -- --features wasmbind
}

main "$@"
22 changes: 16 additions & 6 deletions tests/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@ mod test {
let local: DateTime<Local> = Local::now();

// Ensure time fetched is correct
let actual = Utc.datetime_from_str(env!("NOW"), "%s").unwrap();
let actual = Utc.datetime_from_str(&env!("NOW"), "%s").unwrap();
assert!(utc - actual < chrono::Duration::minutes(5));

let tz = env!("TZ");
eprintln!("testing with tz={}", tz);

// Ensure offset retrieved when getting local time is correct
let expected_offset = match env!("TZ") {
let expected_offset = match tz {
"ACST-9:30" => FixedOffset::east(19 * 30 * 60),
"Asia/Katmandu" => FixedOffset::east(23 * 15 * 60), // No DST thankfully
"EST4" => FixedOffset::east(-4 * 60 * 60),
"UTC0" => FixedOffset::east(0),
_ => panic!("unexpected TZ"),
"EDT" | "EST4" | "-0400" => FixedOffset::east(-4 * 60 * 60),
"EST" | "-0500" => FixedOffset::east(-5 * 60 * 60),
"UTC0" | "+0000" => FixedOffset::east(0),
tz => panic!("unexpected TZ {}", tz),
};
assert_eq!(&expected_offset, local.offset());
assert_eq!(
&expected_offset,
local.offset(),
"expected: {:?} local: {:?}",
expected_offset,
local.offset(),
);
}
}

0 comments on commit b6d7bac

Please sign in to comment.