Skip to content

Commit 882d8fa

Browse files
committed
chore: Add k8s snapshot test, rework test utils
1 parent 64c04c2 commit 882d8fa

File tree

8 files changed

+199
-52
lines changed

8 files changed

+199
-52
lines changed

Cargo.lock

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

crates/stackable-versioned-macros/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ schemars.workspace = true
4646
serde.workspace = true
4747
serde_json.workspace = true
4848
serde_yaml.workspace = true
49+
snafu.workspace = true
4950
trybuild.workspace = true
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#[versioned(
2+
version(name = "v1alpha1"),
3+
version(name = "v1beta1"),
4+
version(name = "v1"),
5+
k8s(
6+
group = "stackable.tech",
7+
singular = "foo",
8+
plural = "foos",
9+
namespaced,
10+
)
11+
)]
12+
// ---
13+
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
14+
pub struct FooSpec {
15+
#[versioned(
16+
added(since = "v1beta1"),
17+
changed(since = "v1", from_name = "bah", from_type = "u16")
18+
)]
19+
bar: usize,
20+
baz: bool,
21+
}

crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots.snap

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

crates/stackable-versioned-macros/src/lib.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -506,15 +506,24 @@ mod test {
506506

507507
#[test]
508508
fn default_snapshots() {
509-
let settings = test_utils::set_snapshot_path();
510-
let _guard = settings.bind_to_scope();
509+
let _settings_guard = test_utils::set_snapshot_path().bind_to_scope();
511510

512511
glob!("../fixtures/inputs/default", "*.rs", |path| {
513-
let input = std::fs::read_to_string(path).unwrap();
514-
let (attrs, input) = test_utils::prepare_from_string(input);
515-
let expanded = versioned_impl(attrs, input).to_string();
516-
let formatted = prettyplease::unparse(&syn::parse_file(&expanded).unwrap());
512+
let formatted = test_utils::expand_from_file(path)
513+
.inspect_err(|err| eprintln!("{err}"))
514+
.unwrap();
515+
assert_snapshot!(formatted);
516+
});
517+
}
518+
519+
#[test]
520+
fn k8s_snapshots() {
521+
let _settings_guard = test_utils::set_snapshot_path().bind_to_scope();
517522

523+
glob!("../fixtures/inputs/k8s", "*.rs", |path| {
524+
let formatted = test_utils::expand_from_file(path)
525+
.inspect_err(|err| eprintln!("{err}"))
526+
.unwrap();
518527
assert_snapshot!(formatted);
519528
});
520529
}

crates/stackable-versioned-macros/src/test_utils.rs

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,70 @@
1-
use std::{path::PathBuf, str::FromStr, sync::LazyLock};
1+
use std::{
2+
path::{Path, PathBuf},
3+
str::FromStr,
4+
sync::LazyLock,
5+
};
26

37
use insta::Settings;
48
use proc_macro2::TokenStream;
59
use regex::Regex;
10+
use snafu::{OptionExt, ResultExt, Snafu};
611
use syn::DeriveInput;
712

13+
use crate::versioned_impl;
14+
815
const DELIMITER: &str = "// ---\n";
916

1017
static REGEX: LazyLock<Regex> = LazyLock::new(|| {
1118
Regex::new(r"#\[versioned\(\n(?P<args>[[:ascii:]]+)\n\)\]")
1219
.expect("failed to compile versioned regex")
1320
});
1421

15-
pub(crate) fn prepare_from_string(input: String) -> (TokenStream, DeriveInput) {
16-
let (attrs, input) = input
17-
.split_once(DELIMITER)
18-
.expect("failed to find delimiter");
22+
#[derive(Debug, Snafu)]
23+
pub(crate) enum Error {
24+
#[snafu(display("failed to read input file"))]
25+
ReadFile { source: std::io::Error },
26+
27+
#[snafu(display("failed to find delimiter"))]
28+
MissingDelimiter,
29+
30+
#[snafu(display("failed to find regex match group"))]
31+
MissingRegexMatchGroup,
32+
33+
#[snafu(display("failed to parse token stream"))]
34+
ParseTokenStream { source: proc_macro2::LexError },
35+
36+
#[snafu(display("failed to parse derive input"))]
37+
ParseDeriveInput { source: syn::Error },
38+
39+
#[snafu(display("failed to parse output file"))]
40+
ParseOutputFile { source: syn::Error },
41+
}
42+
43+
pub(crate) fn expand_from_file(path: &Path) -> Result<String, Error> {
44+
let input = std::fs::read_to_string(path).context(ReadFileSnafu)?;
45+
let (attrs, input) = prepare_from_string(input)?;
46+
47+
let expanded = versioned_impl(attrs, input).to_string();
48+
let parsed = syn::parse_file(&expanded).context(ParseOutputFileSnafu)?;
49+
50+
Ok(prettyplease::unparse(&parsed))
51+
}
52+
53+
fn prepare_from_string(input: String) -> Result<(TokenStream, DeriveInput), Error> {
54+
let (attrs, input) = input.split_once(DELIMITER).context(MissingDelimiterSnafu)?;
1955

2056
let attrs = REGEX
2157
.captures(attrs)
2258
.unwrap()
2359
.name("args")
24-
.expect("args match group must be available")
60+
.context(MissingRegexMatchGroupSnafu)?
2561
.as_str();
2662

27-
let attrs = TokenStream::from_str(attrs).expect("attrs must parse as a token stream");
28-
let input = TokenStream::from_str(input).expect("input mus parse as a token stream");
29-
let input = syn::parse2(input).expect("input must parse as derive input");
63+
let attrs = TokenStream::from_str(attrs).context(ParseTokenStreamSnafu)?;
64+
let input = TokenStream::from_str(input).context(ParseTokenStreamSnafu)?;
65+
let input = syn::parse2(input).context(ParseDeriveInputSnafu)?;
3066

31-
(attrs, input)
67+
Ok((attrs, input))
3268
}
3369

3470
pub(crate) fn set_snapshot_path() -> Settings {

crates/stackable-versioned-macros/tests/k8s/pass/crd.rs

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

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ fn default_macros() {
3232
#[cfg(feature = "k8s")]
3333
#[allow(dead_code)]
3434
mod k8s {
35-
// mod pass {
36-
// mod crd;
37-
// }
38-
3935
// mod fail {
4036
// mod crd;
4137
// }
@@ -45,6 +41,5 @@ mod k8s {
4541
#[test]
4642
fn k8s_macros() {
4743
let t = trybuild::TestCases::new();
48-
t.pass("tests/k8s/pass/*.rs");
4944
t.compile_fail("tests/k8s/fail/*.rs");
5045
}

0 commit comments

Comments
 (0)