- internal/homesick/version: new version_test.go covers String constant and semver format validation - internal/homesick/cli: add list, generate, clone, status, diff, and git-repo helper tests; coverage raised from 62.5% to 71.2% - internal/homesick/core: new helpers_test.go covers runGit pretend, actionVerb, sayStatus, unlinkPath, linkPath, readSubdirs, matchesIgnoredDir, confirmDestroy, ExecAll edge cases, and Link/Unlink default castle wrappers; core_test.go and pull_test.go extended with New constructor and PullAll quiet-mode tests; exec_test.go extended with ExecAll no-repos-dir and error-wrap tests; coverage raised from 75.6% to 80.2%
157 lines
4.6 KiB
Go
157 lines
4.6 KiB
Go
package core_test
|
|
|
|
import (
|
|
"bytes"
|
|
"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)
|
|
}
|
|
|
|
func (s *PullSuite) TestPullAll_UpdatesAllCastlesFromOrigin() {
|
|
remoteA, cloneA := s.createRemoteWithClone("alpha")
|
|
remoteB, cloneB := s.createRemoteWithClone("zeta")
|
|
s.addRemoteCommit(remoteA, "alpha")
|
|
s.addRemoteCommit(remoteB, "zeta")
|
|
|
|
require.NoError(s.T(), s.app.PullAll())
|
|
require.FileExists(s.T(), filepath.Join(cloneA, "home", ".zshrc"))
|
|
require.FileExists(s.T(), filepath.Join(cloneB, "home", ".zshrc"))
|
|
}
|
|
|
|
func (s *PullSuite) TestPullAll_NoCastlesIsNoop() {
|
|
require.NoError(s.T(), s.app.PullAll())
|
|
}
|
|
|
|
func (s *PullSuite) TestPullAll_PrintsCastlePrefixes() {
|
|
_, _ = s.createRemoteWithClone("alpha")
|
|
_, _ = s.createRemoteWithClone("zeta")
|
|
|
|
stdout := &bytes.Buffer{}
|
|
s.app.Stdout = stdout
|
|
|
|
require.NoError(s.T(), s.app.PullAll())
|
|
require.Contains(s.T(), stdout.String(), "alpha:")
|
|
require.Contains(s.T(), stdout.String(), "zeta:")
|
|
}
|
|
|
|
func (s *PullSuite) TestPullAll_QuietSuppressesCastlePrefixes() {
|
|
require.NoError(s.T(), os.MkdirAll(filepath.Join(s.reposDir, "alpha", ".git"), 0o755))
|
|
require.NoError(s.T(), os.MkdirAll(filepath.Join(s.reposDir, "zeta", ".git"), 0o755))
|
|
|
|
stdout := &bytes.Buffer{}
|
|
s.app.Stdout = stdout
|
|
s.app.Quiet = true
|
|
s.app.Pretend = true
|
|
|
|
require.NoError(s.T(), s.app.PullAll())
|
|
require.NotContains(s.T(), stdout.String(), "alpha:")
|
|
require.NotContains(s.T(), stdout.String(), "zeta:")
|
|
}
|