perl-module 0.16.0

Perl module resolution, import analysis, and refactoring — unified facade
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
use std::path::Path;

use perl_module::resolution::use_lib::{
    UseLibAction, UseLibPath, extract_use_lib_operations, extract_use_lib_paths,
    no_lib_cancelled_paths_at_offset, resolve_use_lib_paths,
    resolve_use_lib_paths_from_source_at_offset,
};

#[test]
fn findbin_parent_traversal_is_rejected() -> Result<(), Box<dyn std::error::Error>> {
    let temp = tempfile::tempdir()?;
    let workspace = temp.path().join("workspace");
    let file_dir = workspace.join("project").join("lib");

    std::fs::create_dir_all(&file_dir)?;

    let resolved = resolve_use_lib_paths(
        &[UseLibPath { path: "../../../outside".to_string(), from_findbin: true }],
        &workspace,
        Some(&file_dir),
    );

    assert!(resolved.is_empty(), "findbin traversal should be dropped");
    Ok(())
}

#[test]
fn findbin_dot_segment_is_normalized_inside_workspace() -> Result<(), Box<dyn std::error::Error>> {
    let temp = tempfile::tempdir()?;
    let workspace = temp.path().join("workspace");
    let file_dir = workspace.join("project").join("lib");

    std::fs::create_dir_all(&file_dir)?;

    let resolved = resolve_use_lib_paths(
        &[UseLibPath { path: "../vendor/./lib".to_string(), from_findbin: true }],
        &workspace,
        Some(&file_dir),
    );

    assert_eq!(resolved, vec!["project/vendor/lib".to_string()]);
    Ok(())
}

#[test]
fn absolute_use_lib_path_outside_workspace_is_rejected() -> Result<(), Box<dyn std::error::Error>> {
    let temp = tempfile::tempdir()?;
    let workspace = temp.path().join("workspace");
    let outside = temp.path().join("outside-lib");
    std::fs::create_dir_all(&workspace)?;
    std::fs::create_dir_all(&outside)?;

    let outside_path = outside.to_string_lossy().to_string();
    let resolved = resolve_use_lib_paths(
        &[UseLibPath { path: outside_path, from_findbin: false }],
        &workspace,
        None,
    );

    assert!(resolved.is_empty(), "absolute outside-workspace paths should be dropped");
    Ok(())
}

#[test]
fn absolute_use_lib_path_inside_workspace_is_normalized_to_relative()
-> Result<(), Box<dyn std::error::Error>> {
    let temp = tempfile::tempdir()?;
    let workspace = temp.path().join("workspace");
    let inside = workspace.join("lib").join("Nested");
    std::fs::create_dir_all(&inside)?;

    let inside_path = inside.to_string_lossy().to_string();
    let resolved = resolve_use_lib_paths(
        &[UseLibPath { path: inside_path, from_findbin: false }],
        &workspace,
        None,
    );

    assert_eq!(resolved, vec!["lib/Nested".to_string()]);
    Ok(())
}

#[test]
fn absolute_use_lib_path_with_embedded_dotdot_is_rejected() -> Result<(), Box<dyn std::error::Error>>
{
    // Regression guard: `Path::strip_prefix` is purely lexical.  An absolute path
    // like `<workspace>/../sibling` strips the `<workspace>` prefix lexically but
    // the remainder is `../sibling`, which would escape the workspace.  The guard in
    // `path_to_relative_string` must detect any `ParentDir` component in the
    // stripped result and return `None`.
    let temp = tempfile::tempdir()?;
    let workspace = temp.path().join("workspace");
    std::fs::create_dir_all(&workspace)?;

    // Construct a truly absolute path that lexically starts with the workspace
    // prefix but contains an embedded `..` that escapes it.
    let bypass_path = format!(
        "{}{}..{}sibling",
        workspace.display(),
        std::path::MAIN_SEPARATOR,
        std::path::MAIN_SEPARATOR
    );

    let resolved = resolve_use_lib_paths(
        &[UseLibPath { path: bypass_path.clone(), from_findbin: false }],
        &workspace,
        None,
    );
    assert!(
        resolved.is_empty(),
        "absolute path with embedded `..` must be rejected; bypass_path={bypass_path:?} got: {resolved:?}"
    );
    Ok(())
}

