changepacks-java 0.3.3

Java/Gradle project support for changepacks
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
//! # changepacks-java
//!
//! Java/Gradle project support for changepacks.
//!
//! Implements project discovery and version management for Gradle build files (build.gradle,
//! build.gradle.kts). Handles both Groovy and Kotlin DSL syntax for version declarations.
//! Requires the Gradle wrapper (gradlew) for dynamic version detection.

pub mod finder;
mod gradle_dependency_lexer;
mod gradle_metadata;
pub mod package;
mod properties_version;
#[cfg(test)]
pub(crate) mod test_support;
mod version_lexer;
pub mod version_updater;
pub mod workspace;

pub use finder::GradleProjectFinder;
pub use version_updater::write_gradle_version;

use anyhow::{Context, Result};
use changepacks_core::{Config, UpdateType};
use std::collections::BTreeMap;
use std::ffi::OsString;
use std::future::Future;
use std::path::{Path, PathBuf};

// Per-OS Gradle wrapper commands. Windows uses `gradlew.bat` and backslash;
// every other target uses the POSIX `./gradlew` shell script. These consts
// are shared by `GradlePackage` and `GradleWorkspace` so a single edit
// updates both trait impls without drift.
//
// Gradle's built-in `--dry-run` only previews the task graph, so we run the
// full publish pipeline against an isolated temporary Maven local repository
// via `publishToMavenLocal` instead for dry-runs.
#[cfg(windows)]
pub(crate) const PUBLISH_COMMAND: &str = ".\\gradlew.bat publish";
#[cfg(not(windows))]
pub(crate) const PUBLISH_COMMAND: &str = "./gradlew publish";

#[cfg(windows)]
pub(crate) const DRY_RUN_PUBLISH_COMMAND: &str = ".\\gradlew.bat publishToMavenLocal";
#[cfg(not(windows))]
pub(crate) const DRY_RUN_PUBLISH_COMMAND: &str = "./gradlew publishToMavenLocal";

/// Expand to the three inherent constructors shared verbatim by
/// `GradlePackage` and `GradleWorkspace`.
///
/// Both structs carry exactly the same nine fields and built the same
/// three-step constructor chain (`new` -> `new_with_publish_tasks` ->
/// `new_with_project_path_and_publish_tasks`) from byte-identical bodies;
/// only the order the fields happened to be declared in differed. The
/// final struct literal therefore uses field-init shorthand, which is
/// order-insensitive, so one expansion serves both declaration orders.
///
/// Invoked from inside an `impl GradlePackage` or `impl GradleWorkspace`
/// block. Fully-qualified `::std::option::Option`,
/// `::std::string::String`, `::std::path::PathBuf` and
/// `::std::collections::HashSet` keep the macro hygienic — callers do not
/// need those types in scope at the invocation site.
///
/// Consumer requirement: the struct must have `name`, `version`, `path`,
/// `relative_path`, `project_path`, `is_changed`, `dependencies`,
/// `has_publish_task` and `has_publish_to_maven_local_task` fields.
/// `GradlePackage` and `GradleWorkspace` are the only two intended callers.
macro_rules! impl_gradle_constructors {
    () => {
        #[must_use]
        pub fn new(
            name: ::std::option::Option<::std::string::String>,
            version: ::std::option::Option<::std::string::String>,
            path: ::std::path::PathBuf,
            relative_path: ::std::path::PathBuf,
        ) -> Self {
            Self::new_with_publish_tasks(name, version, path, relative_path, true, true)
        }

        #[must_use]
        pub fn new_with_publish_tasks(
            name: ::std::option::Option<::std::string::String>,
            version: ::std::option::Option<::std::string::String>,
            path: ::std::path::PathBuf,
            relative_path: ::std::path::PathBuf,
            has_publish_task: bool,
            has_publish_to_maven_local_task: bool,
        ) -> Self {
            Self::new_with_project_path_and_publish_tasks(
                name,
                version,
                path,
                relative_path,
                ::std::option::Option::None,
                has_publish_task,
                has_publish_to_maven_local_task,
            )
        }

        #[must_use]
        pub(crate) fn new_with_project_path_and_publish_tasks(
            name: ::std::option::Option<::std::string::String>,
            version: ::std::option::Option<::std::string::String>,
            path: ::std::path::PathBuf,
            relative_path: ::std::path::PathBuf,
            project_path: ::std::option::Option<::std::string::String>,
            has_publish_task: bool,
            has_publish_to_maven_local_task: bool,
        ) -> Self {
            Self {
                name,
                version,
                path,
                relative_path,
                project_path,
                is_changed: false,
                dependencies: ::std::collections::HashSet::new(),
                has_publish_task,
                has_publish_to_maven_local_task,
            }
        }
    };
}

