Skip to main content

nils_test_support/
fixtures.rs

1use std::path::{Path, PathBuf};
2
3pub struct RestSetupFixture {
4    _temp: tempfile::TempDir,
5    pub root: PathBuf,
6    pub setup_dir: PathBuf,
7}
8
9impl RestSetupFixture {
10    /// Creates a temp dir with `setup/rest/` and returns paths.
11    pub fn new() -> Self {
12        let temp = tempfile::TempDir::new().expect("tempdir");
13        let root = temp.path().to_path_buf();
14        let setup_dir = root.join("setup/rest");
15        std::fs::create_dir_all(&setup_dir).expect("create setup/rest");
16        Self {
17            _temp: temp,
18            root,
19            setup_dir,
20        }
21    }
22
23    /// Writes setup/rest/endpoints.env (base file; overridden by endpoints.local.env).
24    pub fn write_endpoints_env(&self, contents: &str) -> PathBuf {
25        write_text(&self.setup_dir.join("endpoints.env"), contents)
26    }
27
28    /// Writes setup/rest/endpoints.local.env (overrides endpoints.env).
29    pub fn write_endpoints_local_env(&self, contents: &str) -> PathBuf {
30        write_text(&self.setup_dir.join("endpoints.local.env"), contents)
31    }
32
33    /// Writes setup/rest/tokens.env (base file; overridden by tokens.local.env).
34    pub fn write_tokens_env(&self, contents: &str) -> PathBuf {
35        write_text(&self.setup_dir.join("tokens.env"), contents)
36    }
37
38    /// Writes setup/rest/tokens.local.env (overrides tokens.env).
39    pub fn write_tokens_local_env(&self, contents: &str) -> PathBuf {
40        write_text(&self.setup_dir.join("tokens.local.env"), contents)
41    }
42}
43
44impl Default for RestSetupFixture {
45    fn default() -> Self {
46        Self::new()
47    }
48}
49
50pub struct GraphqlSetupFixture {
51    _temp: tempfile::TempDir,
52    pub root: PathBuf,
53    pub setup_dir: PathBuf,
54}
55
56impl GraphqlSetupFixture {
57    /// Creates a temp dir with `setup/graphql/` and returns paths.
58    pub fn new() -> Self {
59        let temp = tempfile::TempDir::new().expect("tempdir");
60        let root = temp.path().to_path_buf();
61        let setup_dir = root.join("setup/graphql");
62        std::fs::create_dir_all(&setup_dir).expect("create setup/graphql");
63        Self {
64            _temp: temp,
65            root,
66            setup_dir,
67        }
68    }
69
70    /// Writes setup/graphql/endpoints.env (base file; overridden by endpoints.local.env).
71    pub fn write_endpoints_env(&self, contents: &str) -> PathBuf {
72        write_text(&self.setup_dir.join("endpoints.env"), contents)
73    }
74
75    /// Writes setup/graphql/endpoints.local.env (overrides endpoints.env).
76    pub fn write_endpoints_local_env(&self, contents: &str) -> PathBuf {
77        write_text(&self.setup_dir.join("endpoints.local.env"), contents)
78    }
79
80    /// Writes setup/graphql/jwts.env (base file; overridden by jwts.local.env).
81    pub fn write_jwts_env(&self, contents: &str) -> PathBuf {
82        write_text(&self.setup_dir.join("jwts.env"), contents)
83    }
84
85    /// Writes setup/graphql/jwts.local.env (overrides jwts.env).
86    pub fn write_jwts_local_env(&self, contents: &str) -> PathBuf {
87        write_text(&self.setup_dir.join("jwts.local.env"), contents)
88    }
89
90    /// Writes setup/graphql/schema.env (points to a schema file).
91    pub fn write_schema_env(&self, contents: &str) -> PathBuf {
92        write_text(&self.setup_dir.join("schema.env"), contents)
93    }
94
95    /// Writes a schema file under setup/graphql (use with schema.env).
96    pub fn write_schema_file(&self, name: &str, contents: &str) -> PathBuf {
97        write_text(&self.setup_dir.join(name), contents)
98    }
99}
100
101impl Default for GraphqlSetupFixture {
102    fn default() -> Self {
103        Self::new()
104    }
105}
106
107pub struct SuiteFixture {
108    _temp: tempfile::TempDir,
109    pub root: PathBuf,
110    pub suite_path: PathBuf,
111}
112
113impl SuiteFixture {
114    /// Creates a temp dir for a suite.json and related files.
115    pub fn new() -> Self {
116        let temp = tempfile::TempDir::new().expect("tempdir");
117        let root = temp.path().to_path_buf();
118        let suite_path = root.join("suite.json");
119        Self {
120            _temp: temp,
121            root,
122            suite_path,
123        }
124    }
125
126    /// Writes a minimal REST suite manifest and request file.
127    pub fn write_minimal_rest_suite(&self, case_id: &str, request_rel: &str) -> PathBuf {
128        let request_path = self.root.join(request_rel);
129        write_text(
130            &request_path,
131            r#"{"method":"GET","path":"/health","expect":{"status":200}}"#,
132        );
133        let manifest = format!(
134            r#"{{
135  "version": 1,
136  "cases": [
137    {{
138      "id": "{case_id}",
139      "type": "rest",
140      "request": "{request_rel}"
141    }}
142  ]
143}}"#
144        );
145        write_text(&self.suite_path, &manifest)
146    }
147
148    /// Writes a minimal GraphQL suite manifest and operation file.
149    pub fn write_minimal_graphql_suite(&self, case_id: &str, op_rel: &str) -> PathBuf {
150        let op_path = self.root.join(op_rel);
151        write_text(&op_path, "query Health { __typename }\n");
152        let manifest = format!(
153            r#"{{
154  "version": 1,
155  "cases": [
156    {{
157      "id": "{case_id}",
158      "type": "graphql",
159      "op": "{op_rel}"
160    }}
161  ]
162}}"#
163        );
164        write_text(&self.suite_path, &manifest)
165    }
166}
167
168impl Default for SuiteFixture {
169    fn default() -> Self {
170        Self::new()
171    }
172}
173
174pub fn write_text(path: &Path, contents: &str) -> PathBuf {
175    if let Some(parent) = path.parent() {
176        std::fs::create_dir_all(parent).expect("mkdir");
177    }
178    std::fs::write(path, contents).expect("write fixture");
179    path.to_path_buf()
180}