diff --git a/Cargo.lock b/Cargo.lock index 49ee02d..f715919 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -167,6 +167,12 @@ dependencies = [ "wasip2", ] +[[package]] +name = "glob" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280" + [[package]] name = "hashbrown" version = "0.16.0" @@ -267,6 +273,7 @@ dependencies = [ "once_cell_polyfill", "pathdiff", "snapbox", + "trybuild", ] [[package]] @@ -590,6 +597,15 @@ dependencies = [ "serde_core", ] +[[package]] +name = "serde_spanned" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2789234a13a53fc4be1b51ea1bab45a3c338bdb884862a257d10e5a74ae009e6" +dependencies = [ + "serde_core", +] + [[package]] name = "similar" version = "2.7.0" @@ -631,6 +647,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "target-triple" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ac9aa371f599d22256307c24a9d748c041e548cbf599f35d890f9d365361790" + [[package]] name = "tempfile" version = "3.23.0" @@ -644,6 +666,69 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "toml" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae2a4cf385da23d1d53bc15cdfa5c2109e93d8d362393c801e87da2f72f0e201" +dependencies = [ + "indexmap", + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + +[[package]] +name = "toml_datetime" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a197c0ec7d131bfc6f7e82c8442ba1595aeab35da7adbf05b6b73cd06a16b6be" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_parser" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b551886f449aa90d4fe2bdaa9f4a2577ad2dde302c61ecf262d80b116db95c10" +dependencies = [ + "winnow", +] + +[[package]] +name = "toml_writer" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc842091f2def52017664b53082ecbbeb5c7731092bad69d2c63050401dfd64" + +[[package]] +name = "trybuild" +version = "1.0.111" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ded9fdb81f30a5708920310bfcd9ea7482ff9cba5f54601f7a19a877d5c2392" +dependencies = [ + "glob", + "serde", + "serde_derive", + "serde_json", + "target-triple", + "termcolor", + "toml", +] + [[package]] name = "unarray" version = "0.1.4" @@ -680,6 +765,15 @@ dependencies = [ "wit-bindgen", ] +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "windows-link" version = "0.2.1" @@ -769,6 +863,12 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" +[[package]] +name = "winnow" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21a0236b59786fed61e2a80582dd500fe61f18b5dca67a4a067d0bc9039339cf" + [[package]] name = "wit-bindgen" version = "0.46.0" diff --git a/crates/libtest2/Cargo.toml b/crates/libtest2/Cargo.toml index 419e47c..baafbaf 100644 --- a/crates/libtest2/Cargo.toml +++ b/crates/libtest2/Cargo.toml @@ -39,6 +39,7 @@ escargot = "0.5.8" once_cell_polyfill = "1.56.0" pathdiff = "0.2.1" snapbox = { version = "0.6.0", features = ["json"] } +trybuild = "1.0.111" [lints] workspace = true diff --git a/crates/libtest2/tests/ui.rs b/crates/libtest2/tests/ui.rs new file mode 100644 index 0000000..870c2f9 --- /dev/null +++ b/crates/libtest2/tests/ui.rs @@ -0,0 +1,5 @@ +#[test] +fn ui() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/ui/*.rs"); +} diff --git a/crates/libtest2/tests/ui/ignore_incorrect_usage.rs b/crates/libtest2/tests/ui/ignore_incorrect_usage.rs new file mode 100644 index 0000000..35a9da3 --- /dev/null +++ b/crates/libtest2/tests/ui/ignore_incorrect_usage.rs @@ -0,0 +1,8 @@ +#[libtest2::main] +fn main() {} + +#[libtest2::test] +#[ignore(reason = "invalid syntax right here")] +fn test(_: &libtest2::TestContext) { + panic!("We just need to compile this"); +} diff --git a/crates/libtest2/tests/ui/ignore_incorrect_usage.stderr b/crates/libtest2/tests/ui/ignore_incorrect_usage.stderr new file mode 100644 index 0000000..bd053ff --- /dev/null +++ b/crates/libtest2/tests/ui/ignore_incorrect_usage.stderr @@ -0,0 +1,7 @@ +error: unknown attribute 'ignore(reason = "invalid syntax right here")' + --> tests/ui/ignore_incorrect_usage.rs:4:1 + | +4 | #[libtest2::test] + | ^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/crates/libtest2/tests/ui/should_panic_attribute_typo.rs b/crates/libtest2/tests/ui/should_panic_attribute_typo.rs new file mode 100644 index 0000000..c73ca87 --- /dev/null +++ b/crates/libtest2/tests/ui/should_panic_attribute_typo.rs @@ -0,0 +1,8 @@ +#[libtest2::main] +fn main() {} + +#[libtest2::test] +#[should_panic(expect = "something")] +fn test(_: &libtest2::TestContext) { + panic!("the correct attribute is 'expected = \"...\"'"); +} diff --git a/crates/libtest2/tests/ui/should_panic_attribute_typo.stderr b/crates/libtest2/tests/ui/should_panic_attribute_typo.stderr new file mode 100644 index 0000000..ea16553 --- /dev/null +++ b/crates/libtest2/tests/ui/should_panic_attribute_typo.stderr @@ -0,0 +1,7 @@ +error: unknown attribute 'should_panic(expect = "something")' + --> tests/ui/should_panic_attribute_typo.rs:4:1 + | +4 | #[libtest2::test] + | ^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/crates/libtest2/tests/ui/should_panic_multiple_attributes.rs b/crates/libtest2/tests/ui/should_panic_multiple_attributes.rs new file mode 100644 index 0000000..e4d1f39 --- /dev/null +++ b/crates/libtest2/tests/ui/should_panic_multiple_attributes.rs @@ -0,0 +1,17 @@ +#[libtest2::main] +fn main() {} + +#[libtest2::test] +#[should_panic] +#[should_panic] +fn test_1(_: &libtest2::TestContext) {} + +#[libtest2::test] +#[should_panic] +#[should_panic(reason = "something")] +fn test_2(_: &libtest2::TestContext) {} + +#[libtest2::test] +#[should_panic = "anything"] +#[should_panic] +fn test_3(_: &libtest2::TestContext) {} diff --git a/crates/libtest2/tests/ui/should_panic_multiple_attributes.stderr b/crates/libtest2/tests/ui/should_panic_multiple_attributes.stderr new file mode 100644 index 0000000..f18ebb4 --- /dev/null +++ b/crates/libtest2/tests/ui/should_panic_multiple_attributes.stderr @@ -0,0 +1,23 @@ +error: annotating a test with multiple 'should_panic' attributes is not allowed + --> tests/ui/should_panic_multiple_attributes.rs:4:1 + | +4 | #[libtest2::test] + | ^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: annotating a test with multiple 'should_panic' attributes is not allowed + --> tests/ui/should_panic_multiple_attributes.rs:9:1 + | +9 | #[libtest2::test] + | ^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: annotating a test with multiple 'should_panic' attributes is not allowed + --> tests/ui/should_panic_multiple_attributes.rs:14:1 + | +14 | #[libtest2::test] + | ^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/crates/libtest2/tests/ui/unsupported_attribute.rs b/crates/libtest2/tests/ui/unsupported_attribute.rs new file mode 100644 index 0000000..67cb653 --- /dev/null +++ b/crates/libtest2/tests/ui/unsupported_attribute.rs @@ -0,0 +1,6 @@ +#[libtest2::main] +fn main() {} + +#[libtest2::test] +#[unsafe(no_mangle)] // we just need some unknown attribute here +fn test(_: &libtest2::TestContext) {} diff --git a/crates/libtest2/tests/ui/unsupported_attribute.stderr b/crates/libtest2/tests/ui/unsupported_attribute.stderr new file mode 100644 index 0000000..2aece91 --- /dev/null +++ b/crates/libtest2/tests/ui/unsupported_attribute.stderr @@ -0,0 +1,7 @@ +error: unknown attribute 'unsafe(no_mangle)' + --> tests/ui/unsupported_attribute.rs:4:1 + | +4 | #[libtest2::test] + | ^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/crates/libtest2/tests/ui/unsupported_parameter.rs b/crates/libtest2/tests/ui/unsupported_parameter.rs new file mode 100644 index 0000000..77e15d8 --- /dev/null +++ b/crates/libtest2/tests/ui/unsupported_parameter.rs @@ -0,0 +1,5 @@ +#[libtest2::main] +fn main() {} + +#[libtest2::test] +fn takes_integer(_: i32) {} diff --git a/crates/libtest2/tests/ui/unsupported_parameter.stderr b/crates/libtest2/tests/ui/unsupported_parameter.stderr new file mode 100644 index 0000000..0e1c880 --- /dev/null +++ b/crates/libtest2/tests/ui/unsupported_parameter.stderr @@ -0,0 +1,17 @@ +error[E0308]: mismatched types + --> tests/ui/unsupported_parameter.rs:4:1 + | +4 | #[libtest2::test] + | ^^^^^^^^^^^^^^^^^ + | | + | expected `i32`, found `&TestContext` + | arguments to this function are incorrect + | +note: function defined here + --> tests/ui/unsupported_parameter.rs:4:1 + | +4 | #[libtest2::test] + | ^^^^^^^^^^^^^^^^^ +5 | fn takes_integer(_: i32) {} + | ------ + = note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/crates/libtest2/tests/ui/unsupported_result_types.rs b/crates/libtest2/tests/ui/unsupported_result_types.rs new file mode 100644 index 0000000..5919c73 --- /dev/null +++ b/crates/libtest2/tests/ui/unsupported_result_types.rs @@ -0,0 +1,12 @@ +#[libtest2::main] +fn main() {} + +#[libtest2::test] +fn bad_ok_variant(_: &libtest2::TestContext) -> Result { + Ok(0) +} + +#[libtest2::test] +fn bad_err_variant(_: &libtest2::TestContext) -> Result<(), String> { + Ok(()) +} diff --git a/crates/libtest2/tests/ui/unsupported_result_types.stderr b/crates/libtest2/tests/ui/unsupported_result_types.stderr new file mode 100644 index 0000000..e8493e4 --- /dev/null +++ b/crates/libtest2/tests/ui/unsupported_result_types.stderr @@ -0,0 +1,28 @@ +error[E0277]: the trait bound `Result: IntoRunResult` is not satisfied + --> tests/ui/unsupported_result_types.rs:4:1 + | +4 | #[libtest2::test] + | ^^^^^^^^^^^^^^^^^ + | | + | the trait `IntoRunResult` is not implemented for `Result` + | required by a bound introduced by this call + | + = help: the following other types implement trait `IntoRunResult`: + Result<(), E> + Result<(), RunError> + = note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `String: std::error::Error` is not satisfied + --> tests/ui/unsupported_result_types.rs:9:1 + | +9 | #[libtest2::test] + | ^^^^^^^^^^^^^^^^^ + | | + | the trait `std::error::Error` is not implemented for `String` + | required by a bound introduced by this call + | + = help: the following other types implement trait `IntoRunResult`: + Result<(), E> + Result<(), RunError> + = note: required for `Result<(), String>` to implement `IntoRunResult` + = note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/crates/libtest2/tests/ui/unsupported_return_type.rs b/crates/libtest2/tests/ui/unsupported_return_type.rs new file mode 100644 index 0000000..83cd3d6 --- /dev/null +++ b/crates/libtest2/tests/ui/unsupported_return_type.rs @@ -0,0 +1,7 @@ +#[libtest2::main] +fn main() {} + +#[libtest2::test] +fn integer(_: &libtest2::TestContext) -> i32 { + 0 +} diff --git a/crates/libtest2/tests/ui/unsupported_return_type.stderr b/crates/libtest2/tests/ui/unsupported_return_type.stderr new file mode 100644 index 0000000..d6e7713 --- /dev/null +++ b/crates/libtest2/tests/ui/unsupported_return_type.stderr @@ -0,0 +1,14 @@ +error[E0277]: the trait bound `i32: IntoRunResult` is not satisfied + --> tests/ui/unsupported_return_type.rs:4:1 + | +4 | #[libtest2::test] + | ^^^^^^^^^^^^^^^^^ + | | + | the trait `IntoRunResult` is not implemented for `i32` + | required by a bound introduced by this call + | + = help: the following other types implement trait `IntoRunResult`: + () + Result<(), E> + Result<(), RunError> + = note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info)