mirror of
https://gitlab.com/TECHNOFAB/nixtest.git
synced 2025-12-12 02:00:18 +01:00
refactor: improve junit report to not include escape sequences
This commit is contained in:
parent
4afa8a7957
commit
3a974f218a
3 changed files with 41 additions and 13 deletions
|
|
@ -17,10 +17,16 @@ func printErrors(results Results) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Println(text.FgRed.Sprintf("⚠ Test \"%s\" failed:", result.Name))
|
fmt.Println(text.FgRed.Sprintf("⚠ Test \"%s\" failed:", result.Name))
|
||||||
for line := range strings.Lines(result.Error) {
|
var message string
|
||||||
|
if result.Error.Diff != "" {
|
||||||
|
message = fmt.Sprintf("Diff:\n%s", result.Error.Diff)
|
||||||
|
} else {
|
||||||
|
message = result.Error.Message
|
||||||
|
}
|
||||||
|
for line := range strings.Lines(message) {
|
||||||
fmt.Printf("%s %s", text.FgRed.Sprint("|"), line)
|
fmt.Printf("%s %s", text.FgRed.Sprint("|"), line)
|
||||||
}
|
}
|
||||||
if result.Error == "" {
|
if message == "" {
|
||||||
fmt.Printf("- no output -")
|
fmt.Printf("- no output -")
|
||||||
}
|
}
|
||||||
fmt.Printf("\n\n")
|
fmt.Printf("\n\n")
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,7 @@ func GenerateJUnitReport(name string, results Results) (string, error) {
|
||||||
if !result.Success {
|
if !result.Success {
|
||||||
suite.Failures++
|
suite.Failures++
|
||||||
report.Failures++
|
report.Failures++
|
||||||
testCase.Failure = &result.Error
|
testCase.Failure = &result.Error.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
suite.TestCases = append(suite.TestCases, testCase)
|
suite.TestCases = append(suite.TestCases, testCase)
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ type TestSpec struct {
|
||||||
type TestResult struct {
|
type TestResult struct {
|
||||||
Name string
|
Name string
|
||||||
Success bool
|
Success bool
|
||||||
Error string
|
Error TestResultError
|
||||||
Duration time.Duration
|
Duration time.Duration
|
||||||
Pos string
|
Pos string
|
||||||
Suite string
|
Suite string
|
||||||
|
|
@ -80,6 +80,21 @@ func buildAndParse(derivation string) (any, error) {
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TestResultError struct {
|
||||||
|
Message string
|
||||||
|
Expected string
|
||||||
|
Actual string
|
||||||
|
Diff string
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrefixLines(input string) string {
|
||||||
|
lines := strings.Split(input, "\n")
|
||||||
|
for i := range lines {
|
||||||
|
lines[i] = "| " + lines[i]
|
||||||
|
}
|
||||||
|
return strings.Join(lines, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
func runTest(spec TestSpec) TestResult {
|
func runTest(spec TestSpec) TestResult {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
result := TestResult{
|
result := TestResult{
|
||||||
|
|
@ -87,7 +102,7 @@ func runTest(spec TestSpec) TestResult {
|
||||||
Pos: spec.Pos,
|
Pos: spec.Pos,
|
||||||
Suite: spec.Suite,
|
Suite: spec.Suite,
|
||||||
Success: false,
|
Success: false,
|
||||||
Error: "",
|
Error: TestResultError{},
|
||||||
}
|
}
|
||||||
|
|
||||||
var actual any
|
var actual any
|
||||||
|
|
@ -97,7 +112,7 @@ func runTest(spec TestSpec) TestResult {
|
||||||
var err error
|
var err error
|
||||||
actual, err = buildAndParse(spec.ActualDrv)
|
actual, err = buildAndParse(spec.ActualDrv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Error = fmt.Sprintf("[system] failed to parse drv output: %v", err.Error())
|
result.Error.Message = fmt.Sprintf("[system] failed to parse drv output: %v", err.Error())
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -114,14 +129,14 @@ func runTest(spec TestSpec) TestResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
|
if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
|
||||||
result.Error = "No Snapshot exists yet"
|
result.Error.Message = "No Snapshot exists yet"
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
expected, err = ParseFile[any](filePath)
|
expected, err = ParseFile[any](filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Error = fmt.Sprintf("[system] failed to parse snapshot: %v", err.Error())
|
result.Error.Message = fmt.Sprintf("[system] failed to parse snapshot: %v", err.Error())
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
} else if spec.Type == "unit" {
|
} else if spec.Type == "unit" {
|
||||||
|
|
@ -134,18 +149,25 @@ func runTest(spec TestSpec) TestResult {
|
||||||
result.Success = true
|
result.Success = true
|
||||||
} else {
|
} else {
|
||||||
dmp := diffmatchpatch.New()
|
dmp := diffmatchpatch.New()
|
||||||
text1, err := json.MarshalIndent(actual, "", " ")
|
text1, err := json.MarshalIndent(expected, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Error = fmt.Sprintf("[system] failed to json marshal 'actual': %v", err.Error())
|
result.Error.Message = fmt.Sprintf("[system] failed to json marshal 'expected': %v", err.Error())
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
text2, err := json.MarshalIndent(expected, "", " ")
|
text2, err := json.MarshalIndent(actual, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Error = fmt.Sprintf("[system] failed to json marshal 'expected': %v", err.Error())
|
result.Error.Message = fmt.Sprintf("[system] failed to json marshal 'actual': %v", err.Error())
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
diffs := dmp.DiffMain(string(text1), string(text2), false)
|
diffs := dmp.DiffMain(string(text1), string(text2), false)
|
||||||
result.Error = fmt.Sprintf("Mismatch:\n%s", dmp.DiffPrettyText(diffs))
|
result.Error.Expected = string(text1)
|
||||||
|
result.Error.Actual = string(text2)
|
||||||
|
result.Error.Diff = dmp.DiffPrettyText(diffs)
|
||||||
|
result.Error.Message = fmt.Sprintf(
|
||||||
|
"Expected:\n%s\nGot:\n%s",
|
||||||
|
PrefixLines(string(text1)),
|
||||||
|
PrefixLines(string(text2)),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue