github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/modules/logger/parser/failed_test_marker.go (about) 1 // Package logger/parser contains methods to parse and restructure log output from go testing and terratest 2 package parser 3 4 // TestResultMarker tracks the indentation level of a test result line in go test output. 5 // Example: 6 // --- FAIL: TestSnafu 7 // 8 // --- PASS: TestSnafu/Situation 9 // --- FAIL: TestSnafu/Normal 10 // 11 // The three markers for the above in order are: 12 // TestResultMarker{TestName: "TestSnafu", IndentLevel: 0} 13 // TestResultMarker{TestName: "TestSnafu/Situation", IndentLevel: 4} 14 // TestResultMarker{TestName: "TestSnafu/Normal", IndentLevel: 4} 15 type TestResultMarker struct { 16 TestName string 17 IndentLevel int 18 } 19 20 // TestResultMarkerStack is a stack data structure to store TestResultMarkers 21 type TestResultMarkerStack []TestResultMarker 22 23 // A blank TestResultMarker is considered null. Used when peeking or popping an empty stack. 24 var NULL_TEST_RESULT_MARKER = TestResultMarker{} 25 26 // TestResultMarker.push will push a TestResultMarker object onto the stack, returning the new one. 27 func (s TestResultMarkerStack) push(v TestResultMarker) TestResultMarkerStack { 28 return append(s, v) 29 } 30 31 // TestResultMarker.pop will pop a TestResultMarker object off of the stack, returning the new one with the popped 32 // marker. 33 // When stack is empty, will return an empty object. 34 func (s TestResultMarkerStack) pop() (TestResultMarkerStack, TestResultMarker) { 35 l := len(s) 36 if l == 0 { 37 return s, NULL_TEST_RESULT_MARKER 38 } 39 return s[:l-1], s[l-1] 40 } 41 42 // TestResultMarker.peek will return the top TestResultMarker from the stack, but will not remove it. 43 func (s TestResultMarkerStack) peek() TestResultMarker { 44 l := len(s) 45 if l == 0 { 46 return NULL_TEST_RESULT_MARKER 47 } 48 return s[l-1] 49 } 50 51 // TestResultMarker.isEmpty will return whether or not the stack is empty. 52 func (s TestResultMarkerStack) isEmpty() bool { 53 return len(s) == 0 54 } 55 56 // removeDedentedTestResultMarkers will pop items off of the stack of TestResultMarker objects until the top most item 57 // has an indent level less than the current indent level. 58 // Assumes that the stack is ordered, in that recently pushed items in the stack have higher indent levels. 59 func (s TestResultMarkerStack) removeDedentedTestResultMarkers(currentIndentLevel int) TestResultMarkerStack { 60 // This loop is a garbage collection of the stack, where it removes entries every time we dedent out of a fail 61 // block. 62 for !s.isEmpty() && s.peek().IndentLevel >= currentIndentLevel { 63 s, _ = s.pop() 64 } 65 return s 66 }