cargo-rake 0.6.0

A configuration-driven build tool that runs named targets declared in a Rakefile.toml
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
//! End-to-end CLI tests for the `cargo-rake` binary (the `cargo rake`
//! subcommand).
//!
//! Cargo invokes the subcommand as `cargo rake ...`, so argv arrives as
//! `[cargo-rake, rake, ...]`; the binary drops that leading `rake`. These tests
//! exercise the same behavior as the standalone `rake` binary, invoked with the
//! leading `rake` arg, plus the arg-stripping that is unique to this binary.
//! Per project convention they use `Result<(), Box<dyn Error>>` and `?` rather
//! than `unwrap`/`expect`.

use std::error::Error;
use std::fs;
use std::path::Path;

use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::TempDir;

type TestResult = Result<(), Box<dyn Error>>;

/// A Rakefile exercising plain targets, a `depends_on` chain, a failing target,
/// and a `skip_on_error` dependency feeding a dependent.
#[cfg(not(windows))]
const SAMPLE: &str = r#"
update = false

[[target.hello.command]]
name = "say"
cmd = ["echo", "Hello from rake!"]

[target.default]
depends_on = ["hello"]
[[target.default.command]]
name = "say"
cmd = ["echo", "Running default target"]

[[target.boom.command]]
name = "fail"
cmd = ["sh", "-c", "exit 3"]

[[target.skip.command]]
name = "flaky"
cmd = ["sh", "-c", "exit 1"]
skip_on_error = true

[target.after_skip]
depends_on = ["skip"]
[[target.after_skip.command]]
name = "say"
cmd = ["echo", "ran after skip"]
"#;

#[cfg(windows)]
const SAMPLE: &str = r#"
update = false

[[target.hello.command]]
name = "say"
cmd = ["cmd", "/c", "echo", "Hello from rake!"]

[target.default]
depends_on = ["hello"]
[[target.default.command]]
name = "say"
cmd = ["cmd", "/c", "echo", "Running default target"]

[[target.boom.command]]
name = "fail"
cmd = ["cmd", "/c", "exit", "3"]

[[target.skip.command]]
name = "flaky"
cmd = ["cmd", "/c", "exit", "1"]
skip_on_error = true

[target.after_skip]
depends_on = ["skip"]
[[target.after_skip.command]]
name = "say"
cmd = ["cmd", "/c", "echo", "ran after skip"]
"#;

/// Write `contents` to a `Rakefile.toml` in a fresh temp dir, returning the dir
/// (kept alive so it isn't deleted) for the caller to derive the path from.
fn rakefile_dir(contents: &str) -> Result<TempDir, Box<dyn Error>> {
    let dir = TempDir::new()?;
    fs::write(dir.path().join("Rakefile.toml"), contents)?;
    Ok(dir)
}

/// `cargo-rake rake -f <SAMPLE>`, simulating a `cargo rake ...` invocation,
/// ready for further args.
fn cargo_rake(dir: &TempDir) -> Result<Command, Box<dyn Error>> {
    let mut cmd = Command::cargo_bin("cargo-rake")?;
    let _ = cmd
        .arg("rake")
        .arg("-f")
        .arg(dir.path().join("Rakefile.toml"));
    Ok(cmd)
}

/// Write `.rake/config.toml` with `contents` under `dir`.
fn local_config_dir(dir: &TempDir, contents: &str) -> Result<(), Box<dyn Error>> {
    let rake_dir = dir.path().join(".rake");
    fs::create_dir_all(&rake_dir)?;
    fs::write(rake_dir.join("config.toml"), contents)?;
    Ok(())
}

/// `cargo-rake rake -f <file_arg>` with the process cwd pinned to `dir`, so a
/// `./.rake/config.toml` written under `dir` resolves as the local override
/// regardless of where `file_arg` points.
fn cargo_rake_with_cwd(
    dir: &TempDir,
    file_arg: impl AsRef<Path>,
) -> Result<Command, Box<dyn Error>> {
    let mut cmd = Command::cargo_bin("cargo-rake")?;
    let _ = cmd
        .current_dir(dir.path())
        .arg("rake")
        .arg("-f")
        .arg(file_arg.as_ref());
    Ok(cmd)
}