pub(crate) use impl_gradle_constructors;

/// Expand to the two publish-task flag accessors shared verbatim by the
/// `Package` impl for `GradlePackage` and the `Workspace` impl for
/// `GradleWorkspace`.
///
/// Both traits declare `is_publishable_by_default` and
/// `is_dry_run_publishable_by_default` with the same signature, and Java
/// answers both from the task flags the finder probed off the Gradle wrapper
/// rather than from a single `publishable_by_default` field — so
/// [`changepacks_core::impl_publishable_by_default!`], which reads that field,
/// cannot be reused here.
///
/// These are plain sync methods, so unlike `update_version` / `publish` /
/// `dry_run_publish` they are unaffected by `#[async_trait]`'s rewrite of the
/// `impl` block (the E0195 signature mismatch only bites methods that
/// `async_trait` desugars). Fully-qualified `::std::primitive::bool` keeps the
/// macro hygienic.
///
/// Consumer requirement: the struct must own `has_publish_task` and
/// `has_publish_to_maven_local_task` `bool` fields, both supplied by
/// [`declare_gradle_project!`]. Invoked from inside the `#[async_trait]`
/// `impl Package for GradlePackage` / `impl Workspace for GradleWorkspace`
/// blocks, the only two intended callers.
macro_rules! impl_gradle_publish_task_flags {
    () => {
        fn is_publishable_by_default(&self) -> ::std::primitive::bool {
            self.has_publish_task
        }

        fn is_dry_run_publishable_by_default(&self) -> ::std::primitive::bool {
            self.has_publish_to_maven_local_task
        }
    };
}

pub(crate) use impl_gradle_publish_task_flags;

