github.com/swaros/contxt/module/runner@v0.0.0-20240305083542-3dbd4436ac40/testoutfilter_test.go (about) 1 package runner_test 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "regexp" 8 "strings" 9 10 "github.com/swaros/contxt/module/systools" 11 ) 12 13 // create a output handler to catch the created output while testing 14 type TestOutHandler struct { 15 Msgs []string 16 logFile string 17 keepNewLines bool 18 } 19 20 func NewTestOutHandler() *TestOutHandler { 21 return &TestOutHandler{} 22 } 23 24 func (t *TestOutHandler) SetLogFile(logFile string) { 25 logFile = strings.ReplaceAll(logFile, ":", "_") 26 logFile = strings.ReplaceAll(logFile, "-", "_") 27 logFile = strings.ReplaceAll(logFile, "+", "_") 28 t.logFile = logFile 29 } 30 31 func filterMessages(msgs []string) []string { 32 filtered := []string{} 33 for _, msg := range msgs { 34 filteredStr := systools.PrintableChars(msg) 35 filtered = append(filtered, filteredStr) 36 37 } 38 return filtered 39 } 40 41 // enable or disable the new line filter 42 func (t *TestOutHandler) SetKeepNewLines(keep bool) { 43 t.keepNewLines = keep 44 } 45 46 func (t *TestOutHandler) Stream(msg ...interface{}) { 47 t.Msgs = append(t.Msgs, fmt.Sprint(msg...)) 48 } 49 50 func (t *TestOutHandler) StreamLn(msg ...interface{}) { 51 t.Msgs = append(t.Msgs, fmt.Sprintln(msg...)) 52 } 53 54 // get all the messages as a string 55 func (t *TestOutHandler) String() string { 56 if t.keepNewLines { 57 return fmt.Sprintln(strings.Join(t.Msgs, "\n")) 58 } 59 return fmt.Sprintln(strings.Join(filterMessages(t.Msgs), "\n")) 60 } 61 62 // get all the messages the are created 63 func (t *TestOutHandler) GetMessages() []string { 64 return t.Msgs 65 } 66 67 // clear the messages 68 func (t *TestOutHandler) Clear() { 69 t.Msgs = []string{} 70 } 71 72 func (t *TestOutHandler) ClearAndLog() { 73 t.WriteToLogFile() 74 t.Clear() 75 } 76 77 func (t *TestOutHandler) GetLogFile() string { 78 return t.logFile 79 } 80 81 func (t *TestOutHandler) WriteToLogFile() error { 82 if t.logFile == "" { 83 return nil 84 } 85 f, err := os.OpenFile(t.logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) 86 87 if err == nil { 88 defer f.Close() 89 if _, err := io.WriteString(f, t.String()); err != nil { 90 return err 91 } 92 } else { 93 return err 94 } 95 96 return nil 97 } 98 99 // check if the message is in the output 100 func (t *TestOutHandler) Contains(msg string) bool { 101 for _, m := range t.Msgs { 102 if m == msg { 103 return true 104 } 105 if m == msg+"\n" { 106 return true 107 } 108 if strings.Contains(m, msg) { 109 return true 110 } 111 } 112 return false 113 } 114 115 func (t *TestOutHandler) TestRegexPattern(pattern string) bool { 116 for _, m := range t.Msgs { 117 regexp, err := regexp.Compile(pattern) 118 if err != nil { 119 return false 120 } 121 if regexp.MatchString(m) { 122 return true 123 } 124 } 125 return false 126 } 127 128 // check if the message is in the output 129 func (t *TestOutHandler) Get(msg string) []string { 130 matches := []string{} 131 for _, m := range t.Msgs { 132 133 if strings.Contains(m, msg) { 134 matches = append(matches, m) 135 } 136 } 137 return matches 138 }