mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-23 18:22:23 +08:00
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:
parent
b8b68314fa
commit
3a06de031a
@ -11,6 +11,7 @@ git:
|
||||
depth: 3
|
||||
|
||||
install:
|
||||
- go get golang.org/x/crypto/ssh/terminal
|
||||
- make install
|
||||
|
||||
go_import_path: github.com/gin-gonic/gin
|
||||
|
@ -321,6 +321,10 @@ func handlerNameTest(c *Context) {
|
||||
|
||||
}
|
||||
|
||||
func handlerNameTest1(c *Context) {
|
||||
|
||||
}
|
||||
|
||||
var handlerTest HandlerFunc = func(c *Context) {
|
||||
|
||||
}
|
||||
|
35
debug.go
35
debug.go
@ -6,8 +6,12 @@ package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -20,9 +24,40 @@ func IsDebugging() bool {
|
||||
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) {
|
||||
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)
|
||||
debugPrint("Total %d handlers found...\n\n", nuHandlers)
|
||||
handlerName := nameOfFunction(handlers.Last())
|
||||
debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"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())
|
||||
}
|
||||
|
||||
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) {
|
||||
var w bytes.Buffer
|
||||
setup(&w)
|
||||
defer teardown()
|
||||
|
||||
// Even routes
|
||||
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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user