socket-patch-core 3.2.0

Core library for socket-patch: manifest, hash, crawlers, patch engine, API client
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
//! Integration coverage for `crawlers::python_crawler` paths the
//! apply-CLI suite doesn't drive. Specifically:
//!
//!   - `find_python_dirs` wildcard segments (`python3.*` and `*`)
//!   - `find_python_dirs` recursive descent with intermediate
//!     non-directory entries
//!   - `find_local_venv_site_packages` with VIRTUAL_ENV env var
//!   - `get_global_python_site_packages` with stubbed HOME
//!
//! Built around `tempfile::tempdir()` + serial env-var mutation
//! (via `serial_test::serial`) so tests can rebind HOME / VIRTUAL_ENV
//! without racing each other.

use std::path::Path;

use serial_test::serial;
use socket_patch_core::crawlers::python_crawler::{
    find_local_venv_site_packages, find_python_command_with, find_python_dirs,
    get_global_python_site_packages, parse_python_site_packages_output, read_python_metadata,
};
use socket_patch_core::crawlers::types::CrawlerOptions;
use socket_patch_core::crawlers::PythonCrawler;

#[test]
fn parse_python_site_packages_output_well_formed() {
    let stdout =
        "/usr/local/lib/python3.11/site-packages\n/usr/local/lib/python3.11/dist-packages\n";
    let paths = parse_python_site_packages_output(stdout);
    assert_eq!(paths.len(), 2);
    assert_eq!(
        paths[0],
        std::path::PathBuf::from("/usr/local/lib/python3.11/site-packages")
    );
}

#[test]
fn parse_python_site_packages_output_empty_returns_empty() {
    assert!(parse_python_site_packages_output("").is_empty());
    assert!(parse_python_site_packages_output("\n  \n").is_empty());
}

#[test]
fn parse_python_site_packages_output_trims_and_skips_blanks() {
    let stdout = "  /a/b  \n\n   \n/c/d\n";
    let paths = parse_python_site_packages_output(stdout);
    assert_eq!(paths.len(), 2);
    assert_eq!(paths[0], std::path::PathBuf::from("/a/b"));
    assert_eq!(paths[1], std::path::PathBuf::from("/c/d"));
}

/// `find_python_command_with` with a mock runner that responds
/// success to `python3 --version` must return `Some("python3")` —
/// the first-match-wins arm. Lets tests exercise the success arm
/// without needing python3 on the host's PATH.
#[test]
fn find_python_command_with_mock_runner_prefers_python3() {
    let runner = common::MockCommandRunner::new().with_response(
        "python3",
        &["--version"],
        Some("Python 3.11.5\n"),
    );
    assert_eq!(find_python_command_with(&runner), Some("python3"));
}

/// When `python3` is not present but `python` is, the helper should
/// fall through to the second candidate.
#[test]
fn find_python_command_with_mock_runner_falls_through_to_python() {
    let runner = common::MockCommandRunner::new().with_response(
        "python",
        &["--version"],
        Some("Python 2.7.18\n"),
    );
    assert_eq!(find_python_command_with(&runner), Some("python"));
}

/// When none of `python3`/`python`/`py` are present, the helper
/// returns None.
#[test]
fn find_python_command_with_mock_runner_none_when_no_binary() {
    let runner = common::MockCommandRunner::new();
    assert_eq!(find_python_command_with(&runner), None);
}

/// Helper: stage a fake `python3.X/lib/python3.X/site-packages` tree
/// under `root` so `find_python_dirs(root, ["python3.*", "lib",
/// "python3.*", "site-packages"])` returns it.
async fn stage_python_layout(root: &Path, py_ver: &str) -> std::path::PathBuf {
    let sp = root
        .join(format!("python{py_ver}"))
        .join("lib")
        .join(format!("python{py_ver}"))
        .join("site-packages");
    tokio::fs::create_dir_all(&sp).await.unwrap();
    sp
}

// ── find_python_dirs wildcards ─────────────────────────────────

/// `python3.*` wildcard matches directories whose name starts with
/// `python3.`. Covers the wildcard arm + the `name.starts_with`
/// filter.
#[tokio::test]
async fn find_python_dirs_python3_wildcard_matches_versions() {
    let tmp = tempfile::tempdir().unwrap();
    let p1 = stage_python_layout(tmp.path(), "3.11").await;
    let _p2 = stage_python_layout(tmp.path(), "3.12").await;
    // Also create a non-matching subdir that should be filtered out.
    tokio::fs::create_dir_all(tmp.path().join("python2.7").join("lib"))
        .await
        .unwrap();

    let result = find_python_dirs(
        tmp.path(),
        &["python3.*", "lib", "python3.*", "site-packages"],
    )
    .await;
    assert!(
        result.iter().any(|r| r == &p1),
        "must find python3.11 layout; got {result:?}"
    );
    assert_eq!(result.len(), 2, "must find exactly python3.11 + python3.12");
}

/// `*` generic wildcard matches every directory entry. Covers the
/// generic wildcard branch (L142-L160 of python_crawler.rs).
#[tokio::test]
async fn find_python_dirs_star_wildcard_matches_all() {
    let tmp = tempfile::tempdir().unwrap();
    tokio::fs::create_dir_all(
        tmp.path()
            .join("pkg_a")
            .join("lib")
            .join("python3.11")
            .join("site-packages"),
    )
    .await
    .unwrap();
    tokio::fs::create_dir_all(
        tmp.path()
            .join("pkg_b")
            .join("lib")
            .join("python3.11")
            .join("site-packages"),
    )
    .await
    .unwrap();

    let result = find_python_dirs(tmp.path(), &["*", "lib", "python3.*", "site-packages"]).await;
    assert_eq!(result.len(), 2, "* must match both pkg_a and pkg_b");
}

/// `*` wildcard skips non-directory entries (regular files). Covers
/// the `if !ft.is_dir() { continue; }` arm.
#[tokio::test]
async fn find_python_dirs_star_wildcard_skips_files() {
    let tmp = tempfile::tempdir().unwrap();
    // A regular file at the wildcard position must NOT cause issues.
    tokio::fs::write(tmp.path().join("not_a_dir.txt"), b"x")
        .await
        .unwrap();
    // And one real match.
    tokio::fs::create_dir_all(
        tmp.path()
            .join("real")
            .join("lib")
            .join("python3.11")
            .join("site-packages"),
    )
    .await
    .unwrap();

    let result = find_python_dirs(tmp.path(), &["*", "lib", "python3.*", "site-packages"]).await;
    assert_eq!(result.len(), 1, "regular file must be skipped");
}

/// `find_python_dirs` against a non-existent base path returns empty
/// — the early-return arm.
#[tokio::test]
async fn find_python_dirs_nonexistent_base_returns_empty() {
    let tmp = tempfile::tempdir().unwrap();
    let absent = tmp.path().join("does-not-exist");
    let result = find_python_dirs(&absent, &["python3.*", "site-packages"]).await;
    assert!(result.is_empty());
}

/// `find_python_dirs` with empty segments returns the base path
/// itself (terminal-recursion arm).
#[tokio::test]
async fn find_python_dirs_empty_segments_returns_base() {
    let tmp = tempfile::tempdir().unwrap();
    let result = find_python_dirs(tmp.path(), &[]).await;
    assert_eq!(result.len(), 1);
    assert_eq!(result[0], tmp.path());
}

/// Literal segment branch: non-wildcard segment is treated as a
/// literal subdir.
#[tokio::test]
async fn find_python_dirs_literal_segment_descends() {
    let tmp = tempfile::tempdir().unwrap();
    let target = tmp.path().join("literal_subdir").join("more");
    tokio::fs::create_dir_all(&target).await.unwrap();

    let result = find_python_dirs(tmp.path(), &["literal_subdir", "more"]).await;
    assert_eq!(result.len(), 1);
    assert_eq!(result[0], target);
}

// ── find_local_venv_site_packages ──────────────────────────────

