github.com/solo-io/cue@v0.4.7/internal/cuetest/cuetest.go (about) 1 // Copyright 2021 The CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package testing is a helper package for test packages in the CUE project. 16 // As such it should only be imported in _test.go files. 17 package cuetest 18 19 import ( 20 "fmt" 21 "os" 22 "regexp" 23 "testing" 24 ) 25 26 const ( 27 // envUpdate is used in the definition of UpdateGoldenFiles 28 envUpdate = "CUE_UPDATE" 29 30 // envNonIssues can be set to a regular expression which indicates what 31 // issues we no longer consider issues, i.e. they should have been fixed. 32 // This should generally result in tests that would otherwise be skipped no 33 // longer being skipped. e.g. CUE_NON_ISSUES=. will cause all issue 34 // tracker conditions (e.g. [golang.org/issues/1234]) to be considered 35 // non-issues. 36 envNonIssues = "CUE_NON_ISSUES" 37 ) 38 39 var ( 40 // issuesConditions is a set of regular expressions that defines the set of 41 // conditions that can be used to declare links to issues in various issue 42 // trackers. e.g. in testscript condition form 43 // 44 // [golang.org/issues/1234] 45 // [github.com/govim/govim/issues/4321] 46 issuesConditions = []*regexp.Regexp{ 47 regexp.MustCompile(`^golang\.org/issues?/\d+$`), 48 regexp.MustCompile(`^cuelang\.org/issues?/\d+$`), 49 } 50 ) 51 52 // UpdateGoldenFiles determines whether testscript scripts should update txtar 53 // archives in the event of cmp failures. It corresponds to 54 // testscript.Params.UpdateGoldenFiles. See the docs for 55 // testscript.Params.UpdateGoldenFiles for more details. 56 var UpdateGoldenFiles = os.Getenv(envUpdate) != "" 57 58 // Condition adds support for CUE-specific testscript conditions within 59 // testscript scripts. Supported conditions include: 60 // 61 // [long] - evalutates to true when the long build tag is specified 62 // 63 // [golang.org/issue/N] - evaluates to true unless CUE_NON_ISSUES 64 // is set to a regexp that matches the condition, i.e. golang.org/issue/N 65 // in this case 66 // 67 // [cuelang.org/issue/N] - evaluates to true unless CUE_NON_ISSUES 68 // is set to a regexp that matches the condition, i.e. cuelang.org/issue/N 69 // in this case 70 // 71 func Condition(cond string) (bool, error) { 72 isIssue, nonIssue, err := checkIssueCondition(cond) 73 if err != nil { 74 return false, err 75 } 76 if isIssue { 77 return !nonIssue, nil 78 } 79 switch cond { 80 case "long": 81 return Long, nil 82 } 83 return false, fmt.Errorf("unknown condition %v", cond) 84 } 85 86 // IssueSkip causes the test t to be skipped unless the issue identified 87 // by s is deemed to be a non-issue by CUE_NON_ISSUES. 88 func IssueSkip(t *testing.T, s string) { 89 isIssue, nonIssue, err := checkIssueCondition(s) 90 if err != nil { 91 t.Fatal(err) 92 } 93 if !isIssue { 94 t.Fatalf("issue %q does not match a known issue pattern", s) 95 } 96 if nonIssue { 97 t.Skipf("issue %s", s) 98 } 99 } 100 101 // checkIssueCondition examines s to determine whether it is an issue 102 // condition, in which case isIssue is true. If isIssue, then we check 103 // CUE_NON_ISSUES for a match, in which case nonIssue is true (a value of true 104 // indicates roughly that we don't believe issue s is an issue any more). In 105 // case of any errors err is set. 106 func checkIssueCondition(s string) (isIssue bool, nonIssue bool, err error) { 107 var r *regexp.Regexp 108 if v := os.Getenv(envNonIssues); v != "" { 109 r, err = regexp.Compile(v) 110 if err != nil { 111 return false, false, fmt.Errorf("failed to compile regexp %q specified via %v: %v", v, envNonIssues, err) 112 } 113 } 114 for _, c := range issuesConditions { 115 if c.MatchString(s) { 116 isIssue = true 117 } 118 } 119 if !isIssue { 120 return false, false, nil 121 } 122 return isIssue, r != nil && r.MatchString(s), nil 123 }