tryexpand 0.13.0

Test harness for macro expansion
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
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
use core::panic;
use std::{
    collections::hash_map::DefaultHasher,
    collections::HashSet,
    env,
    fmt::Write,
    hash::{Hash as _, Hasher as _},
    iter::FromIterator,
    path::{Path, PathBuf},
};

use crate::{
    error::{Error, Result},
    message,
    options::Options,
    project::Project,
    test::{Action, Test, TestBehavior, TestPlan, TestStatus},
    TRYEXPAND_ENV_KEY, TRYEXPAND_ENV_VAL_EXPECT, TRYEXPAND_ENV_VAL_OVERWRITE,
};

/// A completed test suite where all tests passed.
///
/// This type is returned by [`ExpandTestSuite::expect_pass`] or [`BuildTestSuite::expect_pass`]
/// after successfully running all tests.
///
/// This is a marker type that serves as proof that the tests completed successfully.
/// It has no public methods and simply indicates successful test execution.
pub struct TestSuitePass {
    #[allow(dead_code)]
    test_suite: TestSuite,
}

/// A completed test suite where all tests failed as expected.
///
/// This type is returned by [`ExpandTestSuite::expect_fail`] or [`BuildTestSuite::expect_fail`]
/// after successfully running all tests that were expected to fail.
///
/// This is a marker type that serves as proof that the tests completed as expected (with failures).
/// It has no public methods and simply indicates successful test execution of failing cases.
pub struct TestSuiteFail {
    #[allow(dead_code)]
    test_suite: TestSuite,
}

/// A test suite builder for macro expansion tests created by [`crate::expand`].
///
/// This type provides methods to configure how tests are run and what happens after expansion.
/// Use the builder pattern to chain configuration methods before calling
/// [`expect_pass`](Self::expect_pass) or [`expect_fail`](Self::expect_fail) to execute the tests.
///
/// # Examples
///
/// ```no_run
/// #[test]
/// fn test_expansion() {
///     tryexpand::expand(["tests/expand/*.rs"])
///         .args(["--features", "test-feature"])
///         .and_check()
///         .expect_pass();
/// }
/// ```
pub struct ExpandTestSuite(pub(crate) TestSuite);

impl ExpandTestSuite {
    /// Adds a single argument to pass to `cargo expand`.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::expand(["tests/*.rs"])
    ///     .arg("--verbose")
    ///     .expect_pass();
    /// ```
    pub fn arg<T>(self, arg: T) -> Self
    where
        T: AsRef<str>,
    {
        Self(self.0.arg(arg))
    }

    /// Adds multiple arguments to pass to `cargo expand`.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::expand(["tests/*.rs"])
    ///     .args(["--features", "test-feature"])
    ///     .expect_pass();
    /// ```
    pub fn args<T, I>(self, args: I) -> Self
    where
        T: AsRef<str>,
        I: IntoIterator<Item = T>,
    {
        Self(self.0.args(args))
    }

    /// Sets an environment variable for the test execution.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::expand(["tests/*.rs"])
    ///     .env("MY_VAR", "value")
    ///     .expect_pass();
    /// ```
    pub fn env<K, V>(self, key: K, value: V) -> Self
    where
        K: AsRef<str>,
        V: AsRef<str>,
    {
        Self(self.0.env(key, value))
    }

    /// Sets multiple environment variables for the test execution.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::expand(["tests/*.rs"])
    ///     .envs([("VAR1", "value1"), ("VAR2", "value2")])
    ///     .expect_pass();
    /// ```
    pub fn envs<K, V, I>(self, envs: I) -> Self
    where
        K: AsRef<str>,
        V: AsRef<str>,
        I: IntoIterator<Item = (K, V)>,
    {
        Self(self.0.envs(envs))
    }

    /// Prevents overwriting existing snapshot files even when `TRYEXPAND=overwrite` is set.
    ///
    /// This is useful when you want to preserve specific snapshots while allowing
    /// others to be updated during test runs.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::expand(["tests/*.rs"])
    ///     .skip_overwrite()
    ///     .expect_pass();
    /// ```
    pub fn skip_overwrite(self) -> Self {
        Self(self.0.skip_overwrite())
    }

