2021-11-06 22:21:35 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-06-13 19:19:04 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-06-13 19:19:04 +00:00
|
|
|
temporaryDirPath, err := os.MkdirTemp(systemTMPDirectoryWithTestPath, "*")
|
|
|
|
checkErrorDuringPreparation(err)
|
|
|
|
return temporaryDirPath
|
|
|
|
}
|
2021-11-06 22:21:35 +00:00
|
|
|
|
2024-06-13 19:19:04 +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)
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
return rootRepositoryDirectory, gitMetadataDirectory
|
2021-11-06 22:21:35 +00:00
|
|
|
}
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
func prepareBasicRepository() TestSetupPaths {
|
2021-11-06 22:21:35 +00:00
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
temporaryDirPath := createRepositoryForTest()
|
2021-11-06 22:21:35 +00:00
|
|
|
|
2024-06-13 19:19:04 +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
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
repository, err := git.Init(filesystem.NewStorage(
|
|
|
|
gitMetadataDirectory,
|
|
|
|
cache.NewObjectLRUDefault()),
|
|
|
|
rootRepositoryDirectory,
|
|
|
|
)
|
2021-11-06 22:21:35 +00:00
|
|
|
checkErrorDuringPreparation(err)
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
testFile, err := rootRepositoryDirectory.Create("TestFile.txt")
|
2021-11-06 22:21:35 +00:00
|
|
|
checkErrorDuringPreparation(err)
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
_, err = testFile.Write([]byte("foo-conent"))
|
2021-11-06 22:21:35 +00:00
|
|
|
checkErrorDuringPreparation(err)
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
repositoryWorkTree, err := repository.Worktree()
|
2021-11-06 22:21:35 +00:00
|
|
|
checkErrorDuringPreparation(err)
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
_, err = repositoryWorkTree.Add(testFile.Name())
|
2021-11-06 22:21:35 +00:00
|
|
|
checkErrorDuringPreparation(err)
|
|
|
|
|
|
|
|
_, err = repositoryWorkTree.Commit("First commit", &git.CommitOptions{})
|
|
|
|
checkErrorDuringPreparation(err)
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
// 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
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
baseGitRepository, gitMetadataDirectory := prepareRepositoryDirectories(
|
|
|
|
path.Join(paths.baseTestDirectory, repositoryName),
|
|
|
|
)
|
2021-11-06 22:21:35 +00:00
|
|
|
|
2024-06-13 19:19:04 +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)
|
2024-06-13 19:19:04 +00:00
|
|
|
return fakeLocalRepository
|
2021-11-06 22:21:35 +00:00
|
|
|
}
|
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
func addLocalRepositoryAsAFakeRemoteRepository(repository *git.Repository, baseTestRepositoryPath string) error {
|
2021-11-06 22:21:35 +00:00
|
|
|
|
2024-06-13 19:19:04 +00:00
|
|
|
_, err := repository.CreateRemote(&gitcfg.RemoteConfig{
|
2021-11-06 22:21:35 +00:00
|
|
|
Name: "subremote",
|
2024-06-13 19:19:04 +00:00
|
|
|
URLs: []string{baseTestRepositoryPath},
|
2021-11-06 22:21:35 +00:00
|
|
|
})
|
2024-06-13 19:19:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return repository.Fetch(&git.FetchOptions{
|
2021-11-06 22:21:35 +00:00
|
|
|
RemoteName: "subremote",
|
|
|
|
})
|
|
|
|
}
|