Skip to content

Commit

Permalink
test(stackable-versioned-macros): Use trybuild to test good/bad examp…
Browse files Browse the repository at this point in the history
…les (#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
  • Loading branch information
NickLarsenNZ authored Aug 23, 2024
1 parent ece7410 commit 68d0cb2
Show file tree
Hide file tree
Showing 21 changed files with 350 additions and 91 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ jobs:
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.RUST_TOOLCHAIN_VERSION }}
# rust-src is required for trybuild stderr output comparison to work
# for our cases.
# See: https://github.com/dtolnay/trybuild/issues/236#issuecomment-1620950759
components: rust-src
- uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3
with:
key: test
Expand Down
2 changes: 1 addition & 1 deletion .scripts/verify_crate_versions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set -euo pipefail
# stackable-versioned-0.1.1
# stackable-webhook-0.3.1

for CRATE in $(find . -mindepth 2 -name Cargo.toml | sed -e 's|^./crates/\([a-z0-9_\-]\+\).*|\1|' | sort); do
for CRATE in $(find ./crates/ -mindepth 2 -name Cargo.toml -print0 | xargs -0 -n 1 dirname | xargs -n 1 basename | sort); do
# Get the version in Cargo.toml
CRATE_VERSION=$(grep 'version' "./crates/$CRATE/Cargo.toml" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
[ -n "$CRATE_VERSION" ] || (
Expand Down
83 changes: 81 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ tracing = "0.1.40"
tracing-appender = "0.2.3"
tracing-opentelemetry = "0.24.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"] }
trybuild = "1.0.99"
url = { version = "2.5.2", features = ["serde"] }
x509-cert = { version = "0.2.5", features = ["builder"] }
zeroize = "1.8.1"
Expand Down
1 change: 1 addition & 0 deletions crates/stackable-versioned-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ quote.workspace = true

[dev-dependencies]
rstest.workspace = true
trybuild.workspace = true
6 changes: 6 additions & 0 deletions crates/stackable-versioned-macros/tests/bad/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# trybuild bad cases

Code that is expected to fail lives here along with the expected compiler output
for each case. Please see the docs in [tests/trybuild.rs].

[tests/trybuild.rs]: ../trybuild.rs
14 changes: 14 additions & 0 deletions crates/stackable-versioned-macros/tests/bad/deprecate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use stackable_versioned_macros::versioned;

fn main() {
#[versioned(
version(name = "v1alpha1"),
version(name = "v1beta1"),
version(name = "v1")
)]
struct Foo {
#[deprecated]
bar: usize,
baz: bool,
}
}
5 changes: 5 additions & 0 deletions crates/stackable-versioned-macros/tests/bad/deprecate.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: deprecation must be done using #[versioned(deprecated(since = "VERSION"))]
--> tests/bad/deprecate.rs:10:9
|
10 | #[deprecated]
| ^
24 changes: 24 additions & 0 deletions crates/stackable-versioned-macros/tests/bad/skip_from_all.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use stackable_versioned_macros::versioned;

fn main() {
#[versioned(
version(name = "v1alpha1"),
version(name = "v1beta1"),
version(name = "v1"),
options(skip(from))
)]
pub struct Foo {
#[versioned(
added(since = "v1beta1"),
deprecated(since = "v1", note = "not needed")
)]
deprecated_bar: usize,
baz: bool,
}

let foo_v1alpha1 = v1alpha1::Foo { baz: true };

