/
mod.rs
169 lines (154 loc) · 4.99 KB
/
mod.rs
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
use bstr::ByteSlice;
use git_refspec::Operation;
use git_testtools::scripted_fixture_repo_read_only;
use std::panic::catch_unwind;
#[test]
#[should_panic]
fn baseline() {
let dir = scripted_fixture_repo_read_only("make_baseline.sh").unwrap();
let baseline = std::fs::read(dir.join("baseline.git")).unwrap();
let mut lines = baseline.lines();
let mut panics = 0;
let mut mismatch = 0;
let mut count = 0;
while let Some(kind_spec) = lines.next() {
count += 1;
let (kind, spec) = kind_spec.split_at(kind_spec.find_byte(b' ').expect("space between kind and spec"));
let err_code: usize = lines
.next()
.expect("err code")
.to_str()
.unwrap()
.parse()
.expect("number");
let op = match kind {
b"fetch" => Operation::Fetch,
b"push" => Operation::Push,
_ => unreachable!("{} unexpected", kind.as_bstr()),
};
let res = catch_unwind(|| try_parse(spec.to_str().unwrap(), op));
match res {
Ok(res) => match (res.is_ok(), err_code == 0) {
(true, true) | (false, false) => {}
_ => {
eprintln!("{res:?} {err_code}");
mismatch += 1;
}
},
Err(_) => {
panics += 1;
}
}
}
if panics != 0 || mismatch != 0 {
panic!(
"Out of {} baseline entries, got {} right, ({} mismatches and {} panics)",
count,
count - (mismatch + panics),
mismatch,
panics
);
}
}
mod invalid {
use crate::parse::try_parse;
use git_refspec::{parse::Error, Operation};
#[test]
fn empty() {
assert!(matches!(try_parse("", Operation::Fetch).unwrap_err(), Error::Empty));
assert!(matches!(try_parse("", Operation::Push).unwrap_err(), Error::Empty));
}
#[test]
fn negative_with_destination() {
for op in [Operation::Fetch, Operation::Push] {
for spec in ["^a:b", "^a:", "^:", "^:b"] {
assert!(matches!(
try_parse(spec, op).unwrap_err(),
Error::NegativeWithDestination
));
}
}
}
mod fetch {
use crate::parse::{assert_parse, b};
use git_refspec::{Fetch, Instruction};
#[test]
fn empty_lhs_colon_rhs_fetches_head_to_destination() {
assert_parse(
":a",
Instruction::Fetch(Fetch::AndUpdateSingle {
src: b("HEAD"),
dst: b("a"),
allow_non_fast_forward: false,
}),
);
assert_parse(
"+:a",
Instruction::Fetch(Fetch::AndUpdateSingle {
src: b("HEAD"),
dst: b("a"),
allow_non_fast_forward: true,
}),
);
}
#[test]
fn colon_alone_is_for_fetching_into_fetchhead() {
assert_parse(
":",
Instruction::Fetch(Fetch::AllMatchingBranches {
allow_non_fast_forward: false,
}),
);
assert_parse(
"+:",
Instruction::Fetch(Fetch::AllMatchingBranches {
allow_non_fast_forward: true,
}),
);
}
}
mod push {
use crate::parse::{assert_parse, b};
use git_refspec::{Instruction, Mode, Push};
#[test]
fn colon_alone_is_for_pushing_matching_refs() {
assert_parse(
":",
Instruction::Push(Push::AllMatchingBranches {
allow_non_fast_forward: false,
}),
);
assert_parse(
"+:",
Instruction::Push(Push::AllMatchingBranches {
allow_non_fast_forward: true,
}),
);
}
#[test]
fn delete() {
assert_parse(":a", Instruction::Push(Push::Delete { ref_or_pattern: b("a") }));
let spec = assert_parse("+:a", Instruction::Push(Push::Delete { ref_or_pattern: b("a") }));
assert_eq!(
spec.mode(),
Mode::Force,
"force is set, even though it has no effect in the actual instruction"
);
}
}
}
mod util {
use git_refspec::{Instruction, Operation, RefSpecRef};
pub fn b(input: &str) -> &bstr::BStr {
input.into()
}
pub fn try_parse(spec: &str, op: Operation) -> Result<RefSpecRef<'_>, git_refspec::parse::Error> {
git_refspec::parse(spec.into(), op)
}
pub fn assert_parse<'a>(spec: &'a str, expected: Instruction<'_>) -> RefSpecRef<'a> {
let spec = try_parse(spec, expected.operation()).expect("no error");
assert_eq!(spec.instruction(), expected);
spec
}
}
pub use util::*;