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