How do I write a unit test for my code that clones a repo using git-go
Below is a sample of the function I have created. I am cloning multiple repos and reading a particular file that is in that repo, I am unsure how to unit test this function.
func cloneRepository(repository string) (repo beans.Repo) {
dir, err := os.MkdirTemp("./", "temp") //To create a temp folder to clone repo in
if err != nil...
_, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: repository,
Depth: 1,
})
var repo beans.Repo
if err = util.ParseYmlFile("filename.yml", &repo) // Custom util function to parse a file in the repository
if err = os.RemoveAll(dir); err != nil{...}
return repo
}
CodePudding user response:
You can mock the git.PlainClone()
function so it returns a custom file for your tests.
Take a look into spf13's lib, that provides a filesystem mocking solution!
CodePudding user response:
What we did in the past was create a bare git repository with some predefined content, put it under e.g. testdata/myrepo.git
and use it during unit-testing.
Commit the repo normally as part of your project.