GitRepositoryManager/commands/status_cmd_test.go

265 lines
7.9 KiB
Go
Raw Normal View History

2021-11-05 17:19:06 +00:00
package commands
import (
"fmt"
"testing"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
"gitlab.com/revalus/grm/config"
)
func TestIfBranchesAreEqual(t *testing.T) {
tmpDirWithInitialRepository := getTestDirForTests()
fakeLocalRepo, err := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
URL: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
})
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
currentReference, err := fakeLocalRepo.Head()
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
remote, err := fakeLocalRepo.Remote("origin")
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
remoteRevision, err := fakeLocalRepo.ResolveRevision(plumbing.Revision(fmt.Sprintf("%v/%v", remote.Config().Name, currentReference.Name().Short())))
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
currentBranchCommit, err := fakeLocalRepo.CommitObject(currentReference.Hash())
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
remoteBranchCommit, err := fakeLocalRepo.CommitObject(*remoteRevision)
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
result := findNumberOfCommitDiffs(currentBranchCommit, remoteBranchCommit)
if result != 0 {
t.Errorf("Expected to get 0 changes, instead of this got %v", result)
}
}
2021-11-06 22:21:35 +00:00
func TestIfCurrentBranchIsDifferent(t *testing.T) {
2021-11-05 17:19:06 +00:00
tmpDirWithInitialRepository := getTestDirForTests()
2021-11-06 22:21:35 +00:00
fakeLocalRepo, err := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
2021-11-05 17:19:06 +00:00
URL: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
})
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
localWorktree, err := fakeLocalRepo.Worktree()
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
2021-11-06 22:21:35 +00:00
makeCommit(localWorktree, "commit 1")
2021-11-05 17:19:06 +00:00
localReference, err := fakeLocalRepo.Head()
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
remoteConnection, err := fakeLocalRepo.Remote("origin")
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
remoteRevision, err := fakeLocalRepo.ResolveRevision(plumbing.Revision(fmt.Sprintf("%v/%v", remoteConnection.Config().Name, localReference.Name().Short())))
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
currentBranchCommit, err := fakeLocalRepo.CommitObject(localReference.Hash())
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
remoteBranchCommit, err := fakeLocalRepo.CommitObject(*remoteRevision)
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
result := findNumberOfCommitDiffs(currentBranchCommit, remoteBranchCommit)
if result != 1 {
t.Errorf("Expected to get 1 changes, instead of this got %v", result)
}
for i := 1; i < 15; i++ {
2021-11-06 22:21:35 +00:00
makeCommit(localWorktree, fmt.Sprintf("Commit +%v", i))
2021-11-05 17:19:06 +00:00
}
localReference, err = fakeLocalRepo.Head()
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
currentBranchCommit, err = fakeLocalRepo.CommitObject(localReference.Hash())
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
result = findNumberOfCommitDiffs(currentBranchCommit, remoteBranchCommit)
if result != 15 {
t.Errorf("Expected to get 5 changes, instead of this got %v", result)
}
}
func TestCommandRepositoryDoesNotExists(t *testing.T) {
tmpDirWithInitialRepository := getTestDirForTests()
2021-11-06 22:21:35 +00:00
fsForLocalRepo, storageForTestRepo := getFSForLocalRepo("noMatterValue", tmpDirWithInitialRepository.rootFS)
2021-11-05 17:19:06 +00:00
2021-11-06 22:21:35 +00:00
_, err := git.Clone(storageForTestRepo, fsForLocalRepo, &git.CloneOptions{
2021-11-05 17:19:06 +00:00
URL: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
})
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
sc := StatusChecker{
workspace: tmpDirWithInitialRepository.rootFS.Root(),
}
repoCfg := config.RepositoryConfig{
Name: "test",
Src: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
Dest: tmpDirWithInitialRepository.rootFS.Root(),
}
ch := make(chan CommandStatus)
go sc.Command(repoCfg, ch)
repoStatus := <-ch
expectedMessage := "repository does not exist"
if !repoStatus.Error {
t.Errorf("Expected error")
}
if repoStatus.Changed {
t.Errorf("Unexpected change value")
}
if repoStatus.Message != expectedMessage {
t.Errorf("Expected to get \"%v\", instead of this got \"%v\"", expectedMessage, repoStatus.Message)
}
}
func TestCommandRepositoryNoRemoteBranch(t *testing.T) {
tmpDirWithInitialRepository := getTestDirForTests()
dirNameForLocalRepository := "testRepo"
2021-11-06 22:21:35 +00:00
fsForLocalRepo, storageForTestRepo := getFSForLocalRepo(dirNameForLocalRepository, tmpDirWithInitialRepository.rootFS)
2021-11-05 17:19:06 +00:00
2021-11-06 22:21:35 +00:00
fakeLocalRepository, err := git.Clone(storageForTestRepo, fsForLocalRepo, &git.CloneOptions{
2021-11-05 17:19:06 +00:00
URL: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
})
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
err = fakeLocalRepository.DeleteRemote("origin")
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
sc := StatusChecker{
workspace: tmpDirWithInitialRepository.rootFS.Root(),
}
repoCfg := config.RepositoryConfig{
Name: "test",
Src: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
Dest: dirNameForLocalRepository,
}
ch := make(chan CommandStatus)
2021-11-06 22:21:35 +00:00
2021-11-05 17:19:06 +00:00
go sc.Command(repoCfg, ch)
repoStatus := <-ch
expectedMessage := "cannot find remote branches"
if !repoStatus.Error {
t.Errorf("Expected error")
}
if repoStatus.Changed {
t.Errorf("Unexpected change value")
}
if repoStatus.Message != expectedMessage {
t.Errorf("Expected to get \"%v\", instead of this got \"%v\"", expectedMessage, repoStatus.Message)
}
}
2021-11-06 22:21:35 +00:00
func TestCommandAllCorrectWithoutChanges(t *testing.T) {
2021-11-05 17:19:06 +00:00
2021-11-06 22:21:35 +00:00
sc, _, repoCfg, _ := getBaseForTestingSyncCommand()
2021-11-05 17:19:06 +00:00
ch := make(chan CommandStatus)
go sc.Command(repoCfg, ch)
repoStatus := <-ch
expectedMessage := "branch master - ( | origin | \u21910 \u21930 )"
if repoStatus.Error {
t.Errorf("Unexpected error")
t.Errorf("Message %v", repoStatus.Message)
}
if repoStatus.Changed {
t.Errorf("Expeected that changed will be true")
}
if repoStatus.Message != expectedMessage {
t.Errorf("Expected to get \"%v\", instead of this got \"%v\"", expectedMessage, repoStatus.Message)
}
2021-11-06 22:21:35 +00:00
}
func TestCommandAllCorrectWithOneChange(t *testing.T) {
sc, fakeLocalRepository, repoCfg, _ := getBaseForTestingSyncCommand()
ch := make(chan CommandStatus)
2021-11-05 17:19:06 +00:00
fakeLocalWorkTree, err := fakeLocalRepository.Worktree()
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
2021-11-06 22:21:35 +00:00
makeCommit(fakeLocalWorkTree, "commit 1")
2021-11-05 17:19:06 +00:00
go sc.Command(repoCfg, ch)
2021-11-06 22:21:35 +00:00
repoStatus := <-ch
expectedMessage := "branch master - ( | origin | \u21911 \u21930 )"
2021-11-05 17:19:06 +00:00
if repoStatus.Message != expectedMessage {
t.Errorf("Expected to get \"%v\", instead of this got \"%v\"", expectedMessage, repoStatus.Message)
}
if repoStatus.Error {
t.Errorf("Unexpected error")
t.Errorf("Message %v", repoStatus.Message)
}
if !repoStatus.Changed {
t.Errorf("Expeected that changed will be true")
}
if repoStatus.Message != expectedMessage {
t.Errorf("Expected to get \"%v\", instead of this got \"%v\"", expectedMessage, repoStatus.Message)
}
}
2021-11-06 22:21:35 +00:00
func TestCommandMultiRemoteNoChanges(t *testing.T) {
2021-11-05 17:19:06 +00:00
2021-11-06 22:21:35 +00:00
sc, _, repoCfg := getBaseForTestingSyncMultipleRemote()
2021-11-05 17:19:06 +00:00
ch := make(chan CommandStatus)
go sc.Command(repoCfg, ch)
repoStatus := <-ch
expectedMessage := "branch master - ( | origin | \u21910 \u21930 ) - ( | subremote | \u21910 \u21930 )"
if repoStatus.Error {
t.Errorf("Unexpected error")
t.Errorf("Message %v", repoStatus.Message)
}
if repoStatus.Changed {
t.Errorf("Expeected that changed will be true")
}
if repoStatus.Message != expectedMessage {
t.Errorf("Expected to get \"%v\", instead of this got \"%v\"", expectedMessage, repoStatus.Message)
}
2021-11-06 22:21:35 +00:00
}
func TestCommandMultiRemoteWithOneChange(t *testing.T) {
sc, fakeLocalRepository, repoCfg := getBaseForTestingSyncMultipleRemote()
2021-11-05 17:19:06 +00:00
fakeLocalWorkTree, err := fakeLocalRepository.Worktree()
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
2021-11-06 22:21:35 +00:00
makeCommit(fakeLocalWorkTree, "commit 1")
checkErrorDuringPreparation(err)
2021-11-05 17:19:06 +00:00
2021-11-06 22:21:35 +00:00
ch := make(chan CommandStatus)
2021-11-05 17:19:06 +00:00
go sc.Command(repoCfg, ch)
2021-11-06 22:21:35 +00:00
repoStatus := <-ch
expectedMessage := "branch master - ( | origin | \u21911 \u21930 ) - ( | subremote | \u21911 \u21930 )"
2021-11-05 17:19:06 +00:00
if repoStatus.Error {
t.Errorf("Unexpected error")
t.Errorf("Message %v", repoStatus.Message)
}
if !repoStatus.Changed {
t.Errorf("Expeected that changed will be true")
}
if repoStatus.Message != expectedMessage {
t.Errorf("Expected to get \"%v\", instead of this got \"%v\"", expectedMessage, repoStatus.Message)
}
}