/// A single-target Rakefile whose command output is later overridden by a
/// `.rake/config.toml` local override in the tests below.
#[cfg(not(windows))]
const OVERRIDABLE: &str = r#"
update = false

[[target.greet.command]]
name = "say"
cmd = ["echo", "base value"]
"#;

#[cfg(windows)]
const OVERRIDABLE: &str = r#"
update = false

[[target.greet.command]]
name = "say"
cmd = ["cmd", "/c", "echo", "base value"]
"#;

/// Replaces `greet`'s whole `command` array (JSON-Merge-Patch semantics
/// replace arrays wholesale, so the override must restate it in full).
#[cfg(not(windows))]
const OVERRIDE_TOML: &str = r#"
[[target.greet.command]]
name = "say"
cmd = ["echo", "override value"]
"#;

#[cfg(windows)]
const OVERRIDE_TOML: &str = r#"
[[target.greet.command]]
name = "say"
cmd = ["cmd", "/c", "echo", "override value"]
"#;

#[test]
fn list_prints_targets() -> TestResult {
    let dir = rakefile_dir(SAMPLE)?;
    cargo_rake(&dir)?
        .arg("list")
        .assert()
        .success()
        .stdout(predicate::str::contains("hello"))
        .stdout(predicate::str::contains(if cfg!(windows) {
            "say: cmd /c echo Hello from rake!"
        } else {
            "say: echo Hello from rake!"
        }))
        .stdout(predicate::str::contains("depends_on: hello"))
        .stdout(predicate::str::contains(if cfg!(windows) {
            "flaky: cmd /c exit 1 (skip_on_error)"
        } else {
            "flaky: sh -c exit 1 (skip_on_error)"
        }));
    Ok(())
}

#[test]
fn syntax_confirms_valid_rakefile() -> TestResult {
    let dir = rakefile_dir(SAMPLE)?;
    cargo_rake(&dir)?
        .arg("syntax")
        .assert()
        .success()
        .stdout(predicate::str::contains("syntax OK"));
    Ok(())
}

#[test]
fn syntax_reports_invalid_rakefile() -> TestResult {
    // A command with an empty `cmd` is a validation error, surfaced by the
    // load that `syntax` performs.
    let dir = rakefile_dir(
        r#"
[[target.broken.command]]
name = "x"
cmd = []
"#,
    )?;
    cargo_rake(&dir)?
        .arg("syntax")
        .assert()
        .failure()
        .stderr(predicate::str::contains("empty 'cmd'"));
    Ok(())
}

#[test]
fn version_flag_prints_semver() -> TestResult {
    Command::cargo_bin("cargo-rake")?
        .args(["rake", "-V"])
        .assert()
        .success()
        .stdout(predicate::str::contains(env!("CARGO_PKG_VERSION")))
        .stdout(predicate::str::contains("rake"));
    Ok(())
}

#[test]
fn help_shows_cargo_rake_bin_name() -> TestResult {
    Command::cargo_bin("cargo-rake")?
        .args(["rake", "--help"])
        .assert()
        .success()
        // clap's configured `bin_name = "cargo rake"` shows in the usage line.
        .stdout(predicate::str::contains("cargo rake"));
    Ok(())
}

#[test]
fn runs_named_target() -> TestResult {
    let dir = rakefile_dir(SAMPLE)?;
    cargo_rake(&dir)?
        .arg("hello")
        .assert()
        .success()
        .stdout(predicate::str::contains("Hello from rake!"))
        // The per-command status and runtime lines are printed to stderr; under
        // assert_cmd stderr is not a TTY, so they appear uncolored. Commands use
        // a fixed "Running" prefix (5 leading spaces in the 12-char column)
        // followed by the "[ rake ]" tag and `[ name ] program args`.
        .stderr(predicate::str::contains(if cfg!(windows) {
            "     Running [ rake ] [ say ] cmd /c echo Hello from rake!"
        } else {
            "     Running [ rake ] [ say ] echo Hello from rake!"
        }))
        // Labels share that column: per-command "Cmd Runtime" gets 1 leading
        // space, the final "Runtime" gets 5.
        .stderr(predicate::str::contains(" Cmd Runtime "))
        .stderr(predicate::str::contains("     Runtime "));
    Ok(())
}

