alef 0.56.0

Opinionated polyglot binding generator for Rust libraries
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
use super::*;
use crate::core::backend::GeneratedFile;
use crate::core::config::{Language, NewAlefConfig, ResolvedCrateConfig};

fn make_config(crate_name: &str) -> ResolvedCrateConfig {
    let cfg: NewAlefConfig = toml::from_str(&format!(
        r#"
[workspace]
languages = ["rust"]
[[crates]]
name = "{crate_name}"
sources = ["src/lib.rs"]
"#
    ))
    .expect("valid config");
    cfg.resolve().unwrap().remove(0)
}

#[test]
fn required_formatters_always_include_rustfmt_and_poly() {
    let tools: Vec<&str> = required_formatters(&[Language::Python])
        .iter()
        .map(|f| f.tool)
        .collect();
    assert!(tools.contains(&"rustfmt"));
    assert!(tools.contains(&"poly"));
    assert!(!tools.contains(&"cargo-sort"), "python has no cargo-sort residual");
}

#[test]
fn required_formatters_add_cargo_sort_for_residual_languages() {
    for language in [
        Language::Wasm,
        Language::Ffi,
        Language::Ruby,
        Language::Elixir,
        Language::R,
    ] {
        let tools: Vec<&str> = required_formatters(&[language]).iter().map(|f| f.tool).collect();
        assert!(tools.contains(&"cargo-sort"), "{language} runs a cargo-sort residual");
    }
}

#[test]
fn required_formatters_add_mix_only_when_elixir_is_generated() {
    let tools: Vec<&str> = required_formatters(&[Language::Elixir])
        .iter()
        .map(|f| f.tool)
        .collect();
    assert!(tools.contains(&"mix"), "Elixir must require `mix` on PATH");

    for language in [
        Language::Python,
        Language::Wasm,
        Language::Ffi,
        Language::Ruby,
        Language::R,
    ] {
        let tools: Vec<&str> = required_formatters(&[language]).iter().map(|f| f.tool).collect();
        assert!(
            !tools.contains(&"mix"),
            "{language} must not require `mix` (only Elixir generation does)"
        );
    }
}

#[test]
fn formatter_error_includes_stdout_and_stderr() {
    let err = run_formatter(
        "sh",
        &["-c", "printf 'stdout text'; printf 'stderr text' >&2; exit 7"],
        Path::new("."),
    )
    .expect_err("formatter should fail");
    let msg = err.to_string();
    assert!(msg.contains("stdout text"), "missing stdout in error: {msg}");
    assert!(msg.contains("stderr text"), "missing stderr in error: {msg}");
}

#[test]
fn wasm_residual_is_cargo_sort_n_on_the_crate_dir() {
    let config = make_config("sample-model");
    let steps = language_residuals(&config, Language::Wasm, Path::new("/repo"));
    assert_eq!(steps.len(), 1, "wasm residual must be a single cargo sort step");
    assert_eq!(steps[0].command, "cargo");
    assert_eq!(
        steps[0].args,
        vec!["sort", "-n", "crates/sample-model-wasm"],
        "cargo sort -n arg must be the wasm crate directory"
    );
    assert_eq!(
        steps[0].work_dir,
        Path::new("/repo"),
        "wasm cargo sort runs at repo root"
    );
}

#[test]
fn wasm_residual_uses_configured_output_path() {
    let cfg: NewAlefConfig = toml::from_str(
        r#"
[workspace]
languages = ["wasm"]
[[crates]]
name = "sample-language-pack"
sources = ["crates/sample-pack-core/src/lib.rs"]
[crates.output]
wasm = "crates/sample-pack-core-wasm/src/"
"#,
    )
    .expect("valid config");
    let config = cfg.resolve().unwrap().remove(0);
    let steps = language_residuals(&config, Language::Wasm, Path::new("/repo"));
    assert_eq!(
        steps[0].args,
        vec!["sort", "-n", "crates/sample-pack-core-wasm"],
        "cargo sort arg must match the crate dir derived from the configured output path"
    );
}

#[test]
fn ffi_residual_is_cargo_sort_n_workspace_wide() {
    let config = make_config("sample-model");
    let steps = language_residuals(&config, Language::Ffi, Path::new("/repo"));
    assert_eq!(steps.len(), 1, "FFI residual must be a single cargo sort step");
    assert_eq!(steps[0].command, "cargo");
    assert_eq!(
        steps[0].args,
        vec!["sort", "-n", "-w"],
        "FFI cargo sort must be workspace-wide with -n flag"
    );
    assert_eq!(steps[0].work_dir, Path::new("/repo"));
}

