enhance test code

This commit is contained in:
Anton Bracke
2025-03-18 11:51:47 +01:00
parent d7bffb58ac
commit b47c13fd44
6 changed files with 80 additions and 60 deletions

View File

@@ -24,14 +24,6 @@ steps:
- <<: *when_path
- '.woodpecker/**'
dummy-web:
image: *golang_image
commands:
- mkdir -p web/dist/
- echo "test" > web/dist/index.html
when:
- path: *when_path
integration:
image: *golang_image
commands:

View File

@@ -7,11 +7,11 @@ import (
)
func TestEnvStart(t *testing.T) {
forge, err := utils.StartForge(t)
if err != nil {
t.Fatalf("Could not start forge %s", err)
}
defer forge.Stop()
// forge, err := utils.StartForge(t)
// if err != nil {
// t.Fatalf("Could not start forge %s", err)
// }
// defer forge.Stop()
server, err := utils.StartServer(t)
if err != nil {

View File

@@ -0,0 +1,50 @@
package utils
import (
"fmt"
"log"
"os/exec"
"strings"
"testing"
)
type Command struct {
cmd *exec.Cmd
env map[string]string
}
func NewTask(cmdName string, args ...string) *Command {
cmd := exec.Command(cmdName, args...)
return &Command{
cmd: cmd,
}
}
func (t *Command) WorkDir(workDir string) *Command {
t.cmd.Dir = workDir
return t
}
func (t *Command) SetEnv(key, value string) *Command {
t.env[key] = value
return t
}
func (t *Command) Run() (string, error) {
log.Printf("# %s %s", t.cmd.Path, strings.Join(t.cmd.Args, " "))
env := []string{}
for key, value := range t.env {
env = append(env, fmt.Sprintf("%s=%s", key, value))
}
t.cmd.Env = env
output, err := t.cmd.Output()
return string(output), err
}
func (t *Command) RunOrFail(te *testing.T) {
output, err := t.Run()
if err != nil {
te.Fatalf("Failed to execute command '%s %s': %v\n%s", t.cmd.Path, strings.Join(t.cmd.Args, " "), err, output)
}
}

View File

@@ -9,11 +9,9 @@ type TestRepo struct {
func (r *TestRepo) Clone(t *testing.T, sourcePath, remoteURL string) error {
r.folder = t.TempDir()
runOrFail(t, "cp", "-r", sourcePath, r.folder)
runOrFail(t, "git", "init")
runOrFail(t, "git", "remote", "add", remoteURL)
NewTask("cp", "-r", sourcePath, r.folder).RunOrFail(t)
NewTask("git", "init").RunOrFail(t)
NewTask("git", "remote", "add", remoteURL).RunOrFail(t)
r.Commit(t, ":tada: init")
@@ -23,13 +21,13 @@ func (r *TestRepo) Clone(t *testing.T, sourcePath, remoteURL string) error {
}
func (r *TestRepo) Commit(t *testing.T, message string) {
runOrFail(t, "git", "commit", "-m", message)
NewTask("git", "commit", "-m", message).RunOrFail(t)
}
func (r *TestRepo) Push(t *testing.T) {
runOrFail(t, "git", "push", "-u", "origin", "main")
NewTask("git", "push", "-u", "origin", "main").RunOrFail(t)
}
func (r *TestRepo) Tag(t *testing.T, name, message string) {
runOrFail(t, "git", "tag", "-a", name, "-m", message)
NewTask("git", "tag", "-a", name, "-m", message).RunOrFail(t)
}

View File

@@ -6,16 +6,18 @@ import (
)
type Service struct {
cmdName string
args []string
env map[string]string
workingDir string
cmd *exec.Cmd
cmd *exec.Cmd
env map[string]string
}
func NewService(cmdName string, args ...string) *Service {
cmd := exec.Command(cmdName, args...)
return &Service{
cmd: cmd,
}
}
func (s *Service) Start() error {
s.cmd = exec.Command(s.cmdName, s.args...)
s.cmd.Dir = s.workingDir
env := []string{}
for key, value := range s.env {
env = append(env, fmt.Sprintf("%s=%s", key, value))
@@ -32,17 +34,12 @@ func (s *Service) Stop() error {
return s.cmd.Process.Kill()
}
func (s *Service) SetEnv(key, value string) {
func (s *Service) SetEnv(key, value string) *Service {
s.env[key] = value
return s
}
func (s *Service) WorkDir(workDir string) {
s.workingDir = workDir
}
func NewService(cmdName string, args ...string) *Service {
return &Service{
cmdName: cmdName,
args: args,
}
func (s *Service) WorkDir(workDir string) *Service {
s.cmd.Dir = workDir
return s
}

View File

@@ -1,41 +1,24 @@
package utils
import (
"log"
"os/exec"
"strings"
"testing"
)
func runOrFail(t *testing.T, workingDir, cmdName string, args ...string) {
log.Printf("# %s %s", cmdName, strings.Join(args, " "))
cmd := exec.Command(cmdName, args...)
cmd.Dir = workingDir
output, err := cmd.Output()
if err != nil {
t.Fatalf("Failed to execute command '%s %s': %v\n%s", cmdName, strings.Join(args, " "), err, output)
}
}
func StartForge(t *testing.T) (*Service, error) {
runOrFail(t, "", "mkdir", "-p", "web/dist")
runOrFail(t, "", "echo", "\"test\"", "web/dist/index.html")
func StartForge(_ *testing.T) (*Service, error) {
service := NewService("echo", "start", "forge")
service.WorkDir("forge/")
return service, service.Start()
}
func StartServer(_ *testing.T) (*Service, error) {
service := NewService("go", "run", "./cmd/server/")
func StartServer(t *testing.T) (*Service, error) {
NewTask("mkdir", "-p", "web/dist").RunOrFail(t)
NewTask("echo", "\"test\"", ">", "web/dist/index.html").RunOrFail(t)
service := NewService("go", "run", "./cmd/server/")
return service, service.Start()
}
func StartAgent(_ *testing.T) (*Service, error) {
service := NewService("go", "run", "./cmd/agent/")
return service, service.Start()
}