#[test]
fn runs_multiple_named_targets() -> TestResult {
    let dir = rakefile_dir(SAMPLE)?;
    // Two roots given together: `hello` and `after_skip` (which runs `skip`
    // first). Both their command outputs appear in one run.
    cargo_rake(&dir)?
        .arg("hello")
        .arg("after_skip")
        .assert()
        .success()
        .stdout(predicate::str::contains("Hello from rake!"))
        .stdout(predicate::str::contains("ran after skip"));
    Ok(())
}

#[test]
fn skips_target_with_caret_prefix() -> TestResult {
    // `all` depends on `clean` and `build`; nothing else needs `clean`, so
    // `^clean` prunes it. `clean`'s output is absent and a `Skipped` line shows.
    let (clean_cmd, build_cmd) = if cfg!(windows) {
        (
            r#"["cmd", "/c", "echo", "CLEANING"]"#,
            r#"["cmd", "/c", "echo", "BUILDING"]"#,
        )
    } else {
        (r#"["echo", "CLEANING"]"#, r#"["echo", "BUILDING"]"#)
    };
    let toml = format!(
        "update = false\n\
         [[target.clean.command]]\nname = \"wipe\"\ncmd = {clean_cmd}\n\
         [[target.build.command]]\nname = \"compile\"\ncmd = {build_cmd}\n\
         [target.all]\ndepends_on = [\"clean\", \"build\"]\n"
    );
    let dir = rakefile_dir(&toml)?;
    cargo_rake(&dir)?
        .arg("all")
        .arg("^clean")
        .assert()
        .success()
        .stdout(predicate::str::contains("BUILDING"))
        .stdout(predicate::str::contains("CLEANING").not())
        .stderr(predicate::str::contains(
            "Skipped [ rake ] [   clean ] skip requested",
        ));
    Ok(())
}

#[test]
fn skip_required_by_other_target_fails_fast() -> TestResult {
    // `build` (not a root) depends on `clean`, so `^clean` is rejected before
    // anything runs (no commands execute, so the echo programs never need to
    // exist on the host).
    let dir = rakefile_dir(
        "update = false\n\
         [[target.clean.command]]\nname = \"wipe\"\ncmd = [\"echo\", \"CLEANING\"]\n\
         [target.build]\ndepends_on = [\"clean\"]\n\
         [[target.build.command]]\nname = \"compile\"\ncmd = [\"echo\", \"BUILDING\"]\n\
         [target.all]\ndepends_on = [\"build\"]\n",
    )?;
    cargo_rake(&dir)?
        .arg("all")
        .arg("^clean")
        .assert()
        .failure()
        .stderr(predicate::str::contains(
            "target 'clean' cannot be skipped: required by build",
        ));
    Ok(())
}

#[test]
fn runs_default_target_when_none_given() -> TestResult {
    let dir = rakefile_dir(SAMPLE)?;
    cargo_rake(&dir)?
        .assert()
        .success()
        // `default` depends on `hello`, so both run, deps first.
        .stdout(predicate::str::contains("Hello from rake!"))
        .stdout(predicate::str::contains("Running default target"));
    Ok(())
}

#[test]
fn missing_rakefile_errors() -> TestResult {
    Command::cargo_bin("cargo-rake")?
        .args(["rake", "-f", "does-not-exist.toml"])
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains("unable to read Rakefile"));
    Ok(())
}

#[test]
fn invalid_toml_errors() -> TestResult {
    let dir = rakefile_dir("oops")?;
    cargo_rake(&dir)?
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains("unable to parse Rakefile"));
    Ok(())
}

#[test]
fn unknown_target_errors() -> TestResult {
    let dir = rakefile_dir(SAMPLE)?;
    cargo_rake(&dir)?
        .arg("nope")
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains("unknown target 'nope'"));
    Ok(())
}