// There are no From impls for any version. You need to convert it manually.
#[allow(dead_code)]
let foo_v1beta1 = v1beta1::Foo::from(foo_v1alpha1);
}
35 changes: 35 additions & 0 deletions crates/stackable-versioned-macros/tests/bad/skip_from_all.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
error[E0308]: mismatched types
--> tests/bad/skip_from_all.rs:23:42
|
23 | let foo_v1beta1 = v1beta1::Foo::from(foo_v1alpha1);
| ------------------ ^^^^^^^^^^^^ expected `v1beta1::Foo`, found `v1alpha1::Foo`
| |
| arguments to this function are incorrect
|
= note: `v1alpha1::Foo` and `v1beta1::Foo` have similar names, but are actually distinct types
note: `v1alpha1::Foo` is defined in module `crate::main::v1alpha1` of the current crate
--> tests/bad/skip_from_all.rs:4:5
|
4 | / #[versioned(
5 | | version(name = "v1alpha1"),
6 | | version(name = "v1beta1"),
7 | | version(name = "v1"),
8 | | options(skip(from))
9 | | )]
| |______^
note: `v1beta1::Foo` is defined in module `crate::main::v1beta1` of the current crate
--> tests/bad/skip_from_all.rs:4:5
|
4 | / #[versioned(
5 | | version(name = "v1alpha1"),
6 | | version(name = "v1beta1"),
7 | | version(name = "v1"),
8 | | options(skip(from))
9 | | )]
| |______^
note: associated function defined here
--> $RUST/core/src/convert/mod.rs
|
| fn from(value: T) -> Self;
| ^^^^
= note: this error originates in the attribute macro `versioned` (in Nightly builds, run with -Z macro-backtrace for more info)
24 changes: 24 additions & 0 deletions crates/stackable-versioned-macros/tests/bad/skip_from_version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use stackable_versioned_macros::versioned;

fn main() {
#[versioned(
version(name = "v1alpha1"),
version(name = "v1beta1", skip(from)),
version(name = "v1")
)]
pub struct Foo {
#[versioned(
added(since = "v1beta1"),
deprecated(since = "v1", note = "not needed")
)]
deprecated_bar: usize,
baz: bool,
}

let foo_v1alpha1 = v1alpha1::Foo { baz: true };
let foo_v1beta1 = v1beta1::Foo::from(foo_v1alpha1);

#[allow(dead_code)]
// v1beta1 has no From impl. You need to convert it manually.
let foo_v1 = v1::Foo::from(foo_v1beta1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
error[E0308]: mismatched types
--> tests/bad/skip_from_version.rs:23:32
|
23 | let foo_v1 = v1::Foo::from(foo_v1beta1);
| ------------- ^^^^^^^^^^^ expected `main::v1::Foo`, found `v1beta1::Foo`
| |
| arguments to this function are incorrect
|
= note: `v1beta1::Foo` and `main::v1::Foo` have similar names, but are actually distinct types
note: `v1beta1::Foo` is defined in module `crate::main::v1beta1` of the current crate
--> tests/bad/skip_from_version.rs:4:5
|
4 | / #[versioned(
5 | | version(name = "v1alpha1"),
6 | | version(name = "v1beta1", skip(from)),
7 | | version(name = "v1")
8 | | )]
| |______^
note: `main::v1::Foo` is defined in module `crate::main::v1` of the current crate
--> tests/bad/skip_from_version.rs:4:5
|
4 | / #[versioned(
5 | | version(name = "v1alpha1"),
6 | | version(name = "v1beta1", skip(from)),
7 | | version(name = "v1")
8 | | )]
| |______^
note: associated function defined here
--> $RUST/core/src/convert/mod.rs
|
| fn from(value: T) -> Self;
| ^^^^
= note: this error originates in the attribute macro `versioned` (in Nightly builds, run with -Z macro-backtrace for more info)
35 changes: 0 additions & 35 deletions crates/stackable-versioned-macros/tests/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,3 @@ fn from_custom_default_fn() {
assert_eq!(foo_v1beta1.bar, 42);
assert!(foo_v1beta1.baz);
}

#[test]
fn skip_from_all() {
#[versioned(
version(name = "v1alpha1"),
version(name = "v1beta1"),
version(name = "v1"),
options(skip(from))
)]
pub struct Foo {
#[versioned(
added(since = "v1beta1"),
deprecated(since = "v1", note = "not needed")
)]
deprecated_bar: usize,
baz: bool,
}
}

#[test]
fn skip_from_version() {
#[versioned(
version(name = "v1alpha1"),
version(name = "v1beta1", skip(from)),
version(name = "v1")
)]
pub struct Foo {
#[versioned(
added(since = "v1beta1"),
deprecated(since = "v1", note = "not needed")
)]
deprecated_bar: usize,
baz: bool,
}
}
6 changes: 6 additions & 0 deletions crates/stackable-versioned-macros/tests/good/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# trybuild good cases

Code that is expected to compile lives here. Please see the docs in
[tests/trybuild.rs].

[tests/trybuild.rs]: ../trybuild.rs
Loading

0 comments on commit 68d0cb2

Please sign in to comment.