#[test]
fn ruby_residual_sorts_the_native_crate() {
    let config = make_config("sample-model");
    let steps = language_residuals(&config, Language::Ruby, Path::new("/repo"));
    assert_eq!(steps.len(), 1, "Ruby residual must be a single cargo sort step");
    assert_eq!(steps[0].command, "cargo");
    assert_eq!(steps[0].args[0], "sort");
    assert_eq!(steps[0].args[1], "-n");
    assert!(
        steps[0].args[2].starts_with("ext/") && steps[0].args[2].ends_with("/native"),
        "cargo sort arg must target ext/<gem>/native, got: {:?}",
        steps[0].args
    );
    assert_eq!(steps[0].work_dir, Path::new("/repo/packages/ruby"));
}

#[test]
fn elixir_residual_is_cargo_sort_then_mix_deps_get_then_mix_format() {
    let config = make_config("sample-model");
    let steps = language_residuals(&config, Language::Elixir, Path::new("/repo"));
    assert_eq!(
        steps.len(),
        3,
        "Elixir residual must be cargo sort, then mix deps.get, then mix format"
    );

    assert_eq!(steps[0].command, "cargo");
    assert_eq!(steps[0].args[0], "sort");
    assert_eq!(steps[0].args[1], "-n");
    assert!(
        steps[0].args[2].starts_with("native/") && steps[0].args[2].ends_with("_nif"),
        "cargo sort arg must target native/<app>_nif, got: {:?}",
        steps[0].args
    );
    assert_eq!(steps[0].work_dir, Path::new("/repo/packages/elixir"));

    assert_eq!(steps[1].command, "mix");
    assert_eq!(steps[1].args, vec!["deps.get"]);
    assert_eq!(
        steps[1].work_dir,
        Path::new("/repo/packages/elixir"),
        "mix deps.get must run in the elixir package dir so it resolves mix.exs"
    );

    assert_eq!(steps[2].command, "mix");
    assert_eq!(steps[2].args, vec!["format"]);
    assert_eq!(
        steps[2].work_dir,
        Path::new("/repo/packages/elixir"),
        "mix format must run in the elixir package dir so it resolves mix.exs"
    );
}

#[test]
fn r_residual_sorts_the_extendr_crate() {
    let config = make_config("sample-model");
    let steps = language_residuals(&config, Language::R, Path::new("/repo"));
    assert_eq!(steps.len(), 1, "R residual must be a single cargo sort step");
    assert_eq!(steps[0].args, vec!["sort", "-n", "packages/r/src/rust"]);
    assert_eq!(steps[0].work_dir, Path::new("/repo"));
}

#[test]
fn csharp_has_no_residual() {
    let config = make_config("sample-model");
    assert!(language_residuals(&config, Language::Csharp, Path::new("/repo")).is_empty());
}

#[test]
fn languages_without_residuals_have_none() {
    let config = make_config("sample-model");
    for lang in [
        Language::Python,
        Language::Node,
        Language::Go,
        Language::Java,
        Language::Kotlin,
        Language::Csharp,
    ] {
        assert!(
            language_residuals(&config, lang, Path::new("/repo")).is_empty(),
            "{lang:?} must have no residual native pass (poly formats it)"
        );
    }
}

#[test]
fn cargo_sort_residuals_returns_fixed_set() {
    let config = make_config("sample-model");
    let steps = cargo_sort_residuals(&config, Path::new("/repo"));
    assert_eq!(steps.len(), 5, "cargo_sort_residuals must return exactly 5 steps");
    for step in &steps {
        assert_eq!(step.command, "cargo");
        assert_eq!(step.args[0], "sort");
        assert_eq!(step.args[1], "-n", "all residuals must use -n flag");
    }
}

#[test]
fn cargo_sort_residuals_includes_workspace_wide_step() {
    let config = make_config("sample-model");
    let steps = cargo_sort_residuals(&config, Path::new("/repo"));
    let has_workspace_wide = steps.iter().any(|s| s.args.contains(&"-w".to_owned()));
    assert!(
        has_workspace_wide,
        "cargo_sort_residuals must include a workspace-wide step"
    );
}

