cargo-rake 0.4.1

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
//! 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 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.
const SAMPLE: &str = r#"
[[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"]
"#;

/// 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)
}

#[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("say: echo Hello from rake!"))
        .stdout(predicate::str::contains("depends_on: hello"))
        .stdout(predicate::str::contains(
            "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(
            "     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 dir = rakefile_dir(
        "[[target.clean.command]]\nname = \"wipe\"\ncmd = [\"echo\", \"CLEANING\"]\n\
         [[target.build.command]]\nname = \"compile\"\ncmd = [\"echo\", \"BUILDING\"]\n\
         [target.all]\ndepends_on = [\"clean\", \"build\"]\n",
    )?;
    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.
    let dir = rakefile_dir(
        "[[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 = "[[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.
const AGGREGATOR: &str = r#"
[[target.one.command]]
name = "say"
cmd = ["echo", "ran one"]

[[target.two.command]]
name = "say"
cmd = ["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`) and whose
/// `install` is a portable no-op (`true`), so the run installs then proceeds.
const NEEDS_TOOL: &str = r#"
[tool.cargo.widget]
check = ["false"]
install = ["true"]

[target.build]
tools = ["widget"]
[[target.build.command]]
name = "say"
cmd = ["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`) with no `install`, so the run
/// aborts before the command with the requirement message and the `hint`.
const NEEDS_OS_TOOL: &str = r#"
[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"]
"#;

#[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(())
}