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

@ -0,0 +1,143 @@
package console
import (
"fmt"
"os"
"sort"
"strings"
"time"
"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"
"gitlab.com/technofab/nixtest/internal/types"
"gitlab.com/technofab/nixtest/internal/util"
)
// PrintErrors prints error messages for failed tests
func PrintErrors(results types.Results, noColor bool) {
for _, suiteResults := range results {
for _, result := range suiteResults {
if result.Status == types.StatusSuccess || result.Status == types.StatusSkipped {
continue
}
fmt.Println(text.FgRed.Sprintf("⚠ Test \"%s/%s\" failed:", result.Spec.Suite, result.Spec.Name))
message := result.ErrorMessage
if result.Status == types.StatusFailure && message == "" {
if noColor {
var err error
message, err = util.ComputeDiff(result.Expected, result.Actual)
if err != nil {
log.Panic().Err(err).Msg("failed to compute diff")
}
} else {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(result.Expected, result.Actual, true)
message = fmt.Sprintf("Diff:\n%s", dmp.DiffPrettyText(diffs))
}
}
if message == "" {
message = "- no output -"
}
for _, line := range strings.Split(strings.TrimRight(message, "\n"), "\n") {
fmt.Printf("%s %s\n", text.FgRed.Sprint("|"), line)
}
fmt.Println()
}
}
}
// PrintSummary prints a table summarizing test results
func PrintSummary(results types.Results, totalSuccessCount int, totalTestCount int) {
t := table.NewWriter()
t.SetStyle(table.StyleLight)
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Suite / Test", "Duration", "Status", "File:Line"})
log.Info().Msg("Summary:")
suiteNames := make([]string, 0, len(results))
for name := range results {
suiteNames = append(suiteNames, name)
}
sort.Strings(suiteNames)
for _, suiteName := range suiteNames {
suiteResults := results[suiteName]
suiteTotal := len(suiteResults)
suiteSuccess := 0
suiteSkipped := 0
for _, res := range suiteResults {
if res.Status == types.StatusSuccess {
suiteSuccess++
} else if res.Status == types.StatusSkipped {
suiteSkipped++
}
}
statusStr := fmt.Sprintf("%d/%d", suiteSuccess, suiteTotal)
if suiteSkipped > 0 {
statusStr += fmt.Sprintf(" (%d skipped)", suiteSkipped)
}
t.AppendRow(table.Row{
text.Bold.Sprint(suiteName),
"",
statusStr,
"",
})
sort.Slice(suiteResults, func(i, j int) bool {
return suiteResults[i].Spec.Name < suiteResults[j].Spec.Name
})
for _, res := range suiteResults {
var symbol string
switch res.Status {
case types.StatusSuccess:
symbol = text.FgGreen.Sprint("✅ PASS")
case types.StatusFailure:
symbol = text.FgRed.Sprint("❌ FAIL")
case types.StatusError:
symbol = text.FgYellow.Sprint("❗ ERROR")
case types.StatusSkipped:
symbol = text.FgBlue.Sprint("⏭️ SKIP")
default:
symbol = "UNKNOWN"
}
t.AppendRow([]any{
" " + res.Spec.Name,
fmt.Sprintf("%s", res.Duration.Round(time.Millisecond)),
symbol,
res.Spec.Pos,
})
}
t.AppendSeparator()
}
overallStatusStr := fmt.Sprintf("%d/%d", totalSuccessCount, totalTestCount)
totalSkipped := 0
for _, suiteResults := range results {
for _, res := range suiteResults {
if res.Status == types.StatusSkipped {
totalSkipped++
}
}
}
if totalSkipped > 0 {
overallStatusStr += fmt.Sprintf(" (%d skipped)", totalSkipped)
}
t.AppendFooter(table.Row{
text.Bold.Sprint("TOTAL"),
"",
text.Bold.Sprint(overallStatusStr),
"",
})
t.Render()
}

View file