#[test]
fn use_and_no_lib_operations_are_extracted_in_order() {
    let source = "\
use lib 'first';\n\
use lib 'second';\n\
no lib 'first';\n\
";

    let ops = extract_use_lib_operations(source);

    assert_eq!(
        ops,
        vec![
            UseLibAction::Add(vec![UseLibPath { path: "first".to_string(), from_findbin: false }]),
            UseLibAction::Add(vec![UseLibPath { path: "second".to_string(), from_findbin: false }]),
            UseLibAction::Remove(vec![UseLibPath {
                path: "first".to_string(),
                from_findbin: false,
            }]),
        ]
    );
}

#[test]
fn use_lib_offset_resolution_obeys_lexical_order() {
    let source = "\
use lib 'first';\n\
use lib 'second';\n\
no lib 'first';\n\
use Lib::Thing;\n\
";

    let offset_at_use = source.find("use Lib::Thing;").unwrap_or(source.len());
    let include_paths = resolve_use_lib_paths_from_source_at_offset(
        source,
        offset_at_use,
        Path::new("/workspace"),
        None,
    );

    assert_eq!(include_paths, vec!["second".to_string()]);
}

#[test]
fn short_findbin_exports_are_treated_as_findbin_paths() {
    // Both double-quoted (interpolating) and single-quoted (literal in real Perl)
    // forms are recognised; the extractor is intentionally quote-type-agnostic.
    let source = "\
use lib '$Bin/../lib';\n\
use lib \"$RealBin/../vendor\";\n\
";

    let ops = extract_use_lib_operations(source);

    assert_eq!(
        ops,
        vec![
            UseLibAction::Add(vec![UseLibPath { path: "../lib".to_string(), from_findbin: true }]),
            UseLibAction::Add(vec![UseLibPath {
                path: "../vendor".to_string(),
                from_findbin: true,
            }]),
        ]
    );
}

#[test]
fn short_findbin_prefix_does_not_match_longer_variable_name() {
    // `$BinDir` and `$RealBinPath` look like they start with `$Bin`/`$RealBin`
    // but are different variables — word-boundary check must reject them.
    let source = "\
use lib \"$BinDir/lib\";\n\
use lib \"$RealBinPath/vendor\";\n\
";

    let ops = extract_use_lib_operations(source);

    // Both paths should be treated as plain (non-FindBin) string paths.
    assert_eq!(
        ops,
        vec![
            UseLibAction::Add(vec![UseLibPath {
                path: "$BinDir/lib".to_string(),
                from_findbin: false,
            }]),
            UseLibAction::Add(vec![UseLibPath {
                path: "$RealBinPath/vendor".to_string(),
                from_findbin: false,
            }]),
        ]
    );
}

#[test]
fn braced_short_findbin_exports_are_treated_as_findbin_paths() {
    let source = "use lib \"${Bin}/../lib\";\n";

    let ops = extract_use_lib_operations(source);

    assert_eq!(
        ops,
        vec![UseLibAction::Add(vec![UseLibPath {
            path: "../lib".to_string(),
            from_findbin: true,
        }]),]
    );
}

#[test]
fn multiline_use_lib_is_extracted() {
    let source = "\
use lib (
    'first',
    \"second\"
);
";

    let paths = extract_use_lib_paths(source);

    assert_eq!(
        paths,
        vec![
            UseLibPath { path: "first".to_string(), from_findbin: false },
            UseLibPath { path: "second".to_string(), from_findbin: false },
        ]
    );
}