    /// Applies a regex filter to normalize stdout output before snapshotting.
    ///
    /// This is useful for removing non-deterministic content like timestamps or paths
    /// from the expanded output.
    ///
    /// # Panics
    ///
    /// Panics if the regex pattern is invalid.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::expand(["tests/*.rs"])
    ///     .filter_stdout(r"/path/to/\S+", "/path/to/file")
    ///     .expect_pass();
    /// ```
    pub fn filter_stdout<P, R>(self, pattern: P, replacement: R) -> Self
    where
        P: AsRef<str>,
        R: AsRef<str>,
    {
        Self(self.0.filter_stdout(pattern, replacement))
    }

    /// Applies a regex filter to normalize stderr output before snapshotting.
    ///
    /// This is useful for removing non-deterministic content like timestamps or paths
    /// from error messages.
    ///
    /// # Panics
    ///
    /// Panics if the regex pattern is invalid.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::expand(["tests/*.rs"])
    ///     .filter_stderr(r"thread '\S+'", "thread 'test'")
    ///     .expect_pass();
    /// ```
    pub fn filter_stderr<P, R>(self, pattern: P, replacement: R) -> Self
    where
        P: AsRef<str>,
        R: AsRef<str>,
    {
        Self(self.0.filter_stderr(pattern, replacement))
    }

    /// Adds a type-checking step after macro expansion.
    ///
    /// After expanding macros, this will run `cargo check` on the expanded code
    /// and snapshot the compiler output.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::expand(["tests/*.rs"])
    ///     .and_check()
    ///     .expect_pass();
    /// ```
    pub fn and_check(self) -> BuildTestSuite {
        BuildTestSuite(self.0.and_check())
    }

    /// Adds a run step after macro expansion.
    ///
    /// After expanding macros, this will run `cargo run` on the expanded code
    /// and snapshot the program output.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::expand(["tests/*.rs"])
    ///     .and_run()
    ///     .expect_pass();
    /// ```
    pub fn and_run(self) -> BuildTestSuite {
        BuildTestSuite(self.0.and_run())
    }

    /// Adds a test execution step after macro expansion.
    ///
    /// After expanding macros, this will run `cargo test` on the expanded code
    /// and snapshot the test output.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::expand(["tests/*.rs"])
    ///     .and_run_tests()
    ///     .expect_pass();
    /// ```
    pub fn and_run_tests(self) -> BuildTestSuite {
        BuildTestSuite(self.0.and_run_tests())
    }

    /// Expects all tests to pass (macro expansion succeeds).
    ///
    /// This consumes the test suite and executes all tests, expecting successful
    /// macro expansion and matching snapshots.
    ///
    /// # Panics
    ///
    /// Panics if any test fails or if snapshots don't match.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// #[test]
    /// fn pass() {
    ///     tryexpand::expand(["tests/pass/*.rs"])
    ///         .expect_pass();
    /// }
    /// ```
    pub fn expect_pass(self) -> TestSuitePass {
        self.0.expect_pass()
    }

    /// Expects all tests to fail (macro expansion produces errors).
    ///
    /// This consumes the test suite and executes all tests, expecting macro expansion
    /// to fail and produce error output that matches the snapshots.
    ///
    /// # Panics
    ///
    /// Panics if any test unexpectedly passes or if error snapshots don't match.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// #[test]
    /// fn fail() {
    ///     tryexpand::expand(["tests/fail/*.rs"])
    ///         .expect_fail();
    /// }
    /// ```
    pub fn expect_fail(self) -> TestSuiteFail {
        self.0.expect_fail()
    }
}

/// A test suite builder for build/run/test operations created by
/// [`crate::check`], [`crate::run`], [`crate::run_tests`], or by calling
/// [`ExpandTestSuite::and_check`], [`ExpandTestSuite::and_run`], or
/// [`ExpandTestSuite::and_run_tests`].
///
/// This type provides methods to configure how tests are run before calling
/// [`expect_pass`](Self::expect_pass) or [`expect_fail`](Self::expect_fail) to execute the tests.
///
/// # Examples
///
/// ```no_run
/// #[test]
/// fn test_check() {
///     tryexpand::check(["tests/check/*.rs"])
///         .args(["--features", "test-feature"])
///         .expect_pass();
/// }
/// ```
pub struct BuildTestSuite(pub(crate) TestSuite);

