Skip to content

Commit

Permalink
Simplify state tests (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Apr 18, 2022
1 parent 9679d6b commit fc61c0d
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 89 deletions.
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
31 changes: 10 additions & 21 deletions git-repository/tests/fixtures/make_cherry_pick_repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,18 @@ set -eu -o pipefail

git init -q

git config commit.gpgsign false
touch file
git add file
git commit -m first file

git config advice.statusHints false
git config advice.resolveConflict false
git config advice.commitBeforeMerge false
git config advice.skippedCherryPicks false

git config init.defaultBranch master

unset GIT_AUTHOR_DATE
unset GIT_COMMITTER_DATE

touch 1
git add 1
git commit -m 1 1
git checkout -b other-branch
echo other-branch > 1
git add 1
git commit -m 1.other 1
git checkout master
echo master > 1
git add 1
git commit -m 1.master 1
echo other-branch > file
git commit -m file.other file

git checkout main
echo main > file
git add file
git commit -m file.main file

# This should fail and leave us in a cherry-pick state
git cherry-pick other-branch || true
27 changes: 1 addition & 26 deletions git-repository/tests/fixtures/make_rebase_i_repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@ set -eu -o pipefail

git init -q

git config commit.gpgsign false

git config advice.statusHints false
git config advice.resolveConflict false
git config advice.commitBeforeMerge false
git config advice.skippedCherryPicks false

git config init.defaultBranch master

unset GIT_AUTHOR_DATE
unset GIT_COMMITTER_DATE

touch 1 2 3
git add 1
git commit -m 1 1
Expand All @@ -23,19 +11,6 @@ git commit -m 2 2
git add 3
git commit -m 3 3

# NOTE: This relies on GNU sed behavior and will fail on *BSDs (including macOS) without GNU
# sed installed.
sed=$(which gsed sed | head -1 || true)

# GNU sed recognizes long arguments, BSD sed does not
# NOTE: We can't rely on $? because set -e guarantees the script will terminate on a non-zero exit
${sed} --version 2&>/dev/null && sed_exit_code=success || sed_exit_code=fail
if [ "${sed_exit_code}" = "fail" ]; then
printf "\n** GNU sed is required for this test but was not found **\n"
exit 1
fi
unset sed_exit_code

# NOTE: Starting around git 2.35.0 --preserve-merges was renamed to --rebase-merges
# however --preserve-merges first appeared in git 2.18. That should cover most use cases.
EDITOR="${sed} -i.bak -z 's/pick/edit/2'" git rebase --rebase-merges --interactive HEAD~2
EDITOR="sed -i.bak 's/pick/edit/g'" git rebase --rebase-merges --interactive HEAD~2
12 changes: 0 additions & 12 deletions git-repository/tests/fixtures/make_revert_repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@ set -eu -o pipefail

git init -q

git config commit.gpgsign false

git config advice.statusHints false
git config advice.resolveConflict false
git config advice.commitBeforeMerge false
git config advice.skippedCherryPicks false

git config init.defaultBranch master

unset GIT_AUTHOR_DATE
unset GIT_COMMITTER_DATE

touch 1 2 3
git add 1
git commit -m 1 1
Expand Down
7 changes: 6 additions & 1 deletion git-repository/tests/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ fn repo(name: &str) -> crate::Result<ThreadSafeRepository> {
Ok(ThreadSafeRepository::open(repo_path)?)
}

fn repo_rw(name: &str) -> crate::Result<(git_repository::Repository, tempfile::TempDir)> {
fn named_repo(name: &str) -> crate::Result<Repository> {
let repo_path = git_testtools::scripted_fixture_repo_read_only(name)?;
Ok(ThreadSafeRepository::open(repo_path)?.to_thread_local())
}

fn repo_rw(name: &str) -> crate::Result<(Repository, tempfile::TempDir)> {
let repo_path = git_testtools::scripted_fixture_repo_writable(name)?;
Ok((
ThreadSafeRepository::discover(repo_path.path())?.to_thread_local(),
Expand Down
40 changes: 12 additions & 28 deletions git-repository/tests/state/mod.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,36 @@
use crate::{repo, Result};
use anyhow::anyhow;
use git_repository::{bstr::ByteSlice, RepositoryState};
use crate::{named_repo, Result};
use git_repository::RepositoryState;

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

let head = repo.head()?;
let head_name = head
.referent_name()
.ok_or_else(|| anyhow!("detached head?"))?
.shorten()
.to_str()?;
assert_eq!("master", head_name);

assert_eq!(Some(RepositoryState::CherryPick), repo.in_progress_operation());
let head_name = head.referent_name().expect("no detached head").shorten();
assert_eq!(head_name, "main");

assert_eq!(repo.in_progress_operation(), Some(RepositoryState::CherryPick));
Ok(())
}

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

let head = repo.head()?;
// TODO: Get rebase head/target
let head_name = head.referent_name();
assert!(head_name.is_none());

assert_eq!(Some(RepositoryState::RebaseInteractive), repo.in_progress_operation());
assert!(head.is_detached());
assert_eq!(repo.in_progress_operation(), Some(RepositoryState::RebaseInteractive));

Ok(())
}

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

let head = repo.head()?;
let head_name = head
.referent_name()
.ok_or_else(|| anyhow!("detached head?"))?
.shorten()
.to_str()?;
assert_eq!("master", head_name);
let head_name = head.referent_name().expect("no detached head").shorten();
assert_eq!(head_name, "main");

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

Expand Down
4 changes: 3 additions & 1 deletion tests/tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ pub fn scripted_fixture_repo_read_only_with_args(
.env("GIT_COMMITTER_DATE", "2000-01-02 00:00:00 +0000")
.env("GIT_COMMITTER_EMAIL", "[email protected]")
.env("GIT_COMMITTER_NAME", "committer")
.env("GIT_CONFIG_COUNT", "1")
.env("GIT_CONFIG_COUNT", "2")
.env("GIT_CONFIG_KEY_0", "commit.gpgsign")
.env("GIT_CONFIG_VALUE_0", "false")
.env("GIT_CONFIG_KEY_1", "init.defaultBranch")
.env("GIT_CONFIG_VALUE_1", "main")
.output()?;
assert!(
output.status.success(),
Expand Down

0 comments on commit fc61c0d

Please sign in to comment.