nextest_runner/show_config/
nextest_version.rs

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
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::config::{NextestVersionConfig, NextestVersionEval, NextestVersionReq};
use owo_colors::{OwoColorize, Style};
use semver::Version;
use std::io::{self, Write};

/// Show version-related configuration.
pub struct ShowNextestVersion<'a> {
    version_cfg: &'a NextestVersionConfig,
    current_version: &'a Version,
    override_version_check: bool,
}

impl<'a> ShowNextestVersion<'a> {
    /// Construct a new [`ShowNextestVersion`].
    pub fn new(
        version_cfg: &'a NextestVersionConfig,
        current_version: &'a Version,
        override_version_check: bool,
    ) -> Self {
        Self {
            version_cfg,
            current_version,
            override_version_check,
        }
    }

    /// Write the version configuration in human-readable form.
    pub fn write_human(&self, writer: &mut dyn Write, colorize: bool) -> io::Result<()> {
        let mut styles = Styles::default();
        if colorize {
            styles.colorize();
        }

        writeln!(
            writer,
            "current nextest version: {}",
            self.current_version.style(styles.version)
        )?;

        write!(writer, "version requirements:")?;

        let mut any_requirements = false;
        if let NextestVersionReq::Version { version, tool } = &self.version_cfg.required {
            if !any_requirements {
                writeln!(writer)?;
            }
            any_requirements = true;
            write!(writer, "    - required: {}", version.style(styles.version))?;
            if let Some(tool) = tool {
                writeln!(writer, " (by tool {})", tool.style(styles.tool))?;
            } else {
                writeln!(writer)?;
            }
        }

        if let NextestVersionReq::Version { version, tool } = &self.version_cfg.recommended {
            if !any_requirements {
                writeln!(writer)?;
            }
            any_requirements = true;
            write!(
                writer,
                "    - recommended: {}",
                version.style(styles.version)
            )?;
            if let Some(tool) = tool {
                writeln!(writer, " (by tool {})", tool.style(styles.tool))?;
            } else {
                writeln!(writer)?;
            }
        }

        if any_requirements {
            write!(writer, "evaluation result: ")?;
            let eval = self
                .version_cfg
                .eval(self.current_version, self.override_version_check);
            match eval {
                NextestVersionEval::Satisfied => {
                    writeln!(writer, "{}", "ok".style(styles.satisfied))?;
                }
                NextestVersionEval::Error { .. } => {
                    writeln!(
                        writer,
                        "{}",
                        "does not meet required version".style(styles.error)
                    )?;
                }
                NextestVersionEval::Warn { .. } => {
                    writeln!(
                        writer,
                        "{}",
                        "does not meet recommended version".style(styles.warning)
                    )?;
                }
                NextestVersionEval::ErrorOverride { .. } => {
                    writeln!(
                        writer,
                        "does not meet required version, but is {}",
                        "overridden".style(styles.overridden),
                    )?;
                }
                crate::config::NextestVersionEval::WarnOverride { .. } => {
                    writeln!(
                        writer,
                        "does not meet recommended version, but is {}",
                        "overridden".style(styles.overridden),
                    )?;
                }
            }
        } else {
            writeln!(writer, " (none)")?;
        }

        Ok(())
    }
}

#[derive(Clone, Debug, Default)]
struct Styles {
    version: Style,
    tool: Style,
    satisfied: Style,
    error: Style,
    warning: Style,
    overridden: Style,
}

impl Styles {
    fn colorize(&mut self) {
        self.version = Style::new().bold();
        self.tool = Style::new().bold().yellow();
        self.satisfied = Style::new().bold().green();
        self.error = Style::new().bold().red();
        self.warning = Style::new().bold().yellow();
        self.overridden = Style::new().bold();
    }
}