浏览代码

fix: refact goci, define a pipline

simon 9 月之前
父节点
当前提交
45889e2f15

+ 24 - 11
rggo/processes/goci/main.go

@@ -5,26 +5,39 @@ import (
 	"fmt"
 	"io"
 	"os"
-	"os/exec"
 )
 
 func run(proj string, out io.Writer) error {
 	if proj == "" {
 		return fmt.Errorf("project directory is required: %w", ErrValidation)
 	}
+	pipeline := make([]step, 2)
+	pipeline[0] = newStep(
+		"go build",
+		"Go Build: SUCCESS",
+		proj,
+		"go",
+		[]string{"build", ".", "errors"},
+	)
+	pipeline[1] = newStep(
+		"go test",
+		"Go Test: SUCCESS",
+		proj,
+		"go",
+		[]string{"test", "-v"},
+	)
 
-	args := []string{"build", ".", "errors"}
-	cmd := exec.Command("go", args...)
-	cmd.Dir = proj
-	if err := cmd.Run(); err != nil {
-		return &stepErr{
-			step:  "go build",
-			msg:   "go build failed",
-			cause: err,
+	for _, s := range pipeline {
+		msg, err := s.execute()
+		if err != nil {
+			return err
+		}
+		_, err = fmt.Fprintln(out, msg)
+		if err != nil {
+			return err
 		}
 	}
-	_, err := fmt.Fprintln(out, "Go Build: SUCCESS")
-	return err
+	return nil
 }
 
 func main() {

+ 1 - 1
rggo/processes/goci/main_test.go

@@ -13,7 +13,7 @@ func TestRun(t *testing.T) {
 		out    string
 		expErr error
 	}{
-		{name: "success", proj: "./testdata/tool/", out: "Go Build: SUCCESS\n", expErr: nil},
+		{name: "success", proj: "./testdata/tool/", out: "Go Build: SUCCESS\nGo Test: SUCCESS\n", expErr: nil},
 		{name: "fail", proj: "./testdata/toolErr/", out: "", expErr: &stepErr{step: "go build"}},
 	}
 

+ 37 - 0
rggo/processes/goci/step.go

@@ -0,0 +1,37 @@
+package main
+
+import (
+	"os/exec"
+)
+
+type step struct {
+	name    string
+	exe     string
+	args    []string
+	message string
+	proj    string
+}
+
+func newStep(name, message, proj, exe string, args []string) step {
+	return step{
+		name:    name,
+		exe:     exe,
+		args:    args,
+		message: message,
+		proj:    proj,
+	}
+}
+
+func (s step) execute() (string, error) {
+	cmd := exec.Command(s.exe, s.args...)
+	cmd.Dir = s.proj
+
+	if err := cmd.Run(); err != nil {
+		return "", &stepErr{
+			step:  s.name,
+			msg:   "failed to execute",
+			cause: err,
+		}
+	}
+	return s.message, nil
+}

+ 22 - 0
rggo/processes/goci/testdata/tool/add_test.go

@@ -0,0 +1,22 @@
+// Author: simon
+// Author: ynwdlxm@163.com
+// Date: 2025/2/12 09:58
+// Desc:
+
+package add
+
+import (
+	"testing"
+)
+
+func TestAdd(t *testing.T) {
+	a := 2
+	b := 3
+
+	exp := 5
+
+	res := add(a, b)
+	if exp != res {
+		t.Errorf("Expected %d, got %d.", exp, res)
+	}
+}

+ 6 - 0
rggo/processes/goci/testdata/toolErr/add_test.go

@@ -0,0 +1,6 @@
+// Author: simon
+// Author: ynwdlxm@163.com
+// Date: 2025/2/12 09:58
+// Desc:
+
+package add