guardy 0.2.4

Fast, secure git hooks in Rust with secret scanning and protected file synchronization
Documentation
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use std::{collections::HashMap, sync::Arc};

use guardy::{
    config::hooks::{HookCommand, HookCondition, HookDefinition, HookScript},
    hooks::conditions::{ConditionEvaluator, should_skip},
};

#[tokio::test]
async fn test_executor_basic_command() {
    // Since executor relies on global CONFIG, we test the command structure
    // and verify it's properly formed for execution
    let mut commands = HashMap::new();
    let test_command = HookCommand {
        run: "echo 'Test Output'".to_string(),
        description: "Test echo command".to_string(),
        ..Default::default()
    };

    // Verify command structure
    assert_eq!(test_command.run, "echo 'Test Output'");
    assert_eq!(test_command.description, "Test echo command");
    assert!(!test_command.continue_on_error);
    assert!(!test_command.interactive);

    commands.insert("test-echo".to_string(), test_command);

    let hook = HookDefinition {
        parallel: false,
        skip: false,
        commands,
        scripts: HashMap::new(),
        ..Default::default()
    };

    // Verify hook structure
    assert!(!hook.parallel);
    assert!(!hook.skip);
    assert_eq!(hook.commands.len(), 1);
    assert!(hook.commands.contains_key("test-echo"));

    println!("✅ Basic command execution structure validated");
}

#[tokio::test]
async fn test_condition_evaluation() {
    let evaluator = ConditionEvaluator::new();

    // Test boolean conditions
    let bool_true = HookCondition::Bool(true);
    let bool_false = HookCondition::Bool(false);

    assert!(evaluator.evaluate(&bool_true).unwrap());
    assert!(!evaluator.evaluate(&bool_false).unwrap());

    // Test array conditions with simple values
    let always_true = HookCondition::Array(vec!["true".to_string()]);
    let always_false = HookCondition::Array(vec!["false".to_string()]);
    let mixed = HookCondition::Array(vec!["false".to_string(), "true".to_string()]);

    assert!(evaluator.evaluate(&always_true).unwrap());
    assert!(!evaluator.evaluate(&always_false).unwrap());
    assert!(evaluator.evaluate(&mixed).unwrap()); // OR logic

    println!("✅ Condition evaluation tests passed");
}

#[tokio::test]
async fn test_should_skip_logic() {
    let evaluator = ConditionEvaluator::new();

    // Test skip=true (should skip)
    let skip_true = HookCondition::Bool(true);
    let only_false = HookCondition::Bool(false);
    assert!(should_skip(&skip_true, &only_false, &evaluator).unwrap());

    // Test skip=false, only=false (should not skip)
    let skip_false = HookCondition::Bool(false);
    assert!(!should_skip(&skip_false, &only_false, &evaluator).unwrap());

    // Test skip=false with only conditions that don't match (should skip)
    let only_conditions = HookCondition::Array(vec!["branch:nonexistent".to_string()]);
    assert!(should_skip(&skip_false, &only_conditions, &evaluator).unwrap());

    // Test skip=false with only=true (should not skip)
    let only_true = HookCondition::Bool(true);
    assert!(!should_skip(&skip_false, &only_true, &evaluator).unwrap());

    println!("✅ Should skip logic tests passed");
}

