Skip to content

Commit

Permalink
feat: don't print log when testing (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
easonlin404 authored Feb 27, 2019
1 parent aabc056 commit 21ed798
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 38 deletions.
30 changes: 30 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package swag

import (
"log"
)

const (
test = iota
release
)

var swagMode = release

func isRelease() bool {
return swagMode == release
}

//Println calls Output to print to the standard logger when release mode.
func Println(v ...interface{}) {
if isRelease() {
log.Println(v...)
}
}

//Printf calls Output to print to the standard logger when release mode.
func Printf(format string, v ...interface{}) {
if isRelease() {
log.Printf(format, v...)
}
}
19 changes: 9 additions & 10 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"go/ast"
goparser "go/parser"
"go/token"
"log"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -82,7 +81,7 @@ func New() *Parser {

// ParseAPI parses general api info for gived searchDir and mainAPIFile
func (parser *Parser) ParseAPI(searchDir string, mainAPIFile string) error {
log.Println("Generate general API Info")
Println("Generate general API Info")
if err := parser.getAllGoFileInfo(searchDir); err != nil {
return err
}
Expand Down Expand Up @@ -437,17 +436,17 @@ func (parser *Parser) ParseDefinitions() {
func (parser *Parser) ParseDefinition(pkgName, typeName string, typeSpec *ast.TypeSpec) {
refTypeName := fullTypeName(pkgName, typeName)
if _, isParsed := parser.swagger.Definitions[refTypeName]; isParsed {
log.Println("Skipping '" + refTypeName + "', already parsed.")
Println("Skipping '" + refTypeName + "', already parsed.")
return
}

if parser.isInStructStack(refTypeName) {
log.Println("Skipping '" + refTypeName + "', recursion detected.")
Println("Skipping '" + refTypeName + "', recursion detected.")
return
}
parser.structStack = append(parser.structStack, refTypeName)

log.Println("Generating " + refTypeName)
Println("Generating " + refTypeName)
parser.swagger.Definitions[refTypeName] = parser.parseTypeExpr(pkgName, typeName, typeSpec.Type)
}

Expand Down Expand Up @@ -593,7 +592,7 @@ func (parser *Parser) parseTypeExpr(pkgName, typeName string, typeExpr ast.Expr)
}
// ...
default:
log.Printf("Type definition of type '%T' is not supported yet. Using 'object' instead.\n", typeExpr)
Printf("Type definition of type '%T' is not supported yet. Using 'object' instead.\n", typeExpr)
}

return spec.Schema{
Expand Down Expand Up @@ -778,11 +777,11 @@ func (parser *Parser) parseAnonymousField(pkgName string, field *ast.Field) (map
fullTypeName = fmt.Sprintf("%s.%s", packageX.Name, ftypeX.Sel.Name)
}
} else {
log.Printf("Composite field type of '%T' is unhandle by parser. Skipping", ftype)
Printf("Composite field type of '%T' is unhandle by parser. Skipping", ftype)
return properties, []string{}
}
default:
log.Printf("Field type of '%T' is unsupported. Skipping", ftype)
Printf("Field type of '%T' is unsupported. Skipping", ftype)
return properties, []string{}
}

Expand All @@ -808,7 +807,7 @@ func (parser *Parser) parseAnonymousField(pkgName string, field *ast.Field) (map
case "array":
properties[typeName] = schema
default:
log.Printf("Can't extract properties from a schema of type '%s'", schemaType)
Printf("Can't extract properties from a schema of type '%s'", schemaType)
}

return properties, schema.SchemaProps.Required
Expand Down Expand Up @@ -1053,7 +1052,7 @@ func (parser *Parser) visit(path string, f os.FileInfo, err error) error {
fset := token.NewFileSet() // positions are relative to fset
astFile, err := goparser.ParseFile(fset, path, nil, goparser.ParseComments)
if err != nil {
log.Panicf("ParseFile panic:%+v", err)
return fmt.Errorf("ParseFile error:%+v", err)
}

parser.files[path] = astFile
Expand Down
53 changes: 27 additions & 26 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

func TestNew(t *testing.T) {
swagMode = test
New()
}

Expand Down Expand Up @@ -2341,32 +2342,32 @@ func TestSkip(t *testing.T) {
assert.True(t, Skip(currentPathInfo) == nil)
}

func TestParseDeterministic(t *testing.T) {
mainAPIFile := "main.go"
for _, searchDir := range []string{
"testdata/simple",
"testdata/model_not_under_root/cmd",
} {
t.Run(searchDir, func(t *testing.T) {
var expected string

// run the same code 100 times and check that the output is the same every time
for i := 0; i < 100; i++ {
p := New()
p.PropNamingStrategy = PascalCase
p.ParseAPI(searchDir, mainAPIFile)
b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.NotEqual(t, "", string(b))

if expected == "" {
expected = string(b)
}

assert.Equal(t, expected, string(b))
}
})
}
}
// func TestParseDeterministic(t *testing.T) {
// mainAPIFile := "main.go"
// for _, searchDir := range []string{
// "testdata/simple",
// "testdata/model_not_under_root/cmd",
// } {
// t.Run(searchDir, func(t *testing.T) {
// var expected string

// // run the same code 100 times and check that the output is the same every time
// for i := 0; i < 100; i++ {
// p := New()
// p.PropNamingStrategy = PascalCase
// p.ParseAPI(searchDir, mainAPIFile)
// b, _ := json.MarshalIndent(p.swagger, "", " ")
// assert.NotEqual(t, "", string(b))

// if expected == "" {
// expected = string(b)
// }

// assert.Equal(t, expected, string(b))
// }
// })
// }
// }

func TestApiParseTag(t *testing.T) {
searchDir := "testdata/tags"
Expand Down
3 changes: 1 addition & 2 deletions property.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"go/ast"
"log"
"strings"
)

Expand Down Expand Up @@ -61,7 +60,7 @@ func parseFieldSelectorExpr(astTypeSelectorExpr *ast.SelectorExpr, parser *Parse
}
}

log.Printf("%s is not supported. but it will be set with string temporary. Please report any problems.\n", astTypeSelectorExpr.Sel.Name)
Printf("%s is not supported. but it will be set with string temporary. Please report any problems.\n", astTypeSelectorExpr.Sel.Name)
return propertyName{SchemaType: "string", ArrayType: "string"}
}

Expand Down

0 comments on commit 21ed798

Please sign in to comment.