-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathgit_tests.rs
More file actions
87 lines (71 loc) · 2.61 KB
/
git_tests.rs
File metadata and controls
87 lines (71 loc) · 2.61 KB
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
use std::path::Path;
use command::r#async::Command;
use command::Stdio;
use tempfile::TempDir;
use super::{detect_current_branch, detect_current_branch_display};
/// Helper: run a git command inside the given repo directory.
async fn git(repo: &Path, args: &[&str]) -> String {
let output = Command::new(warp_util::wsl::git_binary())
.args(args)
.current_dir(repo)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await
.expect("failed to run git");
String::from_utf8_lossy(&output.stdout).trim().to_owned()
}
/// Creates a temp git repo with one commit and returns `(dir_handle, repo_path)`.
async fn init_repo() -> (TempDir, std::path::PathBuf) {
let dir = tempfile::tempdir().expect("failed to create temp dir");
let path = dir.path().to_path_buf();
git(&path, &["init", "-b", "main"]).await;
git(&path, &["config", "user.email", "test@test.com"]).await;
git(&path, &["config", "user.name", "Test"]).await;
git(&path, &["commit", "--allow-empty", "-m", "initial"]).await;
(dir, path)
}
#[tokio::test]
async fn on_normal_branch_returns_branch_name() {
let (_dir, repo) = init_repo().await;
git(&repo, &["checkout", "-b", "feature-xyz"]).await;
assert_eq!(detect_current_branch(&repo).await.unwrap(), "feature-xyz");
assert_eq!(
detect_current_branch_display(&repo).await.unwrap(),
"feature-xyz"
);
}
#[tokio::test]
async fn detached_head_raw_returns_head() {
let (_dir, repo) = init_repo().await;
git(&repo, &["checkout", "--detach", "HEAD"]).await;
assert_eq!(detect_current_branch(&repo).await.unwrap(), "HEAD");
}
#[tokio::test]
async fn detached_head_display_returns_short_sha() {
let (_dir, repo) = init_repo().await;
let full_sha = git(&repo, &["rev-parse", "HEAD"]).await;
git(&repo, &["checkout", "--detach", "HEAD"]).await;
let result = detect_current_branch_display(&repo).await.unwrap();
assert_ne!(
result, "HEAD",
"display variant should not return literal HEAD"
);
assert!(
full_sha.starts_with(&result),
"expected {full_sha} to start with {result}"
);
}
#[tokio::test]
async fn detached_tag_display_returns_short_sha() {
let (_dir, repo) = init_repo().await;
git(&repo, &["tag", "v1.0"]).await;
git(&repo, &["checkout", "v1.0"]).await;
let full_sha = git(&repo, &["rev-parse", "HEAD"]).await;
let result = detect_current_branch_display(&repo).await.unwrap();
assert_ne!(result, "HEAD");
assert!(
full_sha.starts_with(&result),
"expected {full_sha} to start with {result}"
);
}