big-code-analysis 2.0.0

Tool to compute and export code metrics
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
// Sibling-file unit tests for `src/tools.rs`, wired in via `#[path =
// "tools_tests.rs"] mod tests;`. The `./**/*_tests.rs` rule in
// `.bcaignore` keeps this file out of the self-scan walker so the
// production-file metric caps stay tight even as the test suite
// grows.

#![allow(
    clippy::float_cmp,
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::similar_names,
    clippy::doc_markdown,
    clippy::needless_raw_string_hashes,
    clippy::too_many_lines
)]

use pretty_assertions::assert_eq;

use super::*;

#[test]
fn test_read() {
    let tmp_dir = std::env::temp_dir();
    let tmp_path = tmp_dir.join("test_read");
    let data = vec![
        // UTF-16 LE/BE BOM: the file is skipped, not stripped-and-parsed
        // (issue #803) — the interleaved-NUL body is not UTF-8 source.
        (b"\xFF\xFEabc".to_vec(), None),
        (b"\xFE\xFFabc".to_vec(), None),
        (b"\xEF\xBB\xBFabc".to_vec(), Some(b"abc\n".to_vec())),
        (b"\xEF\xBB\xBFabc\n".to_vec(), Some(b"abc\n".to_vec())),
        (b"\xEF\xBBabc\n".to_vec(), None),
        (b"abcdef\n".to_vec(), Some(b"abcdef\n".to_vec())),
        (b"abcdef".to_vec(), Some(b"abcdef\n".to_vec())),
        // CRLF throughout should be normalised to LF
        (b"abc\r\ndef\r\n".to_vec(), Some(b"abc\ndef\n".to_vec())),
        // UTF-8 BOM + CRLF
        (
            b"\xEF\xBB\xBFabc\r\ndef\r\n".to_vec(),
            Some(b"abc\ndef\n".to_vec()),
        ),
    ];
    for (d, expected) in data {
        write_file(&tmp_path, &d).unwrap();
        let res = read_file_with_eol(&tmp_path).unwrap();
        assert_eq!(res, expected);
    }
}

#[cfg(unix)]
#[test]
fn test_get_language_for_file_non_utf8() {
    use std::ffi::OsStr;
    use std::os::unix::ffi::OsStrExt;

    let path = Path::new(OsStr::from_bytes(b"foo.\xff"));
    assert_eq!(get_language_for_file(path), None);
}

#[cfg(unix)]
#[test]
fn test_guess_language_non_utf8() {
    use std::ffi::OsStr;
    use std::os::unix::ffi::OsStrExt;
    use std::path::PathBuf;

    let path = PathBuf::from(OsStr::from_bytes(b"foo.\xff"));
    let (lang, _name) = guess_language(b"int a = 42;", &path);
    assert_eq!(lang, None);
}

#[test]
fn test_guess_file_no_file_name() {
    let all_files: HashMap<String, Vec<PathBuf>> = HashMap::new();
    let current = Path::new("/some/file.c");
    let result = guess_file(current, "..", &all_files);
    assert!(result.is_empty());
}

/// Regression for issue #297: `#include "../foo.h"` from
/// `src/lib/file.c` must resolve to `src/foo.h`, not the
/// same-directory `src/lib/foo.h` that the prior lexical
/// `normalize_path` collapse left as the closest match.
#[test]
fn guess_file_parent_dir_include_resolves_to_sibling() {
    let mut all_files: HashMap<String, Vec<PathBuf>> = HashMap::new();
    all_files.insert(
        "foo.h".to_string(),
        vec![
            PathBuf::from("/proj/src/foo.h"),
            PathBuf::from("/proj/src/lib/foo.h"),
        ],
    );
    let current = Path::new("/proj/src/lib/file.c");
    let result = guess_file(current, "../foo.h", &all_files);
    assert_eq!(result, vec![PathBuf::from("/proj/src/foo.h")]);
}

/// `../inc/foo.h` from `src/lib/file.c` must resolve to
/// `src/inc/foo.h`, not some other `inc/foo.h` deeper in the
/// tree.
#[test]
fn guess_file_parent_subdir_include_resolves_to_correct_inc() {
    let mut all_files: HashMap<String, Vec<PathBuf>> = HashMap::new();
    all_files.insert(
        "foo.h".to_string(),
        vec![
            PathBuf::from("/proj/src/inc/foo.h"),
            PathBuf::from("/proj/src/lib/inc/foo.h"),
            PathBuf::from("/proj/other/inc/foo.h"),
        ],
    );
    let current = Path::new("/proj/src/lib/file.c");
    let result = guess_file(current, "../inc/foo.h", &all_files);
    assert_eq!(result, vec![PathBuf::from("/proj/src/inc/foo.h")]);
}