@ -0,0 +1,228 @@
package console
import (
"bytes"
"fmt"
"io"
"os"
"regexp"
"strings"
"sync"
"testing"
"time"
"github.com/jedib0t/go-pretty/v6/text"
"gitlab.com/technofab/nixtest/internal/types"
)
// captureOutput captures stdout and stderr during the execution of a function
func captureOutput(f func()) (string, string) {
// save original stdout and stderr
originalStdout := os.Stdout
originalStderr := os.Stderr
rOut, wOut, _ := os.Pipe()
rErr, wErr, _ := os.Pipe()
os.Stdout = wOut
os.Stderr = wErr
defer func() {
// restore stdout & stderr
os.Stdout = originalStdout
os.Stderr = originalStderr
}()
outC := make(chan string)
errC := make(chan string)
var wg sync.WaitGroup
wg.Add(1)
go func() {
var buf bytes.Buffer
wg.Done()
_, _ = io.Copy(&buf, rOut)
outC <- buf.String()
}()
wg.Add(1)
go func() {
var buf bytes.Buffer
wg.Done()
_, _ = io.Copy(&buf, rErr)
errC <- buf.String()
}()
f()
wOut.Close()
wErr.Close()
wg.Wait()
stdout := <-outC
stderr := <-errC
return stdout, stderr
}
func TestPrintErrorsColor(t *testing.T) {
results := types.Results{
"Suite1": []types.TestResult{
{
Spec: types.TestSpec{Suite: "Suite1", Name: "TestFailure_Diff"},
Status: types.StatusFailure,
Expected: "line1\nline2 expected\nline3",
Actual: "line1\nline2 actual\nline3",
},
},
}
stdout, _ := captureOutput(func() {
PrintErrors(results, false)
})
ansiEscapePattern := `(?:\\x1b\[[0-9;]*m)*`
pattern := `.*\n` +
`A|A Diff:\n` +
`A|A line1\n` +
`A|A line2 AexpeAAacAAtedAAualA\n` +
`A|A line3.*`
pattern = strings.ReplaceAll(pattern, "A", ansiEscapePattern)
matched, _ := regexp.MatchString(pattern, stdout)
if !matched {
t.Errorf("PrintErrors() TestFailure_Diff diff output mismatch or missing.\nExpected pattern:\n%s\nGot:\n%s", pattern, stdout)
}
}
func TestPrintErrors(t *testing.T) {
text.DisableColors()
defer text.EnableColors()
results := types.Results{
"Suite1": []types.TestResult{
{
Spec: types.TestSpec{Suite: "Suite1", Name: "TestSuccess"},
Status: types.StatusSuccess,
},
{
Spec: types.TestSpec{Suite: "Suite1", Name: "TestFailure_Diff"},
Status: types.StatusFailure,
Expected: "line1\nline2 expected\nline3",
Actual: "line1\nline2 actual\nline3",
},
{
Spec: types.TestSpec{Suite: "Suite1", Name: "TestFailure_Message"},
Status: types.StatusFailure,
ErrorMessage: "This is a specific failure message.\nWith multiple lines.",
},
{
Spec: types.TestSpec{Suite: "Suite1", Name: "TestError"},
Status: types.StatusError,
ErrorMessage: "System error occurred.",
},
{
Spec: types.TestSpec{Suite: "Suite1", Name: "TestEmpty"},
Status: types.StatusError,
ErrorMessage: "",
},
},
}
stdout, _ := captureOutput(func() {
PrintErrors(results, true)
})
if strings.Contains(stdout, "TestSuccess") {
t.Errorf("PrintErrors() should not print success cases, but found 'TestSuccess'")
}
expectedDiffPattern := `\|\s*--- expected\s*\n` + // matches "| --- expected"
`\|\s*\+\+\+ actual\s*\n` + // matches "| +++ actual"
`\|\s*@@ -\d+,\d+ \+\d+,\d+ @@\s*\n` + // matches "| @@ <hunk info> @@"
`\|\s* line1\s*\n` + // matches "| line1" (note the leading space for an "equal" line)
`\|\s*-line2 expected\s*\n` + // matches "| -line2 expected"
`\|\s*\+line2 actual\s*\n` + // matches "| +line2 actual"
`\|\s* line3\s*` // matches "| line3"
matched, _ := regexp.MatchString(expectedDiffPattern, stdout)
if !matched {
t.Errorf("PrintErrors() TestFailure_Diff diff output mismatch or missing.\nExpected pattern:\n%s\nGot:\n%s", expectedDiffPattern, stdout)
}
if !strings.Contains(stdout, "⚠ Test \"Suite1/TestFailure_Message\" failed:") {
t.Errorf("PrintErrors() missing header for TestFailure_Message. Output:\n%s", stdout)
}
if !strings.Contains(stdout, "| This is a specific failure message.") ||
!strings.Contains(stdout, "| With multiple lines.") {
t.Errorf("PrintErrors() TestFailure_Message message output mismatch or missing. Output:\n%s", stdout)
}
if !strings.Contains(stdout, "⚠ Test \"Suite1/TestError\" failed:") {
t.Errorf("PrintErrors() missing header for TestError. Output:\n%s", stdout)
}
if !strings.Contains(stdout, "| System error occurred.") {
t.Errorf("PrintErrors() TestError message output mismatch or missing. Output:\n%s", stdout)
}
if !strings.Contains(stdout, "- no output -") {
t.Errorf("PrintErrors() missing '- no output -'. Output:\n%s", stdout)
}
}
func TestPrintSummary(t *testing.T) {
text.DisableColors()
defer text.EnableColors()
results := types.Results{
"AlphaSuite": []types.TestResult{
{Spec: types.TestSpec{Suite: "AlphaSuite", Name: "TestA", Pos: "alpha.nix:1"}, Status: types.StatusSuccess, Duration: 100 * time.Millisecond},
{Spec: types.TestSpec{Suite: "AlphaSuite", Name: "TestB", Pos: "alpha.nix:2"}, Status: types.StatusFailure, Duration: 200 * time.Millisecond},
},
"BetaSuite": []types.TestResult{
{Spec: types.TestSpec{Suite: "BetaSuite", Name: "TestC", Pos: "beta.nix:1"}, Status: types.StatusSkipped, Duration: 50 * time.Millisecond},
{Spec: types.TestSpec{Suite: "BetaSuite", Name: "TestD", Pos: "beta.nix:2"}, Status: types.StatusError, Duration: 150 * time.Millisecond},
{Spec: types.TestSpec{Suite: "BetaSuite", Name: "TestE", Pos: "beta.nix:3"}, Status: 123, Duration: 150 * time.Millisecond},
},
}
totalSuccessCount := 2
totalTestCount := 4
r, w, _ := os.Pipe()
originalStdout := os.Stdout
os.Stdout = w
PrintSummary(results, totalSuccessCount, totalTestCount)
w.Close()
os.Stdout = originalStdout
var summaryTable bytes.Buffer
_, _ = io.Copy(&summaryTable, r)
stdout := summaryTable.String()
if !strings.Contains(stdout, "AlphaSuite") || !strings.Contains(stdout, "BetaSuite") {
t.Errorf("PrintSummary() missing suite names. Output:\n%s", stdout)
}
// check for test names and statuses
if !strings.Contains(stdout, "TestA") || !strings.Contains(stdout, "PASS") {
t.Errorf("PrintSummary() missing TestA or its PASS status. Output:\n%s", stdout)
}
if !strings.Contains(stdout, "TestB") || !strings.Contains(stdout, "FAIL") {
t.Errorf("PrintSummary() missing TestB or its FAIL status. Output:\n%s", stdout)
}
if !strings.Contains(stdout, "TestE") || !strings.Contains(stdout, "UNKNOWN") {
t.Errorf("PrintSummary() missing TestE or its UNKNOWN status. Output:\n%s", stdout)
}
// check for total summary
expectedTotalSummary := fmt.Sprintf("%d/%d (1 SKIPPED)", totalSuccessCount, totalTestCount)
if !strings.Contains(stdout, expectedTotalSummary) {
t.Errorf("PrintSummary() total summary incorrect. Expected to contain '%s'. Output:\n%s", expectedTotalSummary, stdout)
}
}

View file

@ -0,0 +1,158 @@
package junit
import (
"encoding/xml"
"fmt"
"os"
"strings"
"time"
"gitlab.com/technofab/nixtest/internal/types"
"gitlab.com/technofab/nixtest/internal/util"
)
type JUnitReport struct {
XMLName xml.Name `xml:"testsuites"`
Name string `xml:"name,attr,omitempty"`
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"`
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"`
TestCases []JUnitCase `xml:"testcase"`
}
type JUnitCase struct {
XMLName xml.Name `xml:"testcase"`
Name string `xml:"name,attr"`
Classname string `xml:"classname,attr"`
Time string `xml:"time,attr"`
File string `xml:"file,attr,omitempty"`
Line string `xml:"line,attr,omitempty"`
Failure *JUnitFailure `xml:"failure,omitempty"`
Error *JUnitError `xml:"error,omitempty"`
Skipped *JUnitSkipped `xml:"skipped,omitempty"`
}
type JUnitFailure struct {
XMLName xml.Name `xml:"failure"`
Message string `xml:"message,attr,omitempty"`
Data string `xml:",cdata"`
}
type JUnitError struct {
XMLName xml.Name `xml:"error"`
Message string `xml:"message,attr,omitempty"`
Data string `xml:",cdata"`
}
type JUnitSkipped struct {
XMLName xml.Name `xml:"skipped"`
Message string `xml:"message,attr,omitempty"`
}
// GenerateReport generates the Junit XML content as a string
func GenerateReport(reportName string, results types.Results) (string, error) {
report := JUnitReport{
Name: reportName,
Suites: []JUnitTestSuite{},
}
totalDuration := time.Duration(0)
for suiteName, suiteResults := range results {
suite := JUnitTestSuite{
Name: suiteName,
Tests: len(suiteResults),
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,
Time: durationSeconds,
}
if result.Spec.Pos != "" {
parts := strings.SplitN(result.Spec.Pos, ":", 2)
testCase.File = parts[0]
if len(parts) > 1 {
testCase.Line = parts[1]
}
}
switch result.Status {
case types.StatusFailure:
suite.Failures++
report.Failures++
var failureContent string
if result.ErrorMessage != "" {
failureContent = result.ErrorMessage
} else {
var err error
failureContent, err = util.ComputeDiff(result.Expected, result.Actual)
if err != nil {
return "", fmt.Errorf("failed to compute diff")
}
}
testCase.Failure = &JUnitFailure{Message: "Test failed", Data: failureContent}
case types.StatusError:
suite.Errors++
report.Errors++
testCase.Error = &JUnitError{Message: "Test errored", Data: result.ErrorMessage}
case types.StatusSkipped:
suite.Skipped++
report.Skipped++
testCase.Skipped = &JUnitSkipped{Message: "Test skipped"}
}
report.Tests++
suite.TestCases = append(suite.TestCases, testCase)
}
suite.Time = fmt.Sprintf("%.3f", suiteDuration.Seconds())
report.Suites = append(report.Suites, suite)
}
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
}
// WriteFile generates a Junit report and writes it to the specified path
func WriteFile(filePath string, reportName string, results types.Results) error {
xmlContent, err := GenerateReport(reportName, results)
if err != nil {
return fmt.Errorf("failed to generate junit report content: %w", err)
}
file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("failed to create junit file %s: %w", filePath, err)
}
defer file.Close()
_, err = file.WriteString(xmlContent)
if err != nil {
return fmt.Errorf("failed to write junit report to %s: %w", filePath, err)
}
return nil
}

