265 lines
7.9 KiB
Go
265 lines
7.9 KiB
Go
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(),
|
|
})
|
|
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 := getTestDirForTests()
|
|
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 := getTestDirForTests()
|
|
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(),
|
|
}
|
|
|
|
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"
|
|
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,
|
|
}
|
|
|
|
ch := make(chan CommandStatus)
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestCommandAllCorrectWithoutChanges(t *testing.T) {
|
|
|
|
sc, _, repoCfg, _ := getBaseForTestingSyncCommand()
|
|
|
|
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)
|
|
}
|
|
}
|
|
func TestCommandAllCorrectWithOneChange(t *testing.T) {
|
|
|
|
sc, fakeLocalRepository, repoCfg, _ := getBaseForTestingSyncCommand()
|
|
|
|
ch := make(chan CommandStatus)
|
|
|
|
fakeLocalWorkTree, err := fakeLocalRepository.Worktree()
|
|
checkErrorDuringPreparation(err)
|
|
|
|
makeCommit(fakeLocalWorkTree, "commit 1")
|
|
|
|
go sc.Command(repoCfg, ch)
|
|
repoStatus := <-ch
|
|
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()
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestCommandMultiRemoteWithOneChange(t *testing.T) {
|
|
sc, fakeLocalRepository, repoCfg := getBaseForTestingSyncMultipleRemote()
|
|
|
|
fakeLocalWorkTree, err := fakeLocalRepository.Worktree()
|
|
checkErrorDuringPreparation(err)
|
|
|
|
makeCommit(fakeLocalWorkTree, "commit 1")
|
|
checkErrorDuringPreparation(err)
|
|
|
|
ch := make(chan CommandStatus)
|
|
go sc.Command(repoCfg, ch)
|
|
repoStatus := <-ch
|
|
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)
|
|
}
|
|
}
|