github.com/rzurga/go-swagger@v0.28.1-0.20211109195225-5d1f453ffa3a/scan/classifier_test.go (about) 1 // +build !go1.11 2 3 // Copyright 2015 go-swagger maintainers 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package scan 18 19 import ( 20 gobuild "go/build" 21 goparser "go/parser" 22 "log" 23 "path/filepath" 24 "sort" 25 "testing" 26 27 "golang.org/x/tools/go/loader" 28 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func TestAnnotationMatcher(t *testing.T) { 33 variations := []string{ 34 "// swagger", 35 " swagger", 36 "swagger", 37 " * swagger", 38 } 39 known := []string{ 40 "meta", 41 "route", 42 "model", 43 "parameters", 44 "strfmt", 45 "response", 46 "enum", 47 "default", 48 } 49 50 for _, variation := range variations { 51 for _, tpe := range known { 52 assert.True(t, rxSwaggerAnnotation.MatchString(variation+":"+tpe)) 53 } 54 } 55 } 56 57 func classifierProgram() *loader.Program { 58 var ldr loader.Config 59 ldr.ParserMode = goparser.ParseComments 60 ldr.Build = &gobuild.Default 61 ldr.ImportWithTests("github.com/go-swagger/go-swagger/fixtures/goparsing/classification") 62 ldr.ImportWithTests("github.com/go-swagger/go-swagger/fixtures/goparsing/classification/models") 63 ldr.ImportWithTests("github.com/go-swagger/go-swagger/fixtures/goparsing/classification/operations") 64 prog, err := ldr.Load() 65 if err != nil { 66 log.Fatal(err) 67 } 68 return prog 69 } 70 71 func petstoreProgram() *loader.Program { 72 var ldr loader.Config 73 ldr.ParserMode = goparser.ParseComments 74 ldr.Build = &gobuild.Default 75 ldr.ImportWithTests("github.com/go-swagger/go-swagger/fixtures/goparsing/petstore") 76 ldr.ImportWithTests("github.com/go-swagger/go-swagger/fixtures/goparsing/petstore/models") 77 ldr.ImportWithTests("github.com/go-swagger/go-swagger/fixtures/goparsing/petstore/rest/handlers") 78 prog, err := ldr.Load() 79 if err != nil { 80 log.Fatal(err) 81 } 82 return prog 83 } 84 85 func invalidProgram(name string) *loader.Program { 86 var ldr loader.Config 87 ldr.ParserMode = goparser.ParseComments 88 ldr.ImportWithTests("github.com/go-swagger/go-swagger/fixtures/goparsing/" + name) 89 prog, err := ldr.Load() 90 if err != nil { 91 log.Fatal(err) 92 } 93 return prog 94 } 95 96 func testInvalidProgram(t testing.TB, name string) bool { 97 prog := invalidProgram(name) 98 classifier := &programClassifier{} 99 _, err := classifier.Classify(prog) 100 return assert.Error(t, err) 101 } 102 103 func TestDuplicateAnnotations(t *testing.T) { 104 for _, b := range []string{"model_param", "param_model", "model_response", "response_model"} { 105 testInvalidProgram(t, "invalid_"+b) 106 } 107 } 108 109 func TestClassifier(t *testing.T) { 110 111 prog := petstoreProgram() 112 classifier := &programClassifier{} 113 classified, err := classifier.Classify(prog) 114 assert.NoError(t, err) 115 116 // ensure all the dependencies are there 117 assert.Len(t, classified.Meta, 1) 118 assert.Len(t, classified.Routes, 2) 119 120 var fNames []string 121 for _, file := range classified.Models { 122 fNames = append( 123 fNames, 124 filepath.Base(prog.Fset.File(file.Pos()).Name())) 125 } 126 127 sort.Sort(sort.StringSlice(fNames)) 128 assert.EqualValues(t, []string{"order.go", "pet.go", "tag.go", "user.go"}, fNames) 129 } 130 131 func TestClassifierInclude(t *testing.T) { 132 133 prog := classificationProg 134 classifier := &programClassifier{ 135 Includes: packageFilters([]packageFilter{ 136 {Name: "github.com/go-swagger/go-swagger/fixtures/goparsing/classification"}, 137 {Name: "github.com/go-swagger/go-swagger/fixtures/goparsing/classification/transitive/mods"}, 138 {Name: "github.com/go-swagger/go-swagger/fixtures/goparsing/classification/operations"}, 139 {Name: "github.com/go-swagger/go-swagger/fixtures/goparsing/classification/operations_annotation"}, 140 }), 141 } 142 classified, err := classifier.Classify(prog) 143 assert.NoError(t, err) 144 145 // ensure all the dependencies are there 146 assert.Len(t, classified.Meta, 1) 147 assert.Len(t, classified.Routes, 1) 148 149 //var fNames []string 150 //for _, file := range classified.Models { 151 //fNames = append( 152 //fNames, 153 //filepath.Base(prog.Fset.File(file.Pos()).Name())) 154 //} 155 156 //sort.Sort(sort.StringSlice(fNames)) 157 //assert.EqualValues(t, []string{"pet.go"}, fNames) 158 } 159 160 func TestClassifierExclude(t *testing.T) { 161 162 prog := classificationProg 163 classifier := &programClassifier{ 164 Excludes: packageFilters([]packageFilter{ 165 {Name: "github.com/go-swagger/go-swagger/fixtures/goparsing/classification/transitive/mods"}, 166 }), 167 } 168 classified, err := classifier.Classify(prog) 169 assert.NoError(t, err) 170 171 // ensure all the dependencies are there 172 assert.Len(t, classified.Meta, 1) 173 assert.Len(t, classified.Routes, 1) 174 175 //var fNames []string 176 //for _, file := range classified.Models { 177 //fNames = append( 178 //fNames, 179 //filepath.Base(prog.Fset.File(file.Pos()).Name())) 180 //} 181 182 //sort.Sort(sort.StringSlice(fNames)) 183 //assert.EqualValues(t, []string{"order.go", "user.go"}, fNames) 184 }