251 lines
7.7 KiB
Go
251 lines
7.7 KiB
Go
package commands
|
|
|
|
import (
|
|
"fmt"
|
|
"gitlab.com/revalus/grm/internal/config"
|
|
"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"
|
|
)
|
|
|
|
func TestIfBranchesAreEqual(t *testing.T) {
|
|
tmpDirWithInitialRepository := getTestSetup()
|
|
|
|
fakeLocalRepo, err := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
|
|
URL: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
|
|
})
|
|
checkErrorDuringPreparation(err)
|
|
|
|
currentReference, err := fakeLocalRepo.Head()
|
|
checkErrorDuringPreparation(err)
|
|
|
|
remote, err := fakeLocalRepo.Remote("origin")
|
|
checkErrorDuringPreparation(err)
|
|
|
|
remoteRevision, err := fakeLocalRepo.ResolveRevision(plumbing.Revision(fmt.Sprintf("%v/%v", remote.Config().Name, currentReference.Name().Short())))
|
|
checkErrorDuringPreparation(err)
|
|
|
|
currentBranchCommit, err := fakeLocalRepo.CommitObject(currentReference.Hash())
|
|
checkErrorDuringPreparation(err)
|
|
|
|
remoteBranchCommit, err := fakeLocalRepo.CommitObject(*remoteRevision)
|
|
checkErrorDuringPreparation(err)
|
|
|
|
result := findNumberOfCommitDiffs(currentBranchCommit, remoteBranchCommit)
|
|
if result != 0 {
|
|
t.Errorf("Expected to get 0 changes, instead of this got %v", result)
|
|
}
|
|
}
|
|
|
|
func TestIfCurrentBranchIsDifferent(t *testing.T) {
|
|
|
|
tmpDirWithInitialRepository := getTestSetup()
|
|
fakeLocalRepo, err := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
|
|
URL: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
|
|
})
|
|
checkErrorDuringPreparation(err)
|
|
|
|
localWorktree, err := fakeLocalRepo.Worktree()
|
|
checkErrorDuringPreparation(err)
|
|
|
|
makeCommit(localWorktree, "commit 1")
|
|
|
|
localReference, err := fakeLocalRepo.Head()
|
|
checkErrorDuringPreparation(err)
|
|
|
|
remoteConnection, err := fakeLocalRepo.Remote("origin")
|
|
checkErrorDuringPreparation(err)
|
|
|
|
remoteRevision, err := fakeLocalRepo.ResolveRevision(plumbing.Revision(fmt.Sprintf("%v/%v", remoteConnection.Config().Name, localReference.Name().Short())))
|
|
checkErrorDuringPreparation(err)
|
|
|
|
currentBranchCommit, err := fakeLocalRepo.CommitObject(localReference.Hash())
|
|
checkErrorDuringPreparation(err)
|
|
|
|
remoteBranchCommit, err := fakeLocalRepo.CommitObject(*remoteRevision)
|
|
checkErrorDuringPreparation(err)
|
|
|
|
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++ {
|
|
makeCommit(localWorktree, fmt.Sprintf("Commit +%v", i))
|
|
}
|
|
|
|
localReference, err = fakeLocalRepo.Head()
|
|
checkErrorDuringPreparation(err)
|
|
|
|
currentBranchCommit, err = fakeLocalRepo.CommitObject(localReference.Hash())
|
|
checkErrorDuringPreparation(err)
|
|
|
|
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 := getTestSetup()
|
|
fsForLocalRepo, storageForTestRepo := getFSForLocalRepo("noMatterValue", tmpDirWithInitialRepository.rootFS)
|
|
|
|
_, err := git.Clone(storageForTestRepo, fsForLocalRepo, &git.CloneOptions{
|
|
URL: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
|
|
})
|
|
checkErrorDuringPreparation(err)
|
|
|
|
sc := StatusChecker{
|
|
workspace: tmpDirWithInitialRepository.rootFS.Root(),
|
|
}
|
|
|
|
repoCfg := config.RepositoryConfig{
|
|
Name: "test",
|
|
Src: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
|
|
Dest: tmpDirWithInitialRepository.rootFS.Root(),
|
|
}
|
|
|
|
repoStatus := sc.Command(repoCfg)
|
|
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 := getTestSetup()
|
|
dirNameForLocalRepository := "testRepo"
|
|
fsForLocalRepo, storageForTestRepo := getFSForLocalRepo(dirNameForLocalRepository, tmpDirWithInitialRepository.rootFS)
|
|
|
|
fakeLocalRepository, err := git.Clone(storageForTestRepo, fsForLocalRepo, &git.CloneOptions{
|
|
URL: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
|
|
})
|
|
checkErrorDuringPreparation(err)
|
|
|
|
err = fakeLocalRepository.DeleteRemote("origin")
|
|
checkErrorDuringPreparation(err)
|
|
|
|
sc := StatusChecker{
|
|
workspace: tmpDirWithInitialRepository.rootFS.Root(),
|
|
}
|
|
|
|
repoCfg := config.RepositoryConfig{
|
|
Name: "test",
|
|
Src: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
|
|
Dest: dirNameForLocalRepository,
|
|
}
|
|
|
|
repoStatus := sc.Command(repoCfg)
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestCommandAllCorrectWithoutChanges(t *testing.T) {
|
|
|
|
sc, _, repoCfg, _ := getBaseForTestingSyncCommand()
|
|
|
|
repoStatus := sc.Command(repoCfg)
|
|
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)
|
|
}
|
|
}
|
|
func TestCommandAllCorrectWithOneChange(t *testing.T) {
|
|
|
|
sc, fakeLocalRepository, repoCfg, _ := getBaseForTestingSyncCommand()
|
|
|
|
fakeLocalWorkTree, err := fakeLocalRepository.Worktree()
|
|
checkErrorDuringPreparation(err)
|
|
|
|
makeCommit(fakeLocalWorkTree, "commit 1")
|
|
|
|
repoStatus := sc.Command(repoCfg)
|
|
expectedMessage := "branch master - ( | origin | \u21911 \u21930 )"
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestCommandMultiRemoteNoChanges(t *testing.T) {
|
|
|
|
sc, _, repoCfg := getBaseForTestingSyncMultipleRemote()
|
|
repoStatus := sc.Command(repoCfg)
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestCommandMultiRemoteWithOneChange(t *testing.T) {
|
|
sc, fakeLocalRepository, repoCfg := getBaseForTestingSyncMultipleRemote()
|
|
|
|
fakeLocalWorkTree, err := fakeLocalRepository.Worktree()
|
|
checkErrorDuringPreparation(err)
|
|
|
|
makeCommit(fakeLocalWorkTree, "commit 1")
|
|
checkErrorDuringPreparation(err)
|
|
|
|
repoStatus := sc.Command(repoCfg)
|
|
expectedMessage := "branch master - ( | origin | \u21911 \u21930 ) - ( | subremote | \u21911 \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)
|
|
}
|
|
}
|