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
use impl_prelude::*;
#[derive(Debug, Default, Clone, Copy)]
pub struct RejectSeparateRoot;
impl RejectSeparateRoot {
pub fn new() -> Self {
RejectSeparateRoot {}
}
}
impl Check for RejectSeparateRoot {
fn name(&self) -> &str {
"reject-separate-root"
}
fn check(&self, _: &CheckGitContext, commit: &Commit) -> Result<CheckResult> {
let mut result = CheckResult::new();
if commit.parents.is_empty() {
result.add_error(format!("commit {} not allowed; it is a root commit.", commit.sha1));
}
Ok(result)
}
}
#[cfg(test)]
mod tests {
use checks::RejectSeparateRoot;
use checks::test::*;
static NO_ROOT_TOPIC: &'static str = "ba3dc3cb09a558c88282742413a2dccb17d444fc";
static WITH_ROOT_TOPIC: &'static str = "ff560e8798ef7a9d10bf43660695f7155b49b398";
#[test]
fn test_reject_separate_root_no_root() {
let check = RejectSeparateRoot::new();
let mut conf = GitCheckConfiguration::new();
conf.add_check(&check);
let result = test_check("test_reject_separate_root_no_root", NO_ROOT_TOPIC, &conf);
assert_eq!(result.warnings().len(), 0);
assert_eq!(result.alerts().len(), 0);
assert_eq!(result.errors().len(), 0);
assert_eq!(result.temporary(), false);
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), true);
}
#[test]
fn test_reject_separate_root_with_root() {
let check = RejectSeparateRoot::new();
let mut conf = GitCheckConfiguration::new();
conf.add_check(&check);
let result = test_check("test_reject_separate_root_with_root",
WITH_ROOT_TOPIC,
&conf);
assert_eq!(result.warnings().len(), 0);
assert_eq!(result.alerts().len(), 0);
assert_eq!(result.errors().len(), 1);
assert_eq!(result.errors()[0],
"commit ff560e8798ef7a9d10bf43660695f7155b49b398 not allowed; it is a root \
commit.");
assert_eq!(result.temporary(), false);
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), false);
}
}