Line data Source code
1 : use crate::{example_target, example_targets, initialize, run_example_test, run_tests};
2 : use std::{
3 : env::current_dir,
4 : path::{Path, PathBuf},
5 : };
6 :
7 : enum Target {
8 : SrcBase(PathBuf),
9 : Example(String),
10 : Examples,
11 : }
12 :
13 : #[derive(Clone, Default)]
14 : pub(super) struct Config {
15 : pub(super) rustc_flags: Vec<String>,
16 : pub(super) dylint_toml: Option<String>,
17 : }
18 :
19 : /// Test builder
20 : pub struct Test {
21 : name: String,
22 : target: Target,
23 : config: Config,
24 : }
25 :
26 : impl Test {
27 : /// Test a library on all source files in a directory (similar to [`ui_test`]).
28 : ///
29 : /// [`ui_test`]: crate::ui_test
30 : #[must_use]
31 0 : pub fn src_base(name: &str, src_base: impl AsRef<Path>) -> Self {
32 0 : Self::new(name, Target::SrcBase(src_base.as_ref().to_owned()))
33 0 : }
34 :
35 : /// Test a library on one example target (similar to [`ui_test_example`]).
36 : ///
37 : /// [`ui_test_example`]: crate::ui_test_example
38 : #[must_use]
39 0 : pub fn example(name: &str, example: &str) -> Self {
40 0 : Self::new(name, Target::Example(example.to_owned()))
41 0 : }
42 :
43 : /// Test a library on all example targets (similar to [`ui_test_examples`]).
44 : ///
45 : /// [`ui_test_examples`]: crate::ui_test_examples
46 : #[must_use]
47 0 : pub fn examples(name: &str) -> Self {
48 0 : Self::new(name, Target::Examples)
49 0 : }
50 :
51 : /// Pass flags to the compiler when running the test.
52 0 : pub fn rustc_flags(
53 0 : &mut self,
54 0 : rustc_flags: impl IntoIterator<Item = impl AsRef<str>>,
55 0 : ) -> &mut Self {
56 0 : self.config
57 0 : .rustc_flags
58 0 : .extend(rustc_flags.into_iter().map(|s| s.as_ref().to_owned()));
59 0 : self
60 0 : }
61 :
62 : /// Set the `dylint.toml` file's contents (for testing configurable libraries).
63 0 : pub fn dylint_toml(&mut self, dylint_toml: impl AsRef<str>) -> &mut Self {
64 0 : self.config.dylint_toml = Some(dylint_toml.as_ref().to_owned());
65 0 : self
66 0 : }
67 :
68 : /// Run the test.
69 : #[allow(clippy::needless_pass_by_ref_mut)]
70 0 : pub fn run(&mut self) {
71 0 : self.run_immutable();
72 0 : }
73 :
74 0 : fn new(name: &str, target: Target) -> Self {
75 0 : Self {
76 0 : name: name.to_owned(),
77 0 : target,
78 0 : config: Config::default(),
79 0 : }
80 0 : }
81 :
82 0 : fn run_immutable(&self) {
83 0 : let driver = initialize(&self.name).unwrap();
84 0 :
85 0 : match &self.target {
86 0 : Target::SrcBase(src_base) => {
87 0 : run_tests(driver, src_base, &self.config);
88 0 : }
89 0 : Target::Example(example) => {
90 0 : let metadata = dylint_internal::cargo::current_metadata().unwrap();
91 0 : let current_dir = current_dir().unwrap();
92 0 : let package =
93 0 : dylint_internal::cargo::package_with_root(&metadata, ¤t_dir).unwrap();
94 0 : let target = example_target(&package, example).unwrap();
95 0 :
96 0 : run_example_test(driver, &metadata, &package, &target, &self.config).unwrap();
97 0 : }
98 : Target::Examples => {
99 0 : let metadata = dylint_internal::cargo::current_metadata().unwrap();
100 0 : let current_dir = current_dir().unwrap();
101 0 : let package =
102 0 : dylint_internal::cargo::package_with_root(&metadata, ¤t_dir).unwrap();
103 0 : let targets = example_targets(&package).unwrap();
104 :
105 0 : for target in targets {
106 0 : run_example_test(driver, &metadata, &package, &target, &self.config).unwrap();
107 0 : }
108 : }
109 : }
110 0 : }
111 : }
112 :
113 : #[cfg(test)]
114 : mod test {
115 : use super::*;
116 :
117 : // smoelius: Verify that `rustc_flags` compiles when used as intended.
118 : #[allow(dead_code)]
119 0 : fn rustc_flags() {
120 0 : let _ = Test::src_base("name", PathBuf::new()).rustc_flags(["--test"]);
121 0 : }
122 : }
|