refactor: split into packages and add tests

This commit is contained in:
technofab 2025-06-03 12:05:16 +02:00
parent fd58344ca7
commit 11117e0c0e
28 changed files with 2736 additions and 636 deletions

View file

@ -1,121 +0,0 @@
package main
import (
"fmt"
"maps"
"os"
"regexp"
"slices"
"sort"
"strings"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/rs/zerolog/log"
"github.com/sergi/go-diff/diffmatchpatch"
)
func printErrors(results Results) {
for _, suiteResults := range results {
for _, result := range suiteResults {
if result.Status == StatusSuccess || result.Status == StatusSkipped {
continue
}
fmt.Println(text.FgRed.Sprintf("⚠ Test \"%s\" failed:", result.Spec.Name))
var message string = result.ErrorMessage
// if ErrorMessage is set, prefer that
if result.Status == StatusFailure && message == "" {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(result.Expected, result.Actual, false)
message = fmt.Sprintf("Diff:\n%s", dmp.DiffPrettyText(diffs))
}
endedWithNewline := false
// handle multi-line colored changes
colorState := ""
colorRegex := regexp.MustCompile(`\x1b\[[0-9;]*m`) // Match any escape sequence
for line := range strings.Lines(message) {
coloredLine := colorState + line
endedWithNewline = strings.HasSuffix(coloredLine, "\n")
fmt.Printf("%s %s", text.FgRed.Sprint("|"), coloredLine)
matches := colorRegex.FindAllString(line, -1)
// determine last color code, to copy to next line
if len(matches) > 0 {
lastMatch := matches[len(matches)-1]
if lastMatch == "\x1b[0m" {
colorState = "" // reset color state
} else {
colorState = lastMatch // save color state for next line
}
}
}
if endedWithNewline {
fmt.Printf("%s", text.FgRed.Sprint("|"))
}
if message == "" {
fmt.Printf("- no output -")
}
fmt.Printf("\n\n")
}
}
}
func printSummary(results Results, successCount int, totalCount int) {
t := table.NewWriter()
t.SetStyle(table.StyleLight)
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Test", "Duration", "Pass", "File"})
log.Info().Msg("Summary:")
suitesSorted := slices.Sorted(maps.Keys(results))
for _, suite := range suitesSorted {
suiteResults := results[suite]
suiteTotal := len(suiteResults)
suiteSuccess := 0
for _, res := range suiteResults {
if res.Status == StatusSuccess || res.Status == StatusSkipped {
suiteSuccess++
}
}
t.AppendRow(table.Row{
text.Bold.Sprint(suite),
"",
fmt.Sprintf("%d/%d", suiteSuccess, suiteTotal),
"",
})
sort.Slice(suiteResults, func(i, j int) bool {
return suiteResults[i].Spec.Name < suiteResults[j].Spec.Name
})
for _, res := range suiteResults {
symbol := "❌"
if res.Status == StatusSuccess {
symbol = "✅"
} else if res.Status == StatusError {
symbol = "error"
} else if res.Status == StatusSkipped {
symbol = "skipped"
}
t.AppendRow([]any{
res.Spec.Name,
fmt.Sprintf("%s", res.Duration),
symbol,
res.Spec.Pos,
})
}
t.AppendSeparator()
}
t.AppendFooter(table.Row{
text.Bold.Sprint("TOTAL"),
"",
fmt.Sprintf("%d/%d", successCount, totalCount),
"",
})
t.Render()
}

View file