/// A plain `foo.h` include from `src/lib/file.c` must keep the
/// existing same-directory preference and resolve to
/// `src/lib/foo.h`.
#[test]
fn guess_file_plain_include_keeps_same_directory_preference() {
    let mut all_files: HashMap<String, Vec<PathBuf>> = HashMap::new();
    all_files.insert(
        "foo.h".to_string(),
        vec![
            PathBuf::from("/proj/src/foo.h"),
            PathBuf::from("/proj/src/lib/foo.h"),
        ],
    );
    let current = Path::new("/proj/src/lib/file.c");
    let result = guess_file(current, "foo.h", &all_files);
    assert_eq!(result, vec![PathBuf::from("/proj/src/lib/foo.h")]);
}

/// A `./foo.h` include from `src/lib/file.c` must still resolve
/// to the same-directory `src/lib/foo.h` (CurDir segments are
/// collapsed before joining).
#[test]
fn guess_file_curdir_include_resolves_to_same_directory() {
    let mut all_files: HashMap<String, Vec<PathBuf>> = HashMap::new();
    all_files.insert(
        "foo.h".to_string(),
        vec![
            PathBuf::from("/proj/src/foo.h"),
            PathBuf::from("/proj/src/lib/foo.h"),
        ],
    );
    let current = Path::new("/proj/src/lib/file.c");
    let result = guess_file(current, "./foo.h", &all_files);
    assert_eq!(result, vec![PathBuf::from("/proj/src/lib/foo.h")]);
}

/// `../../foo.h` from `src/a/b/file.c` must resolve up two
/// levels to `src/foo.h`, not be lexically collapsed.
#[test]
fn guess_file_double_parent_include_resolves_two_levels_up() {
    let mut all_files: HashMap<String, Vec<PathBuf>> = HashMap::new();
    all_files.insert(
        "foo.h".to_string(),
        vec![
            PathBuf::from("/proj/src/foo.h"),
            PathBuf::from("/proj/src/a/foo.h"),
            PathBuf::from("/proj/src/a/b/foo.h"),
        ],
    );
    let current = Path::new("/proj/src/a/b/file.c");
    let result = guess_file(current, "../../foo.h", &all_files);
    assert_eq!(result, vec![PathBuf::from("/proj/src/foo.h")]);
}

/// When the relative target does not match any candidate
/// exactly, the existing basename / same-directory / distance
/// fallback chain still applies. With a single candidate, that
/// candidate is returned even if its path differs from the
/// resolved target.
#[test]
fn guess_file_unique_basename_returns_only_candidate() {
    let mut all_files: HashMap<String, Vec<PathBuf>> = HashMap::new();
    all_files.insert(
        "foo.h".to_string(),
        vec![PathBuf::from("/proj/src/lib/foo.h")],
    );
    let current = Path::new("/proj/src/lib/file.c");
    // Resolved target would be `/proj/foo.h`, which does not
    // exist; the unique-basename short-circuit still wins.
    let result = guess_file(current, "../../foo.h", &all_files);
    assert_eq!(result, vec![PathBuf::from("/proj/src/lib/foo.h")]);
}

/// The `mozilla/` prefix strip must still apply, so
/// `#include "mozilla/foo.h"` from `src/lib/file.c` resolves
/// the same way a bare `foo.h` would.
#[test]
fn guess_file_mozilla_prefix_is_stripped_before_resolution() {
    let mut all_files: HashMap<String, Vec<PathBuf>> = HashMap::new();
    all_files.insert(
        "foo.h".to_string(),
        vec![
            PathBuf::from("/proj/src/foo.h"),
            PathBuf::from("/proj/src/lib/foo.h"),
        ],
    );
    let current = Path::new("/proj/src/lib/file.c");
    let result = guess_file(current, "mozilla/foo.h", &all_files);
    assert_eq!(result, vec![PathBuf::from("/proj/src/lib/foo.h")]);
}

