summaryrefslogtreecommitdiff
path: root/rola-utils/space-system/macros/src/space_root_test.rs
blob: 71c48c0e31ae98e324684c8341dafa9ca760aa9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use proc_macro::TokenStream;
use quote::quote;
use syn::{
    DeriveInput, Token,
    parse::{Parse, ParseStream},
    parse_macro_input,
};

/// Parsed content of `#[space_root_test_generic(...)]`.
struct GenericArgs {
    types: Vec<syn::Type>,
}

impl Parse for GenericArgs {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut types = Vec::new();
        if !input.is_empty() {
            types.push(input.parse()?);
            while !input.is_empty() {
                let _: Token![,] = input.parse()?;
                if !input.is_empty() {
                    types.push(input.parse()?);
                }
            }
        }
        Ok(GenericArgs { types })
    }
}

pub(crate) fn internal_space_root_test_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    let name = &input.ident;

    // Extract generic args from `#[space_root_test_generic(...)]`
    let generics = input
        .attrs
        .iter()
        .find(|attr| attr.path().is_ident("space_root_test_generic"))
        .and_then(|attr| attr.parse_args::<GenericArgs>().ok())
        .unwrap_or(GenericArgs { types: Vec::new() });

    // Build the turbofish segment if there are generic args
    let turbofish = if generics.types.is_empty() {
        quote! {}
    } else {
        let params = &generics.types[..];
        quote! { ::< #(#params),* > }
    };

    let test_mod_name = syn::Ident::new(
        &format!(
            "test_{}_space_root",
            just_fmt::snake_case!(name.to_string())
        ),
        name.span(),
    );

    let expanded = quote! {
        #[cfg(test)]
        mod #test_mod_name {
            use super::*;
            use shared_functions::rola_test_sandbox;
            use space_system::{Space, SpaceRoot, SpaceRootFindPattern};
            use std::env::set_current_dir;

            #[tokio::test]
            async fn test_create_space() {
                let sandbox = rola_test_sandbox(stringify!(#name));
                set_current_dir(&*sandbox).unwrap();

                let mut space = Space::new(#name #turbofish ::default());

                match #name #turbofish ::get_pattern() {
                    SpaceRootFindPattern::AbsolutePath(path_buf) => {
                        let dir = sandbox.join("root");
                        println!("Redirect absolute path:");
                        println!("  from: `{}`", path_buf.display());
                        println!("    to: `{}`", dir.display());
                        space.set_override_pattern(Some(
                            SpaceRootFindPattern::AbsolutePath(dir.clone()),
                        ));

                        println!("Checking if {} absolute directory does not exist before initialization", stringify!(#name));
                        assert!(!dir.exists());

                        space.init_here().await.unwrap();

                        println!("Checking if {} absolute directory exists after initialization", stringify!(#name));
                        assert!(dir.exists());
                        println!("\u{001b}[33;1mwarning\u{001b}[0m: Absolute path test completed in isolated environment, may not fully represent system runtime conditions");

                        return;
                    },
                    _ => {}
                }

                println!("Checking if {} does not exist before initialization", stringify!(#name));
                assert!(space.space_dir_current().is_err());

                space.init_here().await.unwrap();

                println!("Checking if {} exists after initialization", stringify!(#name));
                assert!(space.space_dir_current().is_ok());
            }
        }
    };

    TokenStream::from(expanded)
}