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
use std::path::Path;
use pretty_assertions::assert_eq;
use super::{file_patch, FileContents};
// Unix paths: on Windows `/work/...` is not absolute and Git-style `\`
// separators need quoting, so no patch is offered there.
#[cfg(unix)]
#[test]
fn patches_follow_git_layout_with_absolute_paths() {
let cases = [
(
"modified file",
"/work/src/lib.rs",
FileContents::Modified {
old: "fn a() {}\nfn b() {}\n",
new: "fn a() {}\nfn c() {}\n",
},
Some(
"diff --git /work/src/lib.rs /work/src/lib.rs\n\
--- /work/src/lib.rs\n\
+++ /work/src/lib.rs\n\
@@ -1,2 +1,2 @@\n \
fn a() {}\n\
-fn b() {}\n\
+fn c() {}\n",
),
),
(
"created file",
"/work/notes.md",
FileContents::Created("hello\n"),
Some(
"diff --git /work/notes.md /work/notes.md\n\
new file mode 100644\n\
--- /dev/null\n\
+++ /work/notes.md\n\
@@ -0,0 +1 @@\n\
+hello\n",
),
),
(
"created empty file",
"/work/empty",
FileContents::Created(""),
Some("diff --git /work/empty /work/empty\nnew file mode 100644\n"),
),
(
"deleted file",
"/work/old.txt",
FileContents::Deleted("bye\n"),
Some(
"diff --git /work/old.txt /work/old.txt\n\
deleted file mode 100644\n\
--- /work/old.txt\n\
+++ /dev/null\n\
@@ -1 +0,0 @@\n\
-bye\n",
),
),
(
"moved and edited file",
"/work/new.txt",
FileContents::Moved {
from: Path::new("/work/old.txt"),
old: "a\nb\n",
new: "a\nB\n",
},
Some(
"diff --git /work/old.txt /work/new.txt\n\
rename from /work/old.txt\n\
rename to /work/new.txt\n\
--- /work/old.txt\n\
+++ /work/new.txt\n\
@@ -1,2 +1,2 @@\n \
a\n\
-b\n\
+B\n",
),
),
(
"moved file",
"/work/new.txt",
FileContents::Moved {
from: Path::new("/work/old.txt"),
old: "a\n",
new: "a\n",
},
Some(
"diff --git /work/old.txt /work/new.txt\n\
rename from /work/old.txt\n\
rename to /work/new.txt\n",
),
),
(
"move from a path Git would quote",
"/work/new.txt",
FileContents::Moved {
from: Path::new("/work/\"q\".txt"),
old: "a\n",
new: "a\n",
},
None,
),
(
"unchanged file",
"/work/same",
FileContents::Modified {
old: "x\n",
new: "x\n",
},
None,
),
(
"relative path",
"src/lib.rs",
FileContents::Modified {
old: "a\n",
new: "b\n",
},
None,
),
(
"path Git would quote",
"/work/\"q\".txt",
FileContents::Modified {
old: "a\n",
new: "b\n",
},
None,
),
];
for (label, path, contents, expected) in cases {
assert_eq!(
file_patch(Path::new(path), contents).as_deref(),
expected,
"{label}"
);
}
}