mktool 1.5.9

General purpose utility to enhance pkgsrc/mk infrastructure
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
/*
 * Copyright (c) 2024 Jonathan Perkin <jonathan@perkin.org.uk>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

use std::env;
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::process::{Command, Stdio};

const MKTOOL: &str = env!("CARGO_BIN_EXE_mktool");

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

/*
 * Invalid arguments.
 *
 * XXX: This is not fully compatible, checksum.awk exits 3, we exit 2, but
 * unfortunatly this is hardcoded in clap.
 */
#[test]
fn test_checksum_invalid_args() -> Result<()> {
    let cmd = Command::new(MKTOOL).arg("checksum").output()?;
    assert_eq!(cmd.status.code(), Some(2));
    Ok(())
}

/*
 * Test running with no input files.
 */
#[test]
fn test_checksum_no_input() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let cmd = Command::new(MKTOOL).arg("checksum").arg(distinfo).output()?;
    assert_eq!(cmd.status.code(), Some(0));
    assert_eq!(cmd.stdout, "".as_bytes());
    assert_eq!(cmd.stderr, "".as_bytes());
    Ok(())
}

/*
 * Pass a nonexistent distinfo file.  The error message should include both
 * the filename and the reason for the failure.
 */
#[test]
fn test_checksum_bad_distinfo() -> Result<()> {
    let distinfo = PathBuf::from("/nonexistent");
    let cmd = Command::new(MKTOOL).arg("checksum").arg(&distinfo).output()?;
    let stderr = String::from_utf8_lossy(&cmd.stderr);
    assert_eq!(cmd.status.code(), Some(3));
    assert_eq!(cmd.stdout, "".as_bytes());
    assert!(
        stderr.contains("/nonexistent"),
        "expected filename in error: {stderr}"
    );
    assert!(
        stderr.contains("No such file") || stderr.contains("not found"),
        "expected error reason: {stderr}"
    );
    Ok(())
}

/*
 * Passing a file that doesn't exist in distinfo should print a message
 * to stderr and exit 2.
 */
#[test]
fn test_checksum_bad_distfile() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg(&distinfo)
        .arg("foo")
        .output()?;
    assert_eq!(cmd.status.code(), Some(2));
    assert_eq!(cmd.stdout, "".as_bytes());
    assert_eq!(
        cmd.stderr,
        "checksum: No checksum recorded for foo\n".as_bytes()
    );
    Ok(())
}

/*
 * Tests against a valid full distfile test.
 */
#[test]
fn test_checksum_valid_distfile() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let output = format!(
        "{}\n{}\n",
        "=> Checksum BLAKE2s OK for digest1.txt",
        "=> Checksum SHA512 OK for digest1.txt"
    );
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg(&distinfo)
        .arg("digest1.txt")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(0));
    assert_eq!(cmd.stdout, output.as_bytes());
    assert_eq!(cmd.stderr, "".as_bytes());

    /*
     * Identical to previous except the path to the distfile is
     * different (but still valid).
     */
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg(&distinfo)
        .arg("../data/digest1.txt")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(0));
    assert_eq!(cmd.stdout, output.as_bytes());
    assert_eq!(cmd.stderr, "".as_bytes());
    /*
     * Valid, but we only request a single algorithm each time.
     */
    let output = "=> Checksum BLAKE2s OK for digest1.txt\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-a")
        .arg("BLAKE2s")
        .arg(&distinfo)
        .arg("digest1.txt")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(0));
    assert_eq!(cmd.stdout, output.as_bytes());
    assert_eq!(cmd.stderr, "".as_bytes());

    let output = "=> Checksum SHA512 OK for digest2.txt\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-a")
        .arg("SHA512")
        .arg(&distinfo)
        .arg("digest2.txt")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(0));
    assert_eq!(cmd.stdout, output.as_bytes());
    assert_eq!(cmd.stderr, "".as_bytes());

    /*
     * Output should be in distinfo order, with errors last, regardless of
     * order of command line arguments.
     */
    let output = format!(
        "{}\n{}\n{}\n{}\n",
        "=> Checksum BLAKE2s OK for digest1.txt",
        "=> Checksum SHA512 OK for digest1.txt",
        "=> Checksum BLAKE2s OK for digest2.txt",
        "=> Checksum SHA512 OK for digest2.txt"
    );
    let outerr = "checksum: No checksum recorded for digest11.txt\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg(&distinfo)
        .arg("digest2.txt")
        .arg("digest11.txt")
        .arg("digest1.txt")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(2));
    assert_eq!(cmd.stdout, output.as_bytes());
    assert_eq!(cmd.stderr, outerr.as_bytes());
    Ok(())
}

