From 4901f7b6646c55df99a0753d13caf1f862b707f8 Mon Sep 17 00:00:00 2001 From: Micheal Wilkinson Date: Fri, 20 Mar 2026 17:49:26 +0000 Subject: [PATCH] test(destroy): add failing destroy parity tests --- internal/homesick/core/destroy_test.go | 91 ++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 internal/homesick/core/destroy_test.go diff --git a/internal/homesick/core/destroy_test.go b/internal/homesick/core/destroy_test.go new file mode 100644 index 0000000..fe4400b --- /dev/null +++ b/internal/homesick/core/destroy_test.go @@ -0,0 +1,91 @@ +package core_test + +import ( + "io" + "os" + "path/filepath" + "testing" + + "git.hrafn.xyz/aether/gosick/internal/homesick/core" + git "github.com/go-git/go-git/v5" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +type DestroySuite struct { + suite.Suite + tmpDir string + homeDir string + reposDir string + app *core.App +} + +func TestDestroySuite(t *testing.T) { + suite.Run(t, new(DestroySuite)) +} + +func (s *DestroySuite) 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 *DestroySuite) createCastleRepo(castle string) string { + castleRoot := filepath.Join(s.reposDir, castle) + require.NoError(s.T(), os.MkdirAll(filepath.Join(castleRoot, "home"), 0o755)) + _, err := git.PlainInit(castleRoot, false) + require.NoError(s.T(), err) + return castleRoot +} + +func (s *DestroySuite) TestDestroy_RemovesCastleDirectory() { + castleRoot := s.createCastleRepo("dotfiles") + require.DirExists(s.T(), castleRoot) + + require.NoError(s.T(), s.app.Destroy("dotfiles")) + require.NoDirExists(s.T(), castleRoot) +} + +func (s *DestroySuite) TestDestroy_MissingCastleReturnsError() { + err := s.app.Destroy("missing") + require.Error(s.T(), err) +} + +func (s *DestroySuite) TestDestroy_UnlinksDotfilesBeforeRemoval() { + castleRoot := s.createCastleRepo("dotfiles") + tracked := filepath.Join(castleRoot, "home", ".vimrc") + require.NoError(s.T(), os.WriteFile(tracked, []byte("set number\n"), 0o644)) + + require.NoError(s.T(), s.app.LinkCastle("dotfiles")) + homePath := filepath.Join(s.homeDir, ".vimrc") + info, err := os.Lstat(homePath) + require.NoError(s.T(), err) + require.NotZero(s.T(), info.Mode()&os.ModeSymlink) + + require.NoError(s.T(), s.app.Destroy("dotfiles")) + + _, err = os.Lstat(homePath) + require.Error(s.T(), err) + require.True(s.T(), os.IsNotExist(err)) + require.NoDirExists(s.T(), castleRoot) +} + +func (s *DestroySuite) TestDestroy_RemovesSymlinkedCastleOnly() { + target := filepath.Join(s.tmpDir, "local-castle") + require.NoError(s.T(), os.MkdirAll(target, 0o755)) + + symlinkCastle := filepath.Join(s.reposDir, "dotfiles") + require.NoError(s.T(), os.Symlink(target, symlinkCastle)) + + require.NoError(s.T(), s.app.Destroy("dotfiles")) + require.NoFileExists(s.T(), symlinkCastle) + require.DirExists(s.T(), target) +}