From 3a06de031a1c3f2f6841c4247f777e91fdc7114f Mon Sep 17 00:00:00 2001 From: Jithin James Date: Mon, 16 Oct 2017 16:25:56 +0530 Subject: [PATCH] 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 --- .travis.yml | 1 + context_test.go | 4 ++++ debug.go | 35 +++++++++++++++++++++++++++++++++++ debug_test.go | 26 +++++++++++++++++++++++++- 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ec12cad6..cdf79c64 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/context_test.go b/context_test.go index 4854a8a8..56372336 100644 --- a/context_test.go +++ b/context_test.go @@ -321,6 +321,10 @@ func handlerNameTest(c *Context) { } +func handlerNameTest1(c *Context) { + +} + var handlerTest HandlerFunc = func(c *Context) { } diff --git a/debug.go b/debug.go index 449291e6..6df9d2e2 100644 --- a/debug.go +++ b/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) } diff --git a/debug_test.go b/debug_test.go index dfd54c82..9493c89c 100644 --- a/debug_test.go +++ b/debug_test.go @@ -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) {