#[test]
fn test_guess_language() {
    let buf = b"// -*- foo: bar; mode: c++; hello: world\n";
    assert_eq!(guess_language(buf, "foo.cpp"), (Some(LANG::Cpp), "cpp"));

    let buf = b"// -*- c++ -*-\n";
    assert_eq!(guess_language(buf, "foo.cpp"), (Some(LANG::Cpp), "cpp"));

    let buf = b"// -*- foo: bar; bar-mode: c++; hello: world\n";
    assert_eq!(
        guess_language(buf, "foo.py"),
        (Some(LANG::Python), "python")
    );

    let buf = b"/* hello world */\n";
    assert_eq!(guess_language(buf, "foo.cpp"), (Some(LANG::Cpp), "cpp"));

    // Since #721 `.c` routes to the dedicated `LANG::C`. When the file
    // extension and an emacs/vim modeline disagree, `guess_language`
    // deliberately trusts the extension (see the "rely on extension"
    // branch), so a `.c` file with a `ft=c++` modeline still resolves to
    // C — the modeline does not override the extension.
    let buf = b"\n\n\n\n\n\n\n\n\n// vim: set ts=4 ft=c++\n\n\n";
    assert_eq!(guess_language(buf, "foo.c"), (Some(LANG::C), "c"));

    let buf = b"\n\n\n\n\n\n\n\n\n\n\n\n";
    assert_eq!(guess_language(buf, "foo.txt"), (None, ""));

    // Objective-C (`.m`) gets its own `LANG::Objc` and reports `"objc"`
    // since #724. Objective-C++ (`.mm`) stays on the C++ grammar (the
    // ObjC grammar can't parse the C++ half of a `.mm` file) and reports
    // `"cpp"` natively — the former `fake`/`#540` override is gone.
    let buf = b"// -*- mode: objc -*-\n";
    assert_eq!(guess_language(buf, "foo.m"), (Some(LANG::Objc), "objc"));
    let buf = b"// -*- foo: bar; mode: Objective-C++; hello: world\n";
    assert_eq!(guess_language(buf, "foo.mm"), (Some(LANG::Cpp), "cpp"));
}

#[test]
fn guess_language_emacs_mode_on_fifth_line() {
    // Regression test for issue #709. The forward scan used `splitn(5)`
    // with an `i == 3` break, so it inspected only the first 4 lines while
    // splitting off an unbounded 5th remainder it never matched against —
    // a mode-line on the 5th line was silently missed. With four blank
    // leading lines, the emacs header sits on the 5th line and must now be
    // detected against an extension-free path so the mode drives the result.
    let buf = b"\n\n\n\n// -*- mode: c++ -*-\n";
    assert_eq!(guess_language(buf, "noext"), (Some(LANG::Cpp), "cpp"));

    // A mode-line on the 6th line is outside the window and must NOT match;
    // the off-by-one fix widens the window to exactly 5 real lines, not the
    // unbounded remainder the old `splitn` tail used to (accidentally) span.
    let buf = b"\n\n\n\n\n// -*- mode: c++ -*-\n";
    assert_eq!(guess_language(buf, "noext"), (None, ""));
}

#[test]
fn guess_language_vim_modeline_with_trailing_blank_lines() {
    // Regression test for issue #709. The backward scan used `rsplitn(5)`,
    // whose first slot is the empty piece after the file's trailing
    // newline; trailing blank lines then consumed the rest of the window
    // before a real modeline was reached. Filtering empty pieces lets the
    // scan cover five real trailing lines regardless of trailing blanks.
    //
    // The six leading body lines push the modeline past the forward
    // scan window (first five lines), so the match can only come from the
    // *backward* scan — the path the fix changed. The three trailing
    // blank lines then fill the old `rsplitn(5)` window's first slots
    // with empty pieces, so the unfiltered scan broke before reaching the
    // modeline. This buffer therefore fails against the pre-fix
    // `rsplitn(5)` and passes only with the empty-piece filter.
    let buf = b"a\nb\nc\nd\ne\nf\n// vim: set ft=c++\n\n\n\n";
    assert_eq!(guess_language(buf, "noext"), (Some(LANG::Cpp), "cpp"));
}

#[test]
fn guess_language_name_outlives_input_buffer() {
    // The returned name is `&'static str`: it must remain valid after
    // the owned input buffer is dropped. This pins the honest lifetime
    // (issue #506) — the name borrows from static tables / literals
    // (`name()`, `fake::get_true`), never from `buf`.
    let name: &'static str = {
        let buf: Vec<u8> = b"// -*- foo: bar; mode: Objective-C++; hello: world\n".to_vec();
        let (lang, name) = guess_language(&buf, "foo.mm");
        assert_eq!(lang, Some(LANG::Cpp));
        // `.mm` stays on `Cpp`; `name` is `LANG::Cpp.name()` directly
        // (the `fake` override was retired with #724) — a `&'static str`
        // from static tables / literals, never borrowed from `buf`
        // (issue #506).
        name
    };
    assert_eq!(name, "cpp");
}

#[test]
fn guess_language_no_match_name_is_static_empty() {
    // The empty/no-match arm must also yield a `&'static str`.
    let name: &'static str = {
        let buf: Vec<u8> = b"\n\n\n\n".to_vec();
        let (lang, name) = guess_language(&buf, "foo.txt");
        assert_eq!(lang, None);
        name
    };
    assert_eq!(name, "");
}

#[test]
fn shebang_bare_bash() {
    assert_eq!(get_shebang_lang(b"#!/bin/bash\n"), Some(LANG::Bash));
}