#[tokio::test]
async fn test_priority_based_execution() {
    // Test that commands have correct priority values for ordering
    let mut commands = HashMap::new();

    let high_priority = HookCommand {
        run: "echo 'High Priority'".to_string(),
        description: "High priority command".to_string(),
        priority: -10, // Lower number = higher priority
        ..Default::default()
    };

    let medium_priority = HookCommand {
        run: "echo 'Medium Priority'".to_string(),
        description: "Medium priority command".to_string(),
        ..Default::default()
    };

    let low_priority = HookCommand {
        run: "echo 'Low Priority'".to_string(),
        description: "Low priority command".to_string(),
        priority: 10, // Higher number = lower priority
        continue_on_error: false,
        all_files: false,
        glob: Arc::new(vec![]),
        file_types: vec![],
        stage_fixed: false,
        skip: HookCondition::Bool(false),
        only: HookCondition::Bool(false),
        env: HashMap::new(),
        root: None,
        exclude: vec![],
        fail_text: None,
        interactive: false,
        use_stdin: false,
        tags: vec![],
        files: None,
    };

    // Verify priority values are correct for sorting
    assert_eq!(high_priority.priority, -10);
    assert_eq!(medium_priority.priority, 0);
    assert_eq!(low_priority.priority, 10);

    // Verify that priorities would sort correctly
    let mut priorities = vec![
        low_priority.priority,
        high_priority.priority,
        medium_priority.priority,
    ];
    priorities.sort();
    assert_eq!(priorities, vec![-10, 0, 10]);

    commands.insert("high-priority".to_string(), high_priority);
    commands.insert("medium-priority".to_string(), medium_priority);
    commands.insert("low-priority".to_string(), low_priority);

    let hook = HookDefinition {
        parallel: false,
        skip: false,
        commands,
        scripts: HashMap::new(),
        ..Default::default()
    };

    // Verify all commands are in the hook
    assert_eq!(hook.commands.len(), 3);
    assert!(hook.commands.contains_key("high-priority"));
    assert!(hook.commands.contains_key("medium-priority"));
    assert!(hook.commands.contains_key("low-priority"));

    println!("✅ Priority-based execution ordering validated");
}

#[tokio::test]
async fn test_file_glob_patterns() {
    let command = HookCommand {
        run: "echo 'Processing files'".to_string(),
        description: "Test glob patterns".to_string(),
        priority: 0,
        continue_on_error: false,
        all_files: false,
        glob: Arc::new(vec!["*.rs".to_string(), "*.toml".to_string()]),
        file_types: vec![],
        stage_fixed: false,
        skip: HookCondition::Bool(false),
        only: HookCondition::Bool(false),
        env: HashMap::new(),
        root: None,
        exclude: vec!["target/**".to_string()],
        fail_text: None,
        interactive: false,
        use_stdin: false,
        tags: vec![],
        files: None,
    };

    // Test that glob patterns are properly configured
    assert_eq!(command.glob.len(), 2);
    assert!(command.glob.contains(&"*.rs".to_string()));
    assert!(command.glob.contains(&"*.toml".to_string()));
    assert_eq!(command.exclude.len(), 1);
    assert!(command.exclude.contains(&"target/**".to_string()));

    println!("✅ File glob pattern tests passed");
}

#[tokio::test]
async fn test_environment_variables() {
    let mut env = HashMap::new();
    env.insert("TEST_VAR".to_string(), "test_value".to_string());
    env.insert("ANOTHER_VAR".to_string(), "another_value".to_string());

    let command = HookCommand {
        run: "echo $TEST_VAR".to_string(),
        description: "Test environment variables".to_string(),
        priority: 0,
        continue_on_error: false,
        all_files: false,
        glob: Arc::new(vec![]),
        file_types: vec![],
        stage_fixed: false,
        skip: HookCondition::Bool(false),
        only: HookCondition::Bool(false),
        env: env.clone(),
        root: None,
        exclude: vec![],
        fail_text: None,
        interactive: false,
        use_stdin: false,
        tags: vec![],
        files: None,
    };

    assert_eq!(command.env.len(), 2);
    assert_eq!(command.env.get("TEST_VAR"), Some(&"test_value".to_string()));
    assert_eq!(
        command.env.get("ANOTHER_VAR"),
        Some(&"another_value".to_string())
    );

    println!("✅ Environment variable tests passed");
}

