Skip to content

Commit 68d0cb2

Browse files
authored
test(stackable-versioned-macros): Use trybuild to test good/bad examples (#849)
* fix(script): be more explicit about where to find Cargo.tomls * test(stackable-versioned-macros): move tests without assertions into trybuild pass/fail examples * ci(build): add rust-src component for testing
1 parent ece7410 commit 68d0cb2

21 files changed

+350
-91
lines changed

.github/workflows/build.yml

+4
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ jobs:
135135
- uses: dtolnay/rust-toolchain@master
136136
with:
137137
toolchain: ${{ env.RUST_TOOLCHAIN_VERSION }}
138+
# rust-src is required for trybuild stderr output comparison to work
139+
# for our cases.
140+
# See: https://github.com/dtolnay/trybuild/issues/236#issuecomment-1620950759
141+
components: rust-src
138142
- uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3
139143
with:
140144
key: test

.scripts/verify_crate_versions.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ set -euo pipefail
1111
# stackable-versioned-0.1.1
1212
# stackable-webhook-0.3.1
1313

14-
for CRATE in $(find . -mindepth 2 -name Cargo.toml | sed -e 's|^./crates/\([a-z0-9_\-]\+\).*|\1|' | sort); do
14+
for CRATE in $(find ./crates/ -mindepth 2 -name Cargo.toml -print0 | xargs -0 -n 1 dirname | xargs -n 1 basename | sort); do
1515
# Get the version in Cargo.toml
1616
CRATE_VERSION=$(grep 'version' "./crates/$CRATE/Cargo.toml" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
1717
[ -n "$CRATE_VERSION" ] || (

Cargo.lock

+81-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ tracing = "0.1.40"
7070
tracing-appender = "0.2.3"
7171
tracing-opentelemetry = "0.24.0"
7272
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"] }
73+
trybuild = "1.0.99"
7374
url = { version = "2.5.2", features = ["serde"] }
7475
x509-cert = { version = "0.2.5", features = ["builder"] }
7576
zeroize = "1.8.1"

crates/stackable-versioned-macros/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ quote.workspace = true
2222

2323
[dev-dependencies]
2424
rstest.workspace = true
25+
trybuild.workspace = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# trybuild bad cases
2+
3+
Code that is expected to fail lives here along with the expected compiler output
4+
for each case. Please see the docs in [tests/trybuild.rs].
5+
6+
[tests/trybuild.rs]: ../trybuild.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use stackable_versioned_macros::versioned;
2+
3+
fn main() {
4+
#[versioned(
5+
version(name = "v1alpha1"),
6+
version(name = "v1beta1"),
7+
version(name = "v1")
8+
)]
9+
struct Foo {
10+
#[deprecated]
11+
bar: usize,
12+
baz: bool,
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: deprecation must be done using #[versioned(deprecated(since = "VERSION"))]
2+
--> tests/bad/deprecate.rs:10:9
3+
|
4+
10 | #[deprecated]
5+
| ^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use stackable_versioned_macros::versioned;
2+
3+
fn main() {
4+
#[versioned(
5+
version(name = "v1alpha1"),
6+
version(name = "v1beta1"),
7+
version(name = "v1"),
8+
options(skip(from))
9+
)]
10+
pub struct Foo {
11+
#[versioned(
12+
added(since = "v1beta1"),
13+
deprecated(since = "v1", note = "not needed")
14+
)]
15+
deprecated_bar: usize,
16+
baz: bool,
17+
}
18+
19+
let foo_v1alpha1 = v1alpha1::Foo { baz: true };
20+
21+
// There are no From impls for any version. You need to convert it manually.
22+
#[allow(dead_code)]
23+
let foo_v1beta1 = v1beta1::Foo::from(foo_v1alpha1);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0308]: mismatched types
2+
--> tests/bad/skip_from_all.rs:23:42
3+
|
4+
23 | let foo_v1beta1 = v1beta1::Foo::from(foo_v1alpha1);
5+
| ------------------ ^^^^^^^^^^^^ expected `v1beta1::Foo`, found `v1alpha1::Foo`
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
= note: `v1alpha1::Foo` and `v1beta1::Foo` have similar names, but are actually distinct types
10+
note: `v1alpha1::Foo` is defined in module `crate::main::v1alpha1` of the current crate
11+
--> tests/bad/skip_from_all.rs:4:5
12+
|
13+
4 | / #[versioned(
14+
5 | | version(name = "v1alpha1"),
15+
6 | | version(name = "v1beta1"),
16+
7 | | version(name = "v1"),
17+
8 | | options(skip(from))
18+
9 | | )]
19+
| |______^
20+
note: `v1beta1::Foo` is defined in module `crate::main::v1beta1` of the current crate
21+
--> tests/bad/skip_from_all.rs:4:5
22+
|
23+
4 | / #[versioned(
24+
5 | | version(name = "v1alpha1"),
25+
6 | | version(name = "v1beta1"),
26+
7 | | version(name = "v1"),
27+
8 | | options(skip(from))
28+
9 | | )]
29+
| |______^
30+
note: associated function defined here
31+
--> $RUST/core/src/convert/mod.rs
32+
|
33+
| fn from(value: T) -> Self;
34+
| ^^^^
35+
= note: this error originates in the attribute macro `versioned` (in Nightly builds, run with -Z macro-backtrace for more info)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use stackable_versioned_macros::versioned;
2+
3+
fn main() {
4+
#[versioned(
5+
version(name = "v1alpha1"),
6+
version(name = "v1beta1", skip(from)),
7+
version(name = "v1")
8+
)]
9+
pub struct Foo {
10+
#[versioned(
11+
added(since = "v1beta1"),
12+
deprecated(since = "v1", note = "not needed")
13+
)]
14+
deprecated_bar: usize,
15+
baz: bool,
16+
}
17+
18+
let foo_v1alpha1 = v1alpha1::Foo { baz: true };
19+
let foo_v1beta1 = v1beta1::Foo::from(foo_v1alpha1);
20+
21+
#[allow(dead_code)]
22+
// v1beta1 has no From impl. You need to convert it manually.
23+
let foo_v1 = v1::Foo::from(foo_v1beta1);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0308]: mismatched types
2+
--> tests/bad/skip_from_version.rs:23:32
3+
|
4+
23 | let foo_v1 = v1::Foo::from(foo_v1beta1);
5+
| ------------- ^^^^^^^^^^^ expected `main::v1::Foo`, found `v1beta1::Foo`
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
= note: `v1beta1::Foo` and `main::v1::Foo` have similar names, but are actually distinct types
10+
note: `v1beta1::Foo` is defined in module `crate::main::v1beta1` of the current crate
11+
--> tests/bad/skip_from_version.rs:4:5
12+
|
13+
4 | / #[versioned(
14+
5 | | version(name = "v1alpha1"),
15+
6 | | version(name = "v1beta1", skip(from)),
16+
7 | | version(name = "v1")
17+
8 | | )]
18+
| |______^
19+
note: `main::v1::Foo` is defined in module `crate::main::v1` of the current crate
20+
--> tests/bad/skip_from_version.rs:4:5
21+
|
22+
4 | / #[versioned(
23+
5 | | version(name = "v1alpha1"),
24+
6 | | version(name = "v1beta1", skip(from)),
25+
7 | | version(name = "v1")
26+
8 | | )]
27+
| |______^
28+
note: associated function defined here
29+
--> $RUST/core/src/convert/mod.rs
30+
|
31+
| fn from(value: T) -> Self;
32+
| ^^^^
33+
= note: this error originates in the attribute macro `versioned` (in Nightly builds, run with -Z macro-backtrace for more info)