#[test]
fn shebang_env_python3() {
    assert_eq!(
        get_shebang_lang(b"#!/usr/bin/env python3\n"),
        Some(LANG::Python),
    );
}

#[test]
fn shebang_versioned_perl_with_flag() {
    assert_eq!(
        get_shebang_lang(b"#!/usr/bin/perl5.36 -w\n"),
        Some(LANG::Perl),
    );
}

#[test]
fn shebang_env_dash_s_node() {
    assert_eq!(
        get_shebang_lang(b"#!/usr/bin/env -S node --experimental\n"),
        Some(LANG::Javascript),
    );
}

#[test]
fn shebang_env_with_var_assignment() {
    // `env FOO=bar python3` — skip the assignment, find the interpreter.
    assert_eq!(
        get_shebang_lang(b"#!/usr/bin/env FOO=bar python3\n"),
        Some(LANG::Python),
    );
}

#[test]
fn shebang_env_dash_u_consumes_next_token() {
    // `env -u VAR python3` — `-u` is the only `env` short flag that
    // consumes a following argument (the variable name to unset). Without
    // the special case, `VAR` would be misidentified as the interpreter.
    assert_eq!(
        get_shebang_lang(b"#!/usr/bin/env -u VAR python3\n"),
        Some(LANG::Python),
    );
}

#[test]
fn shebang_versioned_lua() {
    assert_eq!(get_shebang_lang(b"#!/usr/bin/lua5.1\n"), Some(LANG::Lua));
}

#[test]
fn shebang_node() {
    assert_eq!(
        get_shebang_lang(b"#!/usr/local/bin/node\n"),
        Some(LANG::Javascript),
    );
}

#[test]
fn shebang_tclsh() {
    assert_eq!(get_shebang_lang(b"#!/usr/bin/tclsh\n"), Some(LANG::Tcl));
}

#[test]
fn shebang_no_trailing_newline() {
    assert_eq!(get_shebang_lang(b"#!/bin/sh"), Some(LANG::Bash));
}

#[test]
fn shebang_crlf_line_ending() {
    // guess_language usually receives LF-normalised input, but be defensive.
    assert_eq!(get_shebang_lang(b"#!/bin/bash\r\n"), Some(LANG::Bash));
}

#[test]
fn shebang_empty_buffer() {
    assert_eq!(get_shebang_lang(b""), None);
}

#[test]
fn shebang_single_byte() {
    assert_eq!(get_shebang_lang(b"#"), None);
}

#[test]
fn shebang_no_shebang_prefix() {
    assert_eq!(get_shebang_lang(b"// not a shebang\n"), None);
}

#[test]
fn shebang_unknown_interpreter() {
    // `ocaml` is a real interpreter the project does not target —
    // a stable sentinel for the "shebang names an interpreter
    // outside the supported set" case (independent of which
    // languages the workspace happens to recognise today).
    assert_eq!(get_shebang_lang(b"#!/usr/bin/ocaml\n"), None);
}

#[test]
fn shebang_env_only_no_interpreter() {
    assert_eq!(get_shebang_lang(b"#!/usr/bin/env\n"), None);
}

#[test]
fn shebang_non_utf8_returns_none() {
    // Invalid UTF-8 on the shebang line must not panic.
    assert_eq!(get_shebang_lang(b"#!/usr/bin/\xff\xfe\n"), None);
}

#[test]
fn guess_language_extension_wins_over_shebang() {
    // The .py extension must outrank a `#!/bin/sh` shebang.
    let buf = b"#!/bin/sh\nprint('hi')\n";
    assert_eq!(
        guess_language(buf, "foo.py"),
        (Some(LANG::Python), "python")
    );
}

#[test]
fn guess_language_shebang_falls_through_when_no_extension() {
    let buf = b"#!/usr/bin/env python3\nprint('hi')\n";
    assert_eq!(guess_language(buf, "run"), (Some(LANG::Python), "python"));
}

#[test]
fn guess_language_shebang_detects_ruby_without_extension() {
    // Gem executables under `bin/` are extensionless Ruby scripts
    // identified solely by their `#!/usr/bin/env ruby` shebang.
    let buf = b"#!/usr/bin/env ruby\nputs 'hi'\n";
    assert_eq!(guess_language(buf, "run"), (Some(LANG::Ruby), "ruby"));
}

#[test]
fn guess_language_shebang_detects_elixir_without_extension() {
    // Extensionless Elixir scripts (`#!/usr/bin/env elixir`) must be
    // identified by their shebang alone — regression for #186.
    let buf = b"#!/usr/bin/env elixir\nIO.puts(\"hi\")\n";
    assert_eq!(guess_language(buf, "run"), (Some(LANG::Elixir), "elixir"));
}

