Open CryZe opened 6 years ago
Hmm... can you explain this in more detail? Or preferably make a minimal reproduction of the problem.
Assuming a crate like this:
#[macro_use]
extern crate stdweb;
#[cfg(not(test))]
fn a() {
js! { console.log( "NOT TEST" ) }
}
#[cfg(test)]
fn a() {
js! { console.log( "TEST" ) }
}
fn main() {
a();
}
#[test]
fn foo() {
a();
}
it seems to work fine:
$ cargo web test --target=wasm32-unknown-unknown --nodejs
warning: debug builds on the wasm32-unknown-unknown are currently totally broken
forcing a release build
Compiling foobar v0.1.0 (file:///tmp/foobar)
Finished release [optimized] target(s) in 0.50s
Garbage collecting "foobar-78aab42fb12bbd3b.wasm"...
Processing "foobar-78aab42fb12bbd3b.wasm"...
Finished processing of "foobar-78aab42fb12bbd3b.wasm"!
TEST
All tests passed!
$ cargo web build --target=wasm32-unknown-unknown && cd target/wasm32-unknown-unknown/release && node foobar.js
warning: debug builds on the wasm32-unknown-unknown are currently totally broken
forcing a release build
Finished release [optimized] target(s) in 0.02s
NOT TEST
This may actually be a cargo / rustc bug. But this is what happens:
If I comment out the whole non-test version and only leave the cfg(test) one, then it can't even compile it, indicating that cfg(test) doesn't actually get set properly:
The crate is here: https://github.com/LiveSplit/livesplit-core You should be able to replicate this easily on the master branch
@CryZe Hmm.... looks like it works for me?
livesplit
de920cd90a3a718e37697f709d2f6fcdbf6cc9d1
rustc
1.29.0-nightly (64f7de921 2018-07-12)`cargo-web
from master
diff --git a/src/platform/wasm/mod.rs b/src/platform/wasm/mod.rs
index f7a7176..10a1cdb 100644
--- a/src/platform/wasm/mod.rs
+++ b/src/platform/wasm/mod.rs
@@ -36,10 +36,12 @@ struct FFIDateTime {
nsecs: u32,
}
+#[cfg(not(test))]
extern "C" {
fn Date_now(data: *mut FFIDateTime);
}
+#[cfg(not(test))]
pub fn utc_now() -> DateTime<Utc> {
unsafe {
let mut date_time: FFIDateTime = uninitialized();
@@ -50,3 +52,8 @@ pub fn utc_now() -> DateTime<Utc> {
)
}
}
+
+#[cfg(test)]
+pub fn utc_now() -> DateTime<Utc> {
+ DateTime::from_utc(NaiveDateTime::from_timestamp(0, 0), Utc)
+}
diff --git a/src/platform/wasm/time.rs b/src/platform/wasm/time.rs
index 60b3b8a..a7800ac 100644
--- a/src/platform/wasm/time.rs
+++ b/src/platform/wasm/time.rs
@@ -1,6 +1,7 @@
use ordered_float::OrderedFloat;
use std::ops::Sub;
+#[cfg(not(test))]
extern "C" {
fn Instant_now() -> f64;
}
@@ -9,9 +10,15 @@ extern "C" {
pub struct Instant(OrderedFloat<f64>);
impl Instant {
+ #[cfg(not(test))]
pub fn now() -> Self {
Instant(OrderedFloat(unsafe { Instant_now() }))
}
+
+ #[cfg(test)]
+ pub fn now() -> Self {
+ Instant(OrderedFloat(0.0))
+ }
}
impl Sub for Instant {
Although some of your tests are panicking anyway:
DEBUG 2018-07-13T23:32:06Z: cargo_web::cmd_test: Launching: "node" "/tmp/livesplit-core/target/wasm32-unknown-unknown/release/parsing-eedda1ef2254ac80.js"
wasm://wasm/00206946:950
^
RuntimeError: unreachable
at __rust_start_panic (wasm-function[949]:33)
at rust_panic (wasm-function[944]:30)
at std::panicking::rust_panic_with_hook::h7d68e2d89a27a696 (wasm-function[939]:444)
at std::panicking::continue_panic_fmt::h22bbe3a13aa1e394 (wasm-function[937]:122)
at rust_begin_unwind (wasm-function[936]:3)
at core::panicking::panic_fmt::hb5d93446f63a2163 (wasm-function[1078]:70)
at core::result::unwrap_failed::he795fa94778e8a55 (wasm-function[44]:130)
at _$LT$core..result..Result$LT$T$C$$u20$E$GT$$GT$::unwrap::hbecdab8a7a315d7e (.llvm.16853545531359078048) (wasm-function[73]:34)
at parsing::parse::file::hd6ab1907a1ef0c02 (.llvm.16853545531359078048) (wasm-function[75]:28)
at parsing::parse::parse_llanfair2::h263da64e05b435ef (.llvm.16853545531359078048) (wasm-function[76]:5)
DEBUG 2018-07-13T23:32:06Z: cargo_web::cmd_test: Status: ExecutionStatus { status: Some(1) }
I tried integrating cargo web test into my project. However I have some extern functions which I use to communicate with browser APIs. This causes problems with cargo web test, as those are not provided obviously. That's fine to me, so I just tried cfg(test)'ing them out, but apparently cargo web test doesn't actually trigger cfg(test) properly, and at least partially compiles with cfg(not(test)), so I can't really use cargo web test.