From 4a422bd241fc9f99bb8c326d4a61deb58aca2292 Mon Sep 17 00:00:00 2001 From: Micheal Wilkinson Date: Fri, 20 Mar 2026 17:43:39 +0000 Subject: [PATCH] test(pull): add failing pull parity tests --- internal/homesick/core/pull_test.go | 114 ++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 internal/homesick/core/pull_test.go diff --git a/internal/homesick/core/pull_test.go b/internal/homesick/core/pull_test.go new file mode 100644 index 0000000..6bc5824 --- /dev/null +++ b/internal/homesick/core/pull_test.go @@ -0,0 +1,114 @@ +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 PullSuite struct { + suite.Suite + tmpDir string + homeDir string + reposDir string + app *core.App +} + +func TestPullSuite(t *testing.T) { + suite.Run(t, new(PullSuite)) +} + +func (s *PullSuite) 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 *PullSuite) createRemoteWithClone(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: "Pull Test", + Email: "pull@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 *PullSuite) addRemoteCommit(remotePath string, castle string) { + workPath := filepath.Join(s.tmpDir, castle+"-work") + repo, err := git.PlainClone(workPath, false, &git.CloneOptions{URL: remotePath}) + require.NoError(s.T(), err) + + filePath := filepath.Join(workPath, "home", ".zshrc") + require.NoError(s.T(), os.MkdirAll(filepath.Dir(filePath), 0o755)) + require.NoError(s.T(), os.WriteFile(filePath, []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: "Pull Test", + Email: "pull@test.local", + When: time.Now(), + }}) + require.NoError(s.T(), err) + require.NoError(s.T(), repo.Push(&git.PushOptions{RemoteName: "origin"})) +} + +func (s *PullSuite) TestPull_UpdatesCastleFromOrigin() { + remotePath, clonePath := s.createRemoteWithClone("dotfiles") + s.addRemoteCommit(remotePath, "dotfiles") + + require.NoError(s.T(), s.app.Pull("dotfiles")) + require.FileExists(s.T(), filepath.Join(clonePath, "home", ".zshrc")) +} + +func (s *PullSuite) TestPull_MissingCastleReturnsError() { + err := s.app.Pull("missing") + require.Error(s.T(), err) +}