/// Build the site-packages relative path for the current OS.
/// Production `find_site_packages_under` looks for `Lib/site-packages`
/// on Windows and `lib/python3.X/site-packages` on Unix — the test
/// fixture must stage whichever the production code expects to find.
fn venv_site_packages_relpath() -> std::path::PathBuf {
    #[cfg(windows)]
    {
        std::path::Path::new("Lib").join("site-packages")
    }
    #[cfg(not(windows))]
    {
        std::path::Path::new("lib")
            .join("python3.11")
            .join("site-packages")
    }
}

/// VIRTUAL_ENV env var pointing at a real venv layout adds it to
/// the discovered list. Covers the first arm of
/// find_local_venv_site_packages.
#[tokio::test]
#[serial]
async fn find_local_venv_site_packages_honors_virtual_env_var() {
    let tmp = tempfile::tempdir().unwrap();
    let venv = tmp.path().join("custom-venv");
    let sp = venv.join(venv_site_packages_relpath());
    tokio::fs::create_dir_all(&sp).await.unwrap();

    let prev = std::env::var("VIRTUAL_ENV").ok();
    std::env::set_var("VIRTUAL_ENV", &venv);
    let result = find_local_venv_site_packages(tmp.path()).await;
    std::env::remove_var("VIRTUAL_ENV");
    if let Some(v) = prev {
        std::env::set_var("VIRTUAL_ENV", v);
    }

    assert!(
        result.iter().any(|p| p == &sp),
        "VIRTUAL_ENV path must surface; got {result:?}"
    );
}

/// `.venv` directory in cwd is discovered when VIRTUAL_ENV is unset.
#[tokio::test]
#[serial]
async fn find_local_venv_site_packages_discovers_dot_venv() {
    let tmp = tempfile::tempdir().unwrap();
    let sp = tmp.path().join(".venv").join(venv_site_packages_relpath());
    tokio::fs::create_dir_all(&sp).await.unwrap();

    let prev = std::env::var("VIRTUAL_ENV").ok();
    std::env::remove_var("VIRTUAL_ENV");
    let result = find_local_venv_site_packages(tmp.path()).await;
    if let Some(v) = prev {
        std::env::set_var("VIRTUAL_ENV", v);
    }
    assert!(
        result.iter().any(|p| p == &sp),
        ".venv must be discovered; got {result:?}"
    );
}

/// `venv` directory in cwd is discovered when neither VIRTUAL_ENV
/// nor .venv exists.
#[tokio::test]
#[serial]
async fn find_local_venv_site_packages_discovers_venv_dir() {
    let tmp = tempfile::tempdir().unwrap();
    let sp = tmp.path().join("venv").join(venv_site_packages_relpath());
    tokio::fs::create_dir_all(&sp).await.unwrap();

    let prev = std::env::var("VIRTUAL_ENV").ok();
    std::env::remove_var("VIRTUAL_ENV");
    let result = find_local_venv_site_packages(tmp.path()).await;
    if let Some(v) = prev {
        std::env::set_var("VIRTUAL_ENV", v);
    }
    assert!(
        result.iter().any(|p| p == &sp),
        "venv must be discovered; got {result:?}"
    );
}

// ── get_global_python_site_packages ─────────────────────────────

/// With HOME stubbed to a tempdir containing a fake anaconda3 layout,
/// the global discovery includes the anaconda site-packages.
#[tokio::test]
#[serial]
async fn get_global_python_site_packages_discovers_anaconda() {
    let tmp = tempfile::tempdir().unwrap();
    let anaconda_sp = tmp
        .path()
        .join("anaconda3")
        .join("lib")
        .join("python3.11")
        .join("site-packages");
    tokio::fs::create_dir_all(&anaconda_sp).await.unwrap();

    let prev_home = std::env::var("HOME").ok();
    std::env::set_var("HOME", tmp.path());
    let result = get_global_python_site_packages().await;
    if let Some(v) = prev_home {
        std::env::set_var("HOME", v);
    }
    // Anaconda must surface; other production paths may also surface
    // since they're scanned unconditionally. The check is "at least
    // the staged path is in the result."
    assert!(
        result.iter().any(|p| p == &anaconda_sp),
        "staged anaconda path must surface; got {result:?}"
    );
}

