bee/generate/swaggergen/go_docs_test.go

225 lines
4.6 KiB
Go
Raw Normal View History

2021-02-06 12:32:17 +00:00
// Copyright 2013 bee authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
2021-02-06 12:10:12 +00:00
package swaggergen
import (
2022-05-23 13:42:04 +00:00
_ "github.com/shopspring/decimal"
2021-02-06 12:10:12 +00:00
"go/ast"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
2022-05-23 13:42:04 +00:00
// package model
2021-02-06 12:10:12 +00:00
//
2022-05-23 13:42:04 +00:00
// import (
// "sync"
2021-02-07 02:47:42 +00:00
//
2022-05-23 13:42:04 +00:00
// "example.com/pkgnotexist"
// "github.com/shopspring/decimal"
// )
2021-02-06 12:10:12 +00:00
//
2022-05-23 13:42:04 +00:00
// type Object struct {
2021-02-07 02:47:42 +00:00
// Field1 decimal.Decimal
// Field2 pkgnotexist.TestType
// Field3 sync.Map
2022-05-23 13:42:04 +00:00
// }
2021-02-06 12:10:12 +00:00
func TestCheckAndLoadPackageOnGoMod(t *testing.T) {
defer os.Setenv("GO111MODULE", os.Getenv("GO111MODULE"))
os.Setenv("GO111MODULE", "on")
2021-02-07 02:47:42 +00:00
testCases := []struct {
pkgName string
pkgImportPath string
imports []*ast.ImportSpec
realType string
curPkgName string
expected bool
}{
2021-02-06 12:10:12 +00:00
{
2021-02-07 02:47:42 +00:00
pkgName: "decimal",
pkgImportPath: "github.com/shopspring/decimal",
imports: []*ast.ImportSpec{
{
Path: &ast.BasicLit{
Value: "github.com/shopspring/decimal",
},
},
2021-02-06 12:10:12 +00:00
},
2021-02-07 02:47:42 +00:00
realType: "decimal.Decimal",
curPkgName: "model",
expected: true,
},
{
pkgName: "pkgnotexist",
pkgImportPath: "example.com/pkgnotexist",
imports: []*ast.ImportSpec{
{
Path: &ast.BasicLit{
Value: "example.com/pkgnotexist",
},
},
},
realType: "pkgnotexist.TestType",
curPkgName: "model",
expected: false,
},
{
pkgName: "sync",
pkgImportPath: "sync",
imports: []*ast.ImportSpec{
{
Path: &ast.BasicLit{
Value: "sync",
},
},
},
realType: "sync.Map",
curPkgName: "model",
expected: false,
2021-02-06 12:10:12 +00:00
},
}
2021-02-07 02:47:42 +00:00
for _, test := range testCases {
checkAndLoadPackage(test.imports, test.realType, test.curPkgName)
result := false
for _, v := range astPkgs {
if v.Name == test.pkgName {
result = true
2021-02-07 02:54:57 +00:00
break
2021-02-07 02:47:42 +00:00
}
}
if test.expected != result {
t.Fatalf("load module error, expected: %v, result: %v", test.expected, result)
2021-02-06 12:10:12 +00:00
}
}
}
2022-05-23 13:42:04 +00:00
// package model
2021-02-06 12:10:12 +00:00
//
2022-05-23 13:42:04 +00:00
// import (
// "sync"
2021-02-07 02:47:42 +00:00
//
2022-05-23 13:42:04 +00:00
// "example.com/comm"
// "example.com/pkgnotexist"
// )
2021-02-06 12:10:12 +00:00
//
2022-05-23 13:42:04 +00:00
// type Object struct {
2021-02-07 02:47:42 +00:00
// Field1 comm.Common
// Field2 pkgnotexist.TestType
// Field3 sync.Map
2022-05-23 13:42:04 +00:00
// }
2021-02-06 12:10:12 +00:00
func TestCheckAndLoadPackageOnGoPath(t *testing.T) {
var (
testCommPkg = `
package comm
type Common struct {
Code string
Error string
}
`
)
gopath, err := ioutil.TempDir("", "gobuild-gopath")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(gopath)
if err := os.MkdirAll(filepath.Join(gopath, "src/example.com/comm"), 0777); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(gopath, "src/example.com/comm/comm.go"), []byte(testCommPkg), 0666); err != nil {
t.Fatal(err)
}
defer os.Setenv("GO111MODULE", os.Getenv("GO111MODULE"))
os.Setenv("GO111MODULE", "off")
defer os.Setenv("GOPATH", os.Getenv("GOPATH"))
os.Setenv("GOPATH", gopath)
build.Default.GOPATH = gopath
2021-02-07 02:47:42 +00:00
testCases := []struct {
pkgName string
pkgImportPath string
imports []*ast.ImportSpec
realType string
curPkgName string
expected bool
}{
2021-02-06 12:10:12 +00:00
{
2021-02-07 02:47:42 +00:00
pkgName: "comm",
pkgImportPath: "example.com/comm",
imports: []*ast.ImportSpec{
{
Path: &ast.BasicLit{
Value: "example.com/comm",
},
},
2021-02-06 12:10:12 +00:00
},
2021-02-07 02:47:42 +00:00
realType: "comm.Common",
curPkgName: "model",
expected: true,
},
{
pkgName: "pkgnotexist",
pkgImportPath: "example.com/pkgnotexist",
imports: []*ast.ImportSpec{
{
Path: &ast.BasicLit{
Value: "example.com/pkgnotexist",
},
},
},
realType: "pkgnotexist.TestType",
curPkgName: "model",
expected: false,
},
{
pkgName: "sync",
pkgImportPath: "sync",
imports: []*ast.ImportSpec{
{
Path: &ast.BasicLit{
Value: "sync",
},
},
},
realType: "sync.Map",
curPkgName: "model",
expected: false,
2021-02-06 12:10:12 +00:00
},
}
2021-02-07 02:47:42 +00:00
for _, test := range testCases {
checkAndLoadPackage(test.imports, test.realType, test.curPkgName)
result := false
for _, v := range astPkgs {
if v.Name == test.pkgName {
result = true
2021-02-07 02:54:57 +00:00
break
2021-02-07 02:47:42 +00:00
}
}
if test.expected != result {
t.Fatalf("load module error, expected: %v, result: %v", test.expected, result)
2021-02-06 12:10:12 +00:00
}
}
}