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
use std::path::Path;
use std::sync::{Arc, Mutex, Once};
use std::time::Duration;
use lazy_static::lazy_static;
use rusqlite::{params, Connection};
pub struct CaseIdentity<'a> {
file: &'a str,
name: &'a str,
}
impl<'a> CaseIdentity<'a> {
pub fn new(file: &'a str, name: &'a str) -> Self {
Self { file, name }
}
}
pub struct Cataloger {
version: String,
connection: Connection,
}
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
#[derive(Debug)]
pub struct Case {
version: String,
file: String,
line_start: usize,
line_end: usize,
name: String,
description: String,
since: String,
compatible_version: String,
authors: String,
created_at: u64,
last_commit_id: String,
last_committer: String,
last_committed_at: u64,
elapsed: Option<u64>,
}
fn prettify_duration(duration: &std::time::Duration) -> String {
let secs = duration.as_secs();
if secs > 0 {
return format!("{:.2} s", duration.as_secs_f64());
}
let millis = duration.as_millis();
if millis > 0 {
return format!("{:.2} ms", duration.as_secs_f64() * 1000.);
}
let micros = duration.as_micros();
if micros > 0 {
return format!("{:.2} ms", duration.as_secs_f64() * 1000_000.);
}
format!("{} ns", duration.as_nanos())
}
impl Case {
const FIELDS: [&'static str; 14] = [
"version",
"file",
"line_start",
"line_end",
"name",
"description",
"since",
"compatible_version",
"authors",
"created_at",
"last_commit_id",
"last_committer",
"last_committed_at",
"elapsed",
];
pub const fn field_names() -> [&'static str; 14] {
Self::FIELDS
}
pub fn into_printable_fields(self) -> Vec<String> {
use chrono::TimeZone;
assert_eq!(
chrono::Local.from_utc_datetime(&chrono::NaiveDateTime::from_timestamp(
self.created_at as _,
0,
)),
chrono::Local.timestamp(self.created_at as _, 0)
);
vec![
self.version,
self.file,
self.line_start.to_string(),
self.line_end.to_string(),
self.name,
self.description,
self.since,
self.compatible_version,
self.authors,
chrono::Local
.timestamp(self.created_at as _, 0)
.to_rfc3339(),
self.last_commit_id[0..7].to_string(),
self.last_committer,
chrono::Local
.timestamp(self.last_committed_at as _, 0)
.to_rfc3339(),
self.elapsed
.map(|elapsed| prettify_duration(&Duration::from_nanos(elapsed)))
.unwrap_or_default(),
]
}
}
impl Cataloger {
pub fn open<P: AsRef<Path>>(path: P) -> Result<Cataloger> {
let version = match std::env::var("CARGO_PKG_VERSION") {
Ok(version) => version.to_string(),
Err(_) => {
let project_root =
project_root::get_project_root().expect("can not find project root");
let manifest = project_root.join("Cargo.toml");
let manifest = cargo_toml::Manifest::from_path(&manifest)?;
let version = manifest.package.map(|p| p.version).unwrap_or_default();
version
}
};
Connection::open(path)
.map(|connection| {
let cataloger = Cataloger {
version,
connection,
};
cataloger.assert_schema().unwrap();
cataloger
})
.map_err(|err| err.into())
}
pub fn open_root() -> Result<Self> {
let project_root = project_root::get_project_root()?;
let path = project_root.join("target").join("test-catalog");
let file = path.join("catalog.db");
let cataloger = Cataloger::open(file)?;
Ok(cataloger)
}
pub fn latest_cases(&self) -> Result<Vec<Case>> {
let mut stmt = self
.connection
.prepare("select * from catalog where version = ?")?;
let mut rows = stmt.query([&self.version])?;
let mut cases = Vec::new();
while let Some(row) = rows.next()? {
let case = Case {
version: row.get(0)?,
file: row.get(1)?,
line_start: row.get(2)?,
line_end: row.get(3)?,
name: row.get(4)?,
description: row.get(5)?,
since: row.get(6)?,
compatible_version: row.get(7)?,
authors: row.get(8)?,
created_at: row.get(9)?,
last_commit_id: row.get(10)?,
last_committer: row.get(11)?,
last_committed_at: row.get(12)?,
elapsed: row.get(13).unwrap_or_default(),
};
cases.push(case);
}
Ok(cases)
}
fn assert_schema(&self) -> Result<()> {
self.connection.execute_batch(
"
BEGIN;
CREATE TABLE IF NOT EXISTS catalog(
version, file, line_start, line_end, name, description, since, compatible_version,
authors, created_at, last_commit_id, last_committer, last_committed_at, elapsed);
CREATE UNIQUE INDEX IF NOT EXISTS idx_version_file_name ON catalog(
version, file, name);
COMMIT;",
)?;
Ok(())
}
pub fn post_test<'a>(
&self,
case: &CaseIdentity<'a>,
duration: &std::time::Duration,
) -> Result<()> {
self.connection.execute(
"update catalog set elapsed = ? where version = ? and file = ? and name = ?",
params![
duration.as_nanos() as u64,
self.version,
case.file,
case.name
],
)?;
Ok(())
}
}
lazy_static! {
static ref CATALOGER: Arc<Mutex<Option<Cataloger>>> = Arc::new(Mutex::new(None));
}
static INIT: Once = Once::new();
pub fn init() {
INIT.call_once(|| {
let project_root = project_root::get_project_root().expect("can not find project root");
let path = project_root.join("target").join("test-catalog");
std::fs::create_dir_all(&path).expect("cannot create dir for tests catalog");
let file = path.join("catalog.db");
let cataloger = Cataloger::open(file).expect("cannot open");
let mut guard = CATALOGER.lock().unwrap();
*guard = Some(cataloger);
});
}
pub fn catalogue(
file: &str,
name: &str,
line_start: usize,
line_end: usize,
description: &str,
since: &str,
compatible_version: &str,
) {
let project_root = project_root::get_project_root().expect("can not find project root");
let repo = git2::Repository::open(project_root).unwrap();
let mut blame_options = git2::BlameOptions::default();
blame_options
.min_line(line_start)
.max_line(line_end)
.track_copies_any_commit_copies(true);
let version = std::env::var("CARGO_PKG_VERSION").unwrap().to_string();
let blame = repo
.blame_file(Path::new(file), Some(&mut blame_options))
.expect("blame");
if let (Some(created_at), authors, last_commit_id, last_committer, Some(last_committed_at)) =
blame
.iter()
.map(|chunk| {
let last_commit_id = chunk.final_commit_id().to_string();
let last_committer = chunk.final_signature().to_string();
let orig_committer = chunk.orig_signature().to_string();
let created_at = chunk.orig_signature().when().seconds();
let last_committed_at = chunk.final_signature().when().seconds();
(
created_at,
orig_committer,
last_commit_id,
last_committer,
last_committed_at,
)
})
.fold(
(None, vec![], String::new(), "".to_string(), None),
|mut acc, item| {
if acc.0.is_none() {
acc.0 = Some(item.0);
} else if acc.0.unwrap() > item.0 {
acc.0 = Some(item.0);
}
if !acc.1.contains(&item.1) {
acc.1.push(item.1);
}
if acc.4.is_none() || acc.0.unwrap() < item.4 {
acc.4 = Some(item.4);
acc.2 = item.2;
acc.3 = item.3;
}
acc
},
)
{
let guard = CATALOGER.lock().unwrap();
let cater = guard.as_ref().unwrap();
cater.connection.execute("
INSERT INTO catalog (
version, file, line_start, line_end, name, description, since, compatible_version, authors, created_at, last_commit_id, last_committer, last_committed_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(version, file, name) DO UPDATE SET
line_start = excluded.line_start,
line_end = excluded.line_end,
description = excluded.description,
authors = excluded.authors,
created_at = excluded.created_at,
since = excluded.since,
last_commit_id = excluded.last_commit_id,
last_committer = excluded.last_committer,
last_committed_at = excluded.last_committed_at",
params![version, file, line_start, line_end, name, description, since, compatible_version, authors.join(","),
created_at, last_commit_id, last_committer, last_committed_at]).unwrap();
}
}
pub fn pre<'a>(_case: &CaseIdentity<'a>) {
init();
}
pub fn post<'a>(case: &CaseIdentity<'a>, duration: &std::time::Duration) {
let guard = CATALOGER.lock().unwrap();
let cater = guard.as_ref().unwrap();
cater.post_test(case, duration).unwrap();
}