gosick #1
116
internal/homesick/core/push_test.go
Normal file
116
internal/homesick/core/push_test.go
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
package core_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.hrafn.xyz/aether/gosick/internal/homesick/core"
|
||||||
|
git "github.com/go-git/go-git/v5"
|
||||||
|
"github.com/go-git/go-git/v5/config"
|
||||||
|
"github.com/go-git/go-git/v5/plumbing/object"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PushSuite struct {
|
||||||
|
suite.Suite
|
||||||
|
tmpDir string
|
||||||
|
homeDir string
|
||||||
|
reposDir string
|
||||||
|
app *core.App
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPushSuite(t *testing.T) {
|
||||||
|
suite.Run(t, new(PushSuite))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *PushSuite) SetupTest() {
|
||||||
|
s.tmpDir = s.T().TempDir()
|
||||||
|
s.homeDir = filepath.Join(s.tmpDir, "home")
|
||||||
|
s.reposDir = filepath.Join(s.homeDir, ".homesick", "repos")
|
||||||
|
require.NoError(s.T(), os.MkdirAll(s.reposDir, 0o755))
|
||||||
|
|
||||||
|
s.app = &core.App{
|
||||||
|
HomeDir: s.homeDir,
|
||||||
|
ReposDir: s.reposDir,
|
||||||
|
Stdout: io.Discard,
|
||||||
|
Stderr: io.Discard,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *PushSuite) createRemoteAndClone(castle string) (string, string) {
|
||||||
|
remotePath := filepath.Join(s.tmpDir, castle+".git")
|
||||||
|
_, err := git.PlainInit(remotePath, true)
|
||||||
|
require.NoError(s.T(), err)
|
||||||
|
|
||||||
|
seedPath := filepath.Join(s.tmpDir, castle+"-seed")
|
||||||
|
seedRepo, err := git.PlainInit(seedPath, false)
|
||||||
|
require.NoError(s.T(), err)
|
||||||
|
|
||||||
|
seedFile := filepath.Join(seedPath, "home", ".vimrc")
|
||||||
|
require.NoError(s.T(), os.MkdirAll(filepath.Dir(seedFile), 0o755))
|
||||||
|
require.NoError(s.T(), os.WriteFile(seedFile, []byte("set number\n"), 0o644))
|
||||||
|
|
||||||
|
wt, err := seedRepo.Worktree()
|
||||||
|
require.NoError(s.T(), err)
|
||||||
|
_, err = wt.Add("home/.vimrc")
|
||||||
|
require.NoError(s.T(), err)
|
||||||
|
|
||||||
|
_, err = wt.Commit("initial", &git.CommitOptions{Author: &object.Signature{
|
||||||
|
Name: "Push Test",
|
||||||
|
Email: "push@test.local",
|
||||||
|
When: time.Now(),
|
||||||
|
}})
|
||||||
|
require.NoError(s.T(), err)
|
||||||
|
|
||||||
|
_, err = seedRepo.CreateRemote(&config.RemoteConfig{Name: "origin", URLs: []string{remotePath}})
|
||||||
|
require.NoError(s.T(), err)
|
||||||
|
require.NoError(s.T(), seedRepo.Push(&git.PushOptions{RemoteName: "origin"}))
|
||||||
|
|
||||||
|
clonePath := filepath.Join(s.reposDir, castle)
|
||||||
|
_, err = git.PlainClone(clonePath, false, &git.CloneOptions{URL: remotePath})
|
||||||
|
require.NoError(s.T(), err)
|
||||||
|
|
||||||
|
return remotePath, clonePath
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *PushSuite) createLocalCommit(clonePath string) {
|
||||||
|
repo, err := git.PlainOpen(clonePath)
|
||||||
|
require.NoError(s.T(), err)
|
||||||
|
|
||||||
|
localFile := filepath.Join(clonePath, "home", ".zshrc")
|
||||||
|
require.NoError(s.T(), os.MkdirAll(filepath.Dir(localFile), 0o755))
|
||||||
|
require.NoError(s.T(), os.WriteFile(localFile, []byte("export EDITOR=vim\n"), 0o644))
|
||||||
|
|
||||||
|
wt, err := repo.Worktree()
|
||||||
|
require.NoError(s.T(), err)
|
||||||
|
_, err = wt.Add("home/.zshrc")
|
||||||
|
require.NoError(s.T(), err)
|
||||||
|
|
||||||
|
_, err = wt.Commit("add zshrc", &git.CommitOptions{Author: &object.Signature{
|
||||||
|
Name: "Push Test",
|
||||||
|
Email: "push@test.local",
|
||||||
|
When: time.Now(),
|
||||||
|
}})
|
||||||
|
require.NoError(s.T(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *PushSuite) TestPush_UpdatesRemoteFromLocalChanges() {
|
||||||
|
remotePath, clonePath := s.createRemoteAndClone("dotfiles")
|
||||||
|
s.createLocalCommit(clonePath)
|
||||||
|
|
||||||
|
require.NoError(s.T(), s.app.Push("dotfiles"))
|
||||||
|
|
||||||
|
verifyPath := filepath.Join(s.tmpDir, "dotfiles-verify")
|
||||||
|
_, err := git.PlainClone(verifyPath, false, &git.CloneOptions{URL: remotePath})
|
||||||
|
require.NoError(s.T(), err)
|
||||||
|
require.FileExists(s.T(), filepath.Join(verifyPath, "home", ".zshrc"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *PushSuite) TestPush_MissingCastleReturnsError() {
|
||||||
|
err := s.app.Push("missing")
|
||||||
|
require.Error(s.T(), err)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user