From b71e4db7db463127d0007ed2021b5da3cc606e54 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Fri, 13 Dec 2024 22:24:18 +0100 Subject: [PATCH] failures not counted correctly in some cases. Summary output added. --- cmd/go2junit/go2junit.go | 29 +++++++++++++++++++++++++++++ cmd/go2junit/output.go | 20 ++++++++++++-------- cmd/golicenses/main.go | 2 +- cmd/golicenses/output.go | 20 ++++++++++++++------ 4 files changed, 56 insertions(+), 15 deletions(-) diff --git a/cmd/go2junit/go2junit.go b/cmd/go2junit/go2junit.go index c98044a..b96dc0b 100644 --- a/cmd/go2junit/go2junit.go +++ b/cmd/go2junit/go2junit.go @@ -128,4 +128,33 @@ func main() { } } + fmt.Printf("\n\nSUMMARY\n\n") + fmt.Printf("%-60s %-10s %-10s %-10s %-10s %-10s %-10s\n\n", "SUITE", "COUNT", "PASSED", "FAILURES", "ERRORS", "DISABLED", "SKIPPED") + for _, suite := range testsuites.Suites { + fmt.Printf("%-60s %-10d %-10d %-10d %-10d %-10d %-10d\n", + suite.Name, + suite.TestCount, + suite.TestCount-suite.Failures-suite.Errors-suite.Skipped-suite.Disabled, + suite.Failures, suite.Errors, suite.Disabled, suite.Skipped) + } + fmt.Printf("\n%-60s %-10d %-10d %-10d %-10d %-10d %-10d\n", + "TOTAL", + testsuites.Tests, + testsuites.Tests-testsuites.Failures-testsuites.Errors-testsuites.Skipped-testsuites.Disabled, + testsuites.Failures, testsuites.Errors, testsuites.Disabled, testsuites.Skipped) + + if testsuites.Failures+testsuites.Errors+testsuites.Skipped+testsuites.Disabled > 0 { + fmt.Printf("\nFAILED TESTS\n\n") + printFailedTests("", "", testsuites.Suites) + } +} + +func printFailedTests(indent string, parentTest string, tests []*Test) { + for _, test := range tests { + if test.Failures > 0 { + testName := strings.TrimPrefix(test.Name, parentTest+"/") + fmt.Printf("%s%s\n", indent, testName) + printFailedTests(indent+" ", test.Name, test.Tests) + } + } } diff --git a/cmd/go2junit/output.go b/cmd/go2junit/output.go index 41e75e7..4821645 100644 --- a/cmd/go2junit/output.go +++ b/cmd/go2junit/output.go @@ -39,8 +39,9 @@ type Test struct { Tests []*Test `xml:"testsuite,omitempty"` SystemOut string `xml:"system-out,omitempty"` - parent *Test - t0 time.Time + hasTests bool + parent *Test + t0 time.Time } type Result struct { @@ -121,7 +122,8 @@ func (testsuites *Testsuites) Start(t time.Time, pkg string) { func (testsuites *Testsuites) Test(t time.Time, pkg string, test string) { // This can be a test suite as well - testsuites.getTest(t, pkg, test) + testobj := testsuites.getTest(t, pkg, test) + testobj.hasTests = true } func (testsuites *Testsuites) Output(t time.Time, pkg string, test string, output string) { @@ -153,12 +155,14 @@ func (testsuites *Testsuites) Skip(t time.Time, pkg string, test string) { func (suite *Test) Complete() { suite.TestCount = 0 - suite.Failures = 0 - suite.Errors = 0 - suite.Disabled = 0 - suite.Skipped = 0 + if len(suite.Tests) > 0 { + suite.Failures = 0 + suite.Errors = 0 + suite.Disabled = 0 + suite.Skipped = 0 + } - if len(suite.Tests) == 0 { + if len(suite.Tests) == 0 && suite.hasTests { suite.TestCount = 1 } diff --git a/cmd/golicenses/main.go b/cmd/golicenses/main.go index ff3dd27..99ebfd7 100644 --- a/cmd/golicenses/main.go +++ b/cmd/golicenses/main.go @@ -6,7 +6,7 @@ func main() { module.GenerateLicenseNames() module.DumpOverview() - module.DumpText() + module.DumpText(true) } func truncateString(s string, length int) string { diff --git a/cmd/golicenses/output.go b/cmd/golicenses/output.go index f04cdce..fe0b8ef 100644 --- a/cmd/golicenses/output.go +++ b/cmd/golicenses/output.go @@ -4,6 +4,7 @@ import ( "crypto/sha512" "encoding/base64" "fmt" + "log" "os" "path/filepath" "sort" @@ -89,7 +90,11 @@ func NewModule(modules []ModuleDependency) *Module { if mod.Dir == "" { continue // Skip modules without local copies } - license := module.findLicense(mod.Dir) + license, err := module.findLicense(mod.Dir) + if err != nil { + log.Printf("ERROR: %v", err) + continue + } module.Licenses[hash(license.Text)] = license dependency := NewDependency(mod.Path, mod.Version, !mod.Indirect, license) module.Dependencies = append(module.Dependencies, dependency) @@ -138,8 +143,11 @@ func (module Module) DumpOverview() { } } -func (module Module) DumpText() { +func (module Module) DumpText(directOnly bool) { for _, dependency := range module.Dependencies { + if directOnly && !dependency.Direct { + continue + } fmt.Println(strings.Repeat("=", 80)) if dependency.Direct { fmt.Printf("Direct dependency") @@ -152,7 +160,7 @@ func (module Module) DumpText() { } } -func (module *Module) findLicense(dir string) *License { +func (module *Module) findLicense(dir string) (*License, error) { licenseFiles := []string{ "LICENSE", "LICENSE.txt", @@ -174,12 +182,12 @@ func (module *Module) findLicense(dir string) *License { hashcode := hash(content) license := module.Licenses[hashcode] if license != nil { - return license + return license, nil } - return NewLicense(content) + return NewLicense(content), nil } } } } - return nil + return nil, fmt.Errorf("No license found in '%s'", dir) }