2021-11-05 17:19:06 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-06-13 19:19:04 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/cache"
|
|
|
|
"github.com/go-git/go-git/v5/storage/filesystem"
|
2024-01-13 14:39:40 +00:00
|
|
|
"gitlab.com/revalus/grm/internal/config"
|
2024-06-13 19:19:04 +00:00
|
|
|
"path"
|
2021-11-05 17:19:06 +00:00
|
|
|
"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) {
|
2024-06-13 19:19:04 +00:00
|
|
|
pathsToTest := prepareBasicRepository()
|
2021-11-05 17:19:06 +00:00
|
|
|
|
|
|
|
fakeLocalRepo, err := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
|
2024-06-13 19:19:04 +00:00
|
|
|
URL: pathsToTest.baseTestRepository,
|
2021-11-05 17:19:06 +00:00
|
|
|
})
|
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
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
pathsToTest := prepareBasicRepository()
|
2021-11-06 22:21:35 +00:00
|
|
|
fakeLocalRepo, err := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
|
2024-06-13 19:19:04 +00:00
|
|
|
URL: pathsToTest.baseTestRepository,
|
2021-11-05 17:19:06 +00:00
|
|
|
})
|
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 {
|
2024-06-13 19:19:04 +00:00
|
|
|
t.Errorf("Expected to get 15 changes, instead of this got %v", result)
|
2021-11-05 17:19:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandRepositoryDoesNotExists(t *testing.T) {
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
pathsToTest := prepareBasicRepository()
|
2021-11-05 17:19:06 +00:00
|
|
|
sc := StatusChecker{
|
2024-06-13 19:19:04 +00:00
|
|
|
workspace: pathsToTest.baseTestDirectory,
|
2021-11-05 17:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
repoCfg := config.RepositoryConfig{
|
|
|
|
Name: "test",
|
2024-06-13 19:19:04 +00:00
|
|
|
Src: pathsToTest.baseTestRepository,
|
|
|
|
Dest: pathsToTest.baseTestDirectory,
|
2021-11-05 17:19:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 17:30:45 +00:00
|
|
|
repoStatus := sc.Command(repoCfg)
|
2021-11-05 17:19:06 +00:00
|
|
|
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) {
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
pathsToTest := prepareBasicRepository()
|
|
|
|
dirNameForLocalRepository := "sub-repository"
|
|
|
|
fsForLocalRepo, gitMetadataDirectory := prepareRepositoryDirectories(
|
|
|
|
path.Join(pathsToTest.baseTestDirectory, dirNameForLocalRepository),
|
|
|
|
)
|
|
|
|
storageForTestRepo := filesystem.NewStorage(gitMetadataDirectory, cache.NewObjectLRUDefault())
|
2021-11-05 17:19:06 +00:00
|
|
|
|
2021-11-06 22:21:35 +00:00
|
|
|
fakeLocalRepository, err := git.Clone(storageForTestRepo, fsForLocalRepo, &git.CloneOptions{
|
2024-06-13 19:19:04 +00:00
|
|
|
URL: pathsToTest.baseTestRepository,
|
2021-11-05 17:19:06 +00:00
|
|
|
})
|
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{
|
2024-06-13 19:19:04 +00:00
|
|
|
workspace: pathsToTest.baseTestDirectory,
|
2021-11-05 17:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
repoCfg := config.RepositoryConfig{
|
|
|
|
Name: "test",
|
2024-06-13 19:19:04 +00:00
|
|
|
Src: pathsToTest.baseTestRepository,
|
2021-11-05 17:19:06 +00:00
|
|
|
Dest: dirNameForLocalRepository,
|
|
|
|
}
|
|
|
|
|
2021-11-08 17:30:45 +00:00
|
|
|
repoStatus := sc.Command(repoCfg)
|
2021-11-05 17:19:06 +00:00
|
|
|
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
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
pathsToTest := prepareBasicRepository()
|
|
|
|
subRepositoryDirectoryName := "sub-repository"
|
|
|
|
|
|
|
|
// Get new empty repository to compare with base repository
|
|
|
|
createAndCloneRepository(subRepositoryDirectoryName, pathsToTest)
|
|
|
|
|
|
|
|
sc := StatusChecker{
|
|
|
|
workspace: pathsToTest.baseTestDirectory,
|
|
|
|
}
|
|
|
|
|
|
|
|
repoCfg := config.RepositoryConfig{
|
|
|
|
Name: "test",
|
|
|
|
Src: pathsToTest.baseTestRepository,
|
|
|
|
Dest: subRepositoryDirectoryName,
|
|
|
|
}
|
2021-11-05 17:19:06 +00:00
|
|
|
|
2021-11-08 17:30:45 +00:00
|
|
|
repoStatus := sc.Command(repoCfg)
|
2021-11-05 17:19:06 +00:00
|
|
|
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) {
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
pathsToTest := prepareBasicRepository()
|
|
|
|
subRepositoryDirectoryName := "sub-repository"
|
|
|
|
fakeLocalRepository := createAndCloneRepository(subRepositoryDirectoryName, pathsToTest)
|
|
|
|
|
|
|
|
sc := StatusChecker{
|
|
|
|
workspace: pathsToTest.baseTestDirectory,
|
|
|
|
}
|
|
|
|
|
|
|
|
repoCfg := config.RepositoryConfig{
|
|
|
|
Name: "test",
|
|
|
|
Src: pathsToTest.baseTestRepository,
|
|
|
|
Dest: subRepositoryDirectoryName,
|
|
|
|
}
|
2021-11-06 22:21:35 +00:00
|
|
|
|
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
|
|
|
|
2021-11-08 17:30:45 +00:00
|
|
|
repoStatus := sc.Command(repoCfg)
|
2021-11-06 22:21:35 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
func TestCommandMultiRemotesNoChanges(t *testing.T) {
|
|
|
|
pathsToTest := prepareBasicRepository()
|
|
|
|
subRepositoryDirectoryName := "sub-repository"
|
|
|
|
fakeLocalRepository := createAndCloneRepository(subRepositoryDirectoryName, pathsToTest)
|
|
|
|
|
|
|
|
sc := StatusChecker{
|
|
|
|
workspace: pathsToTest.baseTestDirectory,
|
|
|
|
}
|
|
|
|
|
|
|
|
repoCfg := config.RepositoryConfig{
|
|
|
|
Name: "test",
|
|
|
|
Src: pathsToTest.baseTestRepository,
|
|
|
|
Dest: subRepositoryDirectoryName,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := addLocalRepositoryAsAFakeRemoteRepository(fakeLocalRepository, pathsToTest.baseTestRepository)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %v", err)
|
|
|
|
}
|
2021-11-05 17:19:06 +00:00
|
|
|
|
2021-11-08 17:30:45 +00:00
|
|
|
repoStatus := sc.Command(repoCfg)
|
2021-11-05 17:19:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
func TestCommandMultiRemotesWithOneChange(t *testing.T) {
|
|
|
|
pathsToTest := prepareBasicRepository()
|
|
|
|
subRepositoryDirectoryName := "sub-repository"
|
|
|
|
fakeLocalRepository := createAndCloneRepository(subRepositoryDirectoryName, pathsToTest)
|
|
|
|
|
|
|
|
sc := StatusChecker{
|
|
|
|
workspace: pathsToTest.baseTestDirectory,
|
|
|
|
}
|
|
|
|
|
|
|
|
repoCfg := config.RepositoryConfig{
|
|
|
|
Name: "test",
|
|
|
|
Src: pathsToTest.baseTestRepository,
|
|
|
|
Dest: subRepositoryDirectoryName,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := addLocalRepositoryAsAFakeRemoteRepository(fakeLocalRepository, pathsToTest.baseTestRepository)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %v", err)
|
|
|
|
}
|
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-08 17:30:45 +00:00
|
|
|
repoStatus := sc.Command(repoCfg)
|
2021-11-06 22:21:35 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|