// ── uv-tools and uv-python discovery ──────────────────────────

/// `uv tool install <pkg>` on macOS installs into
/// `~/Library/Application Support/uv/tools/<pkg>/lib/python3.X/site-packages/`.
/// Stub HOME to a tempdir containing that layout and verify
/// `get_global_python_site_packages` surfaces it.
#[cfg(target_os = "macos")]
#[tokio::test]
#[serial]
async fn get_global_python_site_packages_discovers_uv_tools_macos() {
    let tmp = tempfile::tempdir().unwrap();
    let sp = tmp
        .path()
        .join("Library")
        .join("Application Support")
        .join("uv")
        .join("tools")
        .join("black")
        .join("lib")
        .join("python3.11")
        .join("site-packages");
    tokio::fs::create_dir_all(&sp).await.unwrap();

    let prev_home = std::env::var("HOME").ok();
    std::env::set_var("HOME", tmp.path());
    let result = get_global_python_site_packages().await;
    if let Some(v) = prev_home {
        std::env::set_var("HOME", v);
    }
    assert!(
        result.iter().any(|p| p == &sp),
        "uv tools layout must surface; got {result:?}"
    );
}

/// `uv tool install <pkg>` on Linux installs into
/// `~/.local/share/uv/tools/<pkg>/lib/python3.X/site-packages/`.
#[cfg(all(not(target_os = "macos"), not(windows)))]
#[tokio::test]
#[serial]
async fn get_global_python_site_packages_discovers_uv_tools_linux() {
    let tmp = tempfile::tempdir().unwrap();
    let sp = tmp
        .path()
        .join(".local")
        .join("share")
        .join("uv")
        .join("tools")
        .join("black")
        .join("lib")
        .join("python3.11")
        .join("site-packages");
    tokio::fs::create_dir_all(&sp).await.unwrap();

    let prev_home = std::env::var("HOME").ok();
    std::env::set_var("HOME", tmp.path());
    let result = get_global_python_site_packages().await;
    if let Some(v) = prev_home {
        std::env::set_var("HOME", v);
    }
    assert!(
        result.iter().any(|p| p == &sp),
        "uv tools layout must surface; got {result:?}"
    );
}

/// `uv python install 3.X` installs managed interpreters at
/// `~/.local/share/uv/python/cpython-3.X.*/lib/python3.X/site-packages/`
/// on Linux/macOS. Power users can pip-install directly into that
/// interpreter; the global crawler must surface it.
#[cfg(not(windows))]
#[tokio::test]
#[serial]
async fn get_global_python_site_packages_discovers_uv_python_install() {
    let tmp = tempfile::tempdir().unwrap();
    let sp = tmp
        .path()
        .join(".local")
        .join("share")
        .join("uv")
        .join("python")
        .join("cpython-3.11.6-macos-aarch64-none")
        .join("lib")
        .join("python3.11")
        .join("site-packages");
    tokio::fs::create_dir_all(&sp).await.unwrap();

    let prev_home = std::env::var("HOME").ok();
    std::env::set_var("HOME", tmp.path());
    let result = get_global_python_site_packages().await;
    if let Some(v) = prev_home {
        std::env::set_var("HOME", v);
    }
    assert!(
        result.iter().any(|p| p == &sp),
        "uv-python managed interpreter site-packages must surface; got {result:?}"
    );
}

// ── project-marker fallback in get_site_packages_paths ────────