/*
 * Test input from file / stdin.
 */
#[test]
fn test_checksum_input_file() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let tmpdir = PathBuf::from(env!("CARGO_TARGET_TMPDIR"));
    let tmpfile = tmpdir.join("test_checksum_input_file.txt");
    fs::write(&tmpfile, "digest2.txt\n")?;
    let output = format!(
        "{}\n{}\n{}\n{}\n",
        "=> Checksum BLAKE2s OK for digest1.txt",
        "=> Checksum SHA512 OK for digest1.txt",
        "=> Checksum BLAKE2s OK for digest2.txt",
        "=> Checksum SHA512 OK for digest2.txt"
    );
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg(&distinfo)
        .arg("-I")
        .arg(&tmpfile)
        .arg("digest1.txt")
        .current_dir("tests/data")
        .output()?;
    fs::remove_file(&tmpfile)?;
    assert_eq!(cmd.status.code(), Some(0));
    assert_eq!(cmd.stdout, output.as_bytes());
    assert_eq!(cmd.stderr, "".as_bytes());
    Ok(())
}

#[test]
fn test_checksum_input_stdin() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let output = format!(
        "{}\n{}\n{}\n{}\n",
        "=> Checksum BLAKE2s OK for digest1.txt",
        "=> Checksum SHA512 OK for digest1.txt",
        "=> Checksum BLAKE2s OK for digest2.txt",
        "=> Checksum SHA512 OK for digest2.txt"
    );
    let mut cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg(&distinfo)
        .arg("-I")
        .arg("-")
        .arg("digest2.txt")
        .current_dir("tests/data")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()?;
    let mut stdin = cmd.stdin.take().ok_or("failed to open stdin")?;
    std::thread::spawn(move || {
        let _ = stdin.write_all("digest1.txt".as_bytes());
    });
    let out = cmd.wait_with_output()?;
    assert_eq!(out.status.code(), Some(0));
    assert_eq!(out.stdout, output.as_bytes());
    assert_eq!(out.stderr, "".as_bytes());
    Ok(())
}

/*
 * Test strip suffix mode.  Use filename digest1.txt.suffix but validate
 * against the digest1.txt distinfo entry.
 */
#[test]
fn test_checksum_strip_mode() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");

    let output = "=> Checksum SHA512 OK for digest1.txt\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-a")
        .arg("SHA512")
        .arg("-s")
        .arg(".suffix")
        .arg(&distinfo)
        .arg("digest1.txt.suffix")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(0));
    assert_eq!(cmd.stdout, output.as_bytes());
    assert_eq!(cmd.stderr, "".as_bytes());

    /* Invalid suffix strip.  */
    let output = "checksum: No checksum recorded for digest1.txt.suffix\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-s")
        .arg(".badsuffix")
        .arg(&distinfo)
        .arg("digest1.txt.suffix")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(2));
    assert_eq!(cmd.stdout, "".as_bytes());
    assert_eq!(cmd.stderr, output.as_bytes());
    Ok(())
}

/*
 * Verify that with -s, the file actually opened for hashing is the
 * original (suffix-included) path, not the suffix-stripped lookup name.
 * The setup writes the suffix-included file into a directory that does
 * NOT contain the suffix-stripped filename, so opening by the stripped
 * name would fail with ENOENT.  Also verify that MissingChecksum reports
 * the stripped name (matching checksum.awk), not the supplied path.
 */
#[test]
fn test_checksum_strip_opens_original() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let mut src = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    src.push("tests/data/digest1.txt");
    let tmpdir = PathBuf::from(env!("CARGO_TARGET_TMPDIR"))
        .join("test_checksum_strip_opens_original");
    fs::create_dir_all(&tmpdir)?;
    let tmpfile = tmpdir.join("digest1.txt.fetchtmp");
    fs::copy(&src, &tmpfile)?;
    assert!(!tmpdir.join("digest1.txt").exists());

    let output = "=> Checksum SHA512 OK for digest1.txt\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-a")
        .arg("SHA512")
        .arg("-s")
        .arg(".fetchtmp")
        .arg(&distinfo)
        .arg("digest1.txt.fetchtmp")
        .current_dir(&tmpdir)
        .output()?;
    assert_eq!(cmd.status.code(), Some(0));
    assert_eq!(cmd.stdout, output.as_bytes());
    assert_eq!(cmd.stderr, "".as_bytes());

    let output = "checksum: No SHA1 checksum recorded for digest1.txt\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-a")
        .arg("SHA1")
        .arg("-s")
        .arg(".fetchtmp")
        .arg(&distinfo)
        .arg("digest1.txt.fetchtmp")
        .current_dir(&tmpdir)
        .output()?;
    fs::remove_dir_all(&tmpdir)?;
    assert_eq!(cmd.status.code(), Some(2));
    assert_eq!(cmd.stdout, "".as_bytes());
    assert_eq!(cmd.stderr, output.as_bytes());
    Ok(())
}