#[test]
fn failing_target_propagates_exit_code() -> TestResult {
    let dir = rakefile_dir(SAMPLE)?;
    cargo_rake(&dir)?.arg("boom").assert().code(3);
    Ok(())
}

/// A target whose command names a program that cannot be launched aborts the run
/// with a spawn error. Even on that error path the run still prints the failed
/// command's `Cmd Runtime` and the total `Runtime` before the error message.
#[test]
fn spawn_failure_still_prints_runtimes() -> TestResult {
    let toml = "update = false\n\
                [[target.ghost.command]]\nname = \"missing\"\n\
                cmd = [\"this-program-does-not-exist-cargo-rake\"]\n";
    let dir = rakefile_dir(toml)?;
    cargo_rake(&dir)?
        .arg("ghost")
        .assert()
        .failure()
        // The command was attempted, so its per-command runtime prints...
        .stderr(predicate::str::contains(" Cmd Runtime "))
        // ...and the total runtime prints even though the run aborts...
        .stderr(predicate::str::contains("     Runtime "))
        // ...with the spawn error surfaced afterwards.
        .stderr(predicate::str::contains("could not launch"));
    Ok(())
}

#[test]
fn skip_on_error_continues_chain() -> TestResult {
    let dir = rakefile_dir(SAMPLE)?;
    cargo_rake(&dir)?
        .arg("after_skip")
        .assert()
        // `skip` fails but is tolerated, so the chain reaches `after_skip`.
        .success()
        .stdout(predicate::str::contains("ran after skip"));
    Ok(())
}

/// A target defined purely by `depends_on` (no commands of its own) is valid: it
/// runs its dependencies in order and exits 0.
#[cfg(not(windows))]
const AGGREGATOR: &str = r#"
update = false

[[target.one.command]]
name = "say"
cmd = ["echo", "ran one"]

[[target.two.command]]
name = "say"
cmd = ["echo", "ran two"]

[target.all]
depends_on = ["one", "two"]
"#;

#[cfg(windows)]
const AGGREGATOR: &str = r#"
update = false

[[target.one.command]]
name = "say"
cmd = ["cmd", "/c", "echo", "ran one"]

[[target.two.command]]
name = "say"
cmd = ["cmd", "/c", "echo", "ran two"]

[target.all]
depends_on = ["one", "two"]
"#;

#[test]
fn depends_only_target_runs_dependencies() -> TestResult {
    let dir = rakefile_dir(AGGREGATOR)?;
    cargo_rake(&dir)?
        .arg("all")
        .assert()
        .success()
        .stdout(predicate::str::contains("ran one"))
        .stdout(predicate::str::contains("ran two"));
    Ok(())
}

/// A target whose tool is reported absent (`check` is `false`/`cmd /c exit 1`)
/// and whose `install` is a portable no-op (`true`/`cmd /c exit 0`), so the
/// run installs then proceeds.
#[cfg(not(windows))]
const NEEDS_TOOL: &str = r#"
update = false

[tool.cargo.widget]
check = ["false"]
install = ["true"]

[target.build]
tools = ["widget"]
[[target.build.command]]
name = "say"
cmd = ["echo", "built with widget"]
"#;

#[cfg(windows)]
const NEEDS_TOOL: &str = r#"
update = false

[tool.cargo.widget]
check = ["cmd", "/c", "exit", "1"]
install = ["cmd", "/c", "exit", "0"]

[target.build]
tools = ["widget"]
[[target.build.command]]
name = "say"
cmd = ["cmd", "/c", "echo", "built with widget"]
"#;

#[test]
fn missing_tool_is_installed_before_target() -> TestResult {
    let dir = rakefile_dir(NEEDS_TOOL)?;
    cargo_rake(&dir)?
        .arg("build")
        .assert()
        .success()
        .stdout(predicate::str::contains("built with widget"))
        // The install notice is printed to stderr: the right-justified
        // "Installing" prefix followed by the "[ rake ]" tag, the "[ check ]"
        // name tag, and the tool name.
        .stderr(predicate::str::contains(
            "Installing [ rake ] [ check ] widget",
        ));
    Ok(())
}

