From 4355e7fd9daed91d9053ef974677a1cfe597981f Mon Sep 17 00:00:00 2001 From: Micheal Wilkinson Date: Thu, 19 Mar 2026 14:29:03 +0000 Subject: [PATCH] test(core): add status diff and version suites --- internal/homesick/core/diff_test.go | 76 +++++++++++++++++++++++++ internal/homesick/core/status_test.go | 79 ++++++++++++++++++++++++++ internal/homesick/core/version_test.go | 46 +++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 internal/homesick/core/diff_test.go create mode 100644 internal/homesick/core/status_test.go create mode 100644 internal/homesick/core/version_test.go diff --git a/internal/homesick/core/diff_test.go b/internal/homesick/core/diff_test.go new file mode 100644 index 0000000..9879c6d --- /dev/null +++ b/internal/homesick/core/diff_test.go @@ -0,0 +1,76 @@ +package core_test + +import ( + "bytes" + "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/plumbing/object" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +type DiffSuite struct { + suite.Suite + tmpDir string + homeDir string + reposDir string + stdout *bytes.Buffer + stderr *bytes.Buffer + app *core.App +} + +func TestDiffSuite(t *testing.T) { + suite.Run(t, new(DiffSuite)) +} + +func (s *DiffSuite) 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 *DiffSuite) createCastleRepo(castle string) string { + castleRoot := filepath.Join(s.reposDir, castle) + repo, err := git.PlainInit(castleRoot, false) + require.NoError(s.T(), err) + + filePath := filepath.Join(castleRoot, "home", ".vimrc") + require.NoError(s.T(), os.MkdirAll(filepath.Dir(filePath), 0o755)) + require.NoError(s.T(), os.WriteFile(filePath, []byte("set number\n"), 0o644)) + + wt, err := repo.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: "Behavior Test", + Email: "behavior@test.local", + When: time.Now(), + }}) + require.NoError(s.T(), err) + + return castleRoot +} + +func (s *DiffSuite) TestDiff_WritesGitDiffToAppStdout() { + castleRoot := s.createCastleRepo("castle_repo") + require.NoError(s.T(), os.WriteFile(filepath.Join(castleRoot, "home", ".vimrc"), []byte("changed\n"), 0o644)) + + require.NoError(s.T(), s.app.Diff("castle_repo")) + require.Contains(s.T(), s.stdout.String(), "diff --git") +} diff --git a/internal/homesick/core/status_test.go b/internal/homesick/core/status_test.go new file mode 100644 index 0000000..5448b55 --- /dev/null +++ b/internal/homesick/core/status_test.go @@ -0,0 +1,79 @@ +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/plumbing/object" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +type StatusSuite struct { + suite.Suite + tmpDir string + homeDir string + reposDir string + stdout *bytes.Buffer + stderr *bytes.Buffer + app *core.App +} + +func TestStatusSuite(t *testing.T) { + suite.Run(t, new(StatusSuite)) +} + +func (s *StatusSuite) 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 *StatusSuite) createCastleRepo(castle string) string { + castleRoot := filepath.Join(s.reposDir, castle) + repo, err := git.PlainInit(castleRoot, false) + require.NoError(s.T(), err) + + filePath := filepath.Join(castleRoot, "home", ".vimrc") + require.NoError(s.T(), os.MkdirAll(filepath.Dir(filePath), 0o755)) + require.NoError(s.T(), os.WriteFile(filePath, []byte("set number\n"), 0o644)) + + wt, err := repo.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: "Behavior Test", + Email: "behavior@test.local", + When: time.Now(), + }}) + require.NoError(s.T(), err) + + return castleRoot +} + +func (s *StatusSuite) TestStatus_WritesGitStatusToAppStdout() { + castleRoot := s.createCastleRepo("castle_repo") + require.NoError(s.T(), os.WriteFile(filepath.Join(castleRoot, "home", ".vimrc"), []byte("changed\n"), 0o644)) + + require.NoError(s.T(), s.app.Status("castle_repo")) + require.Contains(s.T(), s.stdout.String(), "modified:") +} + +var _ io.Writer diff --git a/internal/homesick/core/version_test.go b/internal/homesick/core/version_test.go new file mode 100644 index 0000000..b28f03e --- /dev/null +++ b/internal/homesick/core/version_test.go @@ -0,0 +1,46 @@ +package core_test + +import ( + "bytes" + "io" + "os" + "path/filepath" + "testing" + + "git.hrafn.xyz/aether/gosick/internal/homesick/core" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +type VersionSuite struct { + suite.Suite + tmpDir string + homeDir string + reposDir string + stdout *bytes.Buffer + app *core.App +} + +func TestVersionSuite(t *testing.T) { + suite.Run(t, new(VersionSuite)) +} + +func (s *VersionSuite) 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.app = &core.App{ + HomeDir: s.homeDir, + ReposDir: s.reposDir, + Stdout: s.stdout, + Stderr: io.Discard, + } +} + +func (s *VersionSuite) TestVersion_WritesVersionToAppStdout() { + require.NoError(s.T(), s.app.Version("1.2.3")) + require.Equal(s.T(), "1.2.3\n", s.stdout.String()) +}