0
0
mirror of https://github.com/thegeeklab/wp-git-action.git synced 2024-06-02 18:29:41 +02:00
This commit is contained in:
Robert Kaussow 2024-05-14 13:26:39 +02:00
parent e88a8c2365
commit 43243662de
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
3 changed files with 51 additions and 19 deletions

View File

@ -30,6 +30,37 @@ func TestAdd(t *testing.T) {
}
}
func TestIsCleanTree(t *testing.T) {
tests := []struct {
name string
repo Repository
want []string
}{
{
name: "clean working tree",
repo: Repository{
WorkDir: "/path/to/repo",
},
want: []string{gitBin, "diff-index", "--quiet", "HEAD", "--ignore-submodules"},
},
{
name: "unclean working tree",
repo: Repository{
WorkDir: "/path/to/unclean/repo",
},
want: []string{gitBin, "diff-index", "--quiet", "HEAD", "--ignore-submodules"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := tt.repo.IsCleanTree()
assert.Equal(t, tt.want, cmd.Cmd.Args)
assert.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
})
}
}
func TestCommit(t *testing.T) {
tests := []struct {
name string

View File

@ -163,14 +163,8 @@ func (p *Plugin) Execute() error {
batchCmd = append(batchCmd, p.Settings.Repo.ConfigUserEmail())
batchCmd = append(batchCmd, p.Settings.Repo.ConfigSSLVerify(p.Network.InsecureSkipVerify))
for _, cmd := range batchCmd {
if cmd == nil {
continue
}
if err := cmd.Run(); err != nil {
return err
}
if err := ExecBatch(batchCmd); err != nil {
return err
}
for _, actionStr := range p.Settings.Action.Value() {
@ -234,17 +228,7 @@ func (p *Plugin) handleClone() error {
batchCmd = append(batchCmd, p.Settings.Repo.FetchSource())
batchCmd = append(batchCmd, p.Settings.Repo.CheckoutHead())
for _, cmd := range batchCmd {
if cmd == nil {
continue
}
if err := cmd.Run(); err != nil {
return err
}
}
return nil
return ExecBatch(batchCmd)
}
// HandleCommit commits changes locally.

View File

@ -4,6 +4,8 @@ import (
"fmt"
"os"
"path/filepath"
"github.com/thegeeklab/wp-plugin-go/v2/types"
)
const (
@ -26,3 +28,18 @@ func WriteNetrc(path, machine, login, password string) error {
return nil
}
// ExecBatch executes a batch of commands. If any command in the batch fails, the function will return the error.
func ExecBatch(batchCmd []*types.Cmd) error {
for _, cmd := range batchCmd {
if cmd == nil {
continue
}
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}