@ -1,148 +0,0 @@
package main
import (
"encoding/xml"
"fmt"
"os"
"strings"
"time"
"github.com/akedrou/textdiff"
"github.com/akedrou/textdiff/myers"
)
type JUnitReport struct {
XMLName xml.Name `xml:"testsuite"`
Name string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Failures int `xml:"failures,attr"`
Errors int `xml:"errors,attr"`
Skipped int `xml:"skipped,attr"`
Time string `xml:"time,attr"` // in seconds
Suites []JUnitTestSuite `xml:"testsuite"`
}
type JUnitTestSuite struct {
XMLName xml.Name `xml:"testsuite"`
Name string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Failures int `xml:"failures,attr"`
Errors int `xml:"errors,attr"`
Skipped int `xml:"skipped,attr"`
Time string `xml:"time,attr"` // in seconds
TestCases []JUnitCase `xml:"testcase"`
}
type JUnitCase struct {
Name string `xml:"name,attr"`
Classname string `xml:"classname,attr"`
Time string `xml:"time,attr"` // in seconds
File string `xml:"file,attr,omitempty"`
Line string `xml:"line,attr,omitempty"`
Failure *string `xml:"failure,omitempty"`
Error *string `xml:"error,omitempty"`
Skipped *string `xml:"skipped,omitempty"`
}
func GenerateJUnitReport(name string, results Results) (string, error) {
report := JUnitReport{
Name: name,
Tests: 0,
Failures: 0,
Errors: 0,
Skipped: 0,
Suites: []JUnitTestSuite{},
}
totalDuration := time.Duration(0)
for suiteName, suiteResults := range results {
suite := JUnitTestSuite{
Name: suiteName,
Tests: len(suiteResults),
Failures: 0,
Errors: 0,
Skipped: 0,
TestCases: []JUnitCase{},
}
suiteDuration := time.Duration(0)
for _, result := range suiteResults {
durationSeconds := fmt.Sprintf("%.3f", result.Duration.Seconds())
totalDuration += result.Duration
suiteDuration += result.Duration
testCase := JUnitCase{
Name: result.Spec.Name,
Classname: suiteName, // Use suite name as classname
Time: durationSeconds,
}
if result.Spec.Pos != "" {
pos := strings.Split(result.Spec.Pos, ":")
testCase.File = pos[0]
testCase.Line = pos[1]
}
if result.Status == StatusFailure {
suite.Failures++
report.Failures++
if result.ErrorMessage != "" {
testCase.Failure = &result.ErrorMessage
} else {
// FIXME: ComputeEdits deprecated
edits := myers.ComputeEdits(result.Expected, result.Actual)
diff, err := textdiff.ToUnified("expected", "actual", result.Expected, edits, 3)
if err != nil {
return "", err
}
// remove newline hint
diff = strings.ReplaceAll(diff, "\\ No newline at end of file\n", "")
testCase.Failure = &diff
}
} else if result.Status == StatusError {
suite.Errors++
report.Errors++
testCase.Error = &result.ErrorMessage
} else if result.Status == StatusSkipped {
suite.Skipped++
report.Skipped++
skipped := ""
testCase.Skipped = &skipped
}
suite.TestCases = append(suite.TestCases, testCase)
}
suite.Time = fmt.Sprintf("%.3f", suiteDuration.Seconds())
report.Suites = append(report.Suites, suite)
report.Tests += len(suiteResults)
}
report.Time = fmt.Sprintf("%.3f", totalDuration.Seconds())
output, err := xml.MarshalIndent(report, "", " ")
if err != nil {
return "", fmt.Errorf("failed to marshal XML: %w", err)
}
return xml.Header + string(output), nil
}
func GenerateJunitFile(path string, results Results) error {
res, err := GenerateJUnitReport("nixtest", results)
if err != nil {
return fmt.Errorf("failed to generate junit report: %w", err)
}
file, err := os.Create(*junitPath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer file.Close()
_, err = file.WriteString(res)
if err != nil {
return fmt.Errorf("failed to write to file: %w", err)
}
return nil
}

View file

@ -1,385 +1,100 @@
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path"
"reflect"
"regexp"
"strings"
"sync"
"time"
"gitlab.com/technofab/nixtest/internal/config"
appnix "gitlab.com/technofab/nixtest/internal/nix"
"gitlab.com/technofab/nixtest/internal/report/console"
"gitlab.com/technofab/nixtest/internal/report/junit"
"gitlab.com/technofab/nixtest/internal/runner"
appsnap "gitlab.com/technofab/nixtest/internal/snapshot"
"gitlab.com/technofab/nixtest/internal/types"
"gitlab.com/technofab/nixtest/internal/util"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
flag "github.com/spf13/pflag"
)
type SuiteSpec struct {
Name string `json:"name"`
Tests []TestSpec `json:"tests"`
}
type TestSpec struct {
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Expected any `json:"expected,omitempty"`
Actual any `json:"actual,omitempty"`
ActualDrv string `json:"actualDrv,omitempty"`
Script string `json:"script,omitempty"`
Pos string `json:"pos,omitempty"`
Suite string
}
type TestStatus int
const (
StatusSuccess TestStatus = iota
StatusFailure
StatusError
StatusSkipped
)
type TestResult struct {
Spec TestSpec
Status TestStatus
Duration time.Duration
ErrorMessage string
Expected string
Actual string
}
type Results map[string][]TestResult
func buildDerivation(derivation string) (string, error) {
cmd := exec.Command(
"nix",
"build",
derivation+"^*",
"--print-out-paths",
"--no-link",
)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return "", fmt.Errorf("failed to run nix build: %v, %s", err, stderr.String())
}
path := strings.TrimSpace(stdout.String())
return path, nil
}
func buildAndParse(derivation string) (any, error) {
path, err := buildDerivation(derivation)
if err != nil {
return nil, err
}
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var result any
err = json.Unmarshal(data, &result)
if err != nil {
return nil, err
}
return result, nil
}
// builds derivation and runs it
func buildAndRun(derivation string) (exitCode int, stdout bytes.Buffer, stderr bytes.Buffer, err error) {
exitCode = -1
path, err := buildDerivation(derivation)
if err != nil {
return
}
args := []string{"bash", path}
if *pure {
args = append([]string{"env", "-i"}, args...)
}
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err = cmd.Start(); err != nil {
return
}
if err = cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
exitCode = exiterr.ExitCode()
err = nil
}
return
}
return 0, stdout, stderr, nil
}
func PrefixLines(input string) string {
lines := strings.Split(input, "\n")
for i := range lines {
lines[i] = "| " + lines[i]
}
return strings.Join(lines, "\n")
}
func shouldSkip(input string, pattern string) bool {
if pattern == "" {
return false
}
regex, err := regexp.Compile(pattern)
if err != nil {
log.Panic().Err(err).Msg("failed to compile skip regex")
}
return regex.MatchString(input)
}
func isString(value any) bool {
switch value.(type) {
case string:
return true
default:
return false
}
}
func runTest(spec TestSpec) TestResult {
startTime := time.Now()
result := TestResult{
Spec: spec,
Status: StatusSuccess,
}
if shouldSkip(spec.Name, *skipPattern) {
result.Status = StatusSkipped
return result
}
var actual any
var expected any
if spec.ActualDrv != "" {
var err error
actual, err = buildAndParse(spec.ActualDrv)
if err != nil {
result.Status = StatusError
result.ErrorMessage = fmt.Sprintf("[system] failed to parse drv output: %v", err.Error())
goto end
}
} else {
actual = spec.Actual
}
if spec.Type == "snapshot" {
filePath := path.Join(
*snapshotDir,
fmt.Sprintf("%s.snap.json", strings.ToLower(spec.Name)),
)
if *updateSnapshots {
createSnapshot(filePath, actual)
}
if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
result.Status = StatusError
result.ErrorMessage = "No Snapshot exists yet"
goto end
}
var err error
expected, err = ParseFile[any](filePath)
if err != nil {
result.Status = StatusError
result.ErrorMessage = fmt.Sprintf("[system] failed to parse snapshot: %v", err.Error())
goto end
}
} else if spec.Type == "unit" {
expected = spec.Expected
} else if spec.Type == "script" {
exitCode, stdout, stderr, err := buildAndRun(spec.Script)
if err != nil {
result.Status = StatusError
result.ErrorMessage = fmt.Sprintf("[system] failed to run script: %v", err.Error())
}
if exitCode != 0 {
result.Status = StatusFailure
result.ErrorMessage = fmt.Sprintf("[exit code %d]\n[stdout]\n%s\n[stderr]\n%s", exitCode, stdout.String(), stderr.String())
}
// no need for equality checking with "script"
goto end
} else {
log.Panic().Str("type", spec.Type).Msg("Invalid test type")
}
if reflect.DeepEqual(actual, expected) {
result.Status = StatusSuccess
} else {
var text1, text2 string
// just keep strings as is, only json marshal if any of them is not a string
if isString(actual) && isString(expected) {
text1 = actual.(string)
text2 = expected.(string)
} else {
bytes1, err := json.MarshalIndent(expected, "", " ")
if err != nil {
result.Status = StatusError
result.ErrorMessage = fmt.Sprintf("[system] failed to json marshal 'expected': %v", err.Error())
goto end
}
bytes2, err := json.MarshalIndent(actual, "", " ")
if err != nil {
result.Status = StatusError
result.ErrorMessage = fmt.Sprintf("[system] failed to json marshal 'actual': %v", err.Error())
goto end
}
text1 = string(bytes1)
text2 = string(bytes2)
}
result.Status = StatusFailure
result.Expected = text1
result.Actual = text2
}
end:
result.Duration = time.Since(startTime)
return result
}
func createSnapshot(filePath string, actual any) error {
jsonData, err := json.Marshal(actual)
if err != nil {
return err
}
err = os.MkdirAll(path.Dir(filePath), 0777)
if err != nil {
return err
}
err = os.WriteFile(filePath, jsonData, 0644)
if err != nil {
return err
}
return nil
}
// worker to process TestSpec items
func worker(jobs <-chan TestSpec, results chan<- TestResult, wg *sync.WaitGroup) {
defer wg.Done()
for spec := range jobs {
results <- runTest(spec)
}
}
var (
numWorkers *int = flag.Int("workers", 4, "Amount of tests to run in parallel")
testsFile *string = flag.String("tests", "", "Path to JSON file containing tests")
snapshotDir *string = flag.String(
"snapshot-dir", "./snapshots", "Directory where snapshots are stored",
)
junitPath *string = flag.String(
"junit", "", "Path to generate JUNIT report to, leave empty to disable",
)
updateSnapshots *bool = flag.Bool("update-snapshots", false, "Update all snapshots")
skipPattern *string = flag.String("skip", "", "Regular expression to skip (e.g., 'test-.*|.*-b')")
pure *bool = flag.Bool("pure", false, "Unset all env vars before running script tests")
)
func main() {
flag.Parse()
defer func() {
if r := recover(); r != nil {
log.Fatal().Any("r", r).Msg("Panicked")
}
}()
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: zerolog.TimeFieldFormat})
zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
appCfg := config.Load()
log.Info().
Int("workers", *numWorkers).
Int("workers", appCfg.NumWorkers).
Str("testsFile", appCfg.TestsFile).
Msg("Starting nixtest")
if _, err := os.Stat(*testsFile); errors.Is(err, os.ErrNotExist) {
log.Error().Str("file", *testsFile).Msg("Tests file does not exist")
if _, err := os.Stat(appCfg.TestsFile); os.IsNotExist(err) {
log.Error().Str("file", appCfg.TestsFile).Msg("Tests file does not exist")
os.Exit(1)
}
parsedSpecs, err := ParseFile[[]SuiteSpec](*testsFile)
suites, err := util.ParseFile[[]types.SuiteSpec](appCfg.TestsFile)
if err != nil {
log.Error().Err(err).Msg("Failed to load tests from file")
os.Exit(1)
}
totalTests := 0
for _, suite := range parsedSpecs {
for _, suite := range suites {
totalTests += len(suite.Tests)
}
log.Info().
Int("suites", len(parsedSpecs)).
Int("suites", len(suites)).
Int("tests", totalTests).
Msg("Discovered suites")
Msg("Discovered suites and tests")
jobsChan := make(chan TestSpec, totalTests)
resultsChan := make(chan TestResult, totalTests)
nixService := appnix.NewDefaultService()
snapshotService := appsnap.NewDefaultService()
var wg sync.WaitGroup
for i := 1; i <= *numWorkers; i++ {
wg.Add(1)
go worker(jobsChan, resultsChan, &wg)
runnerCfg := runner.Config{
NumWorkers: appCfg.NumWorkers,
SnapshotDir: appCfg.SnapshotDir,
UpdateSnapshots: appCfg.UpdateSnapshots,
SkipPattern: appCfg.SkipPattern,
PureEnv: appCfg.PureEnv,
}
testRunner, err := runner.New(runnerCfg, nixService, snapshotService)
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize test runner")
}
for _, suite := range parsedSpecs {
for _, test := range suite.Tests {
test.Suite = suite.Name
jobsChan <- test
}
}
close(jobsChan)
results := testRunner.RunTests(suites)
wg.Wait()
close(resultsChan)
results := map[string][]TestResult{}
successCount := 0
for r := range resultsChan {
results[r.Spec.Suite] = append(results[r.Spec.Suite], r)
if r.Status == StatusSuccess || r.Status == StatusSkipped {
successCount++
relevantSuccessCount := 0
for _, suiteResults := range results {
for _, r := range suiteResults {
if r.Status == types.StatusSuccess || r.Status == types.StatusSkipped {
relevantSuccessCount++
}
}
}
if *junitPath != "" {
err = GenerateJunitFile(*junitPath, results)
if appCfg.JunitPath != "" {
err = junit.WriteFile(appCfg.JunitPath, "nixtest", results)
if err != nil {
log.Error().Err(err).Msg("Failed to generate junit file")
} else {
log.Info().Str("path", *junitPath).Msg("Generated Junit report")
log.Info().Str("path", appCfg.JunitPath).Msg("Generated Junit report")
}
}
// print errors/logs of failed tests
printErrors(results)
// print errors first then summary
console.PrintErrors(results, appCfg.NoColor)
console.PrintSummary(results, relevantSuccessCount, totalTests)
// show table summary
printSummary(results, successCount, totalTests)
if successCount != totalTests {
os.Exit(2)
if relevantSuccessCount != totalTests {
log.Error().Msgf("Test run finished with failures or errors. %d/%d successful (includes skipped).", relevantSuccessCount, totalTests)
os.Exit(2) // exit 2 on test failures, 1 is for internal errors
}
log.Info().Msg("All tests passed successfully!")
}

View file

@ -1,24 +0,0 @@
package main
import (
"encoding/json"
"fmt"
"os"
)
func ParseFile[T any](filePath string) (result T, err error) {
file, err := os.Open(filePath)
if err != nil {
return result, fmt.Errorf("failed to open file %s: %w", filePath, err)
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&result)
if err != nil {
return result, fmt.Errorf("failed to decode JSON from file %s: %w", filePath, err)
}
return result, nil
}