#[test]
fn guess_language_shebang_detects_iex_without_extension() {
    // `iex` is Elixir's interactive shell; scripts that drive it via
    // `#!/usr/bin/env iex` should also map to Elixir.
    let buf = b"#!/usr/bin/env iex\nIO.puts(\"hi\")\n";
    assert_eq!(guess_language(buf, "run"), (Some(LANG::Elixir), "elixir"));
}

#[test]
fn guess_language_shebang_loses_to_mode_line() {
    // Mode line outranks the shebang.
    let buf = b"#!/usr/bin/env node\n# -*- mode: python -*-\n";
    assert_eq!(guess_language(buf, "run"), (Some(LANG::Python), "python"));
}

#[test]
fn normalize_line_endings_normalizes_crlf() {
    let mut d = b"code\r\n# comment\r\n".to_vec();
    normalize_line_endings(&mut d);
    assert_eq!(d, b"code\n# comment\n");
}

#[test]
fn normalize_line_endings_normalizes_lone_cr() {
    let mut d = b"code\r# comment\r".to_vec();
    normalize_line_endings(&mut d);
    assert_eq!(d, b"code\n# comment\n");
}

#[test]
fn normalize_line_endings_normalizes_cr_before_crlf() {
    // lone CR followed immediately by CRLF → two separate line breaks
    let mut d = b"a\r\r\nb".to_vec();
    normalize_line_endings(&mut d);
    assert_eq!(d, b"a\n\nb\n");
}

#[test]
fn normalize_line_endings_normalizes_crlf_blank_line() {
    let mut d = b"a\r\n\r\nb\r\n".to_vec();
    normalize_line_endings(&mut d);
    assert_eq!(d, b"a\n\nb\n");
}

#[test]
fn normalize_line_endings_empty_buffer() {
    let mut d = b"".to_vec();
    normalize_line_endings(&mut d);
    assert_eq!(d, b"\n");
}

#[test]
fn normalize_eol_adds_missing_trailing_newline() {
    // The issue #640 reproducer: `fn f(){}` with no final newline must gain
    // exactly one `\n` so in-memory callers match the CLI's `read_file_with_eol`.
    assert_eq!(super::normalize_eol(b"fn f(){}".to_vec()), b"fn f(){}\n");
}

#[test]
fn normalize_eol_collapses_crlf_and_terminates() {
    // CRLF endings collapse to LF and a single trailing newline is guaranteed,
    // matching the on-disk normalisation.
    assert_eq!(super::normalize_eol(b"a\r\nb".to_vec()), b"a\nb\n");
}

#[test]
fn normalize_eol_collapses_lone_cr() {
    assert_eq!(super::normalize_eol(b"a\rb\r".to_vec()), b"a\nb\n");
}

#[test]
fn is_generated_at_generated_top() {
    assert!(is_generated(b"// @generated\nfn x() {}\n"));
}

#[test]
fn is_generated_go_do_not_edit() {
    assert!(is_generated(
        b"// Code generated by protoc. DO NOT EDIT.\npackage x\n",
    ));
}

#[test]
fn is_generated_lizard_marker() {
    assert!(is_generated(b"# GENERATED CODE\nprint('x')\n"));
}

#[test]
fn is_generated_python_do_not_edit() {
    assert!(is_generated(b"# DO NOT EDIT\nprint('x')\n"));
}

#[test]
fn is_generated_case_insensitive_marker() {
    assert!(is_generated(b"// @GENERATED\nfn x() {}\n"));
}

#[test]
fn is_generated_marker_only_in_body_is_false() {
    // Marker phrase appearing well past the scan window must not trigger.
    let mut buf = Vec::with_capacity(8 * 1024);
    for i in 0..200 {
        buf.extend_from_slice(format!("// line {i}\n").as_bytes());
    }
    buf.extend_from_slice(b"// @generated  -- but this is line 200+\n");
    assert!(!is_generated(&buf));
}

#[test]
fn is_generated_empty_file_is_false() {
    assert!(!is_generated(b""));
}

#[test]
fn is_generated_non_utf8_does_not_panic() {
    // Non-UTF-8 garbage with no ASCII-marker substring: every byte is
    // 0x80..=0xFF (continuation / invalid in UTF-8 lead positions), so
    // it cannot contain `@generated`, `DO NOT EDIT`, or `GENERATED CODE`
    // as a byte sequence. Verifies both no-panic and the negative
    // result.
    let buf: Vec<u8> = (0x80u8..=0xFFu8).cycle().take(2048).collect();
    assert!(!is_generated(&buf));
}

#[test]
fn is_generated_short_file_with_marker() {
    // File smaller than the scan window with a marker on the first line.
    assert!(is_generated(b"# @generated"));
}