impl BuildTestSuite {
    /// Adds a single argument to pass to the cargo command (`check`, `run`, or `test`).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::check(["tests/*.rs"])
    ///     .arg("--verbose")
    ///     .expect_pass();
    /// ```
    pub fn arg<T>(self, arg: T) -> Self
    where
        T: AsRef<str>,
    {
        Self(self.0.arg(arg))
    }

    /// Adds multiple arguments to pass to the cargo command.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::run(["tests/*.rs"])
    ///     .args(["--features", "test-feature"])
    ///     .expect_pass();
    /// ```
    pub fn args<T, I>(self, args: I) -> Self
    where
        T: AsRef<str>,
        I: IntoIterator<Item = T>,
    {
        Self(self.0.args(args))
    }

    /// Sets an environment variable for the test execution.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::run(["tests/*.rs"])
    ///     .env("MY_VAR", "value")
    ///     .expect_pass();
    /// ```
    pub fn env<K, V>(self, key: K, value: V) -> Self
    where
        K: AsRef<str>,
        V: AsRef<str>,
    {
        Self(self.0.env(key, value))
    }

    /// Sets multiple environment variables for the test execution.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::run_tests(["tests/*.rs"])
    ///     .envs([("VAR1", "value1"), ("VAR2", "value2")])
    ///     .expect_pass();
    /// ```
    pub fn envs<K, V, I>(self, envs: I) -> Self
    where
        K: AsRef<str>,
        V: AsRef<str>,
        I: IntoIterator<Item = (K, V)>,
    {
        Self(self.0.envs(envs))
    }

    /// Prevents overwriting existing snapshot files even when `TRYEXPAND=overwrite` is set.
    ///
    /// This is useful when you want to preserve specific snapshots while allowing
    /// others to be updated during test runs.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::check(["tests/*.rs"])
    ///     .skip_overwrite()
    ///     .expect_pass();
    /// ```
    pub fn skip_overwrite(self) -> Self {
        Self(self.0.skip_overwrite())
    }

    /// Applies a regex filter to normalize stdout output before snapshotting.
    ///
    /// This is useful for removing non-deterministic content like timestamps or paths
    /// from the program output.
    ///
    /// # Panics
    ///
    /// Panics if the regex pattern is invalid.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::run(["tests/*.rs"])
    ///     .filter_stdout(r"\d{4}-\d{2}-\d{2}", "YYYY-MM-DD")
    ///     .expect_pass();
    /// ```
    pub fn filter_stdout<P, R>(self, pattern: P, replacement: R) -> Self
    where
        P: AsRef<str>,
        R: AsRef<str>,
    {
        Self(self.0.filter_stdout(pattern, replacement))
    }

    /// Applies a regex filter to normalize stderr output before snapshotting.
    ///
    /// This is useful for removing non-deterministic content like timestamps or paths
    /// from error messages.
    ///
    /// # Panics
    ///
    /// Panics if the regex pattern is invalid.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// tryexpand::check(["tests/*.rs"])
    ///     .filter_stderr(r"thread '\S+'", "thread 'test'")
    ///     .expect_pass();
    /// ```
    pub fn filter_stderr<P, R>(self, pattern: P, replacement: R) -> Self
    where
        P: AsRef<str>,
        R: AsRef<str>,
    {
        Self(self.0.filter_stderr(pattern, replacement))
    }

    /// Expects all tests to pass.
    ///
    /// This consumes the test suite and executes all tests, expecting the cargo
    /// command to succeed and snapshots to match.
    ///
    /// # Panics
    ///
    /// Panics if any test fails or if snapshots don't match.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// #[test]
    /// fn pass() {
    ///     tryexpand::check(["tests/pass/*.rs"])
    ///         .expect_pass();
    /// }
    /// ```
    pub fn expect_pass(self) -> TestSuitePass {
        self.0.expect_pass()
    }

    /// Expects all tests to fail.
    ///
    /// This consumes the test suite and executes all tests, expecting the cargo
    /// command to fail and produce error output that matches the snapshots.
    ///
    /// # Panics
    ///
    /// Panics if any test unexpectedly passes or if error snapshots don't match.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// #[test]
    /// fn fail() {
    ///     tryexpand::check(["tests/fail/*.rs"])
    ///         .expect_fail();
    /// }
    /// ```
    pub fn expect_fail(self) -> TestSuiteFail {
        self.0.expect_fail()
    }
}

#[derive(Debug)]
pub(crate) struct TestSuite {
    pub(crate) project: Project,
    pub(crate) plan: TestPlan,
    pub(crate) tests: Vec<Test>,
    pub(crate) options: Options,
    pub(crate) call_site: String,
}

impl TestSuite {
    #[track_caller]
    pub(crate) fn new<I, P>(
        patterns: I,
        action: Action,
        location: &std::panic::Location,
    ) -> Result<Self>
    where
        I: IntoIterator<Item = P>,
        P: AsRef<Path>,
    {
        let patterns = Vec::from_iter(patterns);

        if patterns.is_empty() {
            panic!("no file patterns provided");
        }

        let paths_per_pattern: Vec<(PathBuf, Vec<PathBuf>)> = patterns
            .into_iter()
            .map(|pattern| {
                expand_globs(&pattern).map(|paths| {
                    (
                        pattern.as_ref().to_owned(),
                        paths
                            .into_iter()
                            .filter(|path| {
                                !path.to_string_lossy().ends_with(crate::OUT_RS_FILE_SUFFIX)
                            })
                            .collect(),
                    )
                })
            })
            .collect::<Result<_>>()?;

        let (without_matches, with_matches): (Vec<_>, Vec<_>) = paths_per_pattern
            .iter()
            .partition(|(_, paths)| paths.is_empty());

        if !without_matches.is_empty() {
            let unique_patterns: HashSet<&PathBuf> = without_matches
                .into_iter()
                .map(|(pattern, _)| pattern)
                .collect();

            let sorted_patterns = Vec::from_iter(unique_patterns);

            let mut error = String::new();
            writeln!(&mut error, "no matching files found for:").unwrap();
            for pattern in sorted_patterns {
                writeln!(&mut error, "    {}", pattern.display()).unwrap();
            }

            panic!("{}", error);
        }

        let unique_paths: HashSet<PathBuf> = with_matches
            .into_iter()
            .flat_map(|(_, paths)| paths.clone())
            .collect();

        let paths: Vec<PathBuf> = Vec::from_iter(unique_paths);

        let metadata = cargo_metadata::MetadataCommand::new()
            .exec()
            .map_err(|source| Error::CargoMetadata {
                directory: std::env::current_dir().unwrap(),
                source,
            })?;

        let crate_name = env::var("CARGO_PKG_NAME")
            .ok()
            .or_else(|| metadata.root_package().map(|pkg| pkg.name.to_string()))
            .ok_or(Error::CargoPkgName)?;

        let package = metadata
            .packages
            .iter()
            .find(|package| package.name.as_str() == crate_name)
            .ok_or(Error::CargoPackageNotFound)?;

        let target_dir = env::var("CARGO_TARGET_DIR")
            .map(PathBuf::from)
            .unwrap_or_else(|_| metadata.target_directory.as_std_path().to_owned());

        let tests = Self::tests_for(&crate_name, paths);

        let test_suite_id = test_suite_id_from_location(location);

        let project = Project::new(&metadata, package, &test_suite_id, &target_dir, &tests)
            .unwrap_or_else(|err| {
                panic!("Could not create test project: {:#?}", err);
            });

        let plan = TestPlan {
            action,
            post_action: None,
            behavior: test_behavior_from_env().unwrap(),
            expectation: TestStatus::Success,
        };

        let options = Options::default();

        let call_site = format!(
            "{file}:{line}:{column}",
            file = location.file(),
            line = location.line(),
            column = location.column(),
        );

        Ok(Self {
            project,
            plan,
            tests,
            options,
            call_site,
        })
    }

