GitRepositoryManager/internal/commands/common_utils_test.go

119 lines
3.7 KiB
Go
Raw Normal View History

2021-11-06 22:21:35 +00:00
package commands
import (
"fmt"
"os"
"path"
2021-11-06 22:21:35 +00:00
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5"
gitcfg "github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/storage/filesystem"
)
type TestSetupPaths struct {
baseTestDirectory string
baseTestRepository string
2021-11-06 22:21:35 +00:00
}
func checkErrorDuringPreparation(err error) {
if err != nil {
fmt.Printf("Cannot prepare a temporary directory for testing! %v ", err.Error())
os.Exit(2)
}
}
func createRepositoryForTest() string {
systemTMPDirectoryWithTestPath := fmt.Sprintf("%v/grmTest", os.TempDir())
if _, ok := os.Stat(systemTMPDirectoryWithTestPath); ok != nil {
err := os.Mkdir(systemTMPDirectoryWithTestPath, 0777)
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
}
temporaryDirPath, err := os.MkdirTemp(systemTMPDirectoryWithTestPath, "*")
checkErrorDuringPreparation(err)
return temporaryDirPath
}
2021-11-06 22:21:35 +00:00
// prepareRepositoryDirectories - prepare directories for file (rootRepositoryDirectory) and git metadata (gitMetadataDirectory)
func prepareRepositoryDirectories(dirName string) (billy.Filesystem, billy.Filesystem) {
rootRepositoryDirectory := osfs.New(dirName)
gitMetadataDirectory, err := rootRepositoryDirectory.Chroot(".git")
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
return rootRepositoryDirectory, gitMetadataDirectory
2021-11-06 22:21:35 +00:00
}
func prepareBasicRepository() TestSetupPaths {
2021-11-06 22:21:35 +00:00
temporaryDirPath := createRepositoryForTest()
2021-11-06 22:21:35 +00:00
// Create an interface of abstraction over filesystem to provide tests over multiple systems
// baseTestsDirectory - provides to main directory where new directories might be created
baseTestsDirectory := osfs.New(temporaryDirPath)
rootRepositoryDirectory, gitMetadataDirectory := prepareRepositoryDirectories(baseTestsDirectory.Root() + "/base_repository")
2021-11-06 22:21:35 +00:00
repository, err := git.Init(filesystem.NewStorage(
gitMetadataDirectory,
cache.NewObjectLRUDefault()),
rootRepositoryDirectory,
)
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
testFile, err := rootRepositoryDirectory.Create("TestFile.txt")
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
_, err = testFile.Write([]byte("foo-conent"))
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
repositoryWorkTree, err := repository.Worktree()
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
_, err = repositoryWorkTree.Add(testFile.Name())
2021-11-06 22:21:35 +00:00
checkErrorDuringPreparation(err)
_, err = repositoryWorkTree.Commit("First commit", &git.CommitOptions{})
checkErrorDuringPreparation(err)
return TestSetupPaths{
baseTestDirectory: baseTestsDirectory.Root(),
baseTestRepository: rootRepositoryDirectory.Root(),
2021-11-06 22:21:35 +00:00
}
}
func makeCommit(wk *git.Worktree, commitMessage string) {
_, err := wk.Commit(commitMessage, &git.CommitOptions{})
checkErrorDuringPreparation(err)
}
// createAndCloneRepository - create sub-repository with cloned base repository required to verify sync command
func createAndCloneRepository(repositoryName string, paths TestSetupPaths) *git.Repository {
2021-11-06 22:21:35 +00:00
baseGitRepository, gitMetadataDirectory := prepareRepositoryDirectories(
path.Join(paths.baseTestDirectory, repositoryName),
)
2021-11-06 22:21:35 +00:00
storageForSubRepository := filesystem.NewStorage(gitMetadataDirectory, cache.NewObjectLRUDefault())
fakeLocalRepository, err := git.Clone(storageForSubRepository, baseGitRepository, &git.CloneOptions{
URL: paths.baseTestRepository,
2021-11-06 22:21:35 +00:00
})
checkErrorDuringPreparation(err)
return fakeLocalRepository
2021-11-06 22:21:35 +00:00
}
func addLocalRepositoryAsAFakeRemoteRepository(repository *git.Repository, baseTestRepositoryPath string) error {
2021-11-06 22:21:35 +00:00
_, err := repository.CreateRemote(&gitcfg.RemoteConfig{
2021-11-06 22:21:35 +00:00
Name: "subremote",
URLs: []string{baseTestRepositoryPath},
2021-11-06 22:21:35 +00:00
})
if err != nil {
return err
}
return repository.Fetch(&git.FetchOptions{
2021-11-06 22:21:35 +00:00
RemoteName: "subremote",
})
}