#[test]
fn multiline_use_and_no_lib_are_ordered() {
    let source = "\
use lib (
    'first',
    'second'
);
no lib (
    'first'
);
";

    let ops = extract_use_lib_operations(source);

    assert_eq!(
        ops,
        vec![
            UseLibAction::Add(vec![
                UseLibPath { path: "first".to_string(), from_findbin: false },
                UseLibPath { path: "second".to_string(), from_findbin: false },
            ]),
            UseLibAction::Remove(vec![UseLibPath {
                path: "first".to_string(),
                from_findbin: false,
            }]),
        ]
    );
}

#[test]
fn quoted_semicolon_does_not_split_statement() {
    let source = "use lib ('alpha;beta', 'gamma');";

    let paths = extract_use_lib_paths(source);

    assert_eq!(
        paths,
        vec![
            UseLibPath { path: "alpha;beta".to_string(), from_findbin: false },
            UseLibPath { path: "gamma".to_string(), from_findbin: false },
        ]
    );
}

#[test]
fn inline_comment_inside_multiline_use_lib_does_not_truncate_paths() {
    // Perl inline comments (# ...) inside a parenthesized list must be skipped
    // so that paths appearing after the comment are still extracted.
    let source = "\
use lib (
    '/foo/bar',  # the main lib
    '/baz/qux'
);
";

    let paths = extract_use_lib_paths(source);

    assert_eq!(
        paths,
        vec![
            UseLibPath { path: "/foo/bar".to_string(), from_findbin: false },
            UseLibPath { path: "/baz/qux".to_string(), from_findbin: false },
        ]
    );
}

#[test]
fn crlf_line_endings_do_not_affect_extraction() {
    // CRLF (\r\n) line endings are whitespace-normalized by trim(), so
    // multiline use lib with Windows line endings must work identically
    // to the Unix (\n) form.
    let source = "use lib (\r\n    'first',\r\n    'second'\r\n);\r\n";

    let paths = extract_use_lib_paths(source);

    assert_eq!(
        paths,
        vec![
            UseLibPath { path: "first".to_string(), from_findbin: false },
            UseLibPath { path: "second".to_string(), from_findbin: false },
        ]
    );
}

#[test]
fn multiline_qw_use_lib_is_extracted() {
    // qw() with whitespace-separated paths on multiple lines.
    let source = "\
use lib qw(
    /path/one
    /path/two
);
";

    let paths = extract_use_lib_paths(source);

    assert_eq!(
        paths,
        vec![
            UseLibPath { path: "/path/one".to_string(), from_findbin: false },
            UseLibPath { path: "/path/two".to_string(), from_findbin: false },
        ]
    );
}

#[test]
fn escaped_quote_inside_single_quoted_path_is_handled() {
    // 'it\'s a path' is an extreme edge case; the backslash-escaping in
    // split_perl_statements must not cause the closing quote to be missed.
    // The practical value is that \\-terminated paths work correctly.
    let source = "use lib 'normal'; use lib 'also';";

    let paths = extract_use_lib_paths(source);

    assert_eq!(
        paths,
        vec![
            UseLibPath { path: "normal".to_string(), from_findbin: false },
            UseLibPath { path: "also".to_string(), from_findbin: false },
        ]
    );
}

#[test]
fn unterminated_use_lib_does_not_panic() {
    // Malformed Perl: unclosed string or missing semicolon.
    // The extractor must not panic; it may return partial or empty results.
    let sources = ["use lib 'unclosed", "use lib (\"no closing paren", "use lib"];
    for source in &sources {
        // Should not panic; we don't assert on the exact output.
        let _ = extract_use_lib_paths(source);
        let _ = extract_use_lib_operations(source);
    }
}

#[test]
fn comment_with_semicolon_does_not_drop_subsequent_use_lib() {
    // Regression: split_perl_statements must skip Perl line comments (#...\n)
    // so that a semicolon inside a comment does not split the statement and
    // cause the next `use lib` to be dropped.
    let source = "\
use lib '/foo';  # primary lib; see INSTALL for details
use lib '/bar';
";

    let paths = extract_use_lib_paths(source);

    assert_eq!(
        paths,
        vec![
            UseLibPath { path: "/foo".to_string(), from_findbin: false },
            UseLibPath { path: "/bar".to_string(), from_findbin: false },
        ]
    );
}

