-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_run_function.rs
More file actions
281 lines (230 loc) · 7.54 KB
/
Copy pathtest_run_function.rs
File metadata and controls
281 lines (230 loc) · 7.54 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//! Tests for the run() function in lib.rs
//!
//! These tests exercise the CLI argument parsing and template rendering
//! through the public run() function, providing coverage for the main
//! entry point logic.
use std::fs;
use std::io::Write;
use tempfile::TempDir;
use tmpltool::run;
/// Helper to create a temporary template file
fn create_template(dir: &TempDir, name: &str, content: &str) -> String {
let path = dir.path().join(name);
let mut file = fs::File::create(&path).unwrap();
write!(file, "{}", content).unwrap();
path.to_string_lossy().to_string()
}
#[test]
fn test_run_with_template_file() {
let temp_dir = TempDir::new().unwrap();
let template_path = create_template(&temp_dir, "test.tmpl", "Hello World");
let result = run(["tmpltool", &template_path]);
assert!(result.is_ok());
}
#[test]
fn test_run_with_output_file() {
let temp_dir = TempDir::new().unwrap();
let template_path = create_template(&temp_dir, "test.tmpl", "Hello Output");
let output_path = temp_dir.path().join("output.txt");
let result = run([
"tmpltool",
&template_path,
"-o",
output_path.to_str().unwrap(),
]);
assert!(result.is_ok());
assert!(output_path.exists());
let content = fs::read_to_string(&output_path).unwrap();
assert_eq!(content, "Hello Output");
}
#[test]
fn test_run_with_long_output_flag() {
let temp_dir = TempDir::new().unwrap();
let template_path = create_template(&temp_dir, "test.tmpl", "Long flag test");
let output_path = temp_dir.path().join("output.txt");
let result = run([
"tmpltool",
&template_path,
"--output",
output_path.to_str().unwrap(),
]);
assert!(result.is_ok());
assert!(output_path.exists());
}
#[test]
fn test_run_missing_template_file() {
let result = run(["tmpltool", "/nonexistent/path/template.tmpl"]);
assert!(result.is_err());
let err = result.unwrap_err().to_string();
// Error message differs by OS: Unix uses "No such file", Windows uses "cannot find"
assert!(
err.contains("No such file") || err.contains("cannot find"),
"Unexpected error message: {}",
err
);
}
#[test]
fn test_run_invalid_template_syntax() {
let temp_dir = TempDir::new().unwrap();
let template_path = create_template(&temp_dir, "invalid.tmpl", "{{ invalid syntax }}");
let result = run(["tmpltool", &template_path]);
assert!(result.is_err());
}
#[test]
fn test_run_with_trust_flag() {
let temp_dir = TempDir::new().unwrap();
let template_path = create_template(&temp_dir, "test.tmpl", "Trust mode enabled");
let result = run(["tmpltool", "--trust", &template_path]);
assert!(result.is_ok());
}
#[test]
fn test_run_with_validate_json() {
let temp_dir = TempDir::new().unwrap();
let template_path = create_template(
&temp_dir,
"test.tmpl",
r#"{{ to_json(object={"name": "test"}) }}"#,
);
let result = run(["tmpltool", &template_path, "--validate", "json"]);
assert!(result.is_ok());
}
#[test]
fn test_run_with_validate_json_invalid() {
let temp_dir = TempDir::new().unwrap();
let template_path = create_template(&temp_dir, "test.tmpl", "not valid json {");
let result = run(["tmpltool", &template_path, "--validate", "json"]);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.to_string().contains("JSON validation failed"));
}
#[test]
fn test_run_with_validate_yaml() {
let temp_dir = TempDir::new().unwrap();
let template_path = create_template(&temp_dir, "test.tmpl", "name: test\nvalue: 42");
let result = run(["tmpltool", &template_path, "--validate", "yaml"]);
assert!(result.is_ok());
}
#[test]
fn test_run_with_validate_toml() {
let temp_dir = TempDir::new().unwrap();
let template_path = create_template(&temp_dir, "test.tmpl", "[section]\nkey = \"value\"");
let result = run(["tmpltool", &template_path, "--validate", "toml"]);
assert!(result.is_ok());
}
#[test]
fn test_run_with_env_function() {
let temp_dir = TempDir::new().unwrap();
let template_path = create_template(
&temp_dir,
"test.tmpl",
r#"{{ get_env(name="PATH", default="no-path") }}"#,
);
let result = run(["tmpltool", &template_path]);
assert!(result.is_ok());
}
#[test]
fn test_run_help_flag() {
// --help causes clap to print help and return an error
let result = run(["tmpltool", "--help"]);
assert!(result.is_err());
// The error should be from clap (help is treated as early exit)
let err = result.unwrap_err();
// Clap help error contains the help text
assert!(err.to_string().contains("Usage") || err.to_string().contains("tmpltool"));
}
#[test]
fn test_run_version_flag() {
// --version causes clap to print version and return an error
let result = run(["tmpltool", "--version"]);
assert!(result.is_err());
}
#[test]
fn test_run_invalid_flag() {
let result = run(["tmpltool", "--invalid-flag"]);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.to_string().contains("unexpected argument"));
}
#[test]
fn test_run_with_template_using_include() {
let temp_dir = TempDir::new().unwrap();
// Create a partial template
create_template(&temp_dir, "partial.tmpl", "Included content");
// Create main template that includes the partial
let template_path = create_template(
&temp_dir,
"main.tmpl",
r#"Before {% include "partial.tmpl" %} After"#,
);
let result = run(["tmpltool", &template_path]);
assert!(result.is_ok());
}
#[test]
fn test_run_combined_flags() {
let temp_dir = TempDir::new().unwrap();
let template_path = create_template(
&temp_dir,
"test.tmpl",
r#"{{ to_json(object={"valid": true}) }}"#,
);
let output_path = temp_dir.path().join("output.json");
let result = run([
"tmpltool",
"--trust",
&template_path,
"--output",
output_path.to_str().unwrap(),
"--validate",
"json",
]);
assert!(result.is_ok());
assert!(output_path.exists());
}
#[test]
fn test_run_with_multiline_template() {
let temp_dir = TempDir::new().unwrap();
let template_content = r#"Line 1
Line 2
{% for i in range(3) %}
Item {{ i }}
{% endfor %}
End"#;
let template_path = create_template(&temp_dir, "multiline.tmpl", template_content);
let result = run(["tmpltool", &template_path]);
assert!(result.is_ok());
}
#[test]
fn test_run_empty_template() {
let temp_dir = TempDir::new().unwrap();
let template_path = create_template(&temp_dir, "empty.tmpl", "");
let result = run(["tmpltool", &template_path]);
assert!(result.is_ok());
}
#[test]
fn test_run_template_with_conditionals() {
let temp_dir = TempDir::new().unwrap();
let template_path = create_template(
&temp_dir,
"conditional.tmpl",
r#"{% if true %}Yes{% else %}No{% endif %}"#,
);
let result = run(["tmpltool", &template_path]);
assert!(result.is_ok());
}
#[test]
fn test_run_output_to_nested_directory() {
let temp_dir = TempDir::new().unwrap();
let template_path = create_template(&temp_dir, "test.tmpl", "Nested output");
// Create nested directory
let nested_dir = temp_dir.path().join("nested").join("deep");
fs::create_dir_all(&nested_dir).unwrap();
let output_path = nested_dir.join("output.txt");
let result = run([
"tmpltool",
&template_path,
"-o",
output_path.to_str().unwrap(),
]);
assert!(result.is_ok());
assert!(output_path.exists());
}