mirror of
https://gitlab.com/TECHNOFAB/nixtest.git
synced 2025-12-12 10:10:09 +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
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