#[test]
fn leading_comment_with_semicolon_does_not_drop_use_lib() {
    // A comment before the first use lib that contains a semicolon must not
    // create a spurious leading fragment that mangles the statement that follows.
    let source = "\
# This script uses lib; see also INSTALL
use lib 'mylib';
";

    let paths = extract_use_lib_paths(source);

    assert_eq!(paths, vec![UseLibPath { path: "mylib".to_string(), from_findbin: false }]);
}

#[test]
fn empty_source_returns_empty_paths() {
    // Edge case: completely empty input should not crash.
    let source = "";
    let paths = extract_use_lib_paths(source);
    assert!(paths.is_empty());

    let ops = extract_use_lib_operations(source);
    assert!(ops.is_empty());
}

#[test]
fn only_whitespace_returns_empty_paths() {
    // Edge case: input with only whitespace, tabs, newlines.
    let source = "   \n\t\n  ";
    let paths = extract_use_lib_paths(source);
    assert!(paths.is_empty());
}

#[test]
fn only_comments_returns_empty_paths() {
    // Edge case: input with only comments (no actual code).
    let source = "\
# Comment 1
# Comment 2
# Comment with semicolon;
";
    let paths = extract_use_lib_paths(source);
    assert!(paths.is_empty());
}

#[test]
fn qw_with_semicolon_in_path_is_not_split() {
    // Edge case: qw() list item that contains a semicolon (unusual but valid).
    // qw() items are whitespace-separated, not quote-delimited, so a semicolon
    // inside a path would be literal in the output. However, qw() splitter
    // must not confuse the semicolon with a statement terminator.
    let source = "use lib qw(/path/one /path/two); use lib '/path/three';";

    let paths = extract_use_lib_paths(source);

    // Both qw paths and the regular path should be extracted.
    assert_eq!(
        paths,
        vec![
            UseLibPath { path: "/path/one".to_string(), from_findbin: false },
            UseLibPath { path: "/path/two".to_string(), from_findbin: false },
            UseLibPath { path: "/path/three".to_string(), from_findbin: false },
        ]
    );
}

#[test]
fn nested_parentheses_in_qw_are_handled() {
    // Edge case: qw() can use different delimiters; qw(a b) and qw[a b] are both valid.
    // The split logic must handle parentheses correctly even inside qw-ish contexts.
    let source = "\
use lib qw(
    /lib/a
    /lib/b
);
use lib ('/lib/c');
";

    let paths = extract_use_lib_paths(source);

    assert_eq!(
        paths,
        vec![
            UseLibPath { path: "/lib/a".to_string(), from_findbin: false },
            UseLibPath { path: "/lib/b".to_string(), from_findbin: false },
            UseLibPath { path: "/lib/c".to_string(), from_findbin: false },
        ]
    );
}

#[test]
fn escaped_backslash_at_end_of_path() {
    // Edge case: Windows paths with backslashes, or paths ending in \.
    // The escape-tracking logic must correctly handle these.
    let source = r#"use lib 'C:\\temp\\lib'; use lib 'also';"#;

    let paths = extract_use_lib_paths(source);

    assert_eq!(
        paths,
        vec![
            UseLibPath { path: r"C:\\temp\\lib".to_string(), from_findbin: false },
            UseLibPath { path: "also".to_string(), from_findbin: false },
        ]
    );
}

#[test]
fn double_quoted_path_with_escaped_double_quote() {
    // Edge case: double-quoted string containing an escaped quote.
    // The statement splitter must handle the escaped quote so it doesn't
    // terminate the string prematurely.
    let source = r#"use lib "path/to/lib"; use lib 'also';"#;

    let paths = extract_use_lib_paths(source);

    assert_eq!(paths.len(), 2);
    assert!(paths.iter().any(|p| p.path.contains("lib")));
    assert!(paths.iter().any(|p| p.path.contains("also")));
}

