87 lines
2.1 KiB
Go
87 lines
2.1 KiB
Go
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
|