-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathvalidate.rs
More file actions
235 lines (195 loc) · 7.18 KB
/
validate.rs
File metadata and controls
235 lines (195 loc) · 7.18 KB
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
use anyhow::Result;
use serde::Serialize;
use std::path::PathBuf;
use crate::publish::Report;
const DEFAULT_THRESHOLD: f64 = 90.0;
/// Validator for validating coverage reports.
#[derive(Debug, Clone)]
pub struct Validator {
threshold: f64,
}
impl Default for Validator {
fn default() -> Self {
Self {
threshold: DEFAULT_THRESHOLD,
}
}
}
impl Validator {
/// Creates a new validator with the given threshold.
pub fn new(threshold: Option<f64>) -> Self {
Self {
threshold: threshold.unwrap_or(DEFAULT_THRESHOLD),
}
}
/// Validates a coverage report.
pub fn validate(&self, report: &Report) -> Result<ValidationResult> {
let mut validation_result = ValidationResult {
threshold: self.threshold,
..Default::default()
};
report.file_coverages.iter().for_each(|file| {
if PathBuf::from(&file.path).exists() {
validation_result.files_present += 1;
} else {
validation_result.files_missing += 1;
}
});
validation_result.total_files =
validation_result.files_present + validation_result.files_missing;
if validation_result.total_files == 0 {
validation_result.status = ValidationStatus::NoCoverageData;
return Ok(validation_result);
}
validation_result.coverage_percentage =
(validation_result.files_present as f64 / validation_result.total_files as f64) * 100.0;
validation_result.status = if validation_result.coverage_percentage < self.threshold {
ValidationStatus::Invalid
} else {
ValidationStatus::Valid
};
Ok(validation_result)
}
}
#[derive(Debug, Clone, Serialize, Default, PartialEq)]
pub enum ValidationStatus {
#[default]
#[serde(rename = "valid")]
Valid,
#[serde(rename = "invalid")]
Invalid,
#[serde(rename = "no_coverage_data")]
NoCoverageData,
}
#[derive(Debug, Clone, Serialize, Default)]
pub struct ValidationResult {
pub files_present: usize,
pub files_missing: usize,
pub total_files: usize,
pub coverage_percentage: f64,
pub threshold: f64,
pub status: ValidationStatus,
}
#[cfg(test)]
mod tests {
use super::*;
use qlty_analysis::utils::fs::path_to_string;
use qlty_types::tests::v1::{CoverageMetadata, FileCoverage, ReportFile};
use std::collections::HashSet;
use std::fs::{self, File};
use tempfile::tempdir;
fn create_test_report(file_coverages: Vec<FileCoverage>) -> Report {
Report {
metadata: CoverageMetadata::default(),
report_files: vec![ReportFile::default()],
file_coverages,
found_files: HashSet::new(),
missing_files: HashSet::new(),
untracked_files: HashSet::new(),
git_repo_path: None,
totals: Default::default(),
excluded_files_count: 0,
auto_path_fixing_enabled: false,
}
}
fn create_dummy_file(path: &PathBuf) {
let parent = path.parent().unwrap();
fs::create_dir_all(parent).expect("Failed to create parent dirs");
File::create(path).expect("Failed to create dummy file");
}
#[test]
fn test_all_files_present() {
let temp_dir = tempdir().expect("Failed to create temp dir");
let file_path = temp_dir.path().join("file1.rs");
create_dummy_file(&file_path);
let report = create_test_report(vec![FileCoverage {
path: path_to_string(file_path),
..Default::default()
}]);
let validator = Validator::new(Some(90.0));
let result = validator.validate(&report).unwrap();
assert_eq!(result.files_present, 1);
assert_eq!(result.files_missing, 0);
assert_eq!(result.status, ValidationStatus::Valid);
}
#[test]
fn test_some_files_missing() {
let temp_dir = tempdir().expect("Failed to create temp dir");
let existing_path = temp_dir.path().join("file2.rs");
create_dummy_file(&existing_path);
let report = create_test_report(vec![
FileCoverage {
path: path_to_string(existing_path),
..Default::default()
},
FileCoverage {
path: "target/test-data/src/missing.rs".to_string(),
..Default::default()
},
]);
let validator = Validator::new(Some(80.0));
let result = validator.validate(&report).unwrap();
assert_eq!(result.files_present, 1);
assert_eq!(result.files_missing, 1);
assert_eq!(result.status, ValidationStatus::Invalid);
}
#[test]
fn test_no_coverage_data() {
let report = create_test_report(vec![]);
let validator = Validator::new(Some(80.0));
let result = validator.validate(&report).unwrap();
assert_eq!(result.total_files, 0);
assert_eq!(result.status, ValidationStatus::NoCoverageData);
}
#[test]
fn test_threshold_enforcement() {
let temp_dir = tempdir().expect("Failed to create temp dir");
let existing_path = temp_dir.path().join("file3.rs");
create_dummy_file(&existing_path);
let report = create_test_report(vec![
FileCoverage {
path: path_to_string(existing_path),
..Default::default()
},
FileCoverage {
path: "target/test-data/src/missing1.rs".to_string(),
..Default::default()
},
FileCoverage {
path: "target/test-data/src/missing2.rs".to_string(),
..Default::default()
},
]);
// With 33.33% files present and threshold of 30%, should be valid
let validator_lenient = Validator::new(Some(30.0));
let result_lenient = validator_lenient.validate(&report).unwrap();
assert_eq!(result_lenient.status, ValidationStatus::Valid);
// With 33.33% files present and threshold of 50%, should be invalid
let validator_strict = Validator::new(Some(50.0));
let result_strict = validator_strict.validate(&report).unwrap();
assert_eq!(result_strict.status, ValidationStatus::Invalid);
}
#[test]
fn test_default_threshold() {
let temp_dir = tempdir().expect("Failed to create temp dir");
let existing_path = temp_dir.path().join("file4.rs");
create_dummy_file(&existing_path);
let report = create_test_report(vec![
FileCoverage {
path: path_to_string(existing_path),
..Default::default()
},
FileCoverage {
path: "target/test-data/src/missing.rs".to_string(),
..Default::default()
},
]);
// 50% files present with default threshold (90%)
let validator = Validator::default();
let result = validator.validate(&report).unwrap();
assert_eq!(result.files_present, 1);
assert_eq!(result.files_missing, 1);
assert_eq!(result.threshold, DEFAULT_THRESHOLD);
assert_eq!(result.status, ValidationStatus::Invalid);
}
}