Skip to content

Commit 513486a

Browse files
author
Stephan Dilly
committed
cleanup tests
1 parent 1ca0449 commit 513486a

File tree

4 files changed

+52
-71
lines changed

4 files changed

+52
-71
lines changed

asyncgit/src/sync/diff.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ mod tests {
240240
use crate::sync::{
241241
stage_add_file,
242242
status::{get_status, StatusType},
243-
tests::{repo_init, repo_init_empty},
243+
tests::{get_statuses, repo_init, repo_init_empty},
244244
};
245245
use std::{
246246
fs::{self, File},
@@ -280,16 +280,19 @@ mod tests {
280280
let root = repo.path().parent().unwrap();
281281
let repo_path = root.as_os_str().to_str().unwrap();
282282

283-
let res = get_status(repo_path, StatusType::WorkingDir);
284-
assert_eq!(res.len(), 0);
283+
assert_eq!(get_statuses(repo_path), (0, 0));
285284

286285
File::create(&root.join(file_path))
287286
.unwrap()
288287
.write_all(b"test\nfoo")
289288
.unwrap();
290289

290+
assert_eq!(get_statuses(repo_path), (1, 0));
291+
291292
assert_eq!(stage_add_file(repo_path, file_path), true);
292293

294+
assert_eq!(get_statuses(repo_path), (0, 1));
295+
293296
let diff = get_diff(
294297
repo_path,
295298
String::from(file_path.to_str().unwrap()),
@@ -331,8 +334,7 @@ mod tests {
331334
let root = repo.path().parent().unwrap();
332335
let repo_path = root.as_os_str().to_str().unwrap();
333336

334-
let res = get_status(repo_path, StatusType::WorkingDir);
335-
assert_eq!(res.len(), 0);
337+
assert_eq!(get_statuses(repo_path), (0, 0));
336338

337339
let file_path = root.join("bar.txt");
338340

@@ -349,11 +351,7 @@ mod tests {
349351

350352
let res = stage_add_file(repo_path, Path::new("bar.txt"));
351353
assert_eq!(res, true);
352-
assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1);
353-
assert_eq!(
354-
get_status(repo_path, StatusType::WorkingDir).len(),
355-
0
356-
);
354+
assert_eq!(get_statuses(repo_path), (0, 1));
357355

358356
// overwrite with next content
359357
{
@@ -363,11 +361,7 @@ mod tests {
363361
.unwrap();
364362
}
365363

366-
assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1);
367-
assert_eq!(
368-
get_status(repo_path, StatusType::WorkingDir).len(),
369-
1
370-
);
364+
assert_eq!(get_statuses(repo_path), (1, 1));
371365

372366
let res = get_diff(repo_path, "bar.txt".to_string(), false);
373367

asyncgit/src/sync/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub use utils::{
1818

1919
#[cfg(test)]
2020
mod tests {
21+
use super::status::{get_status, StatusType};
2122
use git2::Repository;
2223
use std::process::Command;
2324
use tempfile::TempDir;
@@ -34,6 +35,7 @@ mod tests {
3435
(td, repo)
3536
}
3637

38+
///
3739
pub fn repo_init() -> (TempDir, Repository) {
3840
let td = TempDir::new().unwrap();
3941
let repo = Repository::init(td.path()).unwrap();
@@ -60,6 +62,14 @@ mod tests {
6062
(td, repo)
6163
}
6264

65+
/// helper returning amount of files with changes in the (wd,stage)
66+
pub fn get_statuses(repo_path: &str) -> (usize, usize) {
67+
(
68+
get_status(repo_path, StatusType::WorkingDir).len(),
69+
get_status(repo_path, StatusType::Stage).len(),
70+
)
71+
}
72+
6373
///
6474
pub fn debug_cmd_print(path: &str, cmd: &str) {
6575
eprintln!("\n----\n{}", debug_cmd(path, cmd))

asyncgit/src/sync/reset.rs

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ mod tests {
8383
};
8484
use crate::sync::{
8585
status::{get_status, StatusType},
86-
tests::{debug_cmd_print, repo_init, repo_init_empty},
86+
tests::{
87+
debug_cmd_print, get_statuses, repo_init, repo_init_empty,
88+
},
8789
utils::{commit, stage_add_all, stage_add_file},
8890
};
8991
use std::{
@@ -152,22 +154,14 @@ mod tests {
152154

153155
debug_cmd_print(repo_path, "git status");
154156

155-
assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1);
156-
assert_eq!(
157-
get_status(repo_path, StatusType::WorkingDir).len(),
158-
1
159-
);
157+
assert_eq!(get_statuses(repo_path), (1, 1));
160158

161159
let res = reset_workdir_file(repo_path, "bar.txt");
162160
assert_eq!(res, true);
163161

164162
debug_cmd_print(repo_path, "git status");
165163

166-
assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1);
167-
assert_eq!(
168-
get_status(repo_path, StatusType::WorkingDir).len(),
169-
0
170-
);
164+
assert_eq!(get_statuses(repo_path), (0, 1));
171165
}
172166

173167
#[test]
@@ -186,20 +180,14 @@ mod tests {
186180

187181
debug_cmd_print(repo_path, "git status");
188182

189-
assert_eq!(
190-
get_status(repo_path, StatusType::WorkingDir).len(),
191-
1
192-
);
183+
assert_eq!(get_statuses(repo_path), (1, 0));
193184

194185
let res = reset_workdir_file(repo_path, "foo/bar.txt");
195186
assert_eq!(res, true);
196187

197188
debug_cmd_print(repo_path, "git status");
198189

199-
assert_eq!(
200-
get_status(repo_path, StatusType::WorkingDir).len(),
201-
0
202-
);
190+
assert_eq!(get_statuses(repo_path), (0, 0));
203191
}
204192

205193
#[test]
@@ -233,21 +221,15 @@ mod tests {
233221
.write_all(b"file3\nadded line")?;
234222
}
235223

224+
assert_eq!(get_statuses(repo_path), (5, 0));
225+
236226
stage_add_file(repo_path, Path::new("foo/file5.txt"));
237227

238-
assert_eq!(
239-
get_status(repo_path, StatusType::WorkingDir).len(),
240-
4
241-
);
242-
assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1);
228+
assert_eq!(get_statuses(repo_path), (4, 1));
243229

244230
assert!(reset_workdir_folder(repo_path, "foo"));
245231

246-
assert_eq!(
247-
get_status(repo_path, StatusType::WorkingDir).len(),
248-
1
249-
);
250-
assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1);
232+
assert_eq!(get_statuses(repo_path), (1, 1));
251233

252234
Ok(())
253235
}
@@ -282,22 +264,14 @@ mod tests {
282264

283265
debug_cmd_print(repo_path, "git status");
284266

285-
assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1);
286-
assert_eq!(
287-
get_status(repo_path, StatusType::WorkingDir).len(),
288-
1
289-
);
267+
assert_eq!(get_statuses(repo_path), (1, 1));
290268

291269
let res = reset_workdir_file(repo_path, file);
292270
assert_eq!(res, true);
293271

294272
debug_cmd_print(repo_path, "git status");
295273

296-
assert_eq!(
297-
get_status(repo_path, StatusType::WorkingDir).len(),
298-
0
299-
);
300-
assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1);
274+
assert_eq!(get_statuses(repo_path), (0, 1));
301275
}
302276

303277
#[test]
@@ -312,8 +286,14 @@ mod tests {
312286
.write_all(b"test\nfoo")
313287
.unwrap();
314288

289+
assert_eq!(get_statuses(repo_path), (1, 0));
290+
315291
assert_eq!(stage_add_file(repo_path, file_path), true);
316292

293+
assert_eq!(get_statuses(repo_path), (0, 1));
294+
317295
assert_eq!(reset_stage(repo_path, file_path), true);
296+
297+
assert_eq!(get_statuses(repo_path), (1, 0));
318298
}
319299
}

asyncgit/src/sync/utils.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ mod tests {
118118
use super::*;
119119
use crate::sync::{
120120
status::{get_status, StatusType},
121-
tests::{repo_init, repo_init_empty},
121+
tests::{get_statuses, repo_init, repo_init_empty},
122122
};
123123
use std::{
124124
fs::{self, remove_file, File},
@@ -133,26 +133,20 @@ mod tests {
133133
let root = repo.path().parent().unwrap();
134134
let repo_path = root.as_os_str().to_str().unwrap();
135135

136-
let status_count = |s: StatusType| -> usize {
137-
get_status(repo_path, s).len()
138-
};
139-
140136
File::create(&root.join(file_path))
141137
.unwrap()
142138
.write_all(b"test\nfoo")
143139
.unwrap();
144140

145-
assert_eq!(status_count(StatusType::WorkingDir), 1);
141+
assert_eq!(get_statuses(repo_path), (1, 0));
146142

147143
assert_eq!(stage_add_file(repo_path, file_path), true);
148144

149-
assert_eq!(status_count(StatusType::WorkingDir), 0);
150-
assert_eq!(status_count(StatusType::Stage), 1);
145+
assert_eq!(get_statuses(repo_path), (0, 1));
151146

152147
commit(repo_path, "commit msg");
153148

154-
assert_eq!(status_count(StatusType::Stage), 0);
155-
assert_eq!(status_count(StatusType::WorkingDir), 0);
149+
assert_eq!(get_statuses(repo_path), (0, 0));
156150
}
157151

158152
#[test]
@@ -162,14 +156,22 @@ mod tests {
162156
let root = repo.path().parent().unwrap();
163157
let repo_path = root.as_os_str().to_str().unwrap();
164158

159+
assert_eq!(get_statuses(repo_path), (0, 0));
160+
165161
File::create(&root.join(file_path))
166162
.unwrap()
167163
.write_all(b"test\nfoo")
168164
.unwrap();
169165

166+
assert_eq!(get_statuses(repo_path), (1, 0));
167+
170168
assert_eq!(stage_add_file(repo_path, file_path), true);
171169

170+
assert_eq!(get_statuses(repo_path), (0, 1));
171+
172172
commit(repo_path, "commit msg");
173+
174+
assert_eq!(get_statuses(repo_path), (0, 0));
173175
}
174176

175177
#[test]
@@ -189,10 +191,6 @@ mod tests {
189191
let root = repo.path().parent().unwrap();
190192
let repo_path = root.as_os_str().to_str().unwrap();
191193

192-
let status_count = |s: StatusType| -> usize {
193-
get_status(repo_path, s).len()
194-
};
195-
196194
File::create(&root.join(file_path))
197195
.unwrap()
198196
.write_all(b"test file1 content")
@@ -203,12 +201,11 @@ mod tests {
203201
.write_all(b"test file2 content")
204202
.unwrap();
205203

206-
assert_eq!(status_count(StatusType::WorkingDir), 2);
204+
assert_eq!(get_statuses(repo_path), (2, 0));
207205

208206
assert_eq!(stage_add_file(repo_path, file_path), true);
209207

210-
assert_eq!(status_count(StatusType::WorkingDir), 1);
211-
assert_eq!(status_count(StatusType::Stage), 1);
208+
assert_eq!(get_statuses(repo_path), (1, 1));
212209
}
213210

214211
#[test]

0 commit comments

Comments
 (0)