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
389
390
// Property-based tests for CI/CD integration features.
//
// Feature: s3rm-rs, Property 48: Non-Interactive Environment Detection
// For any execution in a non-interactive environment (no TTY) without --force,
// the tool should error to prevent unsafe unconfirmed deletions. With --force
// or --dry-run, the tool proceeds without prompting.
// **Validates: Requirements 13.1**
//
// Feature: s3rm-rs, Property 49: Output Stream Separation
// For any log output, the tool should write all log messages (including errors)
// to stdout via tracing-subscriber by default.
// **Validates: Requirements 13.6**
#[cfg(test)]
mod tests {
use crate::callback::event_manager::EventManager;
use crate::callback::filter_manager::FilterManager;
use crate::config::{Config, FilterConfig, ForceRetryConfig, TracingConfig};
use crate::safety::{PromptHandler, SafetyChecker};
use crate::types::StoragePath;
use crate::types::error::S3rmError;
use anyhow::Result;
use proptest::prelude::*;
// -----------------------------------------------------------------------
// Mock PromptHandlers
// -----------------------------------------------------------------------
/// Mock prompt handler that simulates a non-interactive (non-TTY) environment.
/// read_confirmation should never be called in non-interactive mode.
struct NonInteractiveHandler;
impl PromptHandler for NonInteractiveHandler {
fn read_confirmation(&self, _target_display: &str, _use_color: bool) -> Result<String> {
unreachable!("read_confirmation must not be called in non-interactive mode")
}
fn is_interactive(&self) -> bool {
false
}
}
/// Mock prompt handler that simulates an interactive (TTY) environment
/// and returns a predetermined response.
struct InteractiveHandler {
response: String,
}
impl InteractiveHandler {
fn new(response: &str) -> Self {
Self {
response: response.to_string(),
}
}
}
impl PromptHandler for InteractiveHandler {
fn read_confirmation(&self, _target_display: &str, _use_color: bool) -> Result<String> {
Ok(self.response.clone())
}
fn is_interactive(&self) -> bool {
true
}
}
// -----------------------------------------------------------------------
// Test helpers
// -----------------------------------------------------------------------
fn make_config(dry_run: bool, force: bool, json_tracing: bool, disable_color: bool) -> Config {
Config {
target: StoragePath::S3 {
bucket: "test-bucket".to_string(),
prefix: String::new(),
},
show_no_progress: false,
target_client_config: None,
force_retry_config: ForceRetryConfig {
force_retry_count: 0,
force_retry_interval_milliseconds: 0,
},
tracing_config: Some(TracingConfig {
tracing_level: log::Level::Info,
json_tracing,
aws_sdk_tracing: false,
span_events_tracing: false,
disable_color_tracing: disable_color,
}),
worker_size: 4,
warn_as_error: false,
dry_run,
rate_limit_objects: None,
max_parallel_listings: 1,
object_listing_queue_size: 1000,
max_parallel_listing_max_depth: 1,
allow_parallel_listings_in_express_one_zone: false,
filter_config: FilterConfig::default(),
max_keys: 1000,
auto_complete_shell: None,
event_callback_lua_script: None,
filter_callback_lua_script: None,
allow_lua_os_library: false,
allow_lua_unsafe_vm: false,
lua_vm_memory_limit: 0,
lua_callback_timeout_milliseconds: 0,
if_match: false,
max_delete: None,
filter_manager: FilterManager::new(),
event_manager: EventManager::new(),
batch_size: 1000,
delete_all_versions: false,
force,
test_user_defined_callback: false,
}
}
// -----------------------------------------------------------------------
// Feature: s3rm-rs, Property 48: Non-Interactive Environment Detection
// **Validates: Requirements 13.1**
//
// When the environment is non-interactive (no TTY) and --force is not set,
// the SafetyChecker MUST return an error to prevent unsafe unconfirmed
// deletions. With --force or --dry-run, the tool proceeds without prompting.
// -----------------------------------------------------------------------
proptest! {
#![proptest_config(ProptestConfig::with_cases(100))]
/// Feature: s3rm-rs, Property 48: Non-Interactive Environment Detection
/// **Validates: Requirements 13.1**
///
/// In a non-interactive environment, when --force or --dry-run is set,
/// the operation proceeds without prompting.
#[test]
fn property_48_non_interactive_with_force_or_dry_run_succeeds(
force in proptest::bool::ANY,
dry_run in proptest::bool::ANY,
json_tracing in proptest::bool::ANY,
) {
// Feature: s3rm-rs, Property 48: Non-Interactive Environment Detection
// **Validates: Requirements 13.1**
prop_assume!(force || dry_run);
let config = make_config(dry_run, force, json_tracing, false);
let checker = SafetyChecker::with_prompt_handler(
&config,
Box::new(NonInteractiveHandler),
);
let result = checker.check_before_deletion();
prop_assert!(
result.is_ok(),
"Non-interactive with force={} or dry_run={} must succeed, \
but got error: {:?}",
force,
dry_run,
result.err(),
);
}
/// Feature: s3rm-rs, Property 48: Non-Interactive Environment Detection
/// **Validates: Requirements 13.1**
///
/// In a non-interactive environment without --force and without --dry-run,
/// the operation MUST fail with an InvalidConfig error.
#[test]
fn property_48_non_interactive_without_force_errors(
json_tracing in proptest::bool::ANY,
) {
// Feature: s3rm-rs, Property 48: Non-Interactive Environment Detection
// **Validates: Requirements 13.1**
let config = make_config(false, false, json_tracing, false);
let checker = SafetyChecker::with_prompt_handler(
&config,
Box::new(NonInteractiveHandler),
);
let result = checker.check_before_deletion();
prop_assert!(
result.is_err(),
"Non-interactive without --force must error"
);
let err = result.unwrap_err();
let s3rm_err = err.downcast_ref::<S3rmError>().unwrap();
prop_assert!(
matches!(s3rm_err, S3rmError::InvalidConfig(_)),
"Expected InvalidConfig error, got: {:?}",
s3rm_err,
);
}
/// Feature: s3rm-rs, Property 48: Non-Interactive Environment Detection
/// **Validates: Requirements 13.1**
///
/// In an interactive environment without dry-run, force, or JSON logging,
/// the confirmation prompt IS exercised. This is the contrast case proving
/// that non-TTY detection is the mechanism that disables prompts.
#[test]
fn property_48_interactive_env_does_prompt(
response in "[a-zA-Z0-9 ]{0,20}",
) {
// Feature: s3rm-rs, Property 48: Non-Interactive Environment Detection
// **Validates: Requirements 13.1**
prop_assume!(response.trim() != "yes");
let config = make_config(false, false, false, false);
let checker = SafetyChecker::with_prompt_handler(
&config,
Box::new(InteractiveHandler::new(&response)),
);
let result = checker.check_before_deletion();
prop_assert!(
result.is_err(),
"Interactive environment with non-'yes' input should be rejected"
);
let err = result.unwrap_err();
let s3rm_err = err.downcast_ref::<S3rmError>().unwrap();
prop_assert_eq!(s3rm_err, &S3rmError::Cancelled);
}
}
/// Feature: s3rm-rs, Property 48: Non-Interactive Environment Detection
/// **Validates: Requirements 13.1**
///
/// Verify non-interactive without force errors even with no tracing config.
#[test]
fn property_48_non_interactive_no_tracing_config_errors() {
let mut config = make_config(false, false, false, false);
config.tracing_config = None;
let checker = SafetyChecker::with_prompt_handler(&config, Box::new(NonInteractiveHandler));
let result = checker.check_before_deletion();
assert!(
result.is_err(),
"Non-interactive without --force must error even without tracing config"
);
}
/// Feature: s3rm-rs, Property 48: Non-Interactive Environment Detection
/// **Validates: Requirements 13.1**
///
/// Verify non-interactive with force succeeds even with no tracing config.
#[test]
fn property_48_non_interactive_no_tracing_config_with_force_succeeds() {
let mut config = make_config(false, true, false, false);
config.tracing_config = None;
let checker = SafetyChecker::with_prompt_handler(&config, Box::new(NonInteractiveHandler));
let result = checker.check_before_deletion();
assert!(result.is_ok(), "Non-interactive with --force must succeed");
}
/// Feature: s3rm-rs, Property 48: Non-Interactive Environment Detection
/// **Validates: Requirements 13.1**
///
/// Verify that JSON logging without --force errors (interactive handler,
/// but JSON logging makes the environment non-interactive for prompts).
#[test]
fn property_48_json_logging_without_force_errors() {
let config = make_config(false, false, true, false);
let checker =
SafetyChecker::with_prompt_handler(&config, Box::new(InteractiveHandler::new("no")));
let result = checker.check_before_deletion();
assert!(result.is_err(), "JSON logging without --force must error");
}
/// Feature: s3rm-rs, Property 48: Non-Interactive Environment Detection
/// **Validates: Requirements 13.1**
///
/// Verify that JSON logging with --force succeeds.
#[test]
fn property_48_json_logging_with_force_succeeds() {
let config = make_config(false, true, true, false);
let checker =
SafetyChecker::with_prompt_handler(&config, Box::new(InteractiveHandler::new("no")));
let result = checker.check_before_deletion();
assert!(result.is_ok(), "JSON logging with --force must succeed");
}
// -----------------------------------------------------------------------
// Feature: s3rm-rs, Property 49: Output Stream Separation
// **Validates: Requirements 13.6**
//
// All log messages (including errors) are written to stdout via
// tracing-subscriber by default. This is verified by checking that
// the tracing configuration uses fmt() (which defaults to stdout)
// without any explicit stderr writer override.
// -----------------------------------------------------------------------
proptest! {
#![proptest_config(ProptestConfig::with_cases(100))]
/// Feature: s3rm-rs, Property 49: Output Stream Separation
/// **Validates: Requirements 13.6**
///
/// For any verbosity flag combination parsed from CLI args, the resulting
/// Config produces a TracingConfig (or None for silent mode) that preserves
/// the json_tracing flag correctly — ensuring JSON mode uses the same
/// stdout-based tracing path.
#[test]
fn property_49_cli_tracing_config_json_propagation(
verbosity_idx in 0..6usize,
json_tracing in proptest::bool::ANY,
) {
// Feature: s3rm-rs, Property 49: Output Stream Separation
// **Validates: Requirements 13.6**
use crate::config::args::parse_from_args;
let verbosity_flags: Vec<Vec<&str>> = vec![
vec!["-qq"],
vec!["-q"],
vec![],
vec!["-v"],
vec!["-vv"],
vec!["-vvv"],
];
let mut args: Vec<&str> = vec!["s3rm", "s3://bucket/prefix/"];
args.extend(verbosity_flags[verbosity_idx].clone());
if json_tracing {
args.push("--json-tracing");
args.push("--force");
}
let cli = parse_from_args(args).unwrap();
let config = Config::try_from(cli).unwrap();
// Silent mode (-qq): no tracing config at all — acceptable per design
// All other modes: TracingConfig is present and json_tracing is propagated
if let Some(tc) = &config.tracing_config {
prop_assert_eq!(tc.json_tracing, json_tracing,
"json_tracing flag must propagate through CLI -> Config pipeline");
}
}
}
/// Feature: s3rm-rs, Property 49: Output Stream Separation
/// **Validates: Requirements 13.6**
///
/// Verify that the tracing config from a default CLI invocation (no special
/// flags) produces a configuration that will route to stdout.
#[test]
fn property_49_default_cli_routes_to_stdout() {
use crate::config::args::parse_from_args;
let args = vec!["s3rm", "s3://bucket/prefix/"];
let cli = parse_from_args(args).unwrap();
let config = Config::try_from(cli).unwrap();
// Default verbosity is Warn level with tracing enabled
let tc = config
.tracing_config
.expect("Default config must have tracing enabled");
assert!(!tc.json_tracing, "JSON tracing off by default");
assert!(!tc.aws_sdk_tracing, "AWS SDK tracing off by default");
assert!(!tc.span_events_tracing, "Span events off by default");
// init_tracing uses tracing_subscriber::fmt() which defaults to stdout.
// No .with_writer(stderr) is called — all output goes to stdout.
}
/// Feature: s3rm-rs, Property 49: Output Stream Separation
/// **Validates: Requirements 13.6**
///
/// Verify JSON logging mode also routes to stdout (not stderr).
#[test]
fn property_49_json_logging_routes_to_stdout() {
use crate::config::args::parse_from_args;
let args = vec![
"s3rm",
"s3://bucket/prefix/",
"--json-tracing",
"--force",
"-v",
];
let cli = parse_from_args(args).unwrap();
let config = Config::try_from(cli).unwrap();
let tc = config
.tracing_config
.expect("JSON tracing must have tracing enabled");
assert!(tc.json_tracing, "JSON tracing must be enabled");
// init_tracing calls subscriber_builder.json().init() which still
// uses the default stdout writer. No stderr override exists.
}
}