/// Declare a Gradle project struct plus its shared inherent constructors.
///
/// `GradlePackage` and `GradleWorkspace` carry exactly the same nine fields
/// — previously declared in two different orders — and each followed the
/// declaration with an inherent impl containing
/// [`impl_gradle_constructors!`], which already hard-codes those field
/// names. Canonicalizing the layout here keeps the two in lockstep; the
/// expansion's struct literal uses field-init shorthand, so the former
/// order difference was cosmetic. Java cannot use
/// `changepacks_core::declare_discovered_project!`: it carries
/// `project_path` plus the two publish-task flags and has no
/// `publishable_by_default` field. Outer attributes (including doc
/// comments) pass through; other inherent methods and every trait impl stay
/// in separate blocks beside the invocation.
macro_rules! declare_gradle_project {
    ($(#[$meta:meta])* pub struct $name:ident) => {
        $(#[$meta])*
        #[derive(::std::fmt::Debug)]
        pub struct $name {
            name: ::std::option::Option<::std::string::String>,
            version: ::std::option::Option<::std::string::String>,
            path: ::std::path::PathBuf,
            relative_path: ::std::path::PathBuf,
            project_path: ::std::option::Option<::std::string::String>,
            is_changed: ::std::primitive::bool,
            dependencies: ::std::collections::HashSet<::std::string::String>,
            has_publish_task: ::std::primitive::bool,
            has_publish_to_maven_local_task: ::std::primitive::bool,
        }

        impl $name {
            crate::impl_gradle_constructors!();
        }
    };
}

pub(crate) use declare_gradle_project;

/// Read a Gradle build script (`build.gradle` or `build.gradle.kts`) and
/// attach the build-file read context to any I/O failure.
///
/// [`finder::GradleProjectFinder::visit`] and [`write_gradle_version`] both
/// open with this exact read, and both must attribute a failure to the build
/// script's own path instead of surfacing a bare `os error`. Owning the read
/// and its `Failed to read Gradle build file <path>` context here keeps the
/// two messages from drifting apart; every other language crate already
/// funnels its manifest read through a single head.
///
/// # Errors
/// Returns the underlying [`std::io::Error`] as the cause, wrapped with the
/// build file path, when the script cannot be read.
pub(crate) async fn read_gradle_build_file(path: &Path) -> Result<String> {
    tokio::fs::read_to_string(path)
        .await
        .with_context(|| format!("Failed to read Gradle build file {}", path.display()))
}

/// Compute the next semver version and write it into the Gradle build file.
///
/// `GradlePackage::update_version` and `GradleWorkspace::update_version` had
/// byte-identical bodies apart from the [`GradleVersionScope`] they select
/// (`ScriptOnly` for a package, `ScriptAndAllProjects` for a workspace root),
/// so the shared body lives here and each trait method is a single delegating
/// call that supplies its own scope.
///
/// This is a plain free function rather than a `macro_rules!`: `#[async_trait]`
/// rewrites the `impl` block before macro bodies expand, so a macro invocation
/// inside the impl would emit an `async fn` that no longer matches the
/// desugared trait signature (E0195) — see the matching note in `package.rs`.
///
/// [`GradleVersionScope`]: version_updater::GradleVersionScope
///
/// # Errors
/// Returns an error when the next version cannot be computed, or when the
/// Gradle build file (or its sibling `gradle.properties`) cannot be rewritten.
/// A failed write leaves `version` untouched.
pub(crate) async fn bump_gradle_version(
    version: &mut Option<String>,
    path: &Path,
    update_type: UpdateType,
    scope: version_updater::GradleVersionScope,
) -> Result<()> {
    changepacks_utils::bump_version_with(version, path, update_type, async |new| {
        write_gradle_version(path, new, scope).await
    })
    .await
}

fn finish_isolated_gradle_dry_run(
    publish_result: Result<changepacks_core::publish::PublishOutput>,
    cleanup_result: Result<()>,
) -> Result<changepacks_core::publish::PublishOutput> {
    match (publish_result, cleanup_result) {
        (Ok(output), Ok(())) => Ok(output),
        (Err(publish_error), Ok(())) => Err(publish_error),
        (Ok(output), Err(cleanup_error)) => {
            let outcome = if output.success {
                "succeeded"
            } else {
                "reported failure"
            };
            Err(anyhow::anyhow!(
                "Gradle dry run {outcome}; stdout: {}; stderr: {}; failed to remove isolated temporary Maven local repository: {cleanup_error:#}",
                output.stdout,
                output.stderr,
            ))
        }
        (Err(publish_error), Err(cleanup_error)) => Err(anyhow::anyhow!(
            "Gradle dry run failed: {publish_error:#}; failed to remove isolated temporary Maven local repository: {cleanup_error:#}"
        )),
    }
}

async fn run_built_in_gradle_dry_run_with<Run, RunFuture>(
    run_gradle: Run,
) -> Result<changepacks_core::publish::PublishOutput>
where
    Run: FnOnce(PathBuf) -> RunFuture,
    RunFuture: Future<Output = Result<changepacks_core::publish::PublishOutput>>,
{
    let maven_local = tempfile::Builder::new()
        .prefix("changepacks-maven-local-")
        .tempdir()
        .context("Failed to create isolated temporary Maven local repository")?;
    let repository = maven_local.path().to_path_buf();
    let publish_result = run_gradle(repository).await;
    let cleanup_result = maven_local
        .close()
        .context("Failed to remove isolated temporary Maven local repository");

    finish_isolated_gradle_dry_run(publish_result, cleanup_result)
}

pub(crate) async fn run_publish_for_path(
    path: &Path,
    relative_path: &Path,
    project_path: Option<&str>,
    config: &Config,
    missing_dir_message: &'static str,
) -> Result<changepacks_core::publish::PublishOutput> {
    if let Some(command) = resolve_publish_override(&config.publish, relative_path) {
        return changepacks_core::publish::run_publish_flow(
            &command,
            path,
            &[],
            missing_dir_message,
        )
        .await;
    }

    finder::run_gradle_publish(
        path,
        relative_path,
        project_path,
        "publish",
        &[],
        missing_dir_message,
    )
    .await
}

pub(crate) async fn run_dry_run_publish_for_path(
    path: &Path,
    relative_path: &Path,
    project_path: Option<&str>,
    config: &Config,
    missing_dir_message: &'static str,
) -> Result<Option<changepacks_core::publish::PublishOutput>> {
    if let Some(command) = resolve_publish_override(&config.publish_dry_run, relative_path) {
        return changepacks_core::publish::run_dry_run_publish_flow(
            Some(&command),
            path,
            &[],
            missing_dir_message,
        )
        .await;
    }

    run_built_in_gradle_dry_run_with(|maven_local| async move {
        let mut repository_argument = OsString::from("-Dmaven.repo.local=");
        repository_argument.push(maven_local);
        finder::run_gradle_publish(
            path,
            relative_path,
            project_path,
            "publishToMavenLocal",
            &[repository_argument],
            missing_dir_message,
        )
        .await
    })
    .await
    .map(Some)
}

fn resolve_publish_override(
    commands: &BTreeMap<String, String>,
    relative_path: &Path,
) -> Option<String> {
    changepacks_core::publish::lookup_by_path_or_language(
        commands,
        relative_path,
        changepacks_core::Language::Java,
    )
    .cloned()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::BTreeMap;
    use std::fs;
    use std::path::PathBuf;
    use tempfile::TempDir;

    fn create_isolation_asserting_wrapper(root: &Path, exit_code: i32) {
        #[cfg(windows)]
        fs::write(
            root.join("gradlew.bat"),
            format!(
                "@echo off\n\
                 if not \"%~1\"==\":libs:core:publishToMavenLocal\" (\n\
                   echo unexpected task: %~1 1>&2\n\
                   exit /b 41\n\
                 )\n\
                 if not \"%~3\"==\"\" (\n\
                   echo unexpected additional argument: %~3 1>&2\n\
                   exit /b 44\n\
                 )\n\
                 set \"repo_argument=%~2\"\n\
                 if not \"%repo_argument:~0,19%\"==\"-Dmaven.repo.local=\" (\n\
                   echo missing isolated Maven repository argument 1>&2\n\
                   exit /b 42\n\
                 )\n\
                 set \"repo=%repo_argument:~19%\"\n\
                 if not exist \"%repo%\" (\n\
                   echo isolated Maven repository did not exist during execution 1>&2\n\
                   exit /b 43\n\
                 )\n\
                 echo isolated_repo=%repo%\n\
                 exit /b {exit_code}\n"
            ),
        )
        .unwrap();

        #[cfg(not(windows))]
        {
            use std::os::unix::fs::PermissionsExt;

            let wrapper = root.join("gradlew");
            fs::write(
                &wrapper,
                format!(
                    "#!/bin/sh\n\
                     if [ \"$#\" -ne 2 ]; then\n\
                       printf 'expected exactly two arguments, received %s\\n' \"$#\" >&2\n\
                       exit 44\n\
                     fi\n\
                     if [ \"$1\" != ':libs:core:publishToMavenLocal' ]; then\n\
                       printf 'unexpected task: %s\\n' \"$1\" >&2\n\
                       exit 41\n\
                     fi\n\
                     repo_argument=$2\n\
                     case $repo_argument in\n\
                       -Dmaven.repo.local=*) repo=${{repo_argument#-Dmaven.repo.local=}} ;;\n\
                       *) printf 'missing isolated Maven repository argument\\n' >&2; exit 42 ;;\n\
                     esac\n\
                     if [ ! -d \"$repo\" ]; then\n\
                       printf 'isolated Maven repository did not exist during execution\\n' >&2\n\
                       exit 43\n\
                     fi\n\
                     printf 'isolated_repo=%s\\n' \"$repo\"\n\
                     exit {exit_code}\n"
                ),
            )
            .unwrap();
            fs::set_permissions(&wrapper, fs::Permissions::from_mode(0o755)).unwrap();
        }
    }

    fn nested_gradle_manifest(temp_dir: &TempDir) -> (PathBuf, PathBuf) {
        let root = temp_dir.path().join("repo with spaces");
        let project_dir = root.join("libs").join("core");
        fs::create_dir_all(&project_dir).unwrap();
        let manifest = project_dir.join("build.gradle.kts");
        fs::write(&manifest, "version = \"1.0.0\"\n").unwrap();
        (root, manifest)
    }

    fn isolated_repository_from(output: &changepacks_core::publish::PublishOutput) -> PathBuf {
        output
            .stdout
            .lines()
            .find_map(|line| line.strip_prefix("isolated_repo="))
            .map(PathBuf::from)
            .expect("fake Gradle wrapper did not report its isolated Maven repository")
    }

    fn captured_publish_output(success: bool) -> changepacks_core::publish::PublishOutput {
        changepacks_core::publish::PublishOutput {
            success,
            stdout: "captured Gradle stdout".to_string(),
            stderr: "captured Gradle stderr".to_string(),
        }
    }

    fn create_no_args_override(project_dir: &Path) -> String {
        #[cfg(windows)]
        {
            fs::write(
                project_dir.join("override-check.bat"),
                "@echo off\n\
                 if not \"%~1\"==\"\" (\n\
                   echo unexpected argument: %~1 1>&2\n\
                   exit /b 51\n\
                 )\n\
                 echo override-without-injected-args\n",
            )
            .unwrap();
            "call override-check.bat".to_string()
        }

        #[cfg(not(windows))]
        {
            use std::os::unix::fs::PermissionsExt;

            let script = project_dir.join("override-check.sh");
            fs::write(
                &script,
                "#!/bin/sh\n\
                 if [ \"$#\" -ne 0 ]; then\n\
                   printf 'unexpected argument: %s\\n' \"$1\" >&2\n\
                   exit 51\n\
                 fi\n\
                 printf 'override-without-injected-args\\n'\n",
            )
            .unwrap();
            fs::set_permissions(&script, fs::Permissions::from_mode(0o755)).unwrap();
            "./override-check.sh".to_string()
        }
    }

    #[test]
    fn test_resolve_publish_override_prefers_path_then_language() {
        let relative_path = Path::new("libs/core/build.gradle.kts");
        let mut commands = BTreeMap::new();
        commands.insert("java".to_string(), "language-command".to_string());
        commands.insert(
            relative_path.to_string_lossy().into_owned(),
            "path-command".to_string(),
        );

        assert_eq!(
            resolve_publish_override(&commands, relative_path).as_deref(),
            Some("path-command")
        );
        commands.remove(relative_path.to_string_lossy().as_ref());
        assert_eq!(
            resolve_publish_override(&commands, relative_path).as_deref(),
            Some("language-command")
        );
        commands.clear();
        assert_eq!(resolve_publish_override(&commands, relative_path), None);
    }

    #[test]
    fn finish_isolated_dry_run_returns_output_when_cleanup_succeeds() {
        let result =
            finish_isolated_gradle_dry_run(Ok(captured_publish_output(true)), Ok(())).unwrap();

        assert!(result.success);
        assert_eq!(result.stdout, "captured Gradle stdout");
        assert_eq!(result.stderr, "captured Gradle stderr");
    }

    #[test]
    fn finish_isolated_dry_run_returns_execution_error_when_cleanup_succeeds() {
        let error = finish_isolated_gradle_dry_run(
            Err(anyhow::anyhow!("wrapper execution failed")),
            Ok(()),
        )
        .unwrap_err();

        assert_eq!(error.to_string(), "wrapper execution failed");
    }

    #[test]
    fn finish_isolated_dry_run_reports_cleanup_error_after_successful_output() {
        let error = finish_isolated_gradle_dry_run(
            Ok(captured_publish_output(true)),
            Err(anyhow::anyhow!("injected cleanup failure")),
        )
        .unwrap_err()
        .to_string();

        assert!(error.contains("Gradle dry run succeeded"), "{error}");
        assert!(error.contains("captured Gradle stdout"), "{error}");
        assert!(error.contains("captured Gradle stderr"), "{error}");
        assert!(error.contains("injected cleanup failure"), "{error}");
    }

    #[test]
    fn finish_isolated_dry_run_retains_nonzero_output_when_cleanup_fails() {
        let error = finish_isolated_gradle_dry_run(
            Ok(captured_publish_output(false)),
            Err(anyhow::anyhow!("injected cleanup failure")),
        )
        .unwrap_err()
        .to_string();

        assert!(error.contains("Gradle dry run reported failure"), "{error}");
        assert!(error.contains("captured Gradle stdout"), "{error}");
        assert!(error.contains("captured Gradle stderr"), "{error}");
        assert!(error.contains("injected cleanup failure"), "{error}");
    }

    #[test]
    fn finish_isolated_dry_run_reports_execution_and_cleanup_errors() {
        let error = finish_isolated_gradle_dry_run(
            Err(anyhow::anyhow!("wrapper execution failed")),
            Err(anyhow::anyhow!("injected cleanup failure")),
        )
        .unwrap_err()
        .to_string();

        assert!(error.contains("wrapper execution failed"), "{error}");
        assert!(error.contains("injected cleanup failure"), "{error}");
    }

    #[tokio::test]
    async fn built_in_dry_run_cleans_isolated_repository_when_wrapper_is_missing() {
        let temp_dir = TempDir::new().unwrap();
        let manifest = temp_dir.path().join("build.gradle.kts");
        fs::write(&manifest, "version = \"1.0.0\"\n").unwrap();
        let mut observed_repository = None;

        let result = run_built_in_gradle_dry_run_with(|maven_local| {
            observed_repository = Some(maven_local.clone());
            async move {
                let mut repository_argument = OsString::from("-Dmaven.repo.local=");
                repository_argument.push(maven_local);
                finder::run_gradle_publish(
                    &manifest,
                    Path::new("build.gradle.kts"),
                    Some(":"),
                    "publishToMavenLocal",
                    &[repository_argument],
                    "Package directory not found",
                )
                .await
            }
        })
        .await;

        let error = result.unwrap_err().to_string();
        assert!(
            error.contains("Gradle wrapper (gradlew) not found"),
            "{error}"
        );
        let observed_repository = observed_repository.unwrap();
        assert!(
            !observed_repository.exists(),
            "temporary Maven repository survived wrapper lookup failure: {}",
            observed_repository.display()
        );
        temp_dir.close().unwrap();
    }

    #[tokio::test]
    async fn built_in_dry_run_uses_exact_subproject_task_and_removes_isolated_repository() {
        let temp_dir = TempDir::new().unwrap();
        let (root, manifest) = nested_gradle_manifest(&temp_dir);
        create_isolation_asserting_wrapper(&root, 0);
        let relative_path = Path::new("libs/core/build.gradle.kts");

        let output = run_dry_run_publish_for_path(
            &manifest,
            relative_path,
            Some(":libs:core"),
            &Config::default(),
            "Package directory not found",
        )
        .await
        .unwrap()
        .unwrap();

        assert!(output.success, "stderr: {}", output.stderr);
        let isolated_repository = isolated_repository_from(&output);
        assert!(
            isolated_repository.file_name().is_some_and(|name| name
                .to_string_lossy()
                .starts_with("changepacks-maven-local-")),
            "Gradle did not receive a changepacks-owned temporary repository: {}",
            isolated_repository.display()
        );
        assert!(
            !isolated_repository.exists(),
            "temporary Maven repository was not cleaned up: {}",
            isolated_repository.display()
        );
        temp_dir.close().unwrap();
    }

    #[tokio::test]
    async fn built_in_dry_run_removes_isolated_repository_after_gradle_failure() {
        let temp_dir = TempDir::new().unwrap();
        let (root, manifest) = nested_gradle_manifest(&temp_dir);
        create_isolation_asserting_wrapper(&root, 29);
        let relative_path = Path::new("libs/core/build.gradle.kts");

        let output = run_dry_run_publish_for_path(
            &manifest,
            relative_path,
            Some(":libs:core"),
            &Config::default(),
            "Package directory not found",
        )
        .await
        .unwrap()
        .unwrap();

        assert!(!output.success);
        let isolated_repository = isolated_repository_from(&output);
        assert!(
            !isolated_repository.exists(),
            "temporary Maven repository was not cleaned up after failure: {}",
            isolated_repository.display()
        );
        temp_dir.close().unwrap();
    }

    #[tokio::test]
    async fn configured_dry_run_override_receives_no_injected_argument() {
        let temp_dir = TempDir::new().unwrap();
        let (_root, manifest) = nested_gradle_manifest(&temp_dir);
        let project_dir = manifest.parent().unwrap();
        let mut publish_dry_run = BTreeMap::new();
        publish_dry_run.insert("java".to_string(), create_no_args_override(project_dir));
        let config = Config {
            publish_dry_run,
            ..Default::default()
        };

        let output = run_dry_run_publish_for_path(
            &manifest,
            Path::new("libs/core/build.gradle.kts"),
            Some(":ignored:for:override"),
            &config,
            "Package directory not found",
        )
        .await
        .unwrap()
        .unwrap();

        assert!(output.success, "stderr: {}", output.stderr);
        assert_eq!(output.stdout.trim(), "override-without-injected-args");
        temp_dir.close().unwrap();
    }
}