diff --git a/.woodpecker/test.yaml b/.woodpecker/test.yaml new file mode 100644 index 0000000..a0a7509 --- /dev/null +++ b/.woodpecker/test.yaml @@ -0,0 +1,9 @@ +--- +when: + event: pull_request + +steps: + golang: + image: golang:1.23.1 + commands: + - go test -v ./... diff --git a/internal/apps/labtime/app_test.go b/internal/apps/labtime/app_test.go new file mode 100644 index 0000000..52f5708 --- /dev/null +++ b/internal/apps/labtime/app_test.go @@ -0,0 +1,31 @@ +package labtime + +import ( + "log" + "os" + "testing" +) + +func TestNewApp(t *testing.T) { + flag := &FlagConfig{ + configFile: "../../../configs/example-config.yaml", // we shouldn't rely on the actual file + } + logger := log.New(os.Stdout, "", 0) // silent? + + app, err := NewApp(flag, logger) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if app.logger != logger { + t.Errorf("expected logger to be set") + } + + if app.scheduler == nil { + t.Errorf("expected scheduler to be initialized") + } + + if app.prometheusHTTPServer == nil { + t.Errorf("expected prometheusHTTPServer to be initialized") + } +} diff --git a/internal/apps/labtime/flag_test.go b/internal/apps/labtime/flag_test.go new file mode 100644 index 0000000..9e73848 --- /dev/null +++ b/internal/apps/labtime/flag_test.go @@ -0,0 +1,45 @@ +package labtime + +import ( + "flag" + "os" + "testing" +) + +func TestLoadFlag(t *testing.T) { + tests := []struct { + name string + args []string + expectedFile string + }{ + { + name: "No flag provided", + args: []string{}, + expectedFile: defaultConfigFile, + }, + { + name: "Flag provided", + args: []string{"-config", "custom.yaml"}, + expectedFile: "custom.yaml", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Save and restore the original os.Args + oldArgs := os.Args + defer func() { os.Args = oldArgs }() + + // Make sure we are using a clean flag set + flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) + + os.Args = append([]string{"cmd"}, tt.args...) + + cfg := LoadFlag() + + if cfg.configFile != tt.expectedFile { + t.Errorf("got %s, want %s", cfg.configFile, tt.expectedFile) + } + }) + } +} diff --git a/internal/monitors/httpmonitor_test.go b/internal/monitors/httpmonitor_test.go new file mode 100644 index 0000000..c89e925 --- /dev/null +++ b/internal/monitors/httpmonitor_test.go @@ -0,0 +1,16 @@ +package monitors + +import "testing" + +func TestHTTPMonitor_ID(t *testing.T) { + monitor := &HTTPMonitor{ + Label: "example", + } + + expectedID := "example" + actualID := monitor.ID() + + if actualID != expectedID { + t.Errorf("expected ID to be %s, but got %s", expectedID, actualID) + } +}