-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathmod.rs
More file actions
369 lines (331 loc) · 11.7 KB
/
Copy pathmod.rs
File metadata and controls
369 lines (331 loc) · 11.7 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
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
mod db;
#[cfg(test)]
mod dummy;
use crate::config::Config;
use crate::crates::Crate;
use crate::experiments::Experiment;
use crate::prelude::*;
pub use crate::results::db::{DatabaseDB, ProgressData};
#[cfg(test)]
pub use crate::results::dummy::DummyDB;
use crate::toolchain::Toolchain;
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use flate2::Compression;
use rustwide::logging::LogStorage;
use std::collections::BTreeSet;
use std::{fmt, io::Read, io::Write, str::FromStr};
pub trait ReadResults {
fn load_log(
&self,
ex: &Experiment,
toolchain: &Toolchain,
krate: &Crate,
) -> Fallible<Option<EncodedLog>>;
fn load_test_result(
&self,
ex: &Experiment,
toolchain: &Toolchain,
krate: &Crate,
) -> Fallible<Option<TestResult>>;
}
pub trait WriteResults {
fn get_result(
&self,
ex: &Experiment,
toolchain: &Toolchain,
krate: &Crate,
) -> Fallible<Option<TestResult>>;
fn update_crate_version(&self, ex: &Experiment, old: &Crate, new: &Crate) -> Fallible<()>;
fn record_result<F>(
&self,
ex: &Experiment,
toolchain: &Toolchain,
krate: &Crate,
existing_logs: Option<LogStorage>,
config: &Config,
encoding_type: EncodingType,
f: F,
) -> Fallible<TestResult>
where
F: FnOnce() -> Fallible<TestResult>;
}
pub trait DeleteResults {
fn delete_all_results(&self, ex: &Experiment) -> Fallible<()>;
fn delete_result(&self, ex: &Experiment, toolchain: &Toolchain, krate: &Crate) -> Fallible<()>;
}
string_enum!(pub enum EncodingType {
Plain => "plain",
Gzip => "gzip",
});
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum EncodedLog {
Plain(Vec<u8>),
Gzip(Vec<u8>),
}
impl EncodedLog {
pub fn to_plain(&self) -> Fallible<Vec<u8>> {
match self {
EncodedLog::Plain(data) => Ok(data.to_vec()),
EncodedLog::Gzip(data) => {
let mut decoded_log = GzDecoder::new(data.as_slice());
let mut new_log = Vec::new();
decoded_log.read_to_end(&mut new_log)?;
Ok(new_log)
}
}
}
pub fn get_encoding_type(&self) -> EncodingType {
match self {
EncodedLog::Plain(_) => EncodingType::Plain,
EncodedLog::Gzip(_) => EncodingType::Gzip,
}
}
pub fn as_slice(&self) -> &[u8] {
match self {
EncodedLog::Plain(data) => data,
EncodedLog::Gzip(data) => data,
}
}
pub fn from_plain_slice(data: &[u8], desired_encoding: EncodingType) -> Fallible<EncodedLog> {
match desired_encoding {
EncodingType::Gzip => {
let mut encoded_log = GzEncoder::new(Vec::new(), Compression::default());
encoded_log.write_all(data)?;
let encoded_log = encoded_log.finish()?;
Ok(EncodedLog::Gzip(encoded_log))
}
EncodingType::Plain => Ok(EncodedLog::Plain(data.to_vec())),
}
}
}
macro_rules! test_result_enum {
(pub enum $name:ident {
with_reason { $($with_reason_name:ident($reason:ident) => $with_reason_repr:expr,)* }
without_reason { $($reasonless_name:ident => $reasonless_repr:expr,)* }
}) => {
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub enum $name {
$($with_reason_name($reason),)*
$($reasonless_name,)*
}
impl FromStr for $name {
type Err = ::failure::Error;
fn from_str(input: &str) -> Fallible<Self> {
// if there is more than one ':' we assume it's part of a failure reason serialization
let parts: Vec<&str> = input.splitn(2, ':').collect();
if parts.len() == 1 {
match parts[0] {
$($with_reason_repr => Ok($name::$with_reason_name($reason::Unknown)),)*
$($reasonless_repr => Ok($name::$reasonless_name),)*
other => Err(TestResultParseError::UnknownResult(other.into()).into()),
}
} else {
match parts[0] {
$($reasonless_repr => Err(TestResultParseError::UnexpectedFailureReason.into()),)*
$($with_reason_repr => Ok($name::$with_reason_name(parts[1].parse()?)),)*
other => Err(TestResultParseError::UnknownResult(other.into()).into()),
}
}
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
$($name::$with_reason_name(reason) => write!(f, "{}:{}", $with_reason_repr, reason),)*
$($name::$reasonless_name => write!(f, "{}", $reasonless_repr),)*
}
}
}
}
}
#[derive(Debug, Fail)]
pub enum TestResultParseError {
#[fail(display = "unknown test result: {}", _0)]
UnknownResult(String),
#[fail(display = "unexpected failure reason")]
UnexpectedFailureReason,
}
// simplified and lighter version of cargo-metadata::diagnostic::DiagnosticCode
#[derive(Debug, PartialEq, Serialize, Deserialize, Eq, Clone, Hash, PartialOrd, Ord)]
pub struct DiagnosticCode {
code: String,
}
impl ::std::fmt::Display for DiagnosticCode {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{}", self.code)
}
}
impl DiagnosticCode {
pub fn from(s: String) -> DiagnosticCode {
DiagnosticCode { code: s }
}
}
impl ::std::str::FromStr for DiagnosticCode {
type Err = ::failure::Error;
fn from_str(s: &str) -> ::failure::Fallible<DiagnosticCode> {
Ok(DiagnosticCode {
code: s.to_string(),
})
}
}
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
pub enum FailureReason {
Unknown,
OOM,
Timeout,
ICE,
NetworkAccess,
BuildScript,
CompilerError(BTreeSet<DiagnosticCode>),
DependsOn(BTreeSet<Crate>),
}
impl Fail for FailureReason {}
impl ::std::fmt::Display for FailureReason {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match self {
FailureReason::Unknown => write!(f, "unknown"),
FailureReason::OOM => write!(f, "oom"),
FailureReason::Timeout => write!(f, "timeout"),
FailureReason::ICE => write!(f, "ice"),
FailureReason::NetworkAccess => write!(f, "network-access"),
FailureReason::BuildScript => write!(f, "build-script"),
FailureReason::CompilerError(codes) => write!(
f,
"compiler-error({})",
codes
.iter()
.map(|diag| diag.code.clone())
.collect::<Vec<String>>()
.join(", "),
),
FailureReason::DependsOn(deps) => write!(
f,
"depends-on({})",
deps.iter()
.map(|dep| dep.id())
.collect::<Vec<String>>()
.join(", "),
),
}
}
}
impl ::std::str::FromStr for FailureReason {
type Err = ::failure::Error;
fn from_str(s: &str) -> ::failure::Fallible<FailureReason> {
if let (Some(idx), true) = (s.find('('), s.ends_with(')')) {
let prefix = &s[..idx];
let contents = s[idx + 1..s.len() - 1].split(", ");
match prefix {
"compiler-error" => Ok(FailureReason::CompilerError(
contents
.map(|st| DiagnosticCode {
code: st.to_string(),
})
.collect(),
)),
"depends-on" => {
let mut krates: BTreeSet<Crate> = BTreeSet::new();
for krate in contents {
krates.insert(krate.parse()?);
}
Ok(FailureReason::DependsOn(krates))
}
_ => bail!("unexpected prefix: {}", prefix),
}
} else {
match s {
"network-access" => Ok(FailureReason::NetworkAccess),
"build-script" => Ok(FailureReason::BuildScript),
"unknown" => Ok(FailureReason::Unknown),
"oom" => Ok(FailureReason::OOM),
"timeout" => Ok(FailureReason::Timeout),
"ice" => Ok(FailureReason::ICE),
_ => bail!("unexpected value: {}", s),
}
}
}
}
impl FailureReason {
pub(crate) fn is_spurious(&self) -> bool {
match *self {
FailureReason::OOM | FailureReason::Timeout | FailureReason::NetworkAccess => true,
FailureReason::CompilerError(_)
| FailureReason::DependsOn(_)
| FailureReason::BuildScript
| FailureReason::Unknown
| FailureReason::ICE => false,
}
}
}
string_enum!(pub enum BrokenReason {
Unknown => "unknown",
CargoToml => "cargo-toml",
Yanked => "yanked",
MissingDependencies => "missing-deps",
MissingGitRepository => "missing-git-repository",
});
test_result_enum!(pub enum TestResult {
with_reason {
BrokenCrate(BrokenReason) => "broken",
BuildFail(FailureReason) => "build-fail",
TestFail(FailureReason) => "test-fail",
}
without_reason {
TestSkipped => "test-skipped",
TestPass => "test-pass",
Skipped => "skipped",
Error => "error",
}
});
from_into_string!(TestResult);
#[cfg(test)]
mod tests {
use crate::crates::*;
use std::collections::BTreeSet;
use std::str::FromStr;
#[test]
fn test_test_result_parsing() {
use super::{
FailureReason::*,
TestResult::{self, *},
};
macro_rules! btreeset {
($($x:expr),+ $(,)?) => (
vec![$($x),+].into_iter().collect::<BTreeSet<_>>()
);
}
macro_rules! test_from_str {
($($str:expr => $rust:expr,)*) => {
$(
// Test parsing from string to rust
assert_eq!(TestResult::from_str($str).unwrap(), $rust);
// Test dumping from rust to string
assert_eq!(&$rust.to_string(), $str);
// Test dumping from rust to string to rust
assert_eq!(TestResult::from_str($rust.to_string().as_ref()).unwrap(), $rust);
)*
};
}
//"build-fail:depends-on()" => BuildFail(DependsOn(vec!["001"])),
test_from_str! {
"build-fail:unknown" => BuildFail(Unknown),
"build-fail:compiler-error(001, 002)" => BuildFail(CompilerError(btreeset!["001".parse().unwrap(), "002".parse().unwrap()])),
"build-fail:compiler-error(001)" => BuildFail(CompilerError(btreeset!["001".parse().unwrap()])),
"build-fail:oom" => BuildFail(OOM),
"build-fail:ice" => BuildFail(ICE),
"test-fail:timeout" => TestFail(Timeout),
"test-pass" => TestPass,
"error" => Error,
"build-fail:depends-on(reg/clint/0.2.1)" => BuildFail(DependsOn(btreeset![Crate::Registry(RegistryCrate{name: "clint".to_string(), version: "0.2.1".to_string()})])),
}
// Backward compatibility
assert_eq!(
TestResult::from_str("build-fail").unwrap(),
BuildFail(Unknown)
);
assert!(TestResult::from_str("error:oom").is_err());
assert!(TestResult::from_str("build-fail:pleasedonotaddthis").is_err());
}
}