crates/stackable-versioned-macros/tests/from.rs

-35
Original file line numberDiff line numberDiff line change
@@ -51,38 +51,3 @@ fn from_custom_default_fn() {
5151
assert_eq!(foo_v1beta1.bar, 42);
5252
assert!(foo_v1beta1.baz);
5353
}
54-
55-
#[test]
56-
fn skip_from_all() {
57-
#[versioned(
58-
version(name = "v1alpha1"),
59-
version(name = "v1beta1"),
60-
version(name = "v1"),
61-
options(skip(from))
62-
)]
63-
pub struct Foo {
64-
#[versioned(
65-
added(since = "v1beta1"),
66-
deprecated(since = "v1", note = "not needed")
67-
)]
68-
deprecated_bar: usize,
69-
baz: bool,
70-
}
71-
}
72-
73-
#[test]
74-
fn skip_from_version() {
75-
#[versioned(
76-
version(name = "v1alpha1"),
77-
version(name = "v1beta1", skip(from)),
78-
version(name = "v1")
79-
)]
80-
pub struct Foo {
81-
#[versioned(
82-
added(since = "v1beta1"),
83-
deprecated(since = "v1", note = "not needed")
84-
)]
85-
deprecated_bar: usize,
86-
baz: bool,
87-
}
88-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# trybuild good cases
2+
3+
Code that is expected to compile lives here. Please see the docs in
4+
[tests/trybuild.rs].
5+
6+
[tests/trybuild.rs]: ../trybuild.rs

0 commit comments

Comments
 (0)