Improve code readability #1
+ Improved tests readability of common_utils_test.go + Fixed minor typos
This commit is contained in:
parent
8cd3ed2127
commit
68636d5e64
8 changed files with 167 additions and 123 deletions
|
|
@ -2,7 +2,10 @@ 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"
|
||||
|
|
@ -12,10 +15,10 @@ import (
|
|||
)
|
||||
|
||||
func TestIfBranchesAreEqual(t *testing.T) {
|
||||
tmpDirWithInitialRepository := getTestSetup()
|
||||
pathsToTest := prepareBasicRepository()
|
||||
|
||||
fakeLocalRepo, err := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
|
||||
URL: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
|
||||
URL: pathsToTest.baseTestRepository,
|
||||
})
|
||||
checkErrorDuringPreparation(err)
|
||||
|
||||
|
|
@ -42,9 +45,9 @@ func TestIfBranchesAreEqual(t *testing.T) {
|
|||
|
||||
func TestIfCurrentBranchIsDifferent(t *testing.T) {
|
||||
|
||||
tmpDirWithInitialRepository := getTestSetup()
|
||||
pathsToTest := prepareBasicRepository()
|
||||
fakeLocalRepo, err := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
|
||||
URL: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
|
||||
URL: pathsToTest.baseTestRepository,
|
||||
})
|
||||
checkErrorDuringPreparation(err)
|
||||
|
||||
|
|
@ -85,28 +88,21 @@ func TestIfCurrentBranchIsDifferent(t *testing.T) {
|
|||
|
||||
result = findNumberOfCommitDiffs(currentBranchCommit, remoteBranchCommit)
|
||||
if result != 15 {
|
||||
t.Errorf("Expected to get 5 changes, instead of this got %v", result)
|
||||
t.Errorf("Expected to get 15 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)
|
||||
|
||||
pathsToTest := prepareBasicRepository()
|
||||
sc := StatusChecker{
|
||||
workspace: tmpDirWithInitialRepository.rootFS.Root(),
|
||||
workspace: pathsToTest.baseTestDirectory,
|
||||
}
|
||||
|
||||
repoCfg := config.RepositoryConfig{
|
||||
Name: "test",
|
||||
Src: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
|
||||
Dest: tmpDirWithInitialRepository.rootFS.Root(),
|
||||
Src: pathsToTest.baseTestRepository,
|
||||
Dest: pathsToTest.baseTestDirectory,
|
||||
}
|
||||
|
||||
repoStatus := sc.Command(repoCfg)
|
||||
|
|
@ -125,12 +121,15 @@ func TestCommandRepositoryDoesNotExists(t *testing.T) {
|
|||
|
||||
func TestCommandRepositoryNoRemoteBranch(t *testing.T) {
|
||||
|
||||
tmpDirWithInitialRepository := getTestSetup()
|
||||
dirNameForLocalRepository := "testRepo"
|
||||
fsForLocalRepo, storageForTestRepo := getFSForLocalRepo(dirNameForLocalRepository, tmpDirWithInitialRepository.rootFS)
|
||||
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: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
|
||||
URL: pathsToTest.baseTestRepository,
|
||||
})
|
||||
checkErrorDuringPreparation(err)
|
||||
|
||||
|
|
@ -138,12 +137,12 @@ func TestCommandRepositoryNoRemoteBranch(t *testing.T) {
|
|||
checkErrorDuringPreparation(err)
|
||||
|
||||
sc := StatusChecker{
|
||||
workspace: tmpDirWithInitialRepository.rootFS.Root(),
|
||||
workspace: pathsToTest.baseTestDirectory,
|
||||
}
|
||||
|
||||
repoCfg := config.RepositoryConfig{
|
||||
Name: "test",
|
||||
Src: tmpDirWithInitialRepository.baseRepository.fileSystem.Root(),
|
||||
Src: pathsToTest.baseTestRepository,
|
||||
Dest: dirNameForLocalRepository,
|
||||
}
|
||||
|
||||
|
|
@ -164,7 +163,21 @@ func TestCommandRepositoryNoRemoteBranch(t *testing.T) {
|
|||
|
||||
func TestCommandAllCorrectWithoutChanges(t *testing.T) {
|
||||
|
||||
sc, _, repoCfg, _ := getBaseForTestingSyncCommand()
|
||||
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 )"
|
||||
|
|
@ -182,7 +195,19 @@ func TestCommandAllCorrectWithoutChanges(t *testing.T) {
|
|||
}
|
||||
func TestCommandAllCorrectWithOneChange(t *testing.T) {
|
||||
|
||||
sc, fakeLocalRepository, repoCfg, _ := getBaseForTestingSyncCommand()
|
||||
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)
|
||||
|
|
@ -207,9 +232,26 @@ func TestCommandAllCorrectWithOneChange(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCommandMultiRemoteNoChanges(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
|
||||
sc, _, repoCfg := getBaseForTestingSyncMultipleRemote()
|
||||
repoStatus := sc.Command(repoCfg)
|
||||
expectedMessage := "branch master - ( | origin | \u21910 \u21930 ) - ( | subremote | \u21910 \u21930 )"
|
||||
|
||||
|
|
@ -225,8 +267,25 @@ func TestCommandMultiRemoteNoChanges(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCommandMultiRemoteWithOneChange(t *testing.T) {
|
||||
sc, fakeLocalRepository, repoCfg := getBaseForTestingSyncMultipleRemote()
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue