Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor kube-derive using darling #435

Merged
merged 4 commits into from Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions kube-derive/Cargo.toml
Expand Up @@ -32,3 +32,4 @@ serde_yaml = "0.8.14"
k8s-openapi = { version = "0.11.0", default-features = false, features = ["v1_20"] }
schemars = { version = "0.8.0", features = ["chrono"] }
chrono = "0.4.19"
trybuild = "1.0"
2 changes: 1 addition & 1 deletion kube-derive/src/custom_resource.rs
Expand Up @@ -46,7 +46,7 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
_ => {
return syn::Error::new_spanned(
&derive_input.ident,
r#"Enums or Unions can not #[derive(CustomResource)"#,
r#"Enums or Unions can not #[derive(CustomResource)]"#,
)
.to_compile_error()
}
Expand Down
10 changes: 10 additions & 0 deletions kube-derive/tests/test_ui.rs
@@ -0,0 +1,10 @@
// Test that `kube-derive` outputs helpful error messages.
// If you make a change, remove `tests/ui/*.stderr` and run `cargo test`.
// Then copy the files that appear under `wip/` if it's what you expected.
// Alternatively, run `TRYBUILD=overwrite cargo test`.
// See https://github.com/dtolnay/trybuild
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect!

#[test]
fn test_failures() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/*.rs");
}
10 changes: 10 additions & 0 deletions kube-derive/tests/ui/enum_fails.rs
@@ -0,0 +1,10 @@
use kube_derive::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
enum FooSpec {
Foo,
}

fn main() {}
5 changes: 5 additions & 0 deletions kube-derive/tests/ui/enum_fails.stderr
@@ -0,0 +1,5 @@
error: Enums or Unions can not #[derive(CustomResource)]
--> $DIR/enum_fails.rs:6:6
|
6 | enum FooSpec {
| ^^^^^^^
11 changes: 11 additions & 0 deletions kube-derive/tests/ui/fail_with_suggestion.rs
@@ -0,0 +1,11 @@
use kube_derive::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
#[kube(group = "clux.dev", version = "v1", kind = "Foo", shortnames = "foo")]
struct FooSpec {
foo: String,
}

fn main() {}
5 changes: 5 additions & 0 deletions kube-derive/tests/ui/fail_with_suggestion.stderr
@@ -0,0 +1,5 @@
error: Unknown field: `shortnames`. Did you mean `shortname`?
--> $DIR/fail_with_suggestion.rs:6:58
|
6 | #[kube(group = "clux.dev", version = "v1", kind = "Foo", shortnames = "foo")]
| ^^^^^^^^^^
10 changes: 10 additions & 0 deletions kube-derive/tests/ui/missing_required.rs
@@ -0,0 +1,10 @@
use kube_derive::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
struct FooSpec {
foo: String,
}

fn main() {}
23 changes: 23 additions & 0 deletions kube-derive/tests/ui/missing_required.stderr
@@ -0,0 +1,23 @@
error: Missing field `group`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shows errors from all the missing required attributes (not stopping at group anymore)

--> $DIR/missing_required.rs:5:10
|
5 | #[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
| ^^^^^^^^^^^^^^
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: Missing field `version`
--> $DIR/missing_required.rs:5:10
|
5 | #[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
| ^^^^^^^^^^^^^^
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: Missing field `kind`
--> $DIR/missing_required.rs:5:10
|
5 | #[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
| ^^^^^^^^^^^^^^
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)