rgrc 0.6.12

Rusty Generic Colouriser
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;

// Include the modules from src
#[path = "../src/style.rs"]
mod style;

#[path = "../src/enhanced_regex.rs"]
mod enhanced_regex;

#[path = "../src/grc.rs"]
mod grc;

use grc::{GrcConfigReader, GrcatConfigEntry, GrcatConfigReader};

/// Helper function to get the project root directory
fn get_project_root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}

/// Helper function to get share directory path
fn get_share_dir() -> PathBuf {
    get_project_root().join("share")
}

/// Helper function to get etc directory path
fn get_etc_dir() -> PathBuf {
    get_project_root().join("etc")
}

#[cfg(test)]
mod grc_config_reader_tests {
    use super::*;

    #[test]
    fn test_grc_conf_parsing() {
        let conf_path = get_etc_dir().join("rgrc.conf");
        assert!(
            conf_path.exists(),
            "grc.conf should exist at {:?}",
            conf_path
        );

        let file = File::open(&conf_path).expect("Failed to open grc.conf");
        let reader = BufReader::new(file);
        let grc_reader = GrcConfigReader::new(reader.lines());

        let configs: Vec<_> = grc_reader.collect();
        assert!(
            !configs.is_empty(),
            "grc.conf should contain configuration entries"
        );

        // Verify each entry has valid regex and config file path
        for (regex, config_file) in &configs {
            assert!(
                !config_file.is_empty(),
                "Config file path should not be empty"
            );
            // Test that regex can match something (is_match now returns bool directly)
            let _ = regex.is_match("test"); // Just verify it doesn't panic
        }

        println!(
            "Successfully parsed {} configuration entries from grc.conf",
            configs.len()
        );
    }

    #[test]
    fn test_grc_conf_specific_commands() {
        let conf_path = get_etc_dir().join("rgrc.conf");
        let file = File::open(&conf_path).expect("Failed to open grc.conf");
        let reader = BufReader::new(file);
        let grc_reader = GrcConfigReader::new(reader.lines());

        let configs: Vec<_> = grc_reader.collect();

        // Check for common commands
        let command_configs: Vec<_> = configs
            .iter()
            .map(|(regex, config)| (regex.as_str(), config.as_str()))
            .collect();

        // Verify some expected commands are present
        let has_ping = command_configs
            .iter()
            .any(|(_, config)| config.contains("ping"));
        let has_ls = command_configs
            .iter()
            .any(|(_, config)| config.contains("ls"));
        let has_diff = command_configs
            .iter()
            .any(|(_, config)| config.contains("diff"));

        assert!(
            has_ping || has_ls || has_diff,
            "grc.conf should contain at least one common command configuration"
        );
    }

    #[test]
    fn test_grc_conf_regex_patterns() {
        let conf_path = get_etc_dir().join("rgrc.conf");
        let file = File::open(&conf_path).expect("Failed to open grc.conf");
        let reader = BufReader::new(file);
        let grc_reader = GrcConfigReader::new(reader.lines());

        for (regex, _config_file) in grc_reader {
            // Test that each regex can be used for matching (is_match now returns bool directly)
            let _ = regex.is_match(""); // Just verify it doesn't panic
        }
    }

    #[test]
    fn test_grc_conf_skip_comments() {
        let conf_path = get_etc_dir().join("rgrc.conf");
        let file = File::open(&conf_path).expect("Failed to open grc.conf");
        let reader = BufReader::new(file);
        let grc_reader = GrcConfigReader::new(reader.lines());

        let configs: Vec<_> = grc_reader.collect();

        // Ensure no config file paths contain comment markers
        for (_, config_file) in &configs {
            assert!(
                !config_file.starts_with('#'),
                "Config file paths should not start with #: {}",
                config_file
            );
        }
    }
}

#[cfg(test)]
mod grcat_config_reader_tests {
    use super::*;

    /// Get all conf.* files from share directory
    fn get_all_conf_files() -> Vec<PathBuf> {
        let share_dir = get_share_dir();
        let mut conf_files = Vec::new();

        if let Ok(entries) = std::fs::read_dir(&share_dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                if let Some(filename) = path.file_name()
                    && filename.to_string_lossy().starts_with("conf.")
                {
                    conf_files.push(path);
                }
            }
        }

        conf_files.sort();
        conf_files
    }

    #[test]
    fn test_all_conf_files_exist() {
        let conf_files = get_all_conf_files();
        assert!(
            !conf_files.is_empty(),
            "Share directory should contain conf.* files"
        );

        println!("Found {} configuration files:", conf_files.len());
        for file in &conf_files {
            println!("  - {:?}", file.file_name().unwrap());
        }
    }

    #[test]
    fn test_parse_all_conf_files() {
        let conf_files = get_all_conf_files();
        let mut successful_parses = 0;
        let mut total_entries = 0;
        let mut files_with_issues = Vec::new();

        for conf_file in &conf_files {
            let filename = conf_file.file_name().unwrap().to_string_lossy();

            match File::open(conf_file) {
                Ok(file) => {
                    let reader = BufReader::new(file);
                    let grcat_reader = GrcatConfigReader::new(reader.lines());

                    // Collect entries, noting that some may be skipped due to unsupported styles
                    let entries: Vec<GrcatConfigEntry> = grcat_reader.collect();

                    successful_parses += 1;
                    total_entries += entries.len();

                    if entries.is_empty() {
                        files_with_issues.push(filename.to_string());
                    }

                    println!("{}: {} entries", filename, entries.len());
                }
                Err(e) => {
                    panic!("Failed to open {}: {}", filename, e);
                }
            }
        }

        assert_eq!(
            successful_parses,
            conf_files.len(),
            "All conf files should be parseable"
        );

        if !files_with_issues.is_empty() {
            println!("\nFiles with no parsed entries (may use unsupported styles):");
            for file in &files_with_issues {
                println!("  - {}", file);
            }
        }

        println!(
            "\nTotal: {} files, {} entries",
            successful_parses, total_entries
        );
        // Allow some files to have no entries due to unsupported styles
        assert!(
            total_entries > 0,
            "Should have parsed at least some configuration entries"
        );
    }

    #[test]
    fn test_conf_ls_specific_patterns() {
        let conf_path = get_share_dir().join("conf.ls");
        let file = File::open(&conf_path).expect("conf.ls should exist");
        let reader = BufReader::new(file);
        let grcat_reader = GrcatConfigReader::new(reader.lines());

        let entries: Vec<GrcatConfigEntry> = grcat_reader.collect();
        assert!(
            !entries.is_empty(),
            "conf.ls should contain at least one entry"
        );

        // Verify each entry has valid regex
        for entry in &entries {
            let _ = entry.regex.is_match(""); // Just verify it doesn't panic
        }

        println!("conf.ls contains {} pattern entries", entries.len());
    }

    #[test]
    fn test_conf_ping_specific_patterns() {
        let conf_path = get_share_dir().join("conf.ping");
        let file = File::open(&conf_path).expect("conf.ping should exist");
        let reader = BufReader::new(file);
        let grcat_reader = GrcatConfigReader::new(reader.lines());

        let entries: Vec<GrcatConfigEntry> = grcat_reader.collect();
        assert!(
            !entries.is_empty(),
            "conf.ping should contain at least one entry"
        );

        println!("conf.ping contains {} pattern entries", entries.len());
    }

    #[test]
    fn test_conf_diff_specific_patterns() {
        let conf_path = get_share_dir().join("conf.diff");
        let file = File::open(&conf_path).expect("conf.diff should exist");
        let reader = BufReader::new(file);
        let grcat_reader = GrcatConfigReader::new(reader.lines());

        let entries: Vec<GrcatConfigEntry> = grcat_reader.collect();
        assert!(
            !entries.is_empty(),
            "conf.diff should contain at least one entry"
        );

        println!("conf.diff contains {} pattern entries", entries.len());
    }

    #[test]
    fn test_conf_netstat_patterns() {
        let conf_path = get_share_dir().join("conf.netstat");
        if conf_path.exists() {
            let file = File::open(&conf_path).expect("conf.netstat should be readable");
            let reader = BufReader::new(file);
            let grcat_reader = GrcatConfigReader::new(reader.lines());

            let entries: Vec<GrcatConfigEntry> = grcat_reader.collect();
            assert!(
                !entries.is_empty(),
                "conf.netstat should contain at least one entry"
            );

            println!("conf.netstat contains {} pattern entries", entries.len());
        }
    }

    #[test]
    fn test_all_conf_files_have_valid_regexes() {
        let conf_files = get_all_conf_files();
        let mut files_tested = 0;
        let mut total_regexes_tested = 0;

        for conf_file in &conf_files {
            let _filename = conf_file.file_name().unwrap().to_string_lossy();

            if let Ok(file) = File::open(conf_file) {
                let reader = BufReader::new(file);
                let grcat_reader = GrcatConfigReader::new(reader.lines());

                for entry in grcat_reader {
                    // Test that each regex can match an empty string without errors
                    let _ = entry.regex.is_match(""); // is_match now returns bool directly
                    total_regexes_tested += 1;
                }

                files_tested += 1;
            }
        }

        println!(
            "Validated {} regexes across {} files",
            total_regexes_tested, files_tested
        );
        assert_eq!(files_tested, conf_files.len());
        println!("Note: Files with unsupported styles (like 'reverse') may have fewer entries");
    }

    #[test]
    fn test_all_conf_files_color_definitions() {
        let conf_files = get_all_conf_files();

        for conf_file in &conf_files {
            let _filename = conf_file.file_name().unwrap().to_string_lossy();

            if let Ok(file) = File::open(conf_file) {
                let reader = BufReader::new(file);
                let grcat_reader = GrcatConfigReader::new(reader.lines());

                for entry in grcat_reader {
                    // Each entry should have a valid regex (is_match now returns bool directly)
                    let _ = entry.regex.is_match(""); // Just verify it doesn't panic

                    // Colors vector can be empty (default) or contain styles
                    // Just verify it's a valid vector
                    let _colors = &entry.colors;
                }
            }
        }
    }

    /// Test individual conf files by name
    #[test]
    fn test_specific_conf_files() {
        let test_files = vec![
            "conf.ant",
            "conf.blkid",
            "conf.configure",
            "conf.curl",
            "conf.cvs",
            "conf.df",
            "conf.dig",
            "conf.dnf",
            "conf.docker-machinels",
            "conf.dockerimages",
            "conf.dockerinfo",
            "conf.dockernetwork",
            "conf.dockerps",
            "conf.dockerpull",
            "conf.dockersearch",
            "conf.dockerversion",
            "conf.du",
            "conf.env",
            "conf.fdisk",
            "conf.findmnt",
            "conf.free",
            "conf.gcc",
            "conf.getfacl",
            "conf.getsebool",
            "conf.go-test",
            "conf.id",
            "conf.ifconfig",
            "conf.iostat_sar",
            "conf.ip",
            "conf.ipaddr",
            "conf.ipneighbor",
            "conf.iproute",
            "conf.iptables",
            "conf.irclog",
            "conf.iwconfig",
            "conf.jobs",
            "conf.kubectl",
            "conf.last",
            "conf.ldap",
            "conf.log",
            "conf.lolcat",
            "conf.lsattr",
            "conf.lsblk",
            "conf.lsmod",
            "conf.lsof",
            "conf.lspci",
            "conf.lsusb",
            "conf.mount",
            "conf.mtr",
            "conf.mvn",
            "conf.nmap",
            "conf.ntpdate",
            "conf.php",
            "conf.ping2",
            "conf.proftpd",
            "conf.ps",
            "conf.pv",
            "conf.semanageboolean",
            "conf.semanagefcontext",
            "conf.semanageuser",
            "conf.sensors",
            "conf.showmount",
            "conf.sockstat",
            "conf.sql",
            "conf.ss",
            "conf.stat",
            "conf.sysctl",
            "conf.systemctl",
            "conf.tcpdump",
            "conf.traceroute",
            "conf.tune2fs",
            "conf.ulimit",
            "conf.uptime",
            "conf.vmstat",
            "conf.wdiff",
            "conf.whois",
            "conf.yaml",
        ];

        let share_dir = get_share_dir();
        let mut found_count = 0;
        let mut parsed_count = 0;

        for filename in &test_files {
            let conf_path = share_dir.join(filename);
            if conf_path.exists() {
                found_count += 1;

                if let Ok(file) = File::open(&conf_path) {
                    let reader = BufReader::new(file);
                    let grcat_reader = GrcatConfigReader::new(reader.lines());
                    let entries: Vec<_> = grcat_reader.collect();

                    if !entries.is_empty() {
                        parsed_count += 1;
                    }
                }
            }
        }

        println!(
            "Found {}/{} test files, successfully parsed {} with entries",
            found_count,
            test_files.len(),
            parsed_count
        );
        assert!(
            found_count > 0,
            "Should find at least some test configuration files"
        );
    }

    #[test]
    fn test_conf_log_count_field_parsing() {
        let conf_path = get_share_dir().join("conf.log");
        let file = File::open(&conf_path).expect("conf.log should exist");
        let reader = BufReader::new(file);
        let grcat_reader = GrcatConfigReader::new(reader.lines());

        let entries: Vec<GrcatConfigEntry> = grcat_reader.collect();
        assert!(
            !entries.is_empty(),
            "conf.log should contain at least one entry"
        );

        // Check that count fields are parsed correctly
        let mut found_once = false;
        let mut found_more = false;
        let mut found_stop = false;

        for entry in &entries {
            match entry.count {
                grc::GrcatConfigEntryCount::Once => found_once = true,
                grc::GrcatConfigEntryCount::More => found_more = true,
                grc::GrcatConfigEntryCount::Stop => found_stop = true,
            }
        }

        assert!(found_once, "Should find at least one 'once' count entry");
        assert!(found_more, "Should find at least one 'more' count entry");
        assert!(found_stop, "Should find at least one 'stop' count entry");

        println!(
            "conf.log count field parsing: once={}, more={}, stop={}",
            found_once, found_more, found_stop
        );
    }

    #[test]
    fn test_conf_ping2_replace_field_parsing() {
        let conf_path = get_share_dir().join("conf.ping2");
        let file = File::open(&conf_path).expect("conf.ping2 should exist");
        let reader = BufReader::new(file);
        let grcat_reader = GrcatConfigReader::new(reader.lines());

        let entries: Vec<GrcatConfigEntry> = grcat_reader.collect();
        assert!(
            !entries.is_empty(),
            "conf.ping2 should contain at least one entry"
        );

        // Check that replace field is parsed correctly
        let mut found_replace = false;
        let mut replace_value = String::new();

        for entry in &entries {
            if !entry.replace.is_empty() {
                found_replace = true;
                replace_value = entry.replace.clone();
                break;
            }
        }

        assert!(
            found_replace,
            "Should find at least one entry with replace field"
        );
        assert_eq!(
            replace_value, "TIMEOUT \\1",
            "Replace value should match expected pattern"
        );

        println!(
            "conf.ping2 replace field parsing: found replace='{}'",
            replace_value
        );
    }

    #[test]
    fn test_count_and_replace_default_values() {
        // Test with a simple config that doesn't specify count or replace
        let config_content = r#"
regexp=test pattern
colours=red
======
"#;

        let reader = BufReader::new(config_content.as_bytes());
        let grcat_reader = GrcatConfigReader::new(reader.lines());

        let entries: Vec<GrcatConfigEntry> = grcat_reader.collect();
        assert_eq!(entries.len(), 1, "Should parse one entry");

        let entry = &entries[0];
        // Check default values
        assert!(
            matches!(entry.count, grc::GrcatConfigEntryCount::More),
            "Default count should be More"
        );
        assert!(
            entry.replace.is_empty(),
            "Default replace should be empty string"
        );

        println!("Default values test passed: count=More, replace=''");
    }

    #[test]
    fn test_conf_netstat_skip_field_parsing() {
        let conf_path = get_share_dir().join("conf.netstat");
        let file = File::open(&conf_path).expect("conf.netstat should exist");
        let reader = BufReader::new(file);
        let grcat_reader = GrcatConfigReader::new(reader.lines());

        let entries: Vec<GrcatConfigEntry> = grcat_reader.collect();
        assert!(
            !entries.is_empty(),
            "conf.netstat should contain at least one entry"
        );

        // Check that skip fields are parsed correctly
        let mut found_skip_true = false;
        let mut found_skip_false = false;

        for entry in &entries {
            if entry.skip {
                found_skip_true = true;
            } else {
                found_skip_false = true;
            }
        }

        assert!(
            found_skip_true,
            "Should find at least one entry with skip=true"
        );
        assert!(
            found_skip_false,
            "Should find at least one entry with skip=false"
        );

        println!(
            "conf.netstat skip field parsing: skip_true={}, skip_false={}",
            found_skip_true, found_skip_false
        );
    }

    #[test]
    fn test_conf_sockstat_skip_field_parsing() {
        let conf_path = get_share_dir().join("conf.sockstat");
        let file = File::open(&conf_path).expect("conf.sockstat should exist");
        let reader = BufReader::new(file);
        let grcat_reader = GrcatConfigReader::new(reader.lines());

        let entries: Vec<GrcatConfigEntry> = grcat_reader.collect();
        assert!(
            !entries.is_empty(),
            "conf.sockstat should contain at least one entry"
        );

        // Check that skip fields are parsed correctly
        let mut found_skip_true = false;
        let mut found_skip_false = false;

        for entry in &entries {
            if entry.skip {
                found_skip_true = true;
            } else {
                found_skip_false = true;
            }
        }

        assert!(
            found_skip_true,
            "Should find at least one entry with skip=true"
        );
        assert!(
            found_skip_false,
            "Should find at least one entry with skip=false"
        );

        println!(
            "conf.sockstat skip field parsing: skip_true={}, skip_false={}",
            found_skip_true, found_skip_false
        );
    }

    #[test]
    fn test_conf_ss_skip_field_parsing() {
        let conf_path = get_share_dir().join("conf.ss");
        let file = File::open(&conf_path).expect("conf.ss should exist");
        let reader = BufReader::new(file);
        let grcat_reader = GrcatConfigReader::new(reader.lines());

        let entries: Vec<GrcatConfigEntry> = grcat_reader.collect();
        assert!(
            !entries.is_empty(),
            "conf.ss should contain at least one entry"
        );

        // Check that skip fields are parsed correctly
        let mut found_skip_true = false;
        let mut found_skip_false = false;

        for entry in &entries {
            if entry.skip {
                found_skip_true = true;
            } else {
                found_skip_false = true;
            }
        }

        assert!(
            found_skip_true,
            "Should find at least one entry with skip=true"
        );
        assert!(
            found_skip_false,
            "Should find at least one entry with skip=false"
        );

        println!(
            "conf.ss skip field parsing: skip_true={}, skip_false={}",
            found_skip_true, found_skip_false
        );
    }

    #[test]
    fn test_skip_default_values() {
        // Test with a simple config that doesn't specify skip
        let config_content = r#"
regexp=test pattern
colours=red
======
"#;

        let reader = BufReader::new(config_content.as_bytes());
        let grcat_reader = GrcatConfigReader::new(reader.lines());

        let entries: Vec<GrcatConfigEntry> = grcat_reader.collect();
        assert_eq!(entries.len(), 1, "Should parse one entry");

        assert!(
            !entries[0].skip,
            "Skip should default to false when not specified"
        );
        println!("Skip default value test passed: skip=false");
    }
}

#[cfg(test)]
mod integration_tests {
    use super::*;

    #[test]
    fn test_grc_conf_references_valid_conf_files() {
        let grc_conf_path = get_etc_dir().join("rgrc.conf");
        let file = File::open(&grc_conf_path).expect("Failed to open grc.conf");
        let reader = BufReader::new(file);
        let grc_reader = GrcConfigReader::new(reader.lines());

        let share_dir = get_share_dir();
        let mut missing_files = Vec::new();
        let mut found_files = 0;

        for (_regex, config_file) in grc_reader {
            // Check if the referenced config file exists in share directory
            let conf_path = share_dir.join(&config_file);
            if conf_path.exists() {
                found_files += 1;
            } else {
                missing_files.push(config_file.clone());
            }
        }

        if !missing_files.is_empty() {
            println!(
                "Warning: grc.conf references {} missing files:",
                missing_files.len()
            );
            for file in &missing_files {
                println!("  - {}", file);
            }
        }

        println!(
            "Found {} referenced configuration files in share/",
            found_files
        );
        assert!(
            found_files > 0,
            "At least some referenced files should exist"
        );
    }

    #[test]
    fn test_complete_workflow_grc_conf_to_grcat() {
        let grc_conf_path = get_etc_dir().join("rgrc.conf");
        let file = File::open(&grc_conf_path).expect("Failed to open grc.conf");
        let reader = BufReader::new(file);
        let grc_reader = GrcConfigReader::new(reader.lines());

        let share_dir = get_share_dir();
        let mut workflows_tested = 0;
        let mut total_workflows = 0;

        for (_regex, config_file) in grc_reader {
            let conf_path = share_dir.join(&config_file);
            if conf_path.exists() {
                total_workflows += 1;
                // Test complete workflow: grc.conf entry -> grcat config file
                if let Ok(file) = File::open(&conf_path) {
                    let reader = BufReader::new(file);
                    let grcat_reader = GrcatConfigReader::new(reader.lines());
                    let _entries: Vec<_> = grcat_reader.collect();

                    // Count as successful workflow even if entries is empty
                    workflows_tested += 1;
                }
            }
        }

        println!(
            "Successfully tested {}/{} complete workflows (grc.conf -> grcat config)",
            workflows_tested, total_workflows
        );
        assert!(
            workflows_tested > 0,
            "Should test at least one complete workflow"
        );
    }

