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
|
use std::path::PathBuf;
use testdir::testdir;
use tokio::fs;
use tokio::sync::OnceCell;
use crate::fs_utils;
use crate::git;
#[tokio::test]
async fn test_fs_utils_create_dir_allow_existing() {
let dir = testdir!();
let foo = dir.join("foo");
assert!(fs_utils::create_dir_allow_existing(&foo).await.is_ok());
assert!(fs::try_exists(&foo).await.unwrap());
assert!(fs_utils::create_dir_allow_existing(&foo).await.is_ok());
}
#[tokio::test]
async fn test_fs_utils_create_dir_allow_existing_file() {
let dir = testdir!();
let foo = dir.join("foo");
assert!(fs::write(&foo, "hello").await.is_ok());
assert!(fs_utils::create_dir_allow_existing(&foo).await.is_err());
}
#[tokio::test]
async fn test_fs_utils_remove_file_allow_not_found() {
let dir = testdir!();
let foo = dir.join("foo");
assert!(fs_utils::remove_file_allow_not_found(&foo).await.is_ok());
assert!(fs::write(&foo, "hello").await.is_ok());
assert!(fs_utils::remove_file_allow_not_found(&foo).await.is_ok());
assert!(!fs::try_exists(&foo).await.unwrap());
}
#[tokio::test]
async fn test_fs_utils_symlink_update_existing() {
let dir = testdir!();
let foo = dir.join("foo");
let bar = dir.join("bar");
let fum = dir.join("fum");
assert!(fs::write(&foo, "hello").await.is_ok());
assert!(fs_utils::symlink_update_existing(&foo, &bar).await.is_ok());
assert!(fs_utils::symlink_update_existing(&foo, &bar).await.is_ok());
assert_eq!(fs::read_link(&bar).await.unwrap(), foo);
assert!(fs_utils::symlink_update_existing(&fum, &bar).await.is_ok());
assert_eq!(fs::read_link(&bar).await.unwrap(), fum);
}
static BARE: OnceCell<git::Repository> = OnceCell::const_new();
async fn git_setup(bare: bool) -> git::Repository {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("src/testdata")
.join(if bare { "bare" } else { "work" });
let repo = git::Repository::new(
path.clone(),
bare,
None::<String>,
None::<String>,
None::<PathBuf>,
);
assert_eq!(repo.remote(), None);
assert_eq!(repo.project_id(), None);
assert_eq!(repo.path(), path);
assert_eq!(repo.socket(), None);
assert_eq!(repo.is_bare(), bare);
repo.setup().await.unwrap();
repo
}
async fn git_config_get(repo: &git::Repository) {
assert_eq!(repo.config_get("user.name").await.unwrap(), "John Smith");
assert_eq!(
repo.config_get("user.email").await.unwrap(),
"jsmith@example.org"
);
assert_eq!(repo.config_get("eyeballs.foo").await.unwrap(), "bar");
assert_eq!(repo.config_get("eyeballs.bar").await.unwrap(), "");
}
async fn git_is_ancestor(repo: &git::Repository) {
assert_eq!(
repo.is_ancestor(
"807fffaad7b35683162669a485ce3e995d56170d",
"d7c502b9c6b833060576a0c4da0287933d603011",
)
.await
.unwrap(),
true,
);
assert_eq!(
repo.is_ancestor(
"d7c502b9c6b833060576a0c4da0287933d603011",
"807fffaad7b35683162669a485ce3e995d56170d",
)
.await
.unwrap(),
false,
);
assert_eq!(
repo.is_ancestor(
"807fffaad7b35683162669a485ce3e995d56170d",
"807fffaad7b35683162669a485ce3e995d56170d",
)
.await
.unwrap(),
true,
);
assert_eq!(
repo.is_ancestor(
"2d05d489d42d4f36dd0ebf52502f243991e010eb",
"2cecdec660a30bf3964cee645d9cee03640ef8dc",
)
.await
.unwrap(),
false,
);
assert!(repo
.is_ancestor(
"1234567890123456789012345678901234567890",
"2cecdec660a30bf3964cee645d9cee03640ef8dc",
)
.await
.is_err(),);
assert!(repo.is_ancestor("<invalid>", "'\"",).await.is_err(),);
}
async fn git_is_equal_content(repo: &git::Repository) {
assert_eq!(
repo.is_equal_content(
"d7c502b9c6b833060576a0c4da0287933d603011",
"d7c502b9c6b833060576a0c4da0287933d603011",
)
.await
.unwrap(),
true,
);
assert_eq!(
repo.is_equal_content(
"d7c502b9c6b833060576a0c4da0287933d603011",
"2d05d489d42d4f36dd0ebf52502f243991e010eb",
)
.await
.unwrap(),
true,
);
assert_eq!(
repo.is_equal_content(
"d7c502b9c6b833060576a0c4da0287933d603011",
"2cecdec660a30bf3964cee645d9cee03640ef8dc",
)
.await
.unwrap(),
false,
);
assert!(repo
.is_equal_content(
"1234567890123456789012345678901234567890",
"2cecdec660a30bf3964cee645d9cee03640ef8dc",
)
.await
.is_err(),);
assert!(repo.is_equal_content("<invalid>", "'\"",).await.is_err(),);
}
async fn git_get_author_commiter(repo: &git::Repository) {
let a1 = repo
.get_author("d7c502b9c6b833060576a0c4da0287933d603011")
.await
.unwrap();
assert_eq!(a1.name, "John Smith");
assert_eq!(a1.email, "jsmith@example.org");
assert_eq!(a1.username, "jsmith");
let c1 = repo
.get_commiter("d7c502b9c6b833060576a0c4da0287933d603011")
.await
.unwrap();
assert_eq!(c1.name, a1.name);
assert_eq!(c1.email, a1.email);
assert_eq!(c1.username, a1.username);
let a2 = repo
.get_author("2cecdec660a30bf3964cee645d9cee03640ef8dc")
.await
.unwrap();
assert_eq!(a2.name, "Test");
assert_eq!(a2.email, "testing@example.org");
assert_eq!(a2.username, "testing");
let c2 = repo
.get_commiter("d7c502b9c6b833060576a0c4da0287933d603011")
.await
.unwrap();
assert_eq!(c2.name, c1.name);
assert_eq!(c2.email, c1.email);
assert_eq!(c2.username, c1.username);
assert!(repo
.get_author("1234567890123456789012345678901234567890")
.await
.is_err());
assert!(repo.get_author("<invalid>").await.is_err());
}
async fn git_fetch(bare: bool) -> git::Repository {
let path = testdir!().join("repo");
let remote_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/testdata/bare");
let remote = remote_path.to_string_lossy().into_owned();
let repo = git::Repository::new(
path,
bare,
Some(remote.as_str()),
None::<String>,
None::<PathBuf>,
);
assert_eq!(repo.remote(), Some(remote.as_str()));
repo.setup().await.unwrap();
if bare {
let (main, branch, other) = tokio::join!(
repo.fetch("main"),
repo.fetch("branch"),
repo.fetch("other"),
);
main.unwrap();
branch.unwrap();
other.unwrap();
} else {
// Not bare repo will complain when you try to fetch into the currently checked
// out branch. So just try fetching other branches.
let (branch, other) = tokio::join!(repo.fetch("branch"), repo.fetch("other"));
branch.unwrap();
other.unwrap();
}
repo
}
#[tokio::test]
async fn test_git_bare_config_get() {
let repo = BARE.get_or_init(|| git_setup(true)).await;
git_config_get(repo).await;
}
#[tokio::test]
async fn test_git_bare_is_ancestor() {
let repo = BARE.get_or_init(|| git_setup(true)).await;
git_is_ancestor(repo).await;
}
#[tokio::test]
async fn test_git_bare_is_equal_content() {
let repo = BARE.get_or_init(|| git_setup(true)).await;
git_is_equal_content(repo).await;
}
#[tokio::test]
async fn test_git_bare_get_author_commiter() {
let repo = BARE.get_or_init(|| git_setup(true)).await;
git_get_author_commiter(repo).await;
}
#[tokio::test]
async fn test_git_fetch() {
git_fetch(false).await;
}
#[tokio::test]
async fn test_git_bare_fetch() {
git_fetch(true).await;
}
#[tokio::test]
async fn test_git_delete_branch() {
// Using git_fetch as we need a writeable git repo
let repo = git_fetch(false).await;
assert!(repo.delete_branch("other").await.is_ok());
assert!(repo.delete_branch("does-not-exist").await.is_err());
}
#[tokio::test]
async fn test_git_bare_delete_branch() {
// Using git_fetch as we need a writeable git repo
let repo = git_fetch(true).await;
assert!(repo.delete_branch("other").await.is_ok());
assert!(repo.delete_branch("does-not-exist").await.is_err());
}
|