#[test]
fn is_generated_utf8_bom_then_marker() {
    let mut buf = Vec::new();
    buf.extend_from_slice(b"\xEF\xBB\xBF");
    buf.extend_from_slice(b"// @generated\nfn x() {}\n");
    assert!(is_generated(&buf));
}

#[test]
fn is_generated_no_marker_returns_false() {
    assert!(!is_generated(
        b"// Hand-written file.\nfn main() { println!(\"hi\"); }\n"
    ));
}

#[test]
fn normalize_line_endings_mixed_endings() {
    // LF + lone-CR + CRLF in one buffer — each is converted independently.
    let mut d = b"a\nb\rc\r\nd".to_vec();
    normalize_line_endings(&mut d);
    assert_eq!(d, b"a\nb\nc\nd\n");
}

// ── guess_file strategy-chain helpers ──────────────────────────────

fn pb(s: &str) -> PathBuf {
    PathBuf::from(s)
}

#[test]
fn unique_filter_returns_some_when_exactly_one_match() {
    let possibilities = vec![pb("src/a.h"), pb("src/b.h"), pb("src/c.h")];
    let current = pb("src/lib.c");
    let got = unique_filter(&possibilities, &current, |p| p.ends_with("b.h"));
    assert_eq!(got, Some(vec![pb("src/b.h")]));
}

#[test]
fn unique_filter_returns_none_when_zero_match() {
    let possibilities = vec![pb("src/a.h"), pb("src/b.h")];
    let current = pb("src/lib.c");
    let got = unique_filter(&possibilities, &current, |p| p.ends_with("xyz.h"));
    assert_eq!(got, None);
}

#[test]
fn unique_filter_returns_none_when_multiple_match() {
    let possibilities = vec![pb("a/foo.h"), pb("b/foo.h"), pb("c/bar.h")];
    let current = pb("lib.c");
    let got = unique_filter(&possibilities, &current, |p| p.ends_with("foo.h"));
    assert_eq!(got, None);
}

#[test]
fn unique_filter_excludes_current_path_from_matches() {
    let current = pb("src/lib.c");
    let possibilities = vec![current.clone(), pb("src/other.c")];
    // `lib.c` would match `ends_with("lib.c")` but is current_path, so the
    // surviving candidate is the unique `other.c` — wait, the predicate is
    // `ends_with("lib.c")` and `other.c` doesn't match. Pin the contract:
    // current_path is filtered out BEFORE the predicate counts toward
    // uniqueness, so a self-match becomes "zero candidates" not "one".
    let got = unique_filter(&possibilities, &current, |p| p.ends_with("lib.c"));
    assert_eq!(got, None);
}

#[test]
fn resolve_against_resolved_prefers_exact_over_suffix() {
    // Two candidates, both end_with the resolved target; the exact match
    // wins because the strategy tries exact first.
    let possibilities = vec![pb("a/b/foo.h"), pb("foo.h")];
    let current = pb("a/b/main.c");
    let resolved = pb("foo.h");
    let got = resolve_against_resolved(&possibilities, &current, Some(&resolved));
    // Only one candidate (`foo.h`) matches `== resolved`, so it wins.
    assert_eq!(got, Some(vec![pb("foo.h")]));
}

#[test]
fn resolve_against_resolved_returns_none_without_resolved_path() {
    let possibilities = vec![pb("foo.h")];
    let current = pb("main.c");
    assert_eq!(
        resolve_against_resolved(&possibilities, &current, None),
        None
    );
}

#[test]
fn resolve_against_parent_keeps_only_siblings() {
    let possibilities = vec![pb("src/a/foo.h"), pb("src/b/foo.h")];
    let current = pb("src/a/main.c");
    let got = resolve_against_parent(&possibilities, &current);
    // Only `src/a/foo.h` starts with `src/a`, so unique.
    assert_eq!(got, Some(vec![pb("src/a/foo.h")]));
}

#[test]
fn resolve_against_parent_returns_none_when_no_parent() {
    let possibilities = vec![pb("foo.h")];
    let current = pb("main.c");
    // `current.parent()` is `Some("")`, and `pb("foo.h").starts_with("")`
    // is true, so the unique-match candidate survives. Document the
    // empty-parent case: same-directory unqualified files DO match.
    let got = resolve_against_parent(&possibilities, &current);
    assert_eq!(got, Some(vec![pb("foo.h")]));
}

#[test]
fn min_distance_candidates_empty_returns_empty() {
    let possibilities: Vec<PathBuf> = vec![];
    let current = pb("src/main.c");
    assert!(min_distance_candidates(&possibilities, &current).is_empty());
}