View file

@ -0,0 +1,116 @@
package junit
import (
"encoding/xml"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
"gitlab.com/technofab/nixtest/internal/types"
)
func formatDurationSeconds(d time.Duration) string {
return fmt.Sprintf("%.3f", d.Seconds())
}
func TestGenerateReport(t *testing.T) {
results := types.Results{
"Suite1": []types.TestResult{
{
Spec: types.TestSpec{Name: "Test1_Success", Suite: "Suite1", Pos: "file1.nix:10"},
Status: types.StatusSuccess,
Duration: 123 * time.Millisecond,
},
{
Spec: types.TestSpec{Name: "Test2_Failure", Suite: "Suite1", Pos: "file1.nix:20"},
Status: types.StatusFailure,
Duration: 234 * time.Millisecond,
Expected: "hello",
Actual: "world",
},
},
"Suite2": []types.TestResult{
{
Spec: types.TestSpec{Name: "Test3_Error", Suite: "Suite2"},
Status: types.StatusError,
Duration: 345 * time.Millisecond,
ErrorMessage: "Something went very wrong",
},
{
Spec: types.TestSpec{Name: "Test4_Failure_Message", Suite: "Suite2"},
Status: types.StatusFailure,
Duration: 456 * time.Millisecond,
ErrorMessage: "hello world",
},
{
Spec: types.TestSpec{Name: "Test5_Skipped", Suite: "Suite2"},
Status: types.StatusSkipped,
Duration: 567 * time.Millisecond,
},
},
}
totalDuration := (123 + 234 + 345 + 456 + 567) * time.Millisecond
reportName := "MyNixtestReport"
xmlString, err := GenerateReport(reportName, results)
if err != nil {
t.Fatalf("GenerateReport() failed: %v", err)
}
if !strings.HasPrefix(xmlString, xml.Header) {
t.Error("GenerateReport() output missing XML header")
}
if !strings.Contains(xmlString, "<testsuites name=\"MyNixtestReport\"") {
t.Errorf("GenerateReport() missing root <testsuites>. Got: %s", xmlString)
}
if !strings.Contains(xmlString, "tests=\"5\"") {
t.Errorf("GenerateReport() incorrect total tests count. Got: %s", xmlString)
}
if !strings.Contains(xmlString, "failures=\"2\"") {
t.Errorf("GenerateReport() incorrect total failures count. Got: %s", xmlString)
}
if !strings.Contains(xmlString, "errors=\"1\"") {
t.Errorf("GenerateReport() incorrect total errors count. Got: %s", xmlString)
}
if !strings.Contains(xmlString, "time=\""+formatDurationSeconds(totalDuration)+"\"") {
t.Errorf("GenerateReport() incorrect total time. Expected %s. Got part: %s", formatDurationSeconds(totalDuration), xmlString)
}
var report JUnitReport
if err := xml.Unmarshal([]byte(strings.TrimPrefix(xmlString, xml.Header)), &report); err != nil {
t.Fatalf("Failed to unmarshal generated XML: %v\nXML:\n%s", err, xmlString)
}
if report.Name != reportName {
t.Errorf("Report.Name = %q, want %q", report.Name, reportName)
}
if len(report.Suites) != 2 {
t.Fatalf("Report.Suites length = %d, want 2", len(report.Suites))
}
}
func TestWriteFile(t *testing.T) {
tempDir := t.TempDir()
filePath := filepath.Join(tempDir, "junit_report.xml")
results := types.Results{
"SuiteSimple": []types.TestResult{
{Spec: types.TestSpec{Name: "SimpleTest", Suite: "SuiteSimple"}, Status: types.StatusSuccess, Duration: 1 * time.Second},
},
}
err := WriteFile(filePath, "TestReport", results)
if err != nil {
t.Fatalf("WriteFile() failed: %v", err)
}
content, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("Failed to read written JUnit file: %v", err)
}
if !strings.Contains(string(content), "<testsuites name=\"TestReport\"") {
t.Error("Written JUnit file content seems incorrect.")
}
}