package core_test import ( "io" "os" "path/filepath" "testing" "git.hrafn.xyz/aether/gosick/internal/homesick/core" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) type UnlinkSuite struct { suite.Suite tmpDir string homeDir string reposDir string app *core.App } func TestUnlinkSuite(t *testing.T) { suite.Run(t, new(UnlinkSuite)) } func (s *UnlinkSuite) 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 *UnlinkSuite) createCastle(castle string) string { castleHome := filepath.Join(s.reposDir, castle, "home") require.NoError(s.T(), os.MkdirAll(castleHome, 0o755)) return castleHome } func (s *UnlinkSuite) writeFile(path string, content string) { require.NoError(s.T(), os.MkdirAll(filepath.Dir(path), 0o755)) require.NoError(s.T(), os.WriteFile(path, []byte(content), 0o644)) } func (s *UnlinkSuite) TestUnlink_RemovesTopLevelSymlinks() { castleHome := s.createCastle("glencairn") dotfile := filepath.Join(castleHome, ".vimrc") s.writeFile(dotfile, "set number\n") require.NoError(s.T(), s.app.Link("glencairn")) require.NoError(s.T(), s.app.Unlink("glencairn")) require.NoFileExists(s.T(), filepath.Join(s.homeDir, ".vimrc")) } func (s *UnlinkSuite) TestUnlink_RemovesNonDotfileSymlinks() { castleHome := s.createCastle("glencairn") binFile := filepath.Join(castleHome, "bin") s.writeFile(binFile, "#!/usr/bin/env bash\n") require.NoError(s.T(), s.app.Link("glencairn")) require.NoError(s.T(), s.app.Unlink("glencairn")) require.NoFileExists(s.T(), filepath.Join(s.homeDir, "bin")) } func (s *UnlinkSuite) TestUnlink_RespectsHomesickSubdir() { castleHome := s.createCastle("glencairn") appDir := filepath.Join(castleHome, ".config", "myapp") s.writeFile(filepath.Join(appDir, "config.toml"), "ok=true\n") s.writeFile(filepath.Join(s.reposDir, "glencairn", ".homesick_subdir"), ".config\n") require.NoError(s.T(), s.app.Link("glencairn")) require.NoError(s.T(), s.app.Unlink("glencairn")) require.DirExists(s.T(), filepath.Join(s.homeDir, ".config")) require.NoFileExists(s.T(), filepath.Join(s.homeDir, ".config", "myapp")) } func (s *UnlinkSuite) TestUnlink_DefaultCastleName() { castleHome := s.createCastle("dotfiles") dotfile := filepath.Join(castleHome, ".zshrc") s.writeFile(dotfile, "export EDITOR=vim\n") require.NoError(s.T(), s.app.Link("dotfiles")) require.NoError(s.T(), s.app.Unlink("")) require.NoFileExists(s.T(), filepath.Join(s.homeDir, ".zshrc")) } func (s *UnlinkSuite) TestUnlink_IgnoresNonSymlinkDestination() { castleHome := s.createCastle("glencairn") dotfile := filepath.Join(castleHome, ".gitconfig") s.writeFile(dotfile, "[user]\n") s.writeFile(filepath.Join(s.homeDir, ".gitconfig"), "local\n") require.NoError(s.T(), s.app.Unlink("glencairn")) require.FileExists(s.T(), filepath.Join(s.homeDir, ".gitconfig")) }