feat(cli): implement exec and exec_all commands
This commit is contained in:
79
internal/homesick/core/exec_test.go
Normal file
79
internal/homesick/core/exec_test.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package core_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.hrafn.xyz/aether/gosick/internal/homesick/core"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type ExecSuite struct {
|
||||
suite.Suite
|
||||
tmpDir string
|
||||
homeDir string
|
||||
reposDir string
|
||||
stdout *bytes.Buffer
|
||||
stderr *bytes.Buffer
|
||||
app *core.App
|
||||
}
|
||||
|
||||
func TestExecSuite(t *testing.T) {
|
||||
suite.Run(t, new(ExecSuite))
|
||||
}
|
||||
|
||||
func (s *ExecSuite) 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 *ExecSuite) createCastle(name string) string {
|
||||
castleRoot := filepath.Join(s.reposDir, name)
|
||||
require.NoError(s.T(), os.MkdirAll(castleRoot, 0o755))
|
||||
return castleRoot
|
||||
}
|
||||
|
||||
func (s *ExecSuite) TestExec_UnknownCastleReturnsError() {
|
||||
err := s.app.Exec("nonexistent", []string{"pwd"})
|
||||
require.Error(s.T(), err)
|
||||
require.Contains(s.T(), err.Error(), "not found")
|
||||
}
|
||||
|
||||
func (s *ExecSuite) TestExec_RunsCommandInCastleRoot() {
|
||||
castleRoot := s.createCastle("dotfiles")
|
||||
|
||||
require.NoError(s.T(), s.app.Exec("dotfiles", []string{"pwd"}))
|
||||
require.Contains(s.T(), s.stdout.String(), castleRoot)
|
||||
}
|
||||
|
||||
func (s *ExecSuite) TestExec_ForwardsStdoutAndStderr() {
|
||||
s.createCastle("dotfiles")
|
||||
|
||||
require.NoError(s.T(), s.app.Exec("dotfiles", []string{"echo out && echo err >&2"}))
|
||||
require.Contains(s.T(), s.stdout.String(), "out")
|
||||
require.Contains(s.T(), s.stderr.String(), "err")
|
||||
}
|
||||
|
||||
func (s *ExecSuite) TestExecAll_RunsCommandForEachCastle() {
|
||||
zeta := s.createCastle("zeta")
|
||||
alpha := s.createCastle("alpha")
|
||||
require.NoError(s.T(), os.MkdirAll(filepath.Join(zeta, ".git"), 0o755))
|
||||
require.NoError(s.T(), os.MkdirAll(filepath.Join(alpha, ".git"), 0o755))
|
||||
|
||||
require.NoError(s.T(), s.app.ExecAll([]string{"basename \"$PWD\""}))
|
||||
require.Equal(s.T(), "alpha\nzeta\n", s.stdout.String())
|
||||
}
|
||||
Reference in New Issue
Block a user