/// An os tool reported absent (`check` is `false`/`cmd /c exit 1`) with no
/// `install`, so the run aborts before the command with the requirement message
/// and the `hint`.
#[cfg(not(windows))]
const NEEDS_OS_TOOL: &str = r#"
update = false

[tool.os.widget]
check = ["false"]
hint = "install widget from your package manager"

[target.build]
tools = ["widget"]
[[target.build.command]]
name = "say"
cmd = ["echo", "should not run"]
"#;

#[cfg(windows)]
const NEEDS_OS_TOOL: &str = r#"
update = false

[tool.os.widget]
check = ["cmd", "/c", "exit", "1"]
hint = "install widget from your package manager"

[target.build]
tools = ["widget"]
[[target.build.command]]
name = "say"
cmd = ["cmd", "/c", "echo", "should not run"]
"#;

#[test]
fn missing_required_os_tool_aborts() -> TestResult {
    let dir = rakefile_dir(NEEDS_OS_TOOL)?;
    cargo_rake(&dir)?
        .arg("build")
        .assert()
        .failure()
        .code(1)
        .stdout(predicate::str::contains("should not run").not())
        .stderr(predicate::str::contains(
            "the 'widget' tool is required but not installed",
        ))
        .stderr(predicate::str::contains(
            "install widget from your package manager",
        ));
    Ok(())
}

#[test]
fn strips_leading_rake_arg() -> TestResult {
    // `cargo-rake rake list` — the leading `rake` is dropped so the rest
    // parses like the standalone binary.
    let dir = rakefile_dir(SAMPLE)?;
    Command::cargo_bin("cargo-rake")?
        .arg("rake")
        .arg("list")
        .arg("-f")
        .arg(dir.path().join("Rakefile.toml"))
        .assert()
        .success()
        .stdout(predicate::str::contains("hello"));
    Ok(())
}

#[test]
fn works_without_rake_prefix() -> TestResult {
    // Stripping is conditional on argv[1] == "rake"; without it the args still
    // parse, since the first arg here is `list`, not `rake`.
    let dir = rakefile_dir(SAMPLE)?;
    Command::cargo_bin("cargo-rake")?
        .arg("list")
        .arg("-f")
        .arg(dir.path().join("Rakefile.toml"))
        .assert()
        .success()
        .stdout(predicate::str::contains("hello"));
    Ok(())
}

#[cfg(not(windows))]
#[test]
fn pre_update_env_var_appears_in_summary() -> TestResult {
    // When the RAKE_SELF_UPDATED env var is set (written by the pre-relaunch
    // binary), the freshly started binary reads it via read_self_update_env()
    // and includes it in the end-of-run print_update_summary() output.
    let dir = rakefile_dir(
        "update = false\n\
         [[target.default.command]]\nname = \"ok\"\ncmd = [\"true\"]\n",
    )?;
    cargo_rake(&dir)?
        .env("RAKE_SELF_UPDATED", "0.4.0|0.5.0")
        .assert()
        .success()
        .stderr(predicate::str::contains("Updated"))
        .stderr(predicate::str::contains("cargo-rake"))
        .stderr(predicate::str::contains("0.4.0"));
    Ok(())
}

#[test]
fn basic_subcommand_prints_status() -> TestResult {
    // `cargo rake basic` does not need a Rakefile — it exits before loading one.
    // Prints "locked" when no license is configured, or "enabled for <licensee>"
    // when a valid license is present on this machine.
    Command::cargo_bin("cargo-rake")?
        .args(["rake", "basic"])
        .assert()
        .success()
        .stderr(predicate::str::contains("locked").or(predicate::str::contains("enabled for")));
    Ok(())
}

#[test]
fn license_subcommand_rejects_malformed_key() -> TestResult {
    // A key with no '.' separator is malformed; the run must fail with exit 1.
    Command::cargo_bin("cargo-rake")?
        .args(["rake", "license", "badkey"])
        .assert()
        .failure()
        .code(1)
        .stderr(predicate::str::contains("malformed"));
    Ok(())
}

