| 1 | // Copyright 2020 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/ |
| 6 | |
| 7 | import ( |
| 8 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 9 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 10 | ) |
| 11 | |
| 12 | func TestValidatescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TestValidate().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 13 | var ( |
| 14 | run = func(p *Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#) (any, error) { |
| 15 | return nil, nil |
| 16 | } |
| 17 | dependsOnSelf = &Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 18 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "dependsOnSelf", |
| 19 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "this analyzer depends on itself", |
| 20 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: run, |
| 21 | } |
| 22 | inCycleA = &Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 23 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "inCycleA", |
| 24 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "this analyzer depends on inCycleB", |
| 25 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: run, |
| 26 | } |
| 27 | inCycleB = &Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 28 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "inCycleB", |
| 29 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "this analyzer depends on inCycleA and notInCycleA", |
| 30 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: run, |
| 31 | } |
| 32 | pointsToCycle = &Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 33 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "pointsToCycle", |
| 34 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "this analyzer depends on inCycleA", |
| 35 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: run, |
| 36 | } |
| 37 | notInCycleA = &Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 38 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "notInCycleA", |
| 39 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "this analyzer depends on notInCycleB and notInCycleC", |
| 40 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: run, |
| 41 | } |
| 42 | notInCycleB = &Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 43 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "notInCycleB", |
| 44 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "this analyzer depends on notInCycleC", |
| 45 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: run, |
| 46 | } |
| 47 | notInCycleC = &Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 48 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "notInCycleC", |
| 49 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "this analyzer has no dependencies", |
| 50 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: run, |
| 51 | } |
| 52 | ) |
| 53 | |
| 54 | dependsOnSelf.Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires. = append(dependsOnSelf.Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires., dependsOnSelf) |
| 55 | inCycleA.Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires. = append(inCycleA.Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires., inCycleB) |
| 56 | inCycleB.Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires. = append(inCycleB.Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires., inCycleA, notInCycleA) |
| 57 | pointsToCycle.Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires. = append(pointsToCycle.Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires., inCycleA) |
| 58 | notInCycleA.Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires. = append(notInCycleA.Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires., notInCycleB, notInCycleC) |
| 59 | notInCycleB.Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires. = append(notInCycleB.Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires., notInCycleC) |
| 60 | notInCycleC.Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires. = []*Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{} |
| 61 | |
| 62 | cases := []struct { |
| 63 | analyzers []*Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer# |
| 64 | wantErr bool |
| 65 | analyzersInCycle map[string]bool |
| 66 | }{ |
| 67 | { |
| 68 | []*Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{dependsOnSelf}, |
| 69 | true, |
| 70 | map[string]bool{"dependsOnSelf": true}, |
| 71 | }, |
| 72 | { |
| 73 | []*Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{inCycleA, inCycleB}, |
| 74 | true, |
| 75 | map[string]bool{"inCycleA": true, "inCycleB": true}, |
| 76 | }, |
| 77 | { |
| 78 | []*Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{pointsToCycle}, |
| 79 | true, |
| 80 | map[string]bool{"inCycleA": true, "inCycleB": true}, |
| 81 | }, |
| 82 | { |
| 83 | []*Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{notInCycleA}, |
| 84 | false, |
| 85 | map[string]bool{}, |
| 86 | }, |
| 87 | } |
| 88 | |
| 89 | for _, c := range cases { |
| 90 | got := Validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Validate().(c.analyzers) |
| 91 | |
| 92 | if !c.wantErr { |
| 93 | if got == nil { |
| 94 | continue |
| 95 | } |
| 96 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("got unexpected error while validating analyzers %v: %v", c.analyzers, got) |
| 97 | } |
| 98 | |
| 99 | if got == nil { |
| 100 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("expected error while validating analyzers %v, but got nil", c.analyzers) |
| 101 | } |
| 102 | |
| 103 | err, ok := got.(*CycleInRequiresGraphErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/CycleInRequiresGraphError#) |
| 104 | if !ok { |
| 105 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("want CycleInRequiresGraphError, got %T", err) |
| 106 | } |
| 107 | |
| 108 | for a := range c.analyzersInCycle { |
| 109 | if !err.AnalyzerNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/CycleInRequiresGraphError#AnalyzerNames.[a] { |
| 110 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("analyzer %s should be in cycle", a) |
| 111 | } |
| 112 | } |
| 113 | for a := range err.AnalyzerNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/CycleInRequiresGraphError#AnalyzerNames. { |
| 114 | if !c.analyzersInCycle[a] { |
| 115 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("analyzer %s should not be in cycle", a) |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func TestCycleInRequiresGraphErrorMessagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TestCycleInRequiresGraphErrorMessage().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 122 | err := CycleInRequiresGraphErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/CycleInRequiresGraphError#{} |
| 123 | errMsg := err.Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/CycleInRequiresGraphError#Error().() |
| 124 | wantSubstring := "cycle detected" |
| 125 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(errMsg, wantSubstring) { |
| 126 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("error string %s does not contain expected substring %q", errMsg, wantSubstring) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestValidateEmptyDocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TestValidateEmptyDoc().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 131 | withoutDoc := &Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 132 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "withoutDoc", |
| 133 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: func(p *Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#) (any, error) { |
| 134 | return nil, nil |
| 135 | }, |
| 136 | } |
| 137 | err := Validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Validate().([]*Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{withoutDoc}) |
| 138 | if err == nil || !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(err.Error(), "is undocumented") { |
| 139 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("got unexpected error while validating analyzers withoutDoc: %v", err) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func TestValidateNoRunscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TestValidateNoRun().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 144 | withoutRun := &Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 145 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "withoutRun", |
| 146 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "this analyzer has no Run", |
| 147 | } |
| 148 | err := Validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Validate().([]*Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{withoutRun}) |
| 149 | if err == nil || !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(err.Error(), "has nil Run") { |
| 150 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("got unexpected error while validating analyzers withoutRun: %v", err) |
| 151 | } |
| 152 | } |