/// A project with `pyproject.toml` but no `.venv` must fall through
/// to global discovery — without this fallback, a fresh clone before
/// `uv sync` returns zero packages even when the project clearly
/// targets a Python ecosystem.
#[tokio::test]
#[serial]
async fn get_site_packages_paths_falls_back_via_pyproject_marker() {
    let project = tempfile::tempdir().unwrap();
    let home = tempfile::tempdir().unwrap();
    // Marker without venv.
    tokio::fs::write(
        project.path().join("pyproject.toml"),
        b"[project]\nname = \"x\"\n",
    )
    .await
    .unwrap();
    // Stage a uv-tools layout under the stubbed HOME so global
    // discovery has something to find.
    #[cfg(target_os = "macos")]
    let staged = home
        .path()
        .join("Library")
        .join("Application Support")
        .join("uv")
        .join("tools")
        .join("ruff")
        .join("lib")
        .join("python3.11")
        .join("site-packages");
    #[cfg(all(not(target_os = "macos"), not(windows)))]
    let staged = home
        .path()
        .join(".local")
        .join("share")
        .join("uv")
        .join("tools")
        .join("ruff")
        .join("lib")
        .join("python3.11")
        .join("site-packages");
    #[cfg(windows)]
    let staged = home.path().join("uv-fake-staged");
    tokio::fs::create_dir_all(&staged).await.unwrap();

    let prev_home = std::env::var("HOME").ok();
    std::env::set_var("HOME", home.path());
    let crawler = PythonCrawler;
    let opts = CrawlerOptions {
        cwd: project.path().to_path_buf(),
        global: false,
        global_prefix: None,
        batch_size: 100,
    };
    let result = crawler.get_site_packages_paths(&opts).await.unwrap();
    if let Some(v) = prev_home {
        std::env::set_var("HOME", v);
    }

    #[cfg(not(windows))]
    assert!(
        result.iter().any(|p| p == &staged),
        "pyproject.toml marker must trigger global fallback; got {result:?}"
    );
    // On Windows the staged layout doesn't match the global crawler's
    // search paths (different env var), so we only assert the gate
    // engaged at all — i.e. some kind of result was produced.
    #[cfg(windows)]
    let _ = result;
}

/// `uv.lock` alone is also a valid Python-project marker — a fresh
/// clone of a uv-managed repo shouldn't need a venv to be scannable.
#[tokio::test]
#[serial]
async fn get_site_packages_paths_falls_back_via_uv_lock_marker() {
    let project = tempfile::tempdir().unwrap();
    let home = tempfile::tempdir().unwrap();
    tokio::fs::write(project.path().join("uv.lock"), b"version = 1\n")
        .await
        .unwrap();

    let prev_home = std::env::var("HOME").ok();
    std::env::set_var("HOME", home.path());
    let crawler = PythonCrawler;
    let opts = CrawlerOptions {
        cwd: project.path().to_path_buf(),
        global: false,
        global_prefix: None,
        batch_size: 100,
    };
    // The result vec may be empty (no global Python layouts staged
    // under the home tempdir), but the call must succeed — the gate
    // engaged. We assert get_site_packages_paths returned Ok rather
    // than panicking, which would only happen if the marker path
    // was wrong.
    let _ = crawler.get_site_packages_paths(&opts).await.unwrap();
    if let Some(v) = prev_home {
        std::env::set_var("HOME", v);
    }
}

/// Without any Python-project marker AND without a venv, local-mode
/// discovery returns an empty Vec — no false positives from scanning
/// a non-Python project.
#[tokio::test]
#[serial]
async fn get_site_packages_paths_no_marker_no_venv_returns_empty() {
    let project = tempfile::tempdir().unwrap();
    let crawler = PythonCrawler;
    let opts = CrawlerOptions {
        cwd: project.path().to_path_buf(),
        global: false,
        global_prefix: None,
        batch_size: 100,
    };
    let prev_virtual_env = std::env::var("VIRTUAL_ENV").ok();
    std::env::remove_var("VIRTUAL_ENV");
    let result = crawler.get_site_packages_paths(&opts).await.unwrap();
    if let Some(v) = prev_virtual_env {
        std::env::set_var("VIRTUAL_ENV", v);
    }
    assert!(
        result.is_empty(),
        "non-python project must produce zero paths; got {result:?}"
    );
}

// ── read_python_metadata ───────────────────────────────────────