    #[test]
    fn test_all_conf_files_in_share_are_valid() {
        let conf_files = get_all_conf_files();
        let mut valid_files = 0;
        let mut invalid_files = Vec::new();

        for conf_file in &conf_files {
            let filename = conf_file.file_name().unwrap().to_string_lossy().to_string();

            match File::open(conf_file) {
                Ok(file) => {
                    let reader = BufReader::new(file);
                    let grcat_reader = GrcatConfigReader::new(reader.lines());

                    // Try to collect all entries - this will validate parsing
                    let entries: Vec<_> = grcat_reader.collect();

                    // Even if a file has no entries, as long as it doesn't error, it's valid
                    valid_files += 1;

                    if entries.is_empty() {
                        println!("  {} - no entries (possibly all comments)", filename);
                    }
                }
                Err(e) => {
                    invalid_files.push((filename.clone(), e.to_string()));
                }
            }
        }

        if !invalid_files.is_empty() {
            println!("Invalid files:");
            for (file, error) in &invalid_files {
                println!("  - {}: {}", file, error);
            }
        }

        println!(
            "Valid configuration files: {}/{}",
            valid_files,
            conf_files.len()
        );
        assert_eq!(
            valid_files,
            conf_files.len(),
            "All configuration files should be valid"
        );
    }

    /// Helper to get all conf files
    fn get_all_conf_files() -> Vec<PathBuf> {
        let share_dir = get_share_dir();
        let mut conf_files = Vec::new();

        if let Ok(entries) = std::fs::read_dir(&share_dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                if let Some(filename) = path.file_name()
                    && filename.to_string_lossy().starts_with("conf.")
                {
                    conf_files.push(path);
                }
            }
        }

        conf_files.sort();
        conf_files
    }
}

#[cfg(test)]
mod edge_case_tests {
    use super::*;

    #[test]
    fn test_empty_colors_handling() {
        // Some entries might not have colors defined
        let conf_path = get_share_dir().join("conf.dummy");
        if conf_path.exists() {
            let file = File::open(&conf_path).expect("Should open conf.dummy");
            let reader = BufReader::new(file);
            let grcat_reader = GrcatConfigReader::new(reader.lines());

            for entry in grcat_reader {
                // Should handle entries with or without colors
                let _colors = &entry.colors;
            }
        }
    }

    #[test]
    fn test_complex_regex_patterns() {
        // Test files with complex regex patterns
        let test_files = vec!["conf.ls", "conf.ping", "conf.netstat", "conf.iptables"];
        let share_dir = get_share_dir();

        for filename in &test_files {
            let conf_path = share_dir.join(filename);
            if conf_path.exists() {
                let file =
                    File::open(&conf_path).unwrap_or_else(|_| panic!("Should open {}", filename));
                let reader = BufReader::new(file);
                let grcat_reader = GrcatConfigReader::new(reader.lines());

                for entry in grcat_reader {
                    // Verify complex regexes are properly parsed (is_match now returns bool directly)
                    let _ = entry.regex.is_match("test"); // Just verify it doesn't panic
                }
            }
        }
    }

    #[test]
    fn test_multiple_color_definitions() {
        let conf_path = get_share_dir().join("conf.ls");
        if conf_path.exists() {
            let file = File::open(&conf_path).expect("Should open conf.ls");
            let reader = BufReader::new(file);
            let grcat_reader = GrcatConfigReader::new(reader.lines());

            let entries: Vec<_> = grcat_reader.collect();

            // conf.ls should have entries with multiple colors for capture groups
            let has_multiple_colors = entries.iter().any(|e| e.colors.len() > 1);

            if has_multiple_colors {
                println!("conf.ls has entries with multiple color definitions");
            }
        }
    }
}