#[test]
fn license_remove_no_license_or_no_terminal() -> TestResult {
    // When no license file is stored: success + "no license key stored".
    // When a file exists but stdin is not a TTY (always in tests): exit 1 + "not a terminal".
    let output = Command::cargo_bin("cargo-rake")?
        .args(["rake", "license", "remove"])
        .output()?;
    let stderr = String::from_utf8_lossy(&output.stderr);
    let no_file = output.status.success() && stderr.contains("no license key stored");
    let no_tty = !output.status.success() && stderr.contains("not a terminal");
    assert!(
        no_file || no_tty,
        "unexpected remove_license output (status={}, stderr={stderr})",
        output.status
    );
    Ok(())
}

#[test]
fn license_info_prints_status() -> TestResult {
    // `cargo rake license info` does not need a Rakefile — it exits before loading one.
    Command::cargo_bin("cargo-rake")?
        .args(["rake", "license", "info"])
        .assert()
        .success()
        .stderr(
            predicate::str::contains("no license active").or(predicate::str::contains("Features")),
        );
    Ok(())
}

#[test]
fn local_config_overrides_command_in_same_directory() -> TestResult {
    let dir = rakefile_dir(OVERRIDABLE)?;
    local_config_dir(&dir, OVERRIDE_TOML)?;
    cargo_rake_with_cwd(&dir, "Rakefile.toml")?
        .arg("greet")
        .assert()
        .success()
        .stdout(predicate::str::contains("override value"))
        .stdout(predicate::str::contains("base value").not());
    Ok(())
}

/// `.rake/config.toml` resolves relative to the process's current working
/// directory, not relative to wherever `-f` points — so it still applies
/// when the Rakefile lives in a subdirectory of the cwd.
#[test]
fn local_config_resolves_against_cwd_not_rakefile_dir() -> TestResult {
    let dir = TempDir::new()?;
    let project_dir = dir.path().join("project");
    fs::create_dir_all(&project_dir)?;
    fs::write(project_dir.join("Rakefile.toml"), OVERRIDABLE)?;
    let rake_dir = dir.path().join(".rake");
    fs::create_dir_all(&rake_dir)?;
    fs::write(rake_dir.join("config.toml"), OVERRIDE_TOML)?;

    let mut cmd = Command::cargo_bin("cargo-rake")?;
    let _ = cmd
        .current_dir(dir.path())
        .arg("rake")
        .arg("-f")
        .arg("project/Rakefile.toml")
        .arg("greet");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("override value"))
        .stdout(predicate::str::contains("base value").not());
    Ok(())
}

/// Negative control for the previous test: a `.rake/config.toml` sitting
/// next to the Rakefile (not at the cwd) must NOT apply, proving resolution
/// is cwd-relative rather than `-f`-relative.
#[test]
fn local_config_next_to_rakefile_is_ignored_when_cwd_differs() -> TestResult {
    let dir = TempDir::new()?;
    let project_dir = dir.path().join("project");
    fs::create_dir_all(&project_dir)?;
    fs::write(project_dir.join("Rakefile.toml"), OVERRIDABLE)?;
    let rake_dir = project_dir.join(".rake");
    fs::create_dir_all(&rake_dir)?;
    fs::write(rake_dir.join("config.toml"), OVERRIDE_TOML)?;

    let mut cmd = Command::cargo_bin("cargo-rake")?;
    let _ = cmd
        .current_dir(dir.path())
        .arg("rake")
        .arg("-f")
        .arg("project/Rakefile.toml")
        .arg("greet");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("base value"))
        .stdout(predicate::str::contains("override value").not());
    Ok(())
}

#[test]
fn missing_local_config_is_a_silent_noop() -> TestResult {
    let dir = rakefile_dir(OVERRIDABLE)?;
    cargo_rake_with_cwd(&dir, "Rakefile.toml")?
        .arg("greet")
        .assert()
        .success()
        .stdout(predicate::str::contains("base value"));
    Ok(())
}