#[test]
fn min_distance_candidates_single_returns_single() {
    let possibilities = vec![pb("src/foo.h")];
    let current = pb("src/main.c");
    let got = min_distance_candidates(&possibilities, &current);
    assert_eq!(got, vec![pb("src/foo.h")]);
}

#[test]
fn min_distance_candidates_excludes_current_path() {
    let current = pb("src/main.c");
    // Self-match: only candidate IS current; result must be empty, not
    // [current]. This is the invariant that protects guess_file from
    // emitting `#include "main.c"` resolving to itself.
    let possibilities = vec![current.clone()];
    assert!(min_distance_candidates(&possibilities, &current).is_empty());
}

#[test]
fn min_distance_candidates_returns_all_ties_at_minimum() {
    // Two candidates at distance 1 (siblings of `src/main.c`), one at
    // distance > 1. Both ties survive; the farther one is dropped.
    let possibilities = vec![pb("src/a.h"), pb("src/b.h"), pb("far/c.h")];
    let current = pb("src/main.c");
    let mut got = min_distance_candidates(&possibilities, &current);
    got.sort(); // get_paths_dist preserves walk order; sort for stable assertion
    assert_eq!(got, vec![pb("src/a.h"), pb("src/b.h")]);
}

#[test]
fn min_distance_candidates_strictly_decreasing_distances() {
    // Pin the `Ordering::Less` arm: each candidate beats the prior min,
    // so the prior survivor set is cleared and replaced. Only the last
    // (closest) candidate should remain. This is the pathological case
    // for the prior `Vec<PathBuf>` implementation that allocated +
    // dropped on every Less arm.
    let possibilities = vec![pb("a/b/c/d/x.h"), pb("a/b/c/x.h"), pb("a/b/x.h")];
    let current = pb("a/b/main.c");
    let got = min_distance_candidates(&possibilities, &current);
    assert_eq!(got, vec![pb("a/b/x.h")]);
}

// Probe-window UTF-8 validation (issues #746 and #758). These cases need
// files longer than the 64-byte probe so the boundary behaviour is actually
// exercised; the inline `test_read` table above covers only sub-probe sizes.
//
// Each test writes a uniquely named temp file so the suite stays safe under
// the default parallel test runner.
fn write_tmp(name: &str, bytes: &[u8]) -> std::path::PathBuf {
    let path = std::env::temp_dir().join(name);
    write_file(&path, bytes).unwrap();
    path
}

#[test]
fn read_eol_accepts_multibyte_split_at_probe_boundary() {
    // #746 (acceptance side): a valid UTF-8 file whose first 64 bytes end in
    // the middle of a multibyte character must not be rejected. 63 ASCII bytes
    // push the two-byte `é` (0xC3 0xA9) so its lead byte lands at offset 63 and
    // its continuation byte lands at offset 64 — outside the probe window. The
    // tail completing the sequence proves the classifier validates the prefix
    // up to `valid_up_to()` rather than rejecting on the split.
    let mut bytes = vec![b'a'; 63];
    bytes.extend_from_slice("é tail content past the probe window\n".as_bytes());
    let path = write_tmp("bca_read_eol_split_multibyte", &bytes);

    let got = read_file_with_eol(&path).unwrap();
    let mut expected = vec![b'a'; 63];
    expected.extend_from_slice("é tail content past the probe window\n".as_bytes());
    assert_eq!(got, Some(expected));
}

#[test]
fn read_eol_rejects_invalid_byte_hidden_by_old_pop() {
    // #746 (rejection side): the old classifier `from_utf8_lossy(...).pop()`
    // unconditionally dropped the trailing replacement character, so a probe
    // whose only non-ASCII byte was an invalid lead byte at the very end
    // (here 0xFF at offset 63) was wrongly accepted — the pop hid the only
    // U+FFFD. A real 0xFF byte is never valid UTF-8 (`error_len` is `Some`),
    // so the byte-level classifier must reject regardless of more file
    // following. This case fails against the pre-fix code.
    let mut bytes = vec![b'a'; 63];
    bytes.push(0xFF);
    bytes.extend_from_slice(&[b'a'; 40]); // ensure file > probe window
    let path = write_tmp("bca_read_eol_invalid_byte_hidden", &bytes);

    assert_eq!(read_file_with_eol(&path).unwrap(), None);
}

#[test]
fn read_eol_accepts_replacement_char_in_probe() {
    // #758: U+FFFD is a legal Unicode scalar; a file that legitimately
    // contains it within the probe window must be accepted, not mistaken for
    // the lossy-decode sentinel the old classifier scanned for.
    let mut bytes = "prefix \u{FFFD} suffix".as_bytes().to_vec();
    // Pad past the probe so the file is longer than the 64-byte window.
    bytes.extend_from_slice(&[b'z'; 80]);
    bytes.push(b'\n');
    let path = write_tmp("bca_read_eol_replacement_char", &bytes);

    let got = read_file_with_eol(&path).unwrap();
    assert_eq!(got, Some(bytes));
}

