mirror of
https://gitlab.com/TECHNOFAB/nixtest.git
synced 2025-12-12 02:00:18 +01:00
refactor: split into packages and add tests
This commit is contained in:
parent
fd58344ca7
commit
11117e0c0e
28 changed files with 2736 additions and 636 deletions
43
internal/config/config.go
Normal file
43
internal/config/config.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"github.com/jedib0t/go-pretty/v6/text"
|
||||
"github.com/rs/zerolog/log"
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
type AppConfig struct {
|
||||
NumWorkers int
|
||||
TestsFile string
|
||||
SnapshotDir string
|
||||
JunitPath string
|
||||
UpdateSnapshots bool
|
||||
SkipPattern string
|
||||
PureEnv bool
|
||||
NoColor bool
|
||||
}
|
||||
|
||||
// loads configuration from cli flags
|
||||
func Load() AppConfig {
|
||||
cfg := AppConfig{}
|
||||
flag.IntVarP(&cfg.NumWorkers, "workers", "w", 4, "Amount of tests to run in parallel")
|
||||
flag.StringVarP(&cfg.TestsFile, "tests", "f", "", "Path to JSON file containing tests (required)")
|
||||
flag.StringVar(&cfg.SnapshotDir, "snapshot-dir", "./snapshots", "Directory where snapshots are stored")
|
||||
flag.StringVar(&cfg.JunitPath, "junit", "", "Path to generate JUNIT report to, leave empty to disable")
|
||||
flag.BoolVarP(&cfg.UpdateSnapshots, "update-snapshots", "u", false, "Update all snapshots")
|
||||
flag.StringVarP(&cfg.SkipPattern, "skip", "s", "", "Regular expression to skip tests (e.g., 'test-.*|.*-b')")
|
||||
flag.BoolVar(&cfg.PureEnv, "pure", false, "Unset all env vars before running script tests")
|
||||
flag.BoolVar(&cfg.NoColor, "no-color", false, "Disable coloring")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if cfg.TestsFile == "" {
|
||||
log.Panic().Msg("Tests file path (-f or --tests) is required.")
|
||||
}
|
||||
|
||||
if cfg.NoColor {
|
||||
text.DisableColors()
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
89
internal/config/config_test.go
Normal file
89
internal/config/config_test.go
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLoad_Defaults(t *testing.T) {
|
||||
originalArgs := os.Args
|
||||
oldFlagSet := pflag.CommandLine
|
||||
defer func() {
|
||||
os.Args = originalArgs
|
||||
pflag.CommandLine = oldFlagSet
|
||||
}()
|
||||
|
||||
// for Load() to not call log.Fatal(), a tests file must be provided
|
||||
os.Args = []string{"cmd", "-f", "dummy.json"}
|
||||
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError) // reset flags
|
||||
|
||||
cfg := Load()
|
||||
|
||||
if cfg.NumWorkers != 4 {
|
||||
t.Errorf("Default NumWorkers: got %d, want 4", cfg.NumWorkers)
|
||||
}
|
||||
if cfg.SnapshotDir != "./snapshots" {
|
||||
t.Errorf("Default SnapshotDir: got %s, want ./snapshots", cfg.SnapshotDir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_Fatal(t *testing.T) {
|
||||
originalArgs := os.Args
|
||||
oldFlagSet := pflag.CommandLine
|
||||
defer func() {
|
||||
os.Args = originalArgs
|
||||
pflag.CommandLine = oldFlagSet
|
||||
}()
|
||||
|
||||
// Load() should panic without tests file
|
||||
os.Args = []string{
|
||||
"cmd",
|
||||
}
|
||||
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError) // Reset flags
|
||||
|
||||
assert.Panics(t, func() { _ = Load() }, "Load should panic withot tests file")
|
||||
}
|
||||
|
||||
func TestLoad_CustomValues(t *testing.T) {
|
||||
originalArgs := os.Args
|
||||
oldFlagSet := pflag.CommandLine
|
||||
defer func() {
|
||||
os.Args = originalArgs
|
||||
pflag.CommandLine = oldFlagSet
|
||||
}()
|
||||
|
||||
// simulate cli args for Load()
|
||||
os.Args = []string{
|
||||
"cmd", // dummy command name
|
||||
"-f", "mytests.json",
|
||||
"--workers", "8",
|
||||
"--snapshot-dir", "/tmp/snaps",
|
||||
"--junit", "report.xml",
|
||||
"-u",
|
||||
"--skip", "specific-test",
|
||||
"--pure",
|
||||
"--no-color",
|
||||
}
|
||||
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError) // Reset flags
|
||||
|
||||
cfg := Load()
|
||||
|
||||
if cfg.TestsFile != "mytests.json" {
|
||||
t.Errorf("TestsFile: got %s, want mytests.json", cfg.TestsFile)
|
||||
}
|
||||
if cfg.NumWorkers != 8 {
|
||||
t.Errorf("NumWorkers: got %d, want 8", cfg.NumWorkers)
|
||||
}
|
||||
if !cfg.UpdateSnapshots {
|
||||
t.Errorf("UpdateSnapshots: got %v, want true", cfg.UpdateSnapshots)
|
||||
}
|
||||
if cfg.SkipPattern != "specific-test" {
|
||||
t.Errorf("SkipPattern: got %s, want specific-test", cfg.SkipPattern)
|
||||
}
|
||||
if !cfg.PureEnv {
|
||||
t.Errorf("PureEnv: got %v, want true", cfg.PureEnv)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue