github.com/jamescostian/go-swagger@v0.30.4-0.20221130163922-68364d6b567b/scan/classifier_test.go (about)

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