Synchronization tool file history tests modified to set up a temporary git repo (#138)

This commit is contained in:
Domas Monkus 2020-11-03 11:19:36 +02:00 committed by GitHub
parent e8103502da
commit 54637e7313
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 122 additions and 46 deletions

View file

@ -7,8 +7,10 @@ import (
"path"
"path/filepath"
"testing"
"time"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/stretchr/testify/assert"
"github.com/mattermost/mattermost-plugin-starter-template/build/sync/plan"
@ -48,7 +50,13 @@ func TestRepoIsCleanChecker(t *testing.T) {
func TestPathExistsChecker(t *testing.T) {
assert := assert.New(t)
wd, err := os.Getwd()
// Set up a working directory.
wd, err := ioutil.TempDir("", "repo")
assert.Nil(err)
defer os.RemoveAll(wd)
err = os.Mkdir(filepath.Join(wd, "t"), 0755)
assert.Nil(err)
err = ioutil.WriteFile(filepath.Join(wd, "t", "test"), []byte("lorem ipsum"), 0644)
assert.Nil(err)
checker := plan.PathExistsChecker{}
@ -61,30 +69,60 @@ func TestPathExistsChecker(t *testing.T) {
}
// Check with existing directory.
assert.Nil(checker.Check("testdata", ctx))
assert.Nil(checker.Check("t", ctx))
// Check with existing file.
assert.Nil(checker.Check("testdata/a", ctx))
assert.Nil(checker.Check("t/test", ctx))
err = checker.Check("nosuchpath", ctx)
assert.NotNil(err)
assert.True(plan.IsCheckFail(err))
}
func tempGitRepo(assert *assert.Assertions) (string, *git.Repository, func()) {
// Setup repository.
wd, err := ioutil.TempDir("", "repo")
assert.Nil(err)
// Initialize a repository.
repo, err := git.PlainInit(wd, false)
assert.Nil(err)
w, err := repo.Worktree()
assert.Nil(err)
// Create repository files.
err = ioutil.WriteFile(filepath.Join(wd, "test"),
[]byte("lorem ipsum"), 0644)
assert.Nil(err)
sig := &object.Signature{
Name: "test",
Email: "test@example.com",
When: time.Now(),
}
_, err = w.Commit("initial commit", &git.CommitOptions{Author: sig})
assert.Nil(err)
pathA := "a.txt"
err = ioutil.WriteFile(filepath.Join(wd, pathA),
[]byte("lorem ipsum"), 0644)
assert.Nil(err)
_, err = w.Add(pathA)
assert.Nil(err)
_, err = w.Commit("add files", &git.CommitOptions{Author: sig})
assert.Nil(err)
return wd, repo, func() { os.RemoveAll(wd) }
}
func TestUnalteredCheckerSameFile(t *testing.T) {
assert := assert.New(t)
// Path to the root of the repo.
wd, err := filepath.Abs("../../../")
assert.Nil(err)
gitRepo, err := git.PlainOpen(wd)
assert.Nil(err)
wd, repo, cleanup := tempGitRepo(assert)
defer cleanup()
ctx := plan.Setup{
Source: plan.RepoSetup{
Path: wd,
Git: gitRepo,
Git: repo,
},
Target: plan.RepoSetup{
Path: wd,
@ -96,25 +134,21 @@ func TestUnalteredCheckerSameFile(t *testing.T) {
checker.Params.TargetRepo = plan.TargetRepo
// Check with the same file - check should succeed
hashPath := "build/sync/plan/testdata/a"
err = checker.Check(hashPath, ctx)
hashPath := "a.txt"
err := checker.Check(hashPath, ctx)
assert.Nil(err)
}
func TestUnalteredCheckerDifferentContents(t *testing.T) {
assert := assert.New(t)
// Path to the root of the repo.
wd, err := filepath.Abs("../../../")
assert.Nil(err)
gitRepo, err := git.PlainOpen(wd)
assert.Nil(err)
wd, repo, cleanup := tempGitRepo(assert)
defer cleanup()
ctx := plan.Setup{
Source: plan.RepoSetup{
Path: wd,
Git: gitRepo,
Git: repo,
},
Target: plan.RepoSetup{
Path: wd,
@ -126,25 +160,18 @@ func TestUnalteredCheckerDifferentContents(t *testing.T) {
checker.Params.TargetRepo = plan.TargetRepo
// Create a file with the same suffix path, but different contents.
hashPath := "build/sync/plan/testdata/a"
tmpDir, err := ioutil.TempDir("", "test")
assert.Nil(err)
defer os.RemoveAll(tmpDir)
fullPath := filepath.Join(tmpDir, "build/sync/plan/testdata")
err = os.MkdirAll(fullPath, 0777)
err = ioutil.WriteFile(filepath.Join(tmpDir, "a.txt"),
[]byte("not lorem ipsum"), 0644)
assert.Nil(err)
file, err := os.OpenFile(filepath.Join(fullPath, "a"), os.O_CREATE|os.O_WRONLY, 0755)
assert.Nil(err)
_, err = file.WriteString("this file has different contents")
assert.Nil(err)
assert.Nil(file.Close())
// Set the plugin path to the temporary directory.
ctx.Target.Path = tmpDir
err = checker.Check(hashPath, ctx)
err = checker.Check("a.txt", ctx)
assert.True(plan.IsCheckFail(err))
assert.EqualError(err, fmt.Sprintf("file %q has been altered", filepath.Join(tmpDir, hashPath)))
assert.EqualError(err, fmt.Sprintf("file %q has been altered", filepath.Join(tmpDir, "a.txt")))
}
@ -153,13 +180,10 @@ func TestUnalteredCheckerDifferentContents(t *testing.T) {
// the checker should pass.
func TestUnalteredCheckerNonExistant(t *testing.T) {
assert := assert.New(t)
hashPath := "build/sync/plan/testdata/a"
hashPath := "a.txt"
// Path to the root of the repo.
wd, err := filepath.Abs("../../../")
assert.Nil(err)
gitRepo, err := git.PlainOpen(wd)
assert.Nil(err)
wd, repo, cleanup := tempGitRepo(assert)
defer cleanup()
// Temporary repo.
tmpDir, err := ioutil.TempDir("", "test")
@ -172,7 +196,7 @@ func TestUnalteredCheckerNonExistant(t *testing.T) {
ctx := plan.Setup{
Source: plan.RepoSetup{
Path: wd,
Git: gitRepo,
Git: repo,
},
Target: plan.RepoSetup{
Path: tmpDir,