/// Well-formed METADATA returns (name, version).
#[tokio::test]
async fn read_python_metadata_well_formed() {
    let tmp = tempfile::tempdir().unwrap();
    let dist_info = tmp.path().join("requests-2.28.0.dist-info");
    tokio::fs::create_dir(&dist_info).await.unwrap();
    tokio::fs::write(
        dist_info.join("METADATA"),
        "Metadata-Version: 2.1\nName: requests\nVersion: 2.28.0\n",
    )
    .await
    .unwrap();

    let result = read_python_metadata(&dist_info).await;
    assert_eq!(result, Some(("requests".to_string(), "2.28.0".to_string())));
}

/// Missing METADATA file → fall back to the `<name>-<version>.dist-info`
/// directory name so a partially-written install stays discoverable.
#[tokio::test]
async fn read_python_metadata_missing_file_falls_back_to_dir_name() {
    let tmp = tempfile::tempdir().unwrap();
    let dist_info = tmp.path().join("requests-2.28.0.dist-info");
    tokio::fs::create_dir(&dist_info).await.unwrap();
    // No METADATA file.

    let result = read_python_metadata(&dist_info).await;
    assert_eq!(result, Some(("requests".to_string(), "2.28.0".to_string())));
}

/// METADATA missing Name field → headers are unusable, so fall back to the
/// directory name rather than dropping the package.
#[tokio::test]
async fn read_python_metadata_missing_name_falls_back_to_dir_name() {
    let tmp = tempfile::tempdir().unwrap();
    let dist_info = tmp.path().join("requests-2.28.0.dist-info");
    tokio::fs::create_dir(&dist_info).await.unwrap();
    tokio::fs::write(
        dist_info.join("METADATA"),
        "Metadata-Version: 2.1\nVersion: 2.28.0\n",
    )
    .await
    .unwrap();

    let result = read_python_metadata(&dist_info).await;
    assert_eq!(result, Some(("requests".to_string(), "2.28.0".to_string())));
}

#[path = "common/mod.rs"]
mod common;

/// `find_by_purls` short-circuits when the site-packages dir is
/// unreadable. Drives the python_crawler.rs:530 read_dir Err arm.
#[cfg(unix)]
#[tokio::test]
async fn find_by_purls_handles_unreadable_site_packages() {
    if common::uid_is_root() {
        eprintln!("SKIP: chmod 000 is a no-op under root");
        return;
    }
    let tmp = tempfile::tempdir().unwrap();
    let site_packages = tmp.path().join("sp");
    tokio::fs::create_dir(&site_packages).await.unwrap();
    common::chmod_unreadable(&site_packages);

    let crawler = PythonCrawler;
    let result = crawler
        .find_by_purls(&site_packages, &["pkg:pypi/requests@2.28.0".to_string()])
        .await
        .unwrap();
    common::chmod_readable(&site_packages);

    assert!(result.is_empty());
}

/// `scan_site_packages` short-circuits when site-packages is
/// unreadable — drives python_crawler.rs:584 read_dir Err arm.
#[cfg(unix)]
#[tokio::test]
async fn crawl_all_handles_unreadable_site_packages() {
    if common::uid_is_root() {
        eprintln!("SKIP: chmod 000 is a no-op under root");
        return;
    }
    let tmp = tempfile::tempdir().unwrap();
    let site_packages = tmp.path().join("sp");
    tokio::fs::create_dir(&site_packages).await.unwrap();
    common::chmod_unreadable(&site_packages);

    let crawler = PythonCrawler;
    let opts = CrawlerOptions {
        cwd: tmp.path().to_path_buf(),
        global: true,
        global_prefix: Some(site_packages.clone()),
        batch_size: 100,
    };
    let result = crawler.crawl_all(&opts).await;
    common::chmod_readable(&site_packages);

    assert!(result.is_empty());
}

/// `PythonCrawler::default()` should forward to `new()`.
#[test]
fn python_crawler_default_and_new_construct_cleanly() {
    let _a = PythonCrawler::default();
    let _b = PythonCrawler::new();
}