/*
 * Test strip suffix mode via -I input file.
 */
#[test]
fn test_checksum_strip_input() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let tmpdir = PathBuf::from(env!("CARGO_TARGET_TMPDIR"));
    let tmpfile = tmpdir.join("test_checksum_strip_input.txt");
    fs::write(&tmpfile, "digest1.txt.suffix\n")?;
    let output = "=> Checksum SHA512 OK for digest1.txt\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-a")
        .arg("SHA512")
        .arg("-s")
        .arg(".suffix")
        .arg("-I")
        .arg(&tmpfile)
        .arg(&distinfo)
        .current_dir("tests/data")
        .output()?;
    fs::remove_file(&tmpfile)?;
    assert_eq!(cmd.status.code(), Some(0));
    assert_eq!(cmd.stdout, output.as_bytes());
    assert_eq!(cmd.stderr, "".as_bytes());

    /*
     * Input file with suffix that doesn't match should use the original
     * filename for the distinfo lookup.
     */
    let tmpfile = tmpdir.join("test_checksum_strip_input_nomatch.txt");
    fs::write(&tmpfile, "digest1.txt\n")?;
    let output = "=> Checksum SHA512 OK for digest1.txt\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-a")
        .arg("SHA512")
        .arg("-s")
        .arg(".badsuffix")
        .arg("-I")
        .arg(&tmpfile)
        .arg(&distinfo)
        .current_dir("tests/data")
        .output()?;
    fs::remove_file(&tmpfile)?;
    assert_eq!(cmd.status.code(), Some(0));
    assert_eq!(cmd.stdout, output.as_bytes());
    assert_eq!(cmd.stderr, "".as_bytes());
    Ok(())
}

/*
 * Passing a nonexistent file to -I should print an error and exit 1.
 */
#[test]
fn test_checksum_bad_input_file() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-I")
        .arg("/nonexistent/inputfile")
        .arg(&distinfo)
        .output()?;
    let stderr = String::from_utf8_lossy(&cmd.stderr);
    assert_eq!(cmd.status.code(), Some(1));
    assert!(
        stderr.contains("/nonexistent/inputfile"),
        "expected filename in error: {stderr}"
    );
    assert!(
        stderr.contains("No such file") || stderr.contains("not found"),
        "expected error reason: {stderr}"
    );
    Ok(())
}

/*
 * Missing distfile with a specific algorithm requested should include the
 * algorithm name in the error message.
 */
#[test]
fn test_checksum_bad_distfile_algorithm() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-a")
        .arg("SHA512")
        .arg(&distinfo)
        .arg("foo")
        .output()?;
    assert_eq!(cmd.status.code(), Some(2));
    assert_eq!(cmd.stdout, "".as_bytes());
    assert_eq!(
        cmd.stderr,
        "checksum: No SHA512 checksum recorded for foo\n".as_bytes()
    );
    Ok(())
}

/*
 * Invalid algorithm name.
 */
#[test]
fn test_checksum_bad_algorithm() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-a")
        .arg("BOGUS")
        .arg(&distinfo)
        .arg("digest1.txt")
        .current_dir("tests/data")
        .output()?;
    let stderr = String::from_utf8_lossy(&cmd.stderr);
    assert_ne!(cmd.status.code(), Some(0));
    assert!(
        stderr.contains("BOGUS"),
        "expected algorithm name in error: {stderr}"
    );
    Ok(())
}

/*
 * Verify -j flag is accepted.
 */
#[test]
fn test_checksum_jobs_flag() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let output = "=> Checksum SHA512 OK for digest1.txt\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-a")
        .arg("SHA512")
        .arg("-j")
        .arg("1")
        .arg(&distinfo)
        .arg("digest1.txt")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(0));
    assert_eq!(cmd.stdout, output.as_bytes());
    assert_eq!(cmd.stderr, "".as_bytes());
    Ok(())
}

