github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/testutils/trace.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package testutils 12 13 import ( 14 "regexp" 15 16 "github.com/cockroachdb/cockroach/pkg/util/log" 17 "github.com/cockroachdb/cockroach/pkg/util/tracing" 18 "github.com/cockroachdb/errors" 19 ) 20 21 // MakeAmbientCtx creates an AmbientContext with a Tracer in it. 22 func MakeAmbientCtx() log.AmbientContext { 23 return log.AmbientContext{ 24 Tracer: tracing.NewTracer(), 25 } 26 } 27 28 // MatchInOrder matches interprets the given slice of strings as a slice of 29 // regular expressions and checks that they match, in order and without overlap, 30 // against the given string. For example, if s=abcdefg and res=ab,cd,fg no error 31 // is returned, whereas res=abc,cde would return a descriptive error about 32 // failing to match cde. 33 func MatchInOrder(s string, res ...string) error { 34 sPos := 0 35 for i := range res { 36 reStr := "(?ms)" + res[i] 37 re, err := regexp.Compile(reStr) 38 if err != nil { 39 return errors.Errorf("regexp %d (%q) does not compile: %s", i, reStr, err) 40 } 41 loc := re.FindStringIndex(s[sPos:]) 42 if loc == nil { 43 // Not found. 44 return errors.Errorf( 45 "unable to find regexp %d (%q) in remaining string:\n\n%s\n\nafter having matched:\n\n%s", 46 i, reStr, s[sPos:], s[:sPos], 47 ) 48 } 49 sPos += loc[1] 50 } 51 return nil 52 }