Skip to content

Commit fc61c0d

Browse files
committed
Simplify state tests (#382)
1 parent 9679d6b commit fc61c0d

File tree

9 files changed

+41
-89
lines changed

9 files changed

+41
-89
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:bffbfdc9de4af0f685e29dfa883e093ba9240a96f42ed96fe73614a323b0a9b0
3+
size 10888
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:116d62faab198c39a3d11248a17f8efe1d3f78d879905fe6b5570587edc01b2c
3+
size 11576
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:4b76a63be1b90ff164633c074caf3ce3ccfd73ed6f587a7a02e7c039af065519
3+
size 10440

git-repository/tests/fixtures/make_cherry_pick_repo.sh

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,18 @@ set -eu -o pipefail
33

44
git init -q
55

6-
git config commit.gpgsign false
6+
touch file
7+
git add file
8+
git commit -m first file
79

8-
git config advice.statusHints false
9-
git config advice.resolveConflict false
10-
git config advice.commitBeforeMerge false
11-
git config advice.skippedCherryPicks false
12-
13-
git config init.defaultBranch master
14-
15-
unset GIT_AUTHOR_DATE
16-
unset GIT_COMMITTER_DATE
17-
18-
touch 1
19-
git add 1
20-
git commit -m 1 1
2110
git checkout -b other-branch
22-
echo other-branch > 1
23-
git add 1
24-
git commit -m 1.other 1
25-
git checkout master
26-
echo master > 1
27-
git add 1
28-
git commit -m 1.master 1
11+
echo other-branch > file
12+
git commit -m file.other file
13+
14+
git checkout main
15+
echo main > file
16+
git add file
17+
git commit -m file.main file
2918

3019
# This should fail and leave us in a cherry-pick state
3120
git cherry-pick other-branch || true

git-repository/tests/fixtures/make_rebase_i_repo.sh

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@ set -eu -o pipefail
33

44
git init -q
55

6-
git config commit.gpgsign false
7-
8-
git config advice.statusHints false
9-
git config advice.resolveConflict false
10-
git config advice.commitBeforeMerge false
11-
git config advice.skippedCherryPicks false
12-
13-
git config init.defaultBranch master
14-
15-
unset GIT_AUTHOR_DATE
16-
unset GIT_COMMITTER_DATE
17-
186
touch 1 2 3
197
git add 1
208
git commit -m 1 1
@@ -23,19 +11,6 @@ git commit -m 2 2
2311
git add 3
2412
git commit -m 3 3
2513

26-
# NOTE: This relies on GNU sed behavior and will fail on *BSDs (including macOS) without GNU
27-
# sed installed.
28-
sed=$(which gsed sed | head -1 || true)
29-
30-
# GNU sed recognizes long arguments, BSD sed does not
31-
# NOTE: We can't rely on $? because set -e guarantees the script will terminate on a non-zero exit
32-
${sed} --version 2&>/dev/null && sed_exit_code=success || sed_exit_code=fail
33-
if [ "${sed_exit_code}" = "fail" ]; then
34-
printf "\n** GNU sed is required for this test but was not found **\n"
35-
exit 1
36-
fi
37-
unset sed_exit_code
38-
3914
# NOTE: Starting around git 2.35.0 --preserve-merges was renamed to --rebase-merges
4015
# however --preserve-merges first appeared in git 2.18. That should cover most use cases.
41-
EDITOR="${sed} -i.bak -z 's/pick/edit/2'" git rebase --rebase-merges --interactive HEAD~2
16+
EDITOR="sed -i.bak 's/pick/edit/g'" git rebase --rebase-merges --interactive HEAD~2

git-repository/tests/fixtures/make_revert_repo.sh

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@ set -eu -o pipefail
33

44
git init -q
55

6-
git config commit.gpgsign false
7-
8-
git config advice.statusHints false
9-
git config advice.resolveConflict false
10-
git config advice.commitBeforeMerge false
11-
git config advice.skippedCherryPicks false
12-
13-
git config init.defaultBranch master
14-
15-
unset GIT_AUTHOR_DATE
16-
unset GIT_COMMITTER_DATE
17-
186
touch 1 2 3
197
git add 1
208
git commit -m 1 1

git-repository/tests/repo.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ fn repo(name: &str) -> crate::Result<ThreadSafeRepository> {
77
Ok(ThreadSafeRepository::open(repo_path)?)
88
}
99

10-
fn repo_rw(name: &str) -> crate::Result<(git_repository::Repository, tempfile::TempDir)> {
10+
fn named_repo(name: &str) -> crate::Result<Repository> {
11+
let repo_path = git_testtools::scripted_fixture_repo_read_only(name)?;
12+
Ok(ThreadSafeRepository::open(repo_path)?.to_thread_local())
13+
}
14+
15+
fn repo_rw(name: &str) -> crate::Result<(Repository, tempfile::TempDir)> {
1116
let repo_path = git_testtools::scripted_fixture_repo_writable(name)?;
1217
Ok((
1318
ThreadSafeRepository::discover(repo_path.path())?.to_thread_local(),

git-repository/tests/state/mod.rs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,36 @@
1-
use crate::{repo, Result};
2-
use anyhow::anyhow;
3-
use git_repository::{bstr::ByteSlice, RepositoryState};
1+
use crate::{named_repo, Result};
2+
use git_repository::RepositoryState;
43

5-
// Can we identify that a cherry pick operation is in progress
64
#[test]
75
fn cherry_pick() -> Result {
8-
let repo = repo("make_cherry_pick_repo.sh").map(|r| r.to_thread_local())?;
6+
let repo = named_repo("make_cherry_pick_repo.sh")?;
97

108
let head = repo.head()?;
11-
let head_name = head
12-
.referent_name()
13-
.ok_or_else(|| anyhow!("detached head?"))?
14-
.shorten()
15-
.to_str()?;
16-
assert_eq!("master", head_name);
17-
18-
assert_eq!(Some(RepositoryState::CherryPick), repo.in_progress_operation());
9+
let head_name = head.referent_name().expect("no detached head").shorten();
10+
assert_eq!(head_name, "main");
1911

12+
assert_eq!(repo.in_progress_operation(), Some(RepositoryState::CherryPick));
2013
Ok(())
2114
}
2215

23-
// Can we identify that we're in the middle of an interactive rebase?
2416
#[test]
2517
fn rebase_interactive() -> Result {
26-
let repo = repo("make_rebase_i_repo.sh").map(|r| r.to_thread_local())?;
18+
let repo = named_repo("make_rebase_i_repo.sh")?;
2719

2820
let head = repo.head()?;
29-
// TODO: Get rebase head/target
30-
let head_name = head.referent_name();
31-
assert!(head_name.is_none());
32-
33-
assert_eq!(Some(RepositoryState::RebaseInteractive), repo.in_progress_operation());
21+
assert!(head.is_detached());
22+
assert_eq!(repo.in_progress_operation(), Some(RepositoryState::RebaseInteractive));
3423

3524
Ok(())
3625
}
3726

38-
// Can we identify a revert operation when we see it?
3927
#[test]
4028
fn revert() -> Result {
41-
let repo = repo("make_revert_repo.sh").map(|r| r.to_thread_local())?;
29+
let repo = named_repo("make_revert_repo.sh")?;
4230

4331
let head = repo.head()?;
44-
let head_name = head
45-
.referent_name()
46-
.ok_or_else(|| anyhow!("detached head?"))?
47-
.shorten()
48-
.to_str()?;
49-
assert_eq!("master", head_name);
32+
let head_name = head.referent_name().expect("no detached head").shorten();
33+
assert_eq!(head_name, "main");
5034

5135
assert_eq!(Some(RepositoryState::Revert), repo.in_progress_operation());
5236

tests/tools/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,11 @@ pub fn scripted_fixture_repo_read_only_with_args(
162162
.env("GIT_COMMITTER_DATE", "2000-01-02 00:00:00 +0000")
163163
.env("GIT_COMMITTER_EMAIL", "committer@example.com")
164164
.env("GIT_COMMITTER_NAME", "committer")
165-
.env("GIT_CONFIG_COUNT", "1")
165+
.env("GIT_CONFIG_COUNT", "2")
166166
.env("GIT_CONFIG_KEY_0", "commit.gpgsign")
167167
.env("GIT_CONFIG_VALUE_0", "false")
168+
.env("GIT_CONFIG_KEY_1", "init.defaultBranch")
169+
.env("GIT_CONFIG_VALUE_1", "main")
168170
.output()?;
169171
assert!(
170172
output.status.success(),

0 commit comments

Comments
 (0)