Skip to content

Commit faa52fe

Browse files
refactor(testkit): drop test IDs and capabilities for now
Nothing reads test IDs or capability requirements until a runner, waivers, or result history exist, so remove the id and requires arguments and the target file's capabilities section. Both can return as optional attribute arguments without changing existing tests. With them gone, testkit-spec only checked the function shape and read the doc comment, so fold it into testkit-macros. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent 93db17b commit faa52fe

9 files changed

Lines changed: 93 additions & 536 deletions

File tree

‎tests/testkit/Cargo.lock‎

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/testkit/Cargo.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
# Shared support for the test families under tests/suites/. Families depend
5-
# on these crates by path.
4+
# Shared support for end-to-end test suites. Suites depend on these crates
5+
# by path.
66
[workspace]
77
resolver = "2"
8-
members = ["testkit", "testkit-macros", "testkit-spec"]
8+
members = ["testkit", "testkit-macros"]

‎tests/testkit/testkit-macros/Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ name = "testkit_macros"
1616
proc-macro = true
1717

1818
[dependencies]
19-
openshell-testkit-spec = { path = "../testkit-spec" }
2019
proc-macro2 = "1"
2120
quote = "1"
2221
syn = { version = "2", features = ["full"] }

‎tests/testkit/testkit-macros/src/lib.rs‎

Lines changed: 81 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,24 @@
33

44
//! The `#[testkit::test]` attribute.
55
//!
6-
//! A test is an `async fn` taking `&mut testkit::Context` and
7-
//! returning `testkit::Result`. Its doc comment is the test's description and
8-
//! is required. `id` is the required stable test ID: random, never changed,
9-
//! and kept when the test is renamed or moved. A missing ID's compile error
10-
//! suggests a fresh one. Requirements are named after target capabilities:
6+
//! A test is an `async fn` taking `&mut testkit::Context` and returning
7+
//! `testkit::Result`. Its doc comment is the test's description and is
8+
//! required:
119
//!
1210
//! ```ignore
1311
//! /// An exposed loopback HTTP service is reachable and goes away after delete.
14-
//! #[testkit::test(id = "t-4f8k2m0q", requires = [service_exposure])]
12+
//! #[testkit::test]
1513
//! async fn expose_service(cx: &mut Context) -> testkit::Result { /* ... */ }
1614
//! ```
1715
//!
1816
//! The attribute expands to a `#[tokio::test]` wrapper with the same name, so
19-
//! the nextest test name is the function name. The original function is nested in
20-
//! the wrapper and passed to `testkit::run`. Parsing lives in `testkit-spec`,
21-
//! so tools that list tests share the macro's rules.
17+
//! the nextest test name is the function name. The original function is nested
18+
//! in the wrapper and passed to `testkit::run`.
2219
2320
use proc_macro::TokenStream;
2421
use proc_macro2::TokenStream as TokenStream2;
2522
use quote::{format_ident, quote};
26-
use syn::{Ident, ItemFn, LitStr};
23+
use syn::{Attribute, Expr, ExprLit, ItemFn, Lit, LitStr, Meta};
2724