// ── find_by_purls + crawl_all over a staged site-packages ─────

/// Helper: stage a well-formed `<pkg>-<version>.dist-info/METADATA`
/// inside a fake site-packages directory.
async fn stage_dist_info(site_packages: &Path, raw_name: &str, version: &str) {
    let dist = site_packages.join(format!("{raw_name}-{version}.dist-info"));
    tokio::fs::create_dir_all(&dist).await.unwrap();
    let metadata = format!("Metadata-Version: 2.1\nName: {raw_name}\nVersion: {version}\n");
    tokio::fs::write(dist.join("METADATA"), metadata)
        .await
        .unwrap();
}

#[tokio::test]
async fn find_by_purls_matches_canonicalized_name() {
    let tmp = tempfile::tempdir().unwrap();
    // PEP 503 canonicalization: "Requests" -> "requests"
    stage_dist_info(tmp.path(), "Requests", "2.28.0").await;

    let crawler = PythonCrawler;
    let result = crawler
        .find_by_purls(tmp.path(), &["pkg:pypi/requests@2.28.0".to_string()])
        .await
        .unwrap();
    assert_eq!(result.len(), 1, "canonical lookup must hit");
}

#[tokio::test]
async fn find_by_purls_strips_qualifiers() {
    let tmp = tempfile::tempdir().unwrap();
    stage_dist_info(tmp.path(), "requests", "2.28.0").await;

    let crawler = PythonCrawler;
    let result = crawler
        .find_by_purls(
            tmp.path(),
            &["pkg:pypi/requests@2.28.0?extension=tar.gz".to_string()],
        )
        .await
        .unwrap();
    assert_eq!(result.len(), 1, "qualifiers must be stripped before lookup");
}

#[tokio::test]
async fn find_by_purls_empty_purls_returns_empty() {
    let tmp = tempfile::tempdir().unwrap();
    stage_dist_info(tmp.path(), "requests", "2.28.0").await;

    let crawler = PythonCrawler;
    let result = crawler.find_by_purls(tmp.path(), &[]).await.unwrap();
    assert!(result.is_empty());
}

#[tokio::test]
async fn find_by_purls_missing_site_packages_returns_empty() {
    let tmp = tempfile::tempdir().unwrap();
    let crawler = PythonCrawler;
    // site_packages_path doesn't exist — read_dir Err arm must yield empty.
    let result = crawler
        .find_by_purls(
            &tmp.path().join("no-such-dir"),
            &["pkg:pypi/requests@2.28.0".to_string()],
        )
        .await
        .unwrap();
    assert!(result.is_empty());
}

#[tokio::test]
async fn find_by_purls_invalid_purl_skipped() {
    let tmp = tempfile::tempdir().unwrap();
    stage_dist_info(tmp.path(), "requests", "2.28.0").await;

    let crawler = PythonCrawler;
    let result = crawler
        .find_by_purls(tmp.path(), &["pkg:not-pypi/foo@1.0".to_string()])
        .await
        .unwrap();
    assert!(result.is_empty());
}

#[tokio::test]
async fn find_by_purls_version_mismatch_returns_empty() {
    let tmp = tempfile::tempdir().unwrap();
    stage_dist_info(tmp.path(), "requests", "2.28.0").await;

    let crawler = PythonCrawler;
    let result = crawler
        .find_by_purls(tmp.path(), &["pkg:pypi/requests@99.99.99".to_string()])
        .await
        .unwrap();
    assert!(result.is_empty());
}

#[tokio::test]
async fn crawl_all_via_site_packages_finds_dist_info_packages() {
    let tmp = tempfile::tempdir().unwrap();
    stage_dist_info(tmp.path(), "Requests", "2.28.0").await;
    stage_dist_info(tmp.path(), "urllib3", "2.0.0").await;
    // A non-dist-info dir should be skipped.
    tokio::fs::create_dir_all(tmp.path().join("ignore-me"))
        .await
        .unwrap();

    let crawler = PythonCrawler;
    let opts = CrawlerOptions {
        cwd: tmp.path().to_path_buf(),
        global: true,
        global_prefix: Some(tmp.path().to_path_buf()),
        batch_size: 100,
    };
    let result = crawler.crawl_all(&opts).await;
    let names: Vec<&str> = result.iter().map(|p| p.name.as_str()).collect();
    assert!(names.contains(&"requests"));
    assert!(names.contains(&"urllib3"));
    assert_eq!(result.len(), 2);
}

