diff --git a/internal/homesick/core/generate_test.go b/internal/homesick/core/generate_test.go new file mode 100644 index 0000000..76b4ad6 --- /dev/null +++ b/internal/homesick/core/generate_test.go @@ -0,0 +1,69 @@ +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 GenerateSuite struct { + suite.Suite + tmpDir string + app *core.App +} + +func TestGenerateSuite(t *testing.T) { + suite.Run(t, new(GenerateSuite)) +} + +func (s *GenerateSuite) SetupTest() { + s.tmpDir = s.T().TempDir() + s.app = &core.App{ + HomeDir: filepath.Join(s.tmpDir, "home"), + ReposDir: filepath.Join(s.tmpDir, "home", ".homesick", "repos"), + Stdout: io.Discard, + Stderr: io.Discard, + } +} + +func (s *GenerateSuite) TestGenerate_CreatesGitRepoAndHomeDir() { + castlePath := filepath.Join(s.tmpDir, "my-castle") + + require.NoError(s.T(), s.app.Generate(castlePath)) + require.DirExists(s.T(), castlePath) + require.DirExists(s.T(), filepath.Join(castlePath, ".git")) + require.DirExists(s.T(), filepath.Join(castlePath, "home")) +} + +func (s *GenerateSuite) TestGenerate_AddsOriginWhenGitHubUserConfigured() { + castlePath := filepath.Join(s.tmpDir, "my-castle") + gitConfig := filepath.Join(s.tmpDir, "gitconfig") + require.NoError(s.T(), os.WriteFile(gitConfig, []byte("[github]\n\tuser = octocat\n"), 0o644)) + s.T().Setenv("GIT_CONFIG_GLOBAL", gitConfig) + s.T().Setenv("GIT_CONFIG_NOSYSTEM", "1") + + require.NoError(s.T(), s.app.Generate(castlePath)) + + configPath := filepath.Join(castlePath, ".git", "config") + content, err := os.ReadFile(configPath) + require.NoError(s.T(), err) + require.Contains(s.T(), string(content), "git@github.com:octocat/my-castle.git") +} + +func (s *GenerateSuite) TestGenerate_DoesNotAddOriginWhenGitHubUserMissing() { + castlePath := filepath.Join(s.tmpDir, "my-castle") + s.T().Setenv("GIT_CONFIG_GLOBAL", filepath.Join(s.tmpDir, "nonexistent-config")) + s.T().Setenv("GIT_CONFIG_NOSYSTEM", "1") + + require.NoError(s.T(), s.app.Generate(castlePath)) + + configPath := filepath.Join(castlePath, ".git", "config") + content, err := os.ReadFile(configPath) + require.NoError(s.T(), err) + require.NotContains(s.T(), string(content), "[remote \"origin\"]") +} diff --git a/internal/homesick/core/open_test.go b/internal/homesick/core/open_test.go new file mode 100644 index 0000000..16eefb2 --- /dev/null +++ b/internal/homesick/core/open_test.go @@ -0,0 +1,86 @@ +package core_test + +import ( + "bytes" + "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 OpenSuite struct { + suite.Suite + tmpDir string + homeDir string + reposDir string + stdout *bytes.Buffer + stderr *bytes.Buffer + app *core.App +} + +func TestOpenSuite(t *testing.T) { + suite.Run(t, new(OpenSuite)) +} + +func (s *OpenSuite) 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.stdout = &bytes.Buffer{} + s.stderr = &bytes.Buffer{} + s.app = &core.App{ + HomeDir: s.homeDir, + ReposDir: s.reposDir, + Stdout: s.stdout, + Stderr: s.stderr, + } +} + +func (s *OpenSuite) 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 *OpenSuite) TestOpen_RequiresEditorEnv() { + s.createCastleRepo("dotfiles") + s.T().Setenv("EDITOR", "") + + err := s.app.Open("dotfiles") + require.Error(s.T(), err) + require.Contains(s.T(), err.Error(), "$EDITOR") +} + +func (s *OpenSuite) TestOpen_MissingCastleReturnsError() { + s.T().Setenv("EDITOR", "vim") + + err := s.app.Open("missing") + require.Error(s.T(), err) + require.Contains(s.T(), err.Error(), "could not open") +} + +func (s *OpenSuite) TestOpen_RunsEditorInCastleRoot() { + castleRoot := s.createCastleRepo("dotfiles") + + capture := filepath.Join(s.tmpDir, "open_capture.txt") + editorScript := filepath.Join(s.tmpDir, "editor.sh") + require.NoError(s.T(), os.WriteFile(editorScript, []byte("#!/bin/sh\npwd > \""+capture+"\"\necho \"$1\" >> \""+capture+"\"\n"), 0o755)) + s.T().Setenv("EDITOR", editorScript) + + require.NoError(s.T(), s.app.Open("dotfiles")) + + content, err := os.ReadFile(capture) + require.NoError(s.T(), err) + require.Equal(s.T(), castleRoot+"\n.\n", string(content)) +} + +var _ io.Writer