package commands import ( "fmt" "github.com/go-git/go-git/v5/plumbing/cache" "github.com/go-git/go-git/v5/storage/filesystem" "gitlab.com/revalus/grm/internal/config" "path" "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) { pathsToTest := prepareBasicRepository() fakeLocalRepo, err := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{ URL: pathsToTest.baseTestRepository, }) 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) { pathsToTest := prepareBasicRepository() fakeLocalRepo, err := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{ URL: pathsToTest.baseTestRepository, }) 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 15 changes, instead of this got %v", result) } } func TestCommandRepositoryDoesNotExists(t *testing.T) { pathsToTest := prepareBasicRepository() sc := StatusChecker{ workspace: pathsToTest.baseTestDirectory, } repoCfg := config.RepositoryConfig{ Name: "test", Src: pathsToTest.baseTestRepository, Dest: pathsToTest.baseTestDirectory, } 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) { pathsToTest := prepareBasicRepository() dirNameForLocalRepository := "sub-repository" fsForLocalRepo, gitMetadataDirectory := prepareRepositoryDirectories( path.Join(pathsToTest.baseTestDirectory, dirNameForLocalRepository), ) storageForTestRepo := filesystem.NewStorage(gitMetadataDirectory, cache.NewObjectLRUDefault()) fakeLocalRepository, err := git.Clone(storageForTestRepo, fsForLocalRepo, &git.CloneOptions{ URL: pathsToTest.baseTestRepository, }) checkErrorDuringPreparation(err) err = fakeLocalRepository.DeleteRemote("origin") checkErrorDuringPreparation(err) sc := StatusChecker{ workspace: pathsToTest.baseTestDirectory, } repoCfg := config.RepositoryConfig{ Name: "test", Src: pathsToTest.baseTestRepository, 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) { 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, } 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) { pathsToTest := prepareBasicRepository() subRepositoryDirectoryName := "sub-repository" fakeLocalRepository := createAndCloneRepository(subRepositoryDirectoryName, pathsToTest) sc := StatusChecker{ workspace: pathsToTest.baseTestDirectory, } repoCfg := config.RepositoryConfig{ Name: "test", Src: pathsToTest.baseTestRepository, Dest: subRepositoryDirectoryName, } 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 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) } 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 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) } 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) } }