#[tokio::test]
async fn test_script_configuration() {
    let mut scripts = HashMap::new();

    let mut env = HashMap::new();
    env.insert("NODE_ENV".to_string(), "test".to_string());

    scripts.insert(
        "test-script.js".to_string(),
        HookScript {
            runner: "node".to_string(),
            env: env.clone(),
        },
    );

    scripts.insert(
        "test-script.py".to_string(),
        HookScript {
            runner: "python3".to_string(),
            env: HashMap::new(),
        },
    );

    let hook = HookDefinition {
        parallel: false,
        skip: false,
        commands: HashMap::new(),
        scripts,
        ..Default::default()
    };

    assert_eq!(hook.scripts.len(), 2);
    assert!(hook.scripts.contains_key("test-script.js"));
    assert!(hook.scripts.contains_key("test-script.py"));

    let node_script = hook.scripts.get("test-script.js").unwrap();
    assert_eq!(node_script.runner, "node");
    assert_eq!(node_script.env.get("NODE_ENV"), Some(&"test".to_string()));

    let python_script = hook.scripts.get("test-script.py").unwrap();
    assert_eq!(python_script.runner, "python3");
    assert!(python_script.env.is_empty());

    println!("✅ Script configuration tests passed");
}

#[tokio::test]
async fn test_interactive_and_stdin_modes() {
    let interactive_command = HookCommand {
        run: "interactive_command".to_string(),
        description: "Interactive command".to_string(),
        priority: 0,
        continue_on_error: false,
        all_files: false,
        glob: Arc::new(vec![]),
        file_types: vec![],
        stage_fixed: false,
        skip: HookCondition::Bool(false),
        only: HookCondition::Bool(false),
        env: HashMap::new(),
        root: None,
        exclude: vec![],
        fail_text: None,
        interactive: true,
        use_stdin: false,
        tags: vec![],
        files: None,
    };

    let stdin_command = HookCommand {
        run: "stdin_command".to_string(),
        description: "Stdin command".to_string(),
        priority: 0,
        continue_on_error: false,
        all_files: false,
        glob: Arc::new(vec![]),
        file_types: vec![],
        stage_fixed: false,
        skip: HookCondition::Bool(false),
        only: HookCondition::Bool(false),
        env: HashMap::new(),
        root: None,
        exclude: vec![],
        fail_text: None,
        interactive: false,
        use_stdin: true,
        tags: vec![],
        files: None,
    };

    assert!(interactive_command.interactive);
    assert!(!interactive_command.use_stdin);
    assert!(!stdin_command.interactive);
    assert!(stdin_command.use_stdin);

    println!("✅ Interactive and stdin mode tests passed");
}

#[tokio::test]
async fn test_fail_text_and_continue_on_error() {
    let command_with_fail_text = HookCommand {
        run: "failing_command".to_string(),
        description: "Command that might fail".to_string(),
        priority: 0,
        continue_on_error: true,
        all_files: false,
        glob: Arc::new(vec![]),
        file_types: vec![],
        stage_fixed: false,
        skip: HookCondition::Bool(false),
        only: HookCondition::Bool(false),
        env: HashMap::new(),
        root: None,
        exclude: vec![],
        fail_text: Some("Custom failure message".to_string()),
        interactive: false,
        use_stdin: false,
        tags: vec![],
        files: None,
    };

    assert!(command_with_fail_text.continue_on_error);
    assert_eq!(
        command_with_fail_text.fail_text,
        Some("Custom failure message".to_string())
    );

    println!("✅ Fail text and continue on error tests passed");
}

#[tokio::test]
async fn test_tags_and_categorization() {
    let tagged_command = HookCommand {
        run: "tagged_command".to_string(),
        description: "Tagged command".to_string(),
        tags: vec![
            "linting".to_string(),
            "formatting".to_string(),
            "quick".to_string(),
        ],
        ..Default::default()
    };

    assert_eq!(tagged_command.tags.len(), 3);
    assert!(tagged_command.tags.contains(&"linting".to_string()));
    assert!(tagged_command.tags.contains(&"formatting".to_string()));
    assert!(tagged_command.tags.contains(&"quick".to_string()));

    println!("✅ Tags and categorization tests passed");
}

#[tokio::test]
async fn test_working_directory_root() {
    let command_with_root = HookCommand {
        run: "command_in_subdir".to_string(),
        description: "Command with root directory".to_string(),
        priority: 0,
        continue_on_error: false,
        all_files: false,
        glob: Arc::new(vec![]),
        file_types: vec![],
        stage_fixed: false,
        skip: HookCondition::Bool(false),
        only: HookCondition::Bool(false),
        env: HashMap::new(),
        root: Some("./packages/guardy".to_string()),
        exclude: vec![],
        fail_text: None,
        interactive: false,
        use_stdin: false,
        tags: vec![],
        files: None,
    };

    assert_eq!(
        command_with_root.root,
        Some("./packages/guardy".to_string())
    );

    println!("✅ Working directory root tests passed");
}

#[test]
fn test_parallel_execution_configuration() {
    let parallel_hook = HookDefinition {
        parallel: true,
        skip: false,
        commands: HashMap::new(),
        scripts: HashMap::new(),
        ..Default::default()
    };

    let sequential_hook = HookDefinition {
        parallel: false,
        skip: false,
        commands: HashMap::new(),
        scripts: HashMap::new(),
        ..Default::default()
    };

    assert!(parallel_hook.parallel);
    assert!(!sequential_hook.parallel);

    println!("✅ Parallel execution configuration tests passed");
}

#[tokio::test]
async fn test_file_placeholder_substitution() {
    // Test command with file placeholders
    let command_with_placeholders = HookCommand {
        run: "echo 'Files: {files}' && echo 'Staged: {staged_files}' && echo 'All: {all_files}'"
            .to_string(),
        description: "Test file placeholder substitution".to_string(),
        glob: Arc::new(vec!["*.rs".to_string()]),
        exclude: vec!["target/**".to_string()],
        tags: vec!["file-processing".to_string()],
        ..Default::default()
    };

    // Verify the command has placeholders that need substitution
    assert!(command_with_placeholders.run.contains("{files}"));
    assert!(command_with_placeholders.run.contains("{staged_files}"));
    assert!(command_with_placeholders.run.contains("{all_files}"));

    // Verify glob and exclude patterns are configured
    assert_eq!(command_with_placeholders.glob.len(), 1);
    assert!(command_with_placeholders.glob.contains(&"*.rs".to_string()));
    assert_eq!(command_with_placeholders.exclude.len(), 1);
    assert!(
        command_with_placeholders
            .exclude
            .contains(&"target/**".to_string())
    );

    println!("✅ File placeholder substitution test structure validated");
}

#[tokio::test]
async fn test_push_files_placeholder() {
    // Test command specific to pre-push hook with {push_files} placeholder
    let push_command = HookCommand {
        run: "echo 'Push files: {push_files}'".to_string(),
        description: "Test push files placeholder".to_string(),
        tags: vec!["pre-push".to_string()],
        ..Default::default()
    };

    // Verify the command has the push_files placeholder
    assert!(push_command.run.contains("{push_files}"));

    println!("✅ Push files placeholder test structure validated");
}

#[tokio::test]
async fn test_stage_fixed_functionality() {
    // Test command with stage_fixed enabled
    let formatting_command = HookCommand {
        run: "cargo fmt {files}".to_string(),
        description: "Format Rust files and stage changes".to_string(),
        glob: Arc::new(vec!["*.rs".to_string()]),
        stage_fixed: true, // This should stage files after formatting
        exclude: vec!["target/**".to_string()],
        tags: vec!["formatting".to_string()],
        ..Default::default()
    };

    // Verify stage_fixed is enabled and command uses file placeholders
    assert!(formatting_command.stage_fixed);
    assert!(formatting_command.run.contains("{files}"));
    assert!(formatting_command.glob.contains(&"*.rs".to_string()));

    println!("✅ Stage fixed functionality test structure validated");
}