/*
 * Verify MKTOOL_JOBS environment variable is accepted.
 */
#[test]
fn test_checksum_jobs_env() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let output = "=> Checksum SHA512 OK for digest1.txt\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-a")
        .arg("SHA512")
        .arg(&distinfo)
        .arg("digest1.txt")
        .env("MKTOOL_JOBS", "1")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(0));
    assert_eq!(cmd.stdout, output.as_bytes());
    assert_eq!(cmd.stderr, "".as_bytes());
    Ok(())
}

/*
 * No checksum type recorded.
 */
#[test]
fn test_checksum_no_checksum() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let output = format!(
        "{}\n{}\n",
        "checksum: No SHA1 checksum recorded for digest1.txt",
        "checksum: No SHA1 checksum recorded for nonexistent.txt",
    );
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-a")
        .arg("SHA1")
        .arg(&distinfo)
        .arg("digest1.txt")
        .arg("nonexistent.txt")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(2));
    assert_eq!(cmd.stdout, "".as_bytes());
    assert_eq!(cmd.stderr, output.as_bytes());
    Ok(())
}

/*
 * Patch mode.
 */
#[test]
fn test_checksum_patch() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let output = "=> Checksum SHA1 OK for patch-Makefile\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-p")
        .arg(&distinfo)
        .arg("patch-Makefile")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(0));
    assert_eq!(cmd.stdout, output.as_bytes());
    assert_eq!(cmd.stderr, "".as_bytes());
    Ok(())
}

/*
 * Patch mode where the path doesn't exactly match the distinfo entry,
 * ensuring that find_entry() functionality is tested.
 */
#[test]
fn test_checksum_patch_path() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo");
    let output = "=> Checksum SHA1 OK for patch-Makefile\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-p")
        .arg(&distinfo)
        .arg("../data/patch-Makefile")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(0));
    assert_eq!(cmd.stdout, output.as_bytes());
    assert_eq!(cmd.stderr, "".as_bytes());
    Ok(())
}

/*
 * Now run some of the same tests but with a modified distinfo file containing
 * invalid checksums.
 */
#[test]
fn test_checksum_mismatch() -> Result<()> {
    let mut distinfo = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    distinfo.push("tests/data/distinfo.bad");

    // Only the first checksum mismatch is printed on failure.
    let output = "checksum: Checksum BLAKE2s mismatch for digest1.txt\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg(&distinfo)
        .arg("digest1.txt")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(1));
    assert_eq!(cmd.stdout, "".as_bytes());
    assert_eq!(cmd.stderr, output.as_bytes());

    let output = "checksum: Checksum SHA512 mismatch for digest1.txt\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-a")
        .arg("SHA512")
        .arg(&distinfo)
        .arg("digest1.txt")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(1));
    assert_eq!(cmd.stdout, "".as_bytes());
    assert_eq!(cmd.stderr, output.as_bytes());

    let output = "checksum: Checksum SHA1 mismatch for patch-Makefile\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-p")
        .arg(&distinfo)
        .arg("patch-Makefile")
        .current_dir("tests/data")
        .output()?;
    assert_eq!(cmd.status.code(), Some(1));
    assert_eq!(cmd.stdout, "".as_bytes());
    assert_eq!(cmd.stderr, output.as_bytes());

    /*
     * Repeat the above with paths that contain a directory component, so
     * the input path differs from the bare filename recorded in distinfo.
     */
    let output = "checksum: Checksum BLAKE2s mismatch for digest1.txt\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg(&distinfo)
        .arg("data/digest1.txt")
        .current_dir("tests")
        .output()?;
    assert_eq!(cmd.status.code(), Some(1));
    assert_eq!(cmd.stdout, "".as_bytes());
    assert_eq!(cmd.stderr, output.as_bytes());

    let output = "checksum: Checksum SHA1 mismatch for patch-Makefile\n";
    let cmd = Command::new(MKTOOL)
        .arg("checksum")
        .arg("-p")
        .arg(&distinfo)
        .arg("data/patch-Makefile")
        .current_dir("tests")
        .output()?;
    assert_eq!(cmd.status.code(), Some(1));
    assert_eq!(cmd.stdout, "".as_bytes());
    assert_eq!(cmd.stderr, output.as_bytes());
    Ok(())
}