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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#![allow(clippy::ignore_without_reason)] // Test file with known limitations
#![allow(missing_docs)]
//! CLI Contract Tests: `ruchy fuzz`
//!
//! **Purpose**: Validate user-facing contract (exit codes, stdio, fuzz reporting)
//! **Layer 4**: CLI expectation testing (black-box validation)
//!
//! **Contract Specification**:
//! - Exit code 0: Fuzz testing completed successfully
//! - Exit code 1: Fuzz testing found crashes OR file not found OR cargo-fuzz missing
//! - stdout: Fuzz report (text/json format)
//! - stderr: Error messages (crashes, missing dependencies)
//! - Options: --iterations, --timeout, --format, --output, --verbose
//!
//! **Reference**: docs/specifications/15-tool-improvement-spec.md (v4.0)
//! **TICR**: docs/testing/TICR-ANALYSIS.md (fuzz: 0.4 → target 0.5)
//!
//! **Note**: These tests are lightweight and don't actually run cargo-fuzz
//! (which can take minutes). They test CLI contract only.
use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;
/// Helper: Create ruchy command
fn ruchy_cmd() -> Command {
assert_cmd::cargo::cargo_bin_cmd!("ruchy")
}
/// Helper: Create temp file with content
fn create_temp_file(dir: &TempDir, name: &str, content: &str) -> std::path::PathBuf {
let path = dir.path().join(name);
fs::write(&path, content).expect("Failed to write temp file");
path
}
// ============================================================================
// CLI CONTRACT TESTS: BASIC BEHAVIOR
// ============================================================================
#[test]
#[ignore = "Fuzz testing takes too long for regular CI"]
fn cli_fuzz_valid_program_runs() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(&temp, "simple.ruchy", "let x = 42\n");
// Note: This will likely fail if cargo-fuzz not installed
// But it tests the CLI contract
let result = ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--iterations")
.arg("10") // Very few iterations
.assert();
// Either succeeds or fails gracefully with error message
let output = result.get_output();
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
let combined = format!("{stdout}{stderr}");
// Should mention fuzz or error about missing cargo-fuzz
assert!(
combined.contains("fuzz") || combined.contains("cargo") || combined.contains("not found"),
"Output should mention fuzzing or missing dependencies"
);
}
#[test]
fn cli_fuzz_missing_file_fails() {
ruchy_cmd()
.arg("fuzz")
.arg("nonexistent_xyz.ruchy")
.arg("--iterations")
.arg("10")
.assert()
.failure(); // Exit code != 0
}
#[test]
fn cli_fuzz_syntax_error_fails() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(&temp, "invalid.ruchy", "let x = \n");
ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--iterations")
.arg("10")
.assert()
.failure(); // Exit code != 0
}
// ============================================================================
// CLI CONTRACT TESTS: ITERATIONS OPTION
// ============================================================================
#[test]
fn cli_fuzz_custom_iterations() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(&temp, "iter_test.ruchy", "let x = 42\n");
ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--iterations")
.arg("100") // Custom iteration count
.assert();
// Just verify the command accepts iteration parameter
}
#[test]
fn cli_fuzz_invalid_iterations_format() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(&temp, "bad_iter.ruchy", "let x = 42\n");
ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--iterations")
.arg("invalid") // Invalid number
.assert()
.failure(); // Should fail due to invalid argument
}
// ============================================================================
// CLI CONTRACT TESTS: TIMEOUT OPTION
// ============================================================================
#[test]
fn cli_fuzz_custom_timeout() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(&temp, "timeout_test.ruchy", "let x = 42\n");
ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--timeout")
.arg("500") // 500ms timeout
.arg("--iterations")
.arg("10")
.assert();
// Just verify the command accepts timeout parameter
}
#[test]
fn cli_fuzz_invalid_timeout_format() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(&temp, "bad_timeout.ruchy", "let x = 42\n");
ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--timeout")
.arg("invalid") // Invalid timeout
.assert()
.failure(); // Should fail due to invalid argument
}
// ============================================================================
// CLI CONTRACT TESTS: FORMAT OPTIONS
// ============================================================================
#[test]
#[ignore = "Fuzz testing takes too long"]
fn cli_fuzz_text_format() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(&temp, "text_test.ruchy", "let x = 42\n");
let result = ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--format")
.arg("text")
.arg("--iterations")
.arg("10")
.assert();
// Check that format flag is accepted
let output = result.get_output();
let combined = format!(
"{}{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
assert!(
combined.contains("fuzz") || combined.contains("not found"),
"Should handle text format"
);
}
#[test]
fn cli_fuzz_json_format() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(&temp, "json_test.ruchy", "let x = 42\n");
let result = ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--format")
.arg("json")
.arg("--iterations")
.arg("10")
.assert();
// Check that JSON format flag is accepted
let output = result.get_output();
let combined = format!(
"{}{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
assert!(!combined.is_empty(), "Should produce output in JSON format");
}
// ============================================================================
// CLI CONTRACT TESTS: OUTPUT FILE
// ============================================================================
#[test]
fn cli_fuzz_output_to_file() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(&temp, "output_test.ruchy", "let x = 42\n");
let output_file = temp.path().join("fuzz_report.txt");
ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--output")
.arg(&output_file)
.arg("--iterations")
.arg("10")
.assert();
// Note: File may or may not be created depending on cargo-fuzz availability
}
// ============================================================================
// CLI CONTRACT TESTS: ERROR MESSAGES
// ============================================================================
#[test]
fn cli_fuzz_missing_file_writes_stderr() {
ruchy_cmd()
.arg("fuzz")
.arg("missing.ruchy")
.arg("--iterations")
.arg("10")
.assert()
.failure()
.stderr(
predicate::str::contains("no bin target")
.or(predicate::str::contains("failed to build"))
.or(predicate::str::contains("Error")),
);
}
#[test]
fn cli_fuzz_syntax_error_writes_stderr() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(&temp, "bad_syntax.ruchy", "fun f( { }\n");
ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--iterations")
.arg("10")
.assert()
.failure()
.stderr(predicate::str::is_empty().not()); // stderr NOT empty
}
// ============================================================================
// CLI CONTRACT TESTS: VERBOSE MODE
// ============================================================================
#[test]
fn cli_fuzz_verbose_flag() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(&temp, "verbose.ruchy", "let x = 42\n");
ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--verbose")
.arg("--iterations")
.arg("10")
.assert();
// Just verify verbose flag is accepted
}
// ============================================================================
// CLI CONTRACT TESTS: EDGE CASES
// ============================================================================
#[test]
fn cli_fuzz_empty_file_fails() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(&temp, "empty.ruchy", "");
// Empty files should fail
ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--iterations")
.arg("10")
.assert()
.failure();
}
#[test]
fn cli_fuzz_complex_program() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(
&temp,
"complex.ruchy",
r"
fun factorial(n) {
if n <= 1 {
1
} else {
n * factorial(n - 1)
}
}
println(factorial(5))
",
);
ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--iterations")
.arg("10")
.assert();
// Complex program should be accepted
}
// ============================================================================
// CLI CONTRACT TESTS: HELP AND USAGE
// ============================================================================
#[test]
fn cli_fuzz_help_flag() {
ruchy_cmd()
.arg("fuzz")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("fuzz").or(predicate::str::contains("Fuzz")));
}
// ============================================================================
// CLI CONTRACT TESTS: ZERO ITERATIONS EDGE CASE
// ============================================================================
#[test]
fn cli_fuzz_zero_iterations() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(&temp, "zero_iter.ruchy", "let x = 42\n");
// Zero iterations might be valid (no tests run) or invalid
ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--iterations")
.arg("0")
.assert();
// Just verify command handles this case
}
#[test]
fn cli_fuzz_very_large_iterations() {
let temp = TempDir::new().unwrap();
let file = create_temp_file(&temp, "large_iter.ruchy", "let x = 42\n");
// Large number of iterations (would take very long)
ruchy_cmd()
.arg("fuzz")
.arg(&file)
.arg("--iterations")
.arg("1000000000") // 1 billion iterations
.timeout(std::time::Duration::from_secs(2))
.assert();
// Timeout will kill it, but tests CLI accepts the parameter
}