#[tokio::test]
async fn crawl_all_with_unparseable_dist_info_skips() {
    let tmp = tempfile::tempdir().unwrap();
    // No version segment in the directory name, so neither the (empty)
    // METADATA nor the dir-name fallback can yield a name/version — the
    // package is genuinely unidentifiable and must be skipped.
    let dist = tmp.path().join("corrupt.dist-info");
    tokio::fs::create_dir_all(&dist).await.unwrap();
    tokio::fs::write(dist.join("METADATA"), b"").await.unwrap();

    let crawler = PythonCrawler;
    let opts = CrawlerOptions {
        cwd: tmp.path().to_path_buf(),
        global: true,
        global_prefix: Some(tmp.path().to_path_buf()),
        batch_size: 100,
    };
    let result = crawler.crawl_all(&opts).await;
    assert!(
        result.is_empty(),
        "a dist-info with no usable metadata or version-bearing name must be skipped"
    );
}

/// `get_site_packages_paths` with `global_prefix` set returns just that
/// prefix — exercises the early-return arm at python_crawler.rs:473-474.
#[tokio::test]
async fn get_site_packages_paths_with_global_prefix_passthrough() {
    let tmp = tempfile::tempdir().unwrap();
    let custom = tmp.path().join("custom-sp");
    tokio::fs::create_dir_all(&custom).await.unwrap();

    let crawler = PythonCrawler;
    let opts = CrawlerOptions {
        cwd: tmp.path().to_path_buf(),
        global: false,
        global_prefix: Some(custom.clone()),
        batch_size: 100,
    };
    let paths = crawler.get_site_packages_paths(&opts).await.unwrap();
    assert_eq!(paths, vec![custom]);
}

// ── METADATA early-break arm ───────────────────────────────────

/// METADATA header lines AFTER the blank line must NOT be parsed — the
/// header parser stops at the first blank line. Here only `Name` is set
/// before the blank line, so the `Version` below it is never read from the
/// headers; the function then falls back to the directory name. We give the
/// directory a *different* version (`9.9.9`) than the post-blank-line header
/// (`2.28.0`) so the result proves the blank-line break fired: a `2.28.0`
/// result would mean the break leaked the trailing header.
#[tokio::test]
async fn read_python_metadata_stops_at_blank_line_then_falls_back() {
    let tmp = tempfile::tempdir().unwrap();
    let dist = tmp.path().join("requests-9.9.9.dist-info");
    tokio::fs::create_dir(&dist).await.unwrap();
    tokio::fs::write(dist.join("METADATA"), "Name: requests\n\nVersion: 2.28.0\n")
        .await
        .unwrap();

    let result = read_python_metadata(&dist).await;
    assert_eq!(
        result,
        Some(("requests".to_string(), "9.9.9".to_string())),
        "blank-line break must fire before Version is read, so the version \
         comes from the dir name; got {result:?}"
    );
}

/// METADATA missing Version field → headers unusable, fall back to the
/// directory name.
#[tokio::test]
async fn read_python_metadata_missing_version_falls_back_to_dir_name() {
    let tmp = tempfile::tempdir().unwrap();
    let dist_info = tmp.path().join("requests-2.28.0.dist-info");
    tokio::fs::create_dir(&dist_info).await.unwrap();
    tokio::fs::write(
        dist_info.join("METADATA"),
        "Metadata-Version: 2.1\nName: requests\n",
    )
    .await
    .unwrap();

    let result = read_python_metadata(&dist_info).await;
    assert_eq!(result, Some(("requests".to_string(), "2.28.0".to_string())));
}