...
1
16
17 package framework
18
19 import (
20 "errors"
21 "fmt"
22 "os"
23 "path/filepath"
24 "sort"
25 "strings"
26 "sync"
27
28 "github.com/onsi/ginkgo/v2/types"
29 )
30
31 var (
32 bugs []Bug
33 bugMutex sync.Mutex
34 )
35
36
37
38
39
40
41
42 func RecordBug(bug Bug) {
43 bugMutex.Lock()
44 defer bugMutex.Unlock()
45
46 bugs = append(bugs, bug)
47 }
48
49 type Bug struct {
50 FileName string
51 LineNumber int
52 Message string
53 }
54
55
56
57
58 func NewBug(message string, skip int) Bug {
59 location := types.NewCodeLocation(skip + 1)
60 return Bug{FileName: location.FileName, LineNumber: location.LineNumber, Message: message}
61 }
62
63
64
65 func FormatBugs() error {
66 bugMutex.Lock()
67 defer bugMutex.Unlock()
68
69 if len(bugs) == 0 {
70 return nil
71 }
72
73 lines := make([]string, 0, len(bugs))
74 wd, err := os.Getwd()
75 if err != nil {
76 return fmt.Errorf("get current directory: %v", err)
77 }
78
79
80
81
82 sort.Slice(bugs, func(i, j int) bool {
83 switch strings.Compare(bugs[i].FileName, bugs[j].FileName) {
84 case -1:
85 return true
86 case 1:
87 return false
88 }
89 if bugs[i].LineNumber < bugs[j].LineNumber {
90 return true
91 }
92 if bugs[i].LineNumber > bugs[j].LineNumber {
93 return false
94 }
95 return bugs[i].Message < bugs[j].Message
96 })
97 for _, bug := range bugs {
98
99 path := bug.FileName
100 if wd != "" {
101 if relpath, err := filepath.Rel(wd, bug.FileName); err == nil {
102 path = relpath
103 }
104 }
105 lines = append(lines, fmt.Sprintf("ERROR: %s:%d: %s\n", path, bug.LineNumber, strings.TrimSpace(bug.Message)))
106 }
107 return errors.New(strings.Join(lines, ""))
108 }
109
View as plain text