#[test]
fn cargo_sort_residuals_excludes_elixirs_mix_steps() {
    // Elixir's own `language_residuals` entry now returns 3 steps (cargo sort,
    // mix deps.get, mix format). `cargo_sort_residuals` must still report exactly
    // 5 steps, all `cargo` -- the fixed set stays a pure cargo-sort aggregator and
    // must not silently start running `mix` commands under a name/contract that
    // says "cargo sort only" (this backs `run_cargo_sort_residuals`, used by the
    // `alef fmt` command).
    let config = make_config("sample-model");
    let steps = cargo_sort_residuals(&config, Path::new("/repo"));
    assert_eq!(steps.len(), 5, "mix steps must not leak into the cargo-sort aggregator");
    assert!(
        steps.iter().all(|s| s.command == "cargo"),
        "cargo_sort_residuals must only ever contain `cargo` steps, got: {steps:?}"
    );
}

#[test]
fn poly_paths_full_regen_is_repo_root() {
    let config = make_config("sample-model");
    let paths = poly_paths(&config, Path::new("/repo"), None, &[Language::Python]);
    assert_eq!(
        paths,
        vec![PathBuf::from("/repo")],
        "full regen formats the whole repo once"
    );
}

#[test]
fn poly_paths_partial_regen_scopes_to_existing_package_dirs() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    let config = make_config("sample-model");
    let py_dir = base.join(config.package_dir(Language::Python));
    std::fs::create_dir_all(&py_dir).unwrap();

    let only: HashSet<Language> = [Language::Python].into_iter().collect();
    let paths = poly_paths(&config, base, Some(&only), &[Language::Python]);
    assert_eq!(
        paths,
        vec![py_dir],
        "partial regen scopes to the changed language's package dir"
    );
}

#[test]
fn poly_paths_partial_regen_drops_nonexistent_dirs() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    let config = make_config("sample-model");
    let only: HashSet<Language> = [Language::Python].into_iter().collect();
    let paths = poly_paths(&config, base, Some(&only), &[Language::Python]);
    assert!(paths.is_empty(), "nonexistent package dirs are dropped");
}

#[test]
fn poly_pass_formats_generated_python_when_poly_installed() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    let py_path = base.join("packages/python/foo.py");
    std::fs::create_dir_all(py_path.parent().unwrap()).unwrap();
    std::fs::write(&py_path, "x=1").unwrap();

    let cfg: NewAlefConfig = toml::from_str(
        r#"
[workspace]
languages = ["python"]
[[crates]]
name = "sample-model"
sources = ["src/lib.rs"]
"#,
    )
    .expect("valid config");
    let config = cfg.resolve().unwrap().remove(0);

    let files: Vec<(Language, Vec<GeneratedFile>)> = vec![(
        Language::Python,
        vec![GeneratedFile {
            path: PathBuf::from("packages/python/foo.py"),
            content: "x=1".to_owned(),
            generated_header: false,
        }],
    )];

    format_generated(&files, &config, base, None);

    let formatted = std::fs::read_to_string(&py_path).unwrap();
    if is_tool_available("poly") {
        assert_eq!(
            formatted, "x = 1\n",
            "with poly installed, `poly fmt --fix` must reformat the generated Python file"
        );
    } else {
        assert_eq!(formatted, "x=1", "without poly the file must be left untouched");
    }
}

#[test]
fn install_poly_hooks_is_noop_outside_git_repo() {
    let dir = tempfile::tempdir().expect("tempdir");
    install_poly_hooks(dir.path());
}

#[test]
fn max_poly_fmt_passes_is_bounded() {
    assert_eq!(
        MAX_POLY_FMT_PASSES, 3,
        "the convergence loop must be capped at 3 passes per the full-regen contract"
    );
}

/// Writes a two-crate cargo workspace at `base` with deliberately unsorted
/// `[dependencies]` in both members. `second_crate_suffix` lets callers simulate a
/// crate that historically had no per-language cargo-sort residual (e.g. a `-py`
/// or `-node` crate) to prove the workspace-wide sort covers it too.
fn write_unsorted_workspace(base: &Path, second_crate_suffix: &str) {
    std::fs::write(
        base.join("Cargo.toml"),
        format!("[workspace]\nmembers = [\"crates/pkg-ffi\", \"crates/pkg{second_crate_suffix}\"]\nresolver = \"2\"\n"),
    )
    .unwrap();
    for member in ["pkg-ffi", &format!("pkg{second_crate_suffix}")] {
        let crate_dir = base.join("crates").join(member);
        std::fs::create_dir_all(crate_dir.join("src")).unwrap();
        std::fs::write(
            crate_dir.join("Cargo.toml"),
            format!(
                "[package]\nname = \"{member}\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n\
                 [dependencies]\nserde = \"1\"\nanyhow = \"1\"\n"
            ),
        )
        .unwrap();
        std::fs::write(crate_dir.join("src/lib.rs"), "pub fn noop() {}\n").unwrap();
    }
}

#[test]
fn run_workspace_cargo_sort_sorts_every_member_regardless_of_language() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    // "-py" simulates a crate suffix (python/pyo3) that has no entry at all in
    // `language_residuals` — the coverage gap this workspace-wide pass replaces.
    write_unsorted_workspace(base, "-py");

    run_workspace_cargo_sort(base);

    let py_toml = std::fs::read_to_string(base.join("crates/pkg-py/Cargo.toml")).unwrap();
    if is_tool_available("cargo-sort") {
        let anyhow_pos = py_toml.find("anyhow").expect("anyhow present");
        let serde_pos = py_toml.find("serde").expect("serde present");
        assert!(
            anyhow_pos < serde_pos,
            "cargo sort -n -w must sort deps alphabetically even for a crate with no \
             per-language residual, got: {py_toml}"
        );
        let check = std::process::Command::new("cargo")
            .args(["sort", "--check", "-w"])
            .current_dir(base)
            .output()
            .expect("cargo sort --check");
        assert!(
            check.status.success(),
            "workspace must be reported sorted after run_workspace_cargo_sort: {}",
            String::from_utf8_lossy(&check.stderr)
        );
    } else {
        assert!(
            py_toml.contains("serde = \"1\"\nanyhow = \"1\""),
            "without cargo-sort, untouched"
        );
    }
}

#[test]
fn run_workspace_cargo_sort_is_noop_without_root_cargo_toml() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    std::fs::write(base.join("marker.txt"), "untouched").unwrap();

    run_workspace_cargo_sort(base);

    assert!(
        !base.join("Cargo.toml").exists(),
        "must not create a Cargo.toml when there was none"
    );
}

#[test]
fn run_cargo_fmt_formats_workspace_rust_files_when_available() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    write_unsorted_workspace(base, "-node");
    let lib_path = base.join("crates/pkg-ffi/src/lib.rs");
    std::fs::write(&lib_path, "pub fn noop( ) {\nlet x=1;\nx;\n}\n").unwrap();

    run_cargo_fmt(base);

    let formatted = std::fs::read_to_string(&lib_path).unwrap();
    if is_tool_available("cargo") && is_tool_available("rustfmt") {
        assert_eq!(
            formatted, "pub fn noop() {\n    let x = 1;\n    x;\n}\n",
            "cargo fmt --all must reformat every workspace member's Rust source"
        );
    } else {
        assert_eq!(
            formatted, "pub fn noop( ) {\nlet x=1;\nx;\n}\n",
            "without cargo/rustfmt, untouched"
        );
    }
}

#[test]
fn run_cargo_fmt_is_noop_without_root_cargo_toml() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    let file_path = base.join("orphan.rs");
    std::fs::write(&file_path, "fn noop( ) {}\n").unwrap();

    run_cargo_fmt(base);

    assert_eq!(
        std::fs::read_to_string(&file_path).unwrap(),
        "fn noop( ) {}\n",
        "a directory with no root Cargo.toml is not a cargo workspace; must be left untouched"
    );
}

/// Writes a minimal, dependency-free mix project at `elixir_dir` (`mix.exs`,
/// `.formatter.exs`, and `lib/sample.ex` with `content`). Deliberately has no
/// `deps` (no `import_deps`), so `mix deps.get`/`mix format` never touch the
/// network -- these tests must stay hermetic per test-independence, unlike the
/// scaffolded `.formatter.exs`'s real `import_deps: [:rustler]`.
fn write_minimal_mix_project(elixir_dir: &Path, content: &str) {
    std::fs::create_dir_all(elixir_dir.join("lib")).unwrap();
    std::fs::write(
        elixir_dir.join("mix.exs"),
        "defmodule Sample.MixProject do\n  use Mix.Project\n\n  def project do\n    [app: :sample, \
         version: \"0.1.0\", elixir: \"~> 1.14\"]\n  end\nend\n",
    )
    .unwrap();
    std::fs::write(
        elixir_dir.join(".formatter.exs"),
        "[inputs: [\"mix.exs\", \"lib/**/*.{ex,exs}\"]]\n",
    )
    .unwrap();
    std::fs::write(elixir_dir.join("lib/sample.ex"), content).unwrap();
}

#[test]
fn run_elixir_mix_format_is_noop_without_packages_elixir() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();

    run_elixir_mix_format(base);

    assert!(
        !base.join("packages/elixir").exists(),
        "must not create a packages/elixir when there was none"
    );
}

#[test]
fn run_elixir_mix_format_reformats_the_generated_package_when_mix_installed() {
    if !is_tool_available("mix") {
        return;
    }
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    let elixir_dir = base.join("packages/elixir");
    write_minimal_mix_project(&elixir_dir, "defmodule Sample do\n  def noop, do:    :ok\nend\n");

    run_elixir_mix_format(base);

    assert_eq!(
        std::fs::read_to_string(elixir_dir.join("lib/sample.ex")).unwrap(),
        "defmodule Sample do\n  def noop, do: :ok\nend\n",
        "mix format must collapse the misaligned `do:` spacing"
    );
}

#[test]
fn poly_format_never_rewrites_elixir_source() {
    if !is_tool_available("poly") || !is_tool_available("mix") {
        return;
    }
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    let elixir_dir = base.join("packages/elixir");
    let correctly_formatted = "defmodule Sample do\n  def clean(map) do\n    map\n    |> Enum.reject(fn {_k, v} -> v == nil end)\n    \
         |> Map.new()\n  end\nend\n";
    write_minimal_mix_project(&elixir_dir, correctly_formatted);
    // A sibling non-Elixir file in the same package dir proves the exclude is
    // scoped to `.ex`/`.exs`, not the whole `packages/elixir` directory (the
    // native NIF crate's Rust sources still need poly's rustfmt engine).
    std::fs::write(elixir_dir.join("unrelated.py"), "x=1").unwrap();

    poly_format(&[base.to_path_buf()], base);

    assert_eq!(
        std::fs::read_to_string(elixir_dir.join("lib/sample.ex")).unwrap(),
        correctly_formatted,
        "poly must never rewrite .ex files -- mix format is their sole authority"
    );
    assert_eq!(
        std::fs::read_to_string(elixir_dir.join("unrelated.py")).unwrap(),
        "x = 1\n",
        "excluding .ex/.exs must not stop poly from formatting other files in the same dir"
    );
}

#[test]
fn format_generated_partial_regen_leaves_mix_formatted_elixir_untouched_by_poly() {
    if !is_tool_available("poly") || !is_tool_available("mix") {
        return;
    }
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    let cfg: NewAlefConfig = toml::from_str(
        r#"
[workspace]
languages = ["elixir"]
[[crates]]
name = "sample-model"
sources = ["src/lib.rs"]
"#,
    )
    .expect("valid config");
    let config = cfg.resolve().unwrap().remove(0);
    let elixir_dir = base.join(config.package_dir(Language::Elixir));
    let correctly_formatted = "defmodule Sample do\n  def clean(map) do\n    map\n    |> Enum.reject(fn {_k, v} -> \
         v == nil end)\n    |> Map.new()\n  end\nend\n";
    write_minimal_mix_project(&elixir_dir, correctly_formatted);

    let files: Vec<(Language, Vec<GeneratedFile>)> = vec![(Language::Elixir, vec![])];
    let only: HashSet<Language> = [Language::Elixir].into_iter().collect();
    format_generated(&files, &config, base, Some(&only));

    assert_eq!(
        std::fs::read_to_string(elixir_dir.join("lib/sample.ex")).unwrap(),
        correctly_formatted,
        "the full format_generated pipeline (poly excluded + mix residual) must leave \
         already-mix-formatted Elixir source unchanged"
    );
}

#[test]
fn poly_fmt_is_clean_reflects_check_state() {
    if !is_tool_available("poly") {
        return;
    }
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    let py_path = base.join("dirty.py");
    std::fs::write(&py_path, "x=1").unwrap();

    assert!(!poly_fmt_is_clean(base), "an unformatted file must report not-clean");

    poly_format(&[base.to_path_buf()], base);

    assert!(
        poly_fmt_is_clean(base),
        "after `poly fmt --fix`, the tree must report clean"
    );
}

