mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-11 04:15:48 +02:00
move shell into test driver
This commit is contained in:
parent
78b495f50a
commit
c5050ecabd
39 changed files with 108 additions and 131 deletions
|
@ -15,119 +15,122 @@ import (
|
|||
type Shell struct {
|
||||
// working directory the shell is invoked in
|
||||
dir string
|
||||
// when running the shell outside the gui we can directly panic on failure,
|
||||
// but inside the gui we need to close the gui before panicking
|
||||
fail func(string)
|
||||
}
|
||||
|
||||
func NewShell(dir string) *Shell {
|
||||
return &Shell{dir: dir}
|
||||
func NewShell(dir string, fail func(string)) *Shell {
|
||||
return &Shell{dir: dir, fail: fail}
|
||||
}
|
||||
|
||||
func (s *Shell) RunCommand(cmdStr string) *Shell {
|
||||
func (self *Shell) RunCommand(cmdStr string) *Shell {
|
||||
args := str.ToArgv(cmdStr)
|
||||
cmd := secureexec.Command(args[0], args[1:]...)
|
||||
cmd.Env = os.Environ()
|
||||
cmd.Dir = s.dir
|
||||
cmd.Dir = self.dir
|
||||
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error running command: %s\n%s", cmdStr, string(output)))
|
||||
self.fail(fmt.Sprintf("error running command: %s\n%s", cmdStr, string(output)))
|
||||
}
|
||||
|
||||
return s
|
||||
return self
|
||||
}
|
||||
|
||||
func (s *Shell) RunShellCommand(cmdStr string) *Shell {
|
||||
func (self *Shell) RunShellCommand(cmdStr string) *Shell {
|
||||
cmd := secureexec.Command("sh", "-c", cmdStr)
|
||||
cmd.Env = os.Environ()
|
||||
cmd.Dir = s.dir
|
||||
cmd.Dir = self.dir
|
||||
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error running shell command: %s\n%s", cmdStr, string(output)))
|
||||
self.fail(fmt.Sprintf("error running shell command: %s\n%s", cmdStr, string(output)))
|
||||
}
|
||||
|
||||
return s
|
||||
return self
|
||||
}
|
||||
|
||||
func (s *Shell) RunShellCommandExpectError(cmdStr string) *Shell {
|
||||
func (self *Shell) RunShellCommandExpectError(cmdStr string) *Shell {
|
||||
cmd := secureexec.Command("sh", "-c", cmdStr)
|
||||
cmd.Env = os.Environ()
|
||||
cmd.Dir = s.dir
|
||||
cmd.Dir = self.dir
|
||||
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
panic(fmt.Sprintf("Expected error running shell command: %s\n%s", cmdStr, string(output)))
|
||||
self.fail(fmt.Sprintf("Expected error running shell command: %s\n%s", cmdStr, string(output)))
|
||||
}
|
||||
|
||||
return s
|
||||
return self
|
||||
}
|
||||
|
||||
func (s *Shell) CreateFile(path string, content string) *Shell {
|
||||
fullPath := filepath.Join(s.dir, path)
|
||||
func (self *Shell) CreateFile(path string, content string) *Shell {
|
||||
fullPath := filepath.Join(self.dir, path)
|
||||
err := os.WriteFile(fullPath, []byte(content), 0o644)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error creating file: %s\n%s", fullPath, err))
|
||||
self.fail(fmt.Sprintf("error creating file: %s\n%s", fullPath, err))
|
||||
}
|
||||
|
||||
return s
|
||||
return self
|
||||
}
|
||||
|
||||
func (s *Shell) CreateDir(path string) *Shell {
|
||||
fullPath := filepath.Join(s.dir, path)
|
||||
func (self *Shell) CreateDir(path string) *Shell {
|
||||
fullPath := filepath.Join(self.dir, path)
|
||||
if err := os.MkdirAll(fullPath, 0o755); err != nil {
|
||||
panic(fmt.Sprintf("error creating directory: %s\n%s", fullPath, err))
|
||||
self.fail(fmt.Sprintf("error creating directory: %s\n%s", fullPath, err))
|
||||
}
|
||||
|
||||
return s
|
||||
return self
|
||||
}
|
||||
|
||||
func (s *Shell) UpdateFile(path string, content string) *Shell {
|
||||
fullPath := filepath.Join(s.dir, path)
|
||||
func (self *Shell) UpdateFile(path string, content string) *Shell {
|
||||
fullPath := filepath.Join(self.dir, path)
|
||||
err := os.WriteFile(fullPath, []byte(content), 0o644)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error updating file: %s\n%s", fullPath, err))
|
||||
self.fail(fmt.Sprintf("error updating file: %s\n%s", fullPath, err))
|
||||
}
|
||||
|
||||
return s
|
||||
return self
|
||||
}
|
||||
|
||||
func (s *Shell) NewBranch(name string) *Shell {
|
||||
return s.RunCommand("git checkout -b " + name)
|
||||
func (self *Shell) NewBranch(name string) *Shell {
|
||||
return self.RunCommand("git checkout -b " + name)
|
||||
}
|
||||
|
||||
func (s *Shell) Checkout(name string) *Shell {
|
||||
return s.RunCommand("git checkout " + name)
|
||||
func (self *Shell) Checkout(name string) *Shell {
|
||||
return self.RunCommand("git checkout " + name)
|
||||
}
|
||||
|
||||
func (s *Shell) Merge(name string) *Shell {
|
||||
return s.RunCommand("git merge --commit --no-ff " + name)
|
||||
func (self *Shell) Merge(name string) *Shell {
|
||||
return self.RunCommand("git merge --commit --no-ff " + name)
|
||||
}
|
||||
|
||||
func (s *Shell) GitAdd(path string) *Shell {
|
||||
return s.RunCommand(fmt.Sprintf("git add \"%s\"", path))
|
||||
func (self *Shell) GitAdd(path string) *Shell {
|
||||
return self.RunCommand(fmt.Sprintf("git add \"%s\"", path))
|
||||
}
|
||||
|
||||
func (s *Shell) GitAddAll() *Shell {
|
||||
return s.RunCommand("git add -A")
|
||||
func (self *Shell) GitAddAll() *Shell {
|
||||
return self.RunCommand("git add -A")
|
||||
}
|
||||
|
||||
func (s *Shell) Commit(message string) *Shell {
|
||||
return s.RunCommand(fmt.Sprintf("git commit -m \"%s\"", message))
|
||||
func (self *Shell) Commit(message string) *Shell {
|
||||
return self.RunCommand(fmt.Sprintf("git commit -m \"%s\"", message))
|
||||
}
|
||||
|
||||
func (s *Shell) EmptyCommit(message string) *Shell {
|
||||
return s.RunCommand(fmt.Sprintf("git commit --allow-empty -m \"%s\"", message))
|
||||
func (self *Shell) EmptyCommit(message string) *Shell {
|
||||
return self.RunCommand(fmt.Sprintf("git commit --allow-empty -m \"%s\"", message))
|
||||
}
|
||||
|
||||
// convenience method for creating a file and adding it
|
||||
func (s *Shell) CreateFileAndAdd(fileName string, fileContents string) *Shell {
|
||||
return s.
|
||||
func (self *Shell) CreateFileAndAdd(fileName string, fileContents string) *Shell {
|
||||
return self.
|
||||
CreateFile(fileName, fileContents).
|
||||
GitAdd(fileName)
|
||||
}
|
||||
|
||||
// convenience method for updating a file and adding it
|
||||
func (s *Shell) UpdateFileAndAdd(fileName string, fileContents string) *Shell {
|
||||
return s.
|
||||
func (self *Shell) UpdateFileAndAdd(fileName string, fileContents string) *Shell {
|
||||
return self.
|
||||
UpdateFile(fileName, fileContents).
|
||||
GitAdd(fileName)
|
||||
}
|
||||
|
@ -135,24 +138,24 @@ func (s *Shell) UpdateFileAndAdd(fileName string, fileContents string) *Shell {
|
|||
// creates commits 01, 02, 03, ..., n with a new file in each
|
||||
// The reason for padding with zeroes is so that it's easier to do string
|
||||
// matches on the commit messages when there are many of them
|
||||
func (s *Shell) CreateNCommits(n int) *Shell {
|
||||
func (self *Shell) CreateNCommits(n int) *Shell {
|
||||
for i := 1; i <= n; i++ {
|
||||
s.CreateFileAndAdd(
|
||||
self.CreateFileAndAdd(
|
||||
fmt.Sprintf("file%02d.txt", i),
|
||||
fmt.Sprintf("file%02d content", i),
|
||||
).
|
||||
Commit(fmt.Sprintf("commit %02d", i))
|
||||
}
|
||||
|
||||
return s
|
||||
return self
|
||||
}
|
||||
|
||||
func (s *Shell) StashWithMessage(message string) *Shell {
|
||||
s.RunCommand(fmt.Sprintf(`git stash -m "%s"`, message))
|
||||
return s
|
||||
func (self *Shell) StashWithMessage(message string) *Shell {
|
||||
self.RunCommand(fmt.Sprintf(`git stash -m "%s"`, message))
|
||||
return self
|
||||
}
|
||||
|
||||
func (s *Shell) SetConfig(key string, value string) *Shell {
|
||||
s.RunCommand(fmt.Sprintf(`git config --local "%s" %s`, key, value))
|
||||
return s
|
||||
func (self *Shell) SetConfig(key string, value string) *Shell {
|
||||
self.RunCommand(fmt.Sprintf(`git config --local "%s" %s`, key, value))
|
||||
return self
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue