-
-
Notifications
You must be signed in to change notification settings - Fork 262
/
convert_to_worktree.rs
102 lines (97 loc) · 2.88 KB
/
convert_to_worktree.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
use bstr::ByteSlice;
use gix_filter::{
eol,
eol::{AttributesDigest, Configuration, Mode},
};
#[test]
fn no_conversion_if_attribute_digest_does_not_allow_it() -> crate::Result {
let mut buf = Vec::new();
for digest in [
AttributesDigest::Binary,
AttributesDigest::TextInput,
AttributesDigest::TextAutoInput,
] {
let changed = eol::convert_to_worktree(b"hi\nho", digest, &mut buf, Default::default())?;
assert!(!changed, "the digest doesn't allow for CRLF changes");
}
Ok(())
}
#[test]
fn no_conversion_if_configuration_does_not_allow_it() -> crate::Result {
let mut buf = Vec::new();
for digest in [AttributesDigest::Text, AttributesDigest::TextAuto] {
for config in [
Configuration {
auto_crlf: eol::AutoCrlf::Input,
eol: Some(Mode::CrLf),
},
Configuration {
auto_crlf: eol::AutoCrlf::Disabled,
eol: Some(Mode::Lf),
},
] {
let changed = eol::convert_to_worktree(b"hi\nho", digest, &mut buf, config)?;
assert!(!changed, "the configuration doesn't allow for changes");
}
}
Ok(())
}
#[test]
fn no_conversion_if_nothing_to_do() -> crate::Result {
let mut buf = Vec::new();
for (input, digest, msg) in [
(
&b"hi\r\nho"[..],
AttributesDigest::TextCrlf,
"no lone line feed to handle",
),
(
&b"binary\0linefeed\nho"[..],
AttributesDigest::TextAutoCrlf,
"binary in auto-mode is never handled",
),
(
&b"binary\nlinefeed\r\nho"[..],
AttributesDigest::TextAutoCrlf,
"mixed crlf and lf is avoided",
),
(
&b"eligible-but-disabled\nhere"[..],
AttributesDigest::Binary,
"designated binary is never handled",
),
] {
let changed = eol::convert_to_worktree(input, digest, &mut buf, Default::default())?;
assert!(!changed, "{msg}");
}
Ok(())
}
#[test]
fn each_nl_is_replaced_with_crnl() -> crate::Result {
let mut buf = Vec::new();
let changed = eol::convert_to_worktree(
b"hi\n\nho\nend",
AttributesDigest::TextCrlf,
&mut buf,
Default::default(),
)?;
assert!(
changed,
"the buffer has to be changed as it is explicitly demanded and has newlines to convert"
);
assert_eq!(buf.as_bstr(), "hi\r\n\r\nho\r\nend");
Ok(())
}
#[test]
fn existing_crnl_are_not_replaced_for_safety_nor_are_lone_cr() -> crate::Result {
let mut buf = Vec::new();
let changed = eol::convert_to_worktree(
b"hi\r\n\nho\r\nend\r",
AttributesDigest::TextCrlf,
&mut buf,
Default::default(),
)?;
assert!(changed);
assert_eq!(buf.as_bstr(), "hi\r\n\r\nho\r\nend\r");
Ok(())
}