    pub(crate) fn arg<T>(self, arg: T) -> Self
    where
        T: AsRef<str>,
    {
        self.args([arg])
    }

    pub(crate) fn args<T, I>(mut self, args: I) -> Self
    where
        T: AsRef<str>,
        I: IntoIterator<Item = T>,
    {
        self.options
            .args
            .extend(args.into_iter().map(|str| str.as_ref().to_owned()));
        self
    }

    pub(crate) fn env<K, V>(self, key: K, value: V) -> Self
    where
        K: AsRef<str>,
        V: AsRef<str>,
    {
        self.envs([(key, value)])
    }

    pub(crate) fn envs<K, V, I>(mut self, envs: I) -> Self
    where
        K: AsRef<str>,
        V: AsRef<str>,
        I: IntoIterator<Item = (K, V)>,
    {
        for (key, value) in envs.into_iter() {
            let key = key.as_ref().to_owned();
            let value = value.as_ref().to_owned();
            self.options.envs.insert(key, value);
        }
        self
    }

    pub(crate) fn skip_overwrite(mut self) -> Self {
        self.options.skip_overwrite = true;
        self
    }

    pub(crate) fn filter_stdout<P, R>(self, pattern: P, replacement: R) -> Self
    where
        P: AsRef<str>,
        R: AsRef<str>,
    {
        self.add_filter(crate::options::FilterTarget::Stdout, pattern, replacement)
    }

    pub(crate) fn filter_stderr<P, R>(self, pattern: P, replacement: R) -> Self
    where
        P: AsRef<str>,
        R: AsRef<str>,
    {
        self.add_filter(crate::options::FilterTarget::Stderr, pattern, replacement)
    }

    fn add_filter<P, R>(
        mut self,
        target: crate::options::FilterTarget,
        pattern: P,
        replacement: R,
    ) -> Self
    where
        P: AsRef<str>,
        R: AsRef<str>,
    {
        use crate::options::RegexFilter;

        let regex = regex::Regex::new(pattern.as_ref())
            .unwrap_or_else(|e| panic!("Invalid regex pattern '{}': {}", pattern.as_ref(), e));

        self.options.filters.push(RegexFilter {
            target,
            pattern: regex,
            replacement: replacement.as_ref().to_owned(),
        });

        self
    }

    pub(crate) fn and_check(self) -> Self {
        self.and_post_check(Action::Check)
    }

    pub(crate) fn and_run(self) -> Self {
        self.and_post_check(Action::Run)
    }

    pub(crate) fn and_run_tests(self) -> Self {
        self.and_post_check(Action::Test)
    }

    pub(crate) fn expect_pass(self) -> TestSuitePass {
        TestSuitePass {
            test_suite: self.expect_result(TestStatus::Success),
        }
    }

    pub(crate) fn expect_fail(self) -> TestSuiteFail {
        TestSuiteFail {
            test_suite: self.expect_result(TestStatus::Failure),
        }
    }

    fn and_post_check(mut self, action: Action) -> Self {
        if let Some(existing_action) = &self.plan.post_action {
            let cmd = match existing_action {
                Action::Expand => panic!("unexpected `expand` as post-action"),
                Action::Check => "check",
                Action::Test => "test",
                Action::Run => "run",
            };
            panic!("Post-expand action already set to `cargo {cmd}`!");
        }

        self.plan.post_action = Some(action);
        self
    }

    fn expect_result(mut self, expectation: TestStatus) -> Self {
        self.plan.expectation = expectation;
        self
    }

    #[track_caller] // LOAD-BEARING, DO NOT REMOVE!
    pub(crate) fn try_run(&mut self) -> Result<()> {
        let TestSuite {
            project,
            plan,
            tests,
            options,
            call_site,
        } = self;

        let total_tests = tests.len();

        println!(
            "Running {tests} macro expansion tests from {suite} ...\n",
            tests = total_tests,
            suite = call_site
        );

        let mut failures = HashSet::new();

        let max_errors = 2;
        let mut command_errors = 0;

        for test in tests {
            let test_path = test.path.to_owned();
            let result = test.run(plan, project, options, &mut |outcome| {
                message::report_outcome(&test_path, &outcome);

                match outcome.as_status() {
                    TestStatus::Success => {}
                    TestStatus::Failure => {
                        failures.insert(test_path.clone());
                    }
                }
            });

            if let Err(err) = result {
                let error = err.to_string();
                message::command_failure(&test.path, &error);
                command_errors += 1;

                if command_errors > max_errors {
                    message::command_abortion(command_errors);
                }
            }
        }

        if !failures.is_empty() {
            let mut message = String::new();

            writeln!(&mut message).unwrap();
            writeln!(
                &mut message,
                "{} of {} tests failed:",
                failures.len(),
                total_tests
            )
            .unwrap();
            writeln!(&mut message).unwrap();

            let mut sorted_failures = Vec::from_iter(failures);
            sorted_failures.sort();

            for failure in sorted_failures {
                writeln!(&mut message, "    {}", failure.display()).unwrap();
            }

            eprintln!();
            panic!("{}", message);
        }

        Ok(())
    }

    fn tests_for<I, P>(crate_name: &str, paths: I) -> Vec<Test>
    where
        I: IntoIterator<Item = P>,
        P: AsRef<Path>,
    {
        let mut tests: Vec<_> = paths
            .into_iter()
            .map(|path| {
                let path = path.as_ref().to_path_buf();
                let bin = {
                    let mut hasher = DefaultHasher::default();
                    path.hash(&mut hasher);
                    // Taking the lower-case of a base62 hash leads to collisions
                    // but the number of tests we're dealing with shouldn't
                    // realistically cause any issues in practice:
                    let test_id = base62::encode(hasher.finish()).to_lowercase();
                    format!("{crate_name}_{test_id}")
                };
                Test { bin, path }
            })
            .collect();

        tests.sort_by_cached_key(|test| test.path.clone());

        tests
    }
}

impl Drop for TestSuite {
    fn drop(&mut self) {
        match self.try_run() {
            Ok(()) => {}
            Err(err) => {
                panic!("Test suite failed with error: {err:?}");
            }
        }
    }
}

fn test_suite_id_from_location(caller_location: &std::panic::Location) -> String {
    use std::hash::{Hash as _, Hasher as _};
    let mut hasher = ::std::collections::hash_map::DefaultHasher::default();
    caller_location.file().hash(&mut hasher);
    caller_location.line().hash(&mut hasher);
    caller_location.column().hash(&mut hasher);
    // Taking the lower-case of a base62 hash leads to collisions
    // but the number of tests we're dealing with shouldn't
    // realistically cause any issues in practice:
    base62::encode(hasher.finish()).to_lowercase()
}

pub(crate) fn test_behavior_from_env() -> Result<TestBehavior> {
    let key = TRYEXPAND_ENV_KEY;
    let Some(var) = std::env::var_os(key) else {
        return Ok(TestBehavior::ExpectFiles);
    };
    let value = var.to_string_lossy().into_owned();
    match value.as_str() {
        TRYEXPAND_ENV_VAL_EXPECT => Ok(TestBehavior::ExpectFiles),
        TRYEXPAND_ENV_VAL_OVERWRITE => Ok(TestBehavior::OverwriteFiles),
        _ => Err(Error::UnrecognizedEnv {
            key: key.to_owned(),
            value,
        }),
    }
}

fn expand_globs(path: impl AsRef<Path>) -> Result<Vec<PathBuf>> {
    let path = path.as_ref();

    fn glob(pattern: &str) -> Result<Vec<PathBuf>> {
        let mut paths = glob::glob(pattern)?
            .map(|entry| entry.map_err(Error::from))
            .collect::<Result<Vec<PathBuf>>>()?;
        paths.sort();
        Ok(paths)
    }

    let path_string = path.as_os_str().to_string_lossy();

    Ok(glob(&path_string)?.into_iter().collect())
}