2825
#[proc_macro_attribute]
2926
pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream {
@@ -34,16 +31,35 @@ pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream {
3431

3532
fn expand(attr: TokenStream2, item: TokenStream2) -> syn::Result<TokenStream2> {
3633
let mut body: ItemFn = syn::parse2(item)?;
37-
let spec = testkit_spec::parse(attr, &body)?;
34+
if !attr.is_empty() {
35+
return Err(syn::Error::new_spanned(
36+
attr,
37+
"`#[testkit::test]` takes no arguments",
38+
));
39+
}
40+
if body.sig.asyncness.is_none() {
41+
return Err(syn::Error::new_spanned(
42+
body.sig.fn_token,
43+
"testkit tests must be `async fn`",
44+
));
45+
}
46+
if body.sig.inputs.len() != 1 {
47+
return Err(syn::Error::new_spanned(
48+
&body.sig.inputs,
49+
"testkit tests take exactly one argument: `cx: &mut testkit::Context`",
50+
));
51+
}
52+
let description = doc_comment(&body.attrs);
53+
if description.is_empty() {
54+
return Err(syn::Error::new(
55+
body.sig.ident.span(),
56+
"testkit tests need a doc comment describing the behavior they verify",
57+
));
58+
}
3859

39-
let name = spec.name;
60+
let name = body.sig.ident.clone();
4061
let name_str = LitStr::new(&name.to_string(), name.span());
41-
let id = LitStr::new(&spec.id, name.span());
42-
let description = LitStr::new(&spec.description, name.span());
43-
let requires = spec.requires.iter().map(|capability| {
44-
let variant = Ident::new(&to_camel_case(&capability.to_string()), capability.span());
45-
quote!(::testkit::Capability::#variant)
46-
});
62+
let description = LitStr::new(&description, name.span());
4763

4864
let vis = body.vis.clone();
4965
let doc_attrs: Vec<_> = body
@@ -63,10 +79,8 @@ fn expand(attr: TokenStream2, item: TokenStream2) -> syn::Result<TokenStream2> {
6379
#body
6480
::testkit::run(
6581
::testkit::TestInfo {
66-
id: #id,
6782
name: #name_str,
6883
description: #description,
69-
requires: &[#(#requires),*],
7084
},
7185
body,
7286
)
@@ -75,16 +89,23 @@ fn expand(attr: TokenStream2, item: TokenStream2) -> syn::Result<TokenStream2> {
7589
})
7690
}
7791

78-
fn to_camel_case(snake: &str) -> String {
79-
snake
80-
.split('_')
81-
.map(|word| {
82-
let mut chars = word.chars();
83-
chars.next().map_or_else(String::new, |first| {
84-
first.to_uppercase().chain(chars).collect()
85-
})
92+
/// The doc comment, joined into one line. Empty if there is none.
93+
fn doc_comment(attrs: &[Attribute]) -> String {
94+
attrs
95+
.iter()
96+
.filter_map(|attr| match &attr.meta {
97+
Meta::NameValue(meta) if meta.path.is_ident("doc") => match &meta.value {
98+
Expr::Lit(ExprLit {
99+
lit: Lit::Str(line),
100+
..
101+
}) => Some(line.value().trim().to_string()),
102+
_ => None,
103+
},
104+
_ => None,
86105
})
87-
.collect()
106+
.filter(|line| !line.is_empty())
107+
.collect::<Vec<_>>()
108+
.join(" ")
88109
}
89110

90111
#[cfg(test)]
@@ -98,39 +119,53 @@ mod tests {
98119
#[test]
99120
fn expands_to_wrapper_with_same_name() {
100121
let out = expand_str(
101-
"id = \"t-0123abcd\"",
102-
"/// Does a thing.\nasync fn does_a_thing(cx: &mut Context) -> testkit::Result { Ok(()) }",
122+
"",
123+
"/// First line\n/// second line.\nasync fn does_a_thing(cx: &mut Context) -> testkit::Result { Ok(()) }",
103124
)
104125
.unwrap();
105126
assert!(out.contains("async fn does_a_thing ()"), "{out}");
106127
assert!(out.contains("async fn body (cx : & mut Context)"), "{out}");
107-
assert!(out.contains("id : \"t-0123abcd\""), "{out}");
108128
assert!(out.contains("name : \"does_a_thing\""), "{out}");
109-
assert!(out.contains("description : \"Does a thing.\""), "{out}");
110-
}
111-
112-
#[test]
113-
fn maps_requirements_to_capabilities() {
114-
let out = expand_str(
115-
"id = \"t-0123abcd\", requires = [service_exposure, gpu]",
116-
"/// Doc.\nasync fn t(cx: &mut Context) -> testkit::Result { Ok(()) }",
117-
)
118-
.unwrap();
119129
assert!(
120-
out.contains(
121-
"requires : & [:: testkit :: Capability :: ServiceExposure , :: testkit :: Capability :: Gpu]"
122-
),
130+
out.contains("description : \"First line second line.\""),
123131
"{out}"
124132
);
125133
}
126134

127135
#[test]
128-
fn reports_spec_errors() {
136+
fn rejects_missing_doc_comment() {
129137
let err = expand_str(
130138
"",
131139
"async fn t(cx: &mut Context) -> testkit::Result { Ok(()) }",
132140
)
133141
.unwrap_err();
134142
assert!(err.to_string().contains("doc comment"), "{err}");
135143
}
144+
145+
#[test]
146+
fn rejects_sync_fn() {
147+
let err = expand_str(
148+
"",
149+
"/// Doc.\nfn t(cx: &mut Context) -> testkit::Result { Ok(()) }",
150+
)
151+
.unwrap_err();
152+
assert!(err.to_string().contains("async fn"), "{err}");
153+
}
154+
155+
#[test]
156+
fn rejects_wrong_arity() {
157+
let err =
158+
expand_str("", "/// Doc.\nasync fn t() -> testkit::Result { Ok(()) }").unwrap_err();
159+
assert!(err.to_string().contains("exactly one argument"), "{err}");
160+
}
161+
162+
#[test]
163+
fn rejects_arguments() {
164+
let err = expand_str(
165+
"id = \"t-0123abcd\"",
166+
"/// Doc.\nasync fn t(cx: &mut Context) -> testkit::Result { Ok(()) }",
167+
)
168+
.unwrap_err();
169+
assert!(err.to_string().contains("takes no arguments"), "{err}");
170+
}
136171
}

‎tests/testkit/testkit-spec/Cargo.toml‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)