#[test]
fn double_quoted_path_with_escaped_double_quote_and_semicolon()
-> Result<(), Box<dyn std::error::Error>> {
    let source = r#"use lib "path/\"with;quote"; use lib 'also';"#;

    let paths = extract_use_lib_paths(source);

    assert_eq!(
        paths,
        vec![
            UseLibPath { path: r#"path/\"with;quote"#.to_string(), from_findbin: false },
            UseLibPath { path: "also".to_string(), from_findbin: false },
        ]
    );
    Ok(())
}

#[test]
fn single_quoted_path_with_escaped_single_quote() -> Result<(), Box<dyn std::error::Error>> {
    // Edge case: single-quoted string with escaped single quote.
    // In Perl, only \\ and \' are special in single quotes.
    let source = r"use lib 'it\'s; a path'; use lib 'also';";

    let paths = extract_use_lib_paths(source);

    assert_eq!(
        paths,
        vec![
            UseLibPath { path: r"it\'s; a path".to_string(), from_findbin: false },
            UseLibPath { path: "also".to_string(), from_findbin: false },
        ]
    );
    Ok(())
}

#[test]
fn comment_with_multiple_semicolons() {
    // Edge case: comment containing multiple semicolons.
    let source = "\
use lib '/foo'; # Important: see docs at URL; also check README;
use lib '/bar';
";

    let paths = extract_use_lib_paths(source);

    assert_eq!(
        paths,
        vec![
            UseLibPath { path: "/foo".to_string(), from_findbin: false },
            UseLibPath { path: "/bar".to_string(), from_findbin: false },
        ]
    );
}

#[test]
fn alternating_single_and_double_quotes_in_statement() {
    // Edge case: statement with mixed quote types (valid Perl).
    let source = r#"use lib '/lib1', "/lib2", '/lib3'; use lib "/lib4";"#;

    let paths = extract_use_lib_paths(source);

    assert_eq!(paths.len(), 4, "Expected 4 paths");
}

#[test]
fn hash_character_inside_double_quoted_string() {
    // Edge case: # inside a double-quoted string is NOT a comment.
    let source = r#"use lib "/path#with#hashes"; use lib '/other';"#;

    let paths = extract_use_lib_paths(source);

    // The first path should include the hashes (they're inside quotes).
    let first_path = &paths[0];
    assert!(first_path.path.contains('#'), "Expected # to be in path: {}", first_path.path);
}

#[test]
fn hash_character_inside_single_quoted_string() {
    // Edge case: # inside a single-quoted string is NOT a comment.
    let source = "use lib '/path#with#hashes'; use lib '/other';";

    let paths = extract_use_lib_paths(source);

    let first_path = &paths[0];
    assert!(first_path.path.contains('#'));
}

#[test]
fn very_long_path_list() {
    // Boundary: large number of paths (stress test).
    let mut source = String::new();
    let num_paths = 100;
    source.push_str("use lib qw(\n");
    for i in 0..num_paths {
        source.push_str(&format!("    /path/{}\n", i));
    }
    source.push_str(");\n");

    let paths = extract_use_lib_paths(&source);

    assert_eq!(paths.len(), num_paths, "Expected {} paths", num_paths);
}

#[test]
fn unicode_paths_are_extracted() {
    // Edge case: Unicode characters in paths (non-ASCII).
    let source = "use lib '/lib/日本語'; use lib '/lib/中文'; use lib '/lib/русский';";

    let paths = extract_use_lib_paths(source);

    assert_eq!(paths.len(), 3);
    assert!(paths[0].path.contains(''));
    assert!(paths[1].path.contains(''));
    assert!(paths[2].path.contains('р'));
}

#[test]
fn multiline_with_mixed_comments_and_code() {
    // Regression: complex real-world scenario with comments scattered throughout.
    let source = "\
# Global configuration
use lib 'default';  # Add default lib

# Also use project-specific lib; important for imports
use lib (
    'project/lib',  # project-local
    'vendor/lib'    # vendored; see VENDOR.md for details
);

# Override path if env var is set; see also setup.sh
no lib 'default';
";

    let paths = extract_use_lib_paths(source);
    let ops = extract_use_lib_operations(source);

    // Paths: default, project/lib, vendor/lib
    assert_eq!(paths.len(), 3);

    // Operations: Add default, Add [project, vendor], Remove default
    assert_eq!(ops.len(), 3);
}

#[test]
fn no_lib_without_use_lib() {
    // Edge case: 'no lib' statement without any preceding 'use lib'.
    let source = "no lib 'phantom';";

    let ops = extract_use_lib_operations(source);

    assert_eq!(
        ops,
        vec![UseLibAction::Remove(vec![UseLibPath {
            path: "phantom".to_string(),
            from_findbin: false,
        }])]
    );
}

#[test]
fn empty_parentheses_in_use_lib() {
    // Edge case: use lib () with no paths (malformed but shouldn't crash).
    let source = "use lib (); use lib 'valid';";

    let paths = extract_use_lib_paths(source);

    // Should extract the valid path and skip the empty one.
    assert!(paths.iter().any(|p| p.path == "valid"));
}

#[test]
fn statement_without_semicolon_at_eof() {
    // Boundary: statement at end of file without trailing semicolon.
    let source = "use lib '/path'";

    let paths = extract_use_lib_paths(source);

    assert_eq!(paths, vec![UseLibPath { path: "/path".to_string(), from_findbin: false }]);
}

#[test]
fn multiple_statements_on_same_line() {
    // Edge case: multiple use lib statements on one line (valid Perl).
    let source = "use lib '/a'; use lib '/b'; use lib '/c';";

    let paths = extract_use_lib_paths(source);

    assert_eq!(paths.len(), 3);
    assert!(paths.iter().any(|p| p.path == "/a"));
    assert!(paths.iter().any(|p| p.path == "/b"));
    assert!(paths.iter().any(|p| p.path == "/c"));
}

#[test]
fn mixed_use_and_no_lib_on_same_line() {
    // Edge case: both use lib and no lib on same line.
    let source = "use lib '/first'; no lib '/first'; use lib '/second';";

    let ops = extract_use_lib_operations(source);

    assert_eq!(ops.len(), 3);
    assert!(matches!(&ops[0], UseLibAction::Add(_)), "First op should be Add");
    assert!(matches!(&ops[1], UseLibAction::Remove(_)), "Second op should be Remove");
    assert!(matches!(&ops[2], UseLibAction::Add(_)), "Third op should be Add");
}

#[test]
fn carriage_return_line_feed_in_comment() {
    // Boundary: CRLF line endings inside a comment.
    let source = "use lib '/a'; # comment\r\nuse lib '/b';\r\n";

    let paths = extract_use_lib_paths(source);

    assert_eq!(paths.len(), 2);
}

#[test]
fn tab_characters_in_statement() {
    // Boundary: tabs as whitespace inside statement.
    let source = "use\tlib\t(\t'/a',\t'/b'\t);\n";

    let paths = extract_use_lib_paths(source);

    assert_eq!(paths.len(), 2);
}

#[test]
fn deeply_nested_quotes_in_single_statement() {
    // Edge case: statement with multiple levels of quoting (valid Perl concatenation).
    let source = r#"use lib "prefix" . "/suffix"; use lib '/other';"#;

    let paths = extract_use_lib_paths(source);

    // The concatenation won't be evaluated, but paths inside quotes should be extracted.
    assert!(!paths.is_empty());
}

#[test]
fn comment_only_on_comment_line_before_use_lib() {
    // Regression: comment-only lines before use lib should not interfere.
    let source = "\
# Comment 1
# Comment 2; with semicolon
use lib '/path';
";

    let paths = extract_use_lib_paths(source);

    assert_eq!(paths, vec![UseLibPath { path: "/path".to_string(), from_findbin: false }]);
}

#[test]
fn findbin_variable_with_path_containing_semicolon_in_comment()
-> Result<(), Box<dyn std::error::Error>> {
    // Regression: FindBin extraction should work even with tricky comments.
    let source = "\
use lib \"$RealBin/../lib\"; # Historical reason; keep this
use lib \"/override\";
";

    let ops = extract_use_lib_operations(source);

    assert_eq!(ops.len(), 2);
    let UseLibAction::Add(paths) = &ops[0] else {
        return Err("Expected Add for ops[0]".into());
    };
    assert_eq!(paths.len(), 1);
    assert!(paths[0].from_findbin);
    Ok(())
}

#[test]
fn qw_list_with_multiple_items_on_separate_lines() {
    // Edge case: qw() list with items on separate lines (whitespace-separated).
    let source = "\
use lib qw(
    /path/one
    /path/two
);
";

    let paths = extract_use_lib_paths(source);

    // Already tested in multiline_qw_use_lib_is_extracted, but here we verify
    // boundary behavior with exactly 2 items.
    assert_eq!(paths.len(), 2);
    assert_eq!(paths[0].path, "/path/one");
    assert_eq!(paths[1].path, "/path/two");
}

