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") }