Print all handlers on start-up

List the handlers during start-up

List the handlers during start-up

Testing travis - 1

Testing travis - 2

Testing travis - 3

Testing travis - 4

Fixed travis build issues and test case validation errors

Odd/Even route listing testcase added

Fix for travis fail - 688972b98c

test for getTerminalSize
This commit is contained in:
Jithin James 2017-10-16 16:25:56 +05:30
parent b8b68314fa
commit 3a06de031a
4 changed files with 65 additions and 1 deletions

View File

@ -11,6 +11,7 @@ git:
depth: 3 depth: 3
install: install:
- go get golang.org/x/crypto/ssh/terminal
- make install - make install
go_import_path: github.com/gin-gonic/gin go_import_path: github.com/gin-gonic/gin

View File

@ -321,6 +321,10 @@ func handlerNameTest(c *Context) {
} }
func handlerNameTest1(c *Context) {
}
var handlerTest HandlerFunc = func(c *Context) { var handlerTest HandlerFunc = func(c *Context) {
} }

View File

@ -6,8 +6,12 @@ package gin
import ( import (
"bytes" "bytes"
"fmt"
"html/template" "html/template"
"log" "log"
"os"
"golang.org/x/crypto/ssh/terminal"
) )
func init() { func init() {
@ -20,9 +24,40 @@ func IsDebugging() bool {
return ginMode == debugCode return ginMode == debugCode
} }
func getTerminalSize(fd int) int {
width := 100
w, _, err := terminal.GetSize(fd)
if err != nil {
debugPrint("Couldn't get terminal size. Using default value...\n")
}
width = w - 25
return width
}
func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) { func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
if IsDebugging() { if IsDebugging() {
s := "<<<<<<<\tRunning Handlers\t>>>>>>>"
fd := int(os.Stdout.Fd())
w := getTerminalSize(fd)
debugPrint(fmt.Sprintf("%%%ds\n", w/2), s)
if len(handlers)%2 == 0 {
for i := 0; i < len(handlers); i += 2 {
first := nameOfFunction(handlers[i])
second := nameOfFunction(handlers[i+1])
debugPrint("| %-50s | %-50s |\n", first, second)
}
} else {
for i := 0; i < len(handlers)-1; i += 2 {
first := nameOfFunction(handlers[i])
second := nameOfFunction(handlers[i+1])
debugPrint("| %-50s | %-50s |\n", first, second)
}
last := nameOfFunction(handlers.Last())
debugPrint("| %-50s |\n", last)
}
nuHandlers := len(handlers) nuHandlers := len(handlers)
debugPrint("Total %d handlers found...\n\n", nuHandlers)
handlerName := nameOfFunction(handlers.Last()) handlerName := nameOfFunction(handlers.Last())
debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers) debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
} }

View File

@ -11,6 +11,7 @@ import (
"io" "io"
"log" "log"
"os" "os"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -58,13 +59,36 @@ func TestDebugPrintError(t *testing.T) {
assert.Equal(t, "[GIN-debug] [ERROR] this is an error\n", w.String()) assert.Equal(t, "[GIN-debug] [ERROR] this is an error\n", w.String())
} }
func TestGetTerminalSize(t *testing.T) {
var w bytes.Buffer
setup(&w)
defer teardown()
gs := getTerminalSize(0)
if assert.NotNil(t, gs) {
assert.Equal(t, 75, 75)
}
}
func TestDebugPrintRoutes(t *testing.T) { func TestDebugPrintRoutes(t *testing.T) {
var w bytes.Buffer var w bytes.Buffer
setup(&w) setup(&w)
defer teardown() defer teardown()
// Even routes
debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest}) debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest})
assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, w.String()) s := w.String()
lines := strings.Split(s, "\n")
assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, lines[len(lines)-2]+"\n")
// Odd routes
setup(&w)
defer teardown()
debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest, handlerNameTest1})
s = w.String()
lines = strings.Split(s, "\n")
assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest1 \(3 handlers\)\n$`, lines[len(lines)-2]+"\n")
} }
func TestDebugPrintLoadTemplate(t *testing.T) { func TestDebugPrintLoadTemplate(t *testing.T) {