#[test]
fn read_eol_rejects_stray_invalid_byte_in_probe() {
    // A genuinely invalid byte (0xFF is never a valid UTF-8 lead byte) inside
    // the probe is real corruption and must be rejected, even when more file
    // follows (so it cannot be confused with a truncated trailing sequence).
    let mut bytes = b"valid prefix \xFF more".to_vec();
    bytes.extend_from_slice(&[b'q'; 80]);
    bytes.push(b'\n');
    let path = write_tmp("bca_read_eol_stray_invalid_byte", &bytes);

    assert_eq!(read_file_with_eol(&path).unwrap(), None);
}

#[test]
fn read_eol_rejects_short_file_truncated_mid_multibyte() {
    // A short file (<= probe size) that ends in an incomplete multibyte
    // sequence is genuinely truncated — there is no further data to complete
    // it — so it must be rejected. `0xC3` is the lead byte of a two-byte
    // sequence with no continuation byte following.
    let path = write_tmp("bca_read_eol_short_truncated", b"abcd\xC3");
    assert_eq!(read_file_with_eol(&path).unwrap(), None);
}

#[test]
fn read_eol_accepts_plain_ascii_past_probe() {
    // Sanity: an ordinary ASCII file longer than the probe is accepted and
    // line-ending normalised.
    let bytes = b"the quick brown fox jumps over the lazy dog several times over\n".repeat(3);
    let path = write_tmp("bca_read_eol_plain_ascii", &bytes);
    assert_eq!(read_file_with_eol(&path).unwrap(), Some(bytes));
}

// Build a UTF-16 byte stream (without BOM) from an ASCII string by
// interleaving a NUL after each byte, in the requested endianness.
fn utf16_ascii_body(text: &str, little_endian: bool) -> Vec<u8> {
    text.bytes()
        .flat_map(|b| if little_endian { [b, 0x00] } else { [0x00, b] })
        .collect()
}

#[test]
fn read_eol_rejects_utf16_le_bom() {
    // #803: a UTF-16-LE file (FF FE BOM + interleaved-NUL ASCII body) must be
    // skipped, not stripped-and-parsed. Before the fix the BOM was dropped and
    // the body — every byte <= 0x7F, so valid single-byte UTF-8 — passed the
    // probe, feeding NUL-interleaved garbage to the parser. The body is padded
    // well past the 64-byte probe so the file is unambiguously not too-small.
    let mut bytes = vec![0xFF, 0xFE];
    bytes.extend(utf16_ascii_body(
        "int main() { return 0; } // padding to exceed the probe window\n",
        true,
    ));
    let path = write_tmp("bca_read_eol_utf16_le_bom", &bytes);
    assert_eq!(read_file_with_eol(&path).unwrap(), None);
}

#[test]
fn read_eol_rejects_utf16_be_bom() {
    // #803, big-endian sibling: FE FF BOM + NUL-then-byte body. Same rejection.
    let mut bytes = vec![0xFE, 0xFF];
    bytes.extend(utf16_ascii_body(
        "int main() { return 0; } // padding to exceed the probe window\n",
        false,
    ));
    let path = write_tmp("bca_read_eol_utf16_be_bom", &bytes);
    assert_eq!(read_file_with_eol(&path).unwrap(), None);
}

#[test]
fn read_eol_strips_utf8_bom_and_keeps_body() {
    // #803 (acceptance side): a UTF-8 BOM prefixes genuine UTF-8, so it is
    // stripped and the body is read normally — UTF-8 BOM handling must NOT
    // regress when UTF-16 BOMs start being rejected.
    let mut bytes = vec![0xEF, 0xBB, 0xBF];
    bytes.extend_from_slice(b"abc");
    let path = write_tmp("bca_read_eol_utf8_bom_body", &bytes);
    assert_eq!(read_file_with_eol(&path).unwrap(), Some(b"abc\n".to_vec()));
}

#[cfg(unix)]
#[test]
fn read_eol_propagates_non_eof_io_error() {
    // #804: a real I/O fault during the probe read must propagate as `Err`,
    // not collapse to `Ok(None)`. On Unix, opening a directory succeeds but
    // `read_exact` fails with a non-`UnexpectedEof` error (`IsADirectory`),
    // exercising the propagation branch. Before the fix every `read_exact`
    // failure became `Ok(None)`, swallowing this fault.
    let dir = std::env::temp_dir();
    let err = read_file_with_eol(&dir).expect_err("reading a directory must error");
    assert_ne!(err.kind(), std::io::ErrorKind::UnexpectedEof);
}