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
use super::super::file::search_manifest_and_workspace_dir_for_nonempty_file_name_match;
use super::{Rule, RuleOutcome};
use cargo_metadata::Metadata;
use regex::Regex;
use std::io::Write;
use std::path::Path;

/// Rule that asserts a good Rust project:
/// "Should have a rustfmt.toml file in the project directory."
///
/// # Justification
///
/// Code style linting is a valuable tool to enhance project-wide
/// consistency and readability for new developers.
/// `rustfmt` is the de-facto standard style linter in the Rust
/// community.
///
/// A rustfmt.toml file suggests that the project
/// maintainers are aware of the advantages of unified formatting,
/// and have taken the time to select the `rustfmt` rules that make
/// sense for this project, even if the choice is simply to use the
/// defaults.
#[derive(Debug, Default)]
pub struct HasRustfmtFile;

lazy_static! {
    static ref HAS_RUSTFMT_FILE: Regex =
        Regex::new(r"^\.?(legacy-)?rustfmt.toml$").expect("Failed to create HasRustfmtFile regex.");
}

impl Rule for HasRustfmtFile {
    fn description(&self) -> &'static str {
        "Should have a rustfmt.toml file in the project directory."
    }

    fn evaluate(
        &self,
        cargo_manifest_file_path: &Path,
        _verbose: bool,
        metadata: &Option<Metadata>,
        _print_output: &mut Write,
    ) -> RuleOutcome {
        search_manifest_and_workspace_dir_for_nonempty_file_name_match(
            &HAS_RUSTFMT_FILE,
            cargo_manifest_file_path,
            metadata,
        )
    }
}
#[cfg(test)]
mod tests {
    use super::super::test_support::*;
    use super::*;
    use std::fs::File;
    use tempfile::tempdir;

    // TODO - Test for workspace style project edge cases

    #[test]
    fn has_rustfmt_happy_path() {
        let dir = tempdir().expect("Failed to make a temp dir");
        let file_path = dir.path().join("rustfmt.toml");
        let mut file = File::create(file_path).expect("Could not make target file");
        file.write_all(b"Hello, I am a rustfmt file.")
            .expect("Could not write to target file");
        let rule = HasRustfmtFile::default();
        let VerbosityOutcomes {
            verbose,
            not_verbose,
        } = execute_rule_against_project_dir_all_verbosities(dir.path(), &rule);
        assert_eq!(RuleOutcome::Success, verbose.outcome);
        assert_eq!(RuleOutcome::Success, not_verbose.outcome);
    }

    #[test]
    fn has_rustfmt_period_prefix_allowed() {
        let dir = tempdir().expect("Failed to make a temp dir");
        let file_path = dir.path().join(".rustfmt.toml");
        let mut file = File::create(file_path).expect("Could not make target file");
        file.write_all(b"Hello, I am a rustfmt file.")
            .expect("Could not write to target file");
        let rule = HasRustfmtFile::default();
        let VerbosityOutcomes {
            verbose,
            not_verbose,
        } = execute_rule_against_project_dir_all_verbosities(dir.path(), &rule);
        assert_eq!(RuleOutcome::Success, verbose.outcome);
        assert_eq!(RuleOutcome::Success, not_verbose.outcome);
    }

    #[test]
    fn has_rustfmt_legacy_prefix_allowed() {
        let dir = tempdir().expect("Failed to make a temp dir");
        let file_path = dir.path().join("legacy-rustfmt.toml");
        let mut file = File::create(file_path).expect("Could not make target file");
        file.write_all(b"Hello, I am a rustfmt file.")
            .expect("Could not write to target file");
        let rule = HasRustfmtFile::default();
        let VerbosityOutcomes {
            verbose,
            not_verbose,
        } = execute_rule_against_project_dir_all_verbosities(dir.path(), &rule);
        assert_eq!(RuleOutcome::Success, verbose.outcome);
        assert_eq!(RuleOutcome::Success, not_verbose.outcome);
    }

    #[test]
    fn has_rustfmt_additional_suffices_disallowed() {
        let dir = tempdir().expect("Failed to make a temp dir");
        let file_path = dir.path().join("rustfmt.toml.whatever");
        let mut file = File::create(file_path).expect("Could not make target file");
        file.write_all(b"Hello, I am a rustfmt file.")
            .expect("Could not write to target file");
        let rule = HasRustfmtFile::default();
        let VerbosityOutcomes {
            verbose,
            not_verbose,
        } = execute_rule_against_project_dir_all_verbosities(dir.path(), &rule);
        assert_eq!(RuleOutcome::Failure, verbose.outcome);
        assert_eq!(RuleOutcome::Failure, not_verbose.outcome);
    }

    #[test]
    fn has_rustfmt_empty_rustfmt_file_fails() {
        let dir = tempdir().expect("Failed to make a temp dir");
        {
            let file_path = dir.path().join("rustfmt.toml");
            let mut f = File::create(file_path).expect("Could not make target file");
            f.write_all(b"").expect("Could not write emptiness to file");
            f.flush().expect("Could not flush file");
            f.sync_all().expect("Could not sync file");
        }
        let rule = HasRustfmtFile::default();
        let VerbosityOutcomes {
            verbose,
            not_verbose,
        } = execute_rule_against_project_dir_all_verbosities(dir.path(), &rule);
        assert_eq!(RuleOutcome::Failure, verbose.outcome);
        assert_eq!(RuleOutcome::Failure, not_verbose.outcome);
    }

    #[test]
    fn has_rustfmt_nonempty_blank_content_rustfmt_file_succeeds() {
        let dir = tempdir().expect("Failed to make a temp dir");
        {
            let file_path = dir.path().join("rustfmt.toml");
            let mut f = File::create(file_path).expect("Could not make target file");
            f.write_all(b"\n")
                .expect("Could not write a newline to file");
            f.flush().expect("Could not flush file");
            f.sync_all().expect("Could not sync file");
        }
        let rule = HasRustfmtFile::default();
        let VerbosityOutcomes {
            verbose,
            not_verbose,
        } = execute_rule_against_project_dir_all_verbosities(dir.path(), &rule);
        assert_eq!(RuleOutcome::Success, verbose.outcome);
        assert_eq!(RuleOutcome::Success, not_verbose.outcome);
    }

    #[test]
    fn has_rustfmt_no_rustfmt_file_at_all_fails() {
        let dir = tempdir().expect("Failed to make a temp dir");
        let rule = HasRustfmtFile::default();
        let VerbosityOutcomes {
            verbose,
            not_verbose,
        } = execute_rule_against_project_dir_all_verbosities(dir.path(), &rule);
        assert_eq!(RuleOutcome::Failure, verbose.outcome);
        assert_eq!(RuleOutcome::Failure, not_verbose.outcome);
    }
}