/// poly rewrites changed files via atomic rename, which resets the mode to `0644`.
/// `poly_format` must put the executable bit back, or every regen strips it from
/// the generated shebang scripts and poly's own `file-safety` lint rejects the
/// next commit.
#[cfg(unix)]
#[test]
fn poly_format_restores_executable_bit_on_reformatted_shebang_script() {
    use std::os::unix::fs::PermissionsExt as _;

    if !is_tool_available("poly") {
        return;
    }
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    let script = base.join("run_tests.php");
    std::fs::write(&script, "#!/usr/bin/env php\n<?php\n$x   =   1;\necho $x;\n").unwrap();
    std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();

    poly_format(&[base.to_path_buf()], base);

    let mode = std::fs::metadata(&script).unwrap().permissions().mode();
    assert_eq!(
        mode & 0o777,
        0o755,
        "poly_format must restore the pre-format mode, got {mode:#o}"
    );
}

/// The snapshot must not walk dependency-cache and build directories: on a
/// repo-root pass they dwarf the tree being formatted and hold no generated
/// scripts.
#[cfg(unix)]
#[test]
fn executable_mode_snapshot_skips_dependency_and_build_directories() {
    use std::os::unix::fs::PermissionsExt as _;

    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    let tracked = base.join("scripts/run.sh");
    let ignored = base.join("node_modules/.bin/tool");
    for path in [&tracked, &ignored] {
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(path, "#!/bin/sh\n").unwrap();
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
    }

    let snapshot = snapshot_executable_modes(&[base.to_path_buf()]);

    let recorded: Vec<&PathBuf> = snapshot.iter().map(|(path, _)| path).collect();
    assert_eq!(recorded, vec![&tracked], "only the non-skipped script may be recorded");
}

#[test]
fn converge_full_regen_formatting_leaves_workspace_sorted_and_poly_fmt_check_clean() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    // "-swift" simulates another crate suffix with no per-language residual today.
    write_unsorted_workspace(base, "-swift");
    std::fs::write(
        base.join("crates/pkg-swift/src/lib.rs"),
        "pub fn noop( ) {\nlet x=1;\nx;\n}\n",
    )
    .unwrap();

    converge_full_regen_formatting(base);

    if is_tool_available("cargo-sort") {
        let swift_toml = std::fs::read_to_string(base.join("crates/pkg-swift/Cargo.toml")).unwrap();
        let anyhow_pos = swift_toml.find("anyhow").expect("anyhow present");
        let serde_pos = swift_toml.find("serde").expect("serde present");
        assert!(
            anyhow_pos < serde_pos,
            "workspace-wide sort must cover every crate on a full regen"
        );
    }
    if is_tool_available("cargo") && is_tool_available("rustfmt") {
        let formatted = std::fs::read_to_string(base.join("crates/pkg-swift/src/lib.rs")).unwrap();
        assert_eq!(formatted, "pub fn noop() {\n    let x = 1;\n    x;\n}\n");
    }
    if is_tool_available("poly") {
        assert!(
            poly_fmt_is_clean(base),
            "the convergence loop must leave `poly fmt --check` clean, satisfying the \
             zero-drift full-regen contract"
        );
    }
}

#[test]
fn format_generated_full_regen_routes_through_convergence_loop() {
    let dir = tempfile::tempdir().expect("tempdir");
    let base = dir.path();
    write_unsorted_workspace(base, "-py");

    let config = make_config("sample-model");
    let files: Vec<(Language, Vec<GeneratedFile>)> = vec![(Language::Ffi, vec![])];

    // A full regen (`only_languages = None`) must go through
    // `converge_full_regen_formatting`, not the single-pass + per-language-residual
    // branch, so it also sorts the "-py" crate that has no residual entry.
    format_generated(&files, &config, base, None);

    if is_tool_available("cargo-sort") {
        let py_toml = std::fs::read_to_string(base.join("crates/pkg-py/Cargo.toml")).unwrap();
        let anyhow_pos = py_toml.find("anyhow").expect("anyhow present");
        let serde_pos = py_toml.find("serde").expect("serde present");
        assert!(
            anyhow_pos < serde_pos,
            "format_generated(..., None) must workspace-sort crates with no per-language residual"
        );
    }
}