#[test]
fn statement_split_at_correct_boundary_with_adjacent_semicolons() {
    // Regression: ensure statement splitting happens at the right place
    // when there are multiple semicolons (in quotes and outside).
    let source = "use lib 'a;b'; use lib 'c';";

    let paths = extract_use_lib_paths(source);

    assert_eq!(
        paths,
        vec![
            UseLibPath { path: "a;b".to_string(), from_findbin: false },
            UseLibPath { path: "c".to_string(), from_findbin: false },
        ]
    );
}

#[test]
fn bare_path_without_quotes_in_use_lib() {
    // Edge case: bare words (no quotes) in use lib (unusual, may be function calls).
    // The extractor may not handle these, but it shouldn't crash.
    let source = "use lib qw(bare_word_path);";

    let paths = extract_use_lib_paths(source);

    // Should extract the bare word as a path.
    assert!(paths.iter().any(|p| p.path.contains("bare_word")));
}

/// `no_lib_cancelled_paths_at_offset` must return paths that were explicitly
/// removed by `no lib` and not subsequently re-added before the given offset.
///
/// This is the key mechanism that allows `no lib 'lib'; use GoneModule;` to
/// suppress module resolution for GoneModule even when `lib` is present as a
/// configured workspace include path.
#[test]
fn no_lib_cancelled_paths_returns_removed_paths() {
    let workspace = std::path::Path::new("/workspace");
    // Pattern: use lib → no lib → offset within `use GoneModule`
    let source = "use lib 'lib';\nno lib 'lib';\nuse GoneModule;\n";

    // Offset at start of `use GoneModule` = after "use lib 'lib';\nno lib 'lib';\n"
    let offset = "use lib 'lib';\nno lib 'lib';\n".len();

    let cancelled = no_lib_cancelled_paths_at_offset(source, offset, workspace, None);
    assert!(
        cancelled.contains(&"lib".to_string()),
        "no_lib_cancelled_paths_at_offset must return 'lib' when cancelled by no lib; got: {:?}",
        cancelled
    );
}

/// If `use lib` re-adds a path after `no lib` removes it, the path should NOT
/// appear in cancelled paths at an offset after the re-addition.
#[test]
fn no_lib_cancelled_paths_excludes_readded_paths() {
    let workspace = std::path::Path::new("/workspace");
    // no lib removes lib, then use lib re-adds it.
    let source = "use lib 'lib';\nno lib 'lib';\nuse lib 'lib';\nuse Mod;\n";

    // Offset at start of `use Mod` — after the re-adding `use lib 'lib'`.
    let offset = "use lib 'lib';\nno lib 'lib';\nuse lib 'lib';\n".len();

    let cancelled = no_lib_cancelled_paths_at_offset(source, offset, workspace, None);
    assert!(
        !cancelled.contains(&"lib".to_string()),
        "re-added path must not appear in cancelled list; got: {:?}",
        cancelled
    );
}