From 06bc259b299812b7da7a7846b81601ba489d39f9 Mon Sep 17 00:00:00 2001 From: Maxi_Mega <52792549+Maxi-Mega@users.noreply.github.com> Date: Mon, 1 Aug 2022 12:16:49 +0200 Subject: [PATCH] :art: Fix padding around app name in startup message when containing non-ascii characters (#1987) * Fix padding around app name in startup message when it contains non-ascii characters * fix conflict, allow ending space only for odd length strings * move startup message tests to listen_test.go --- app_test.go | 56 ----------------------------------------- listen.go | 5 ++-- listen_test.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 58 deletions(-) diff --git a/app_test.go b/app_test.go index f64c32fbc4..c812e93a0a 100644 --- a/app_test.go +++ b/app_test.go @@ -10,16 +10,13 @@ import ( "fmt" "io" "io/ioutil" - "log" "mime/multipart" "net" "net/http" "net/http/httptest" - "os" "reflect" "regexp" "strings" - "sync" "testing" "time" @@ -1348,59 +1345,6 @@ func Test_App_SmallReadBuffer(t *testing.T) { utils.AssertEqual(t, nil, app.Listen(":4006")) } -func captureOutput(f func()) string { - reader, writer, err := os.Pipe() - if err != nil { - panic(err) - } - stdout := os.Stdout - stderr := os.Stderr - defer func() { - os.Stdout = stdout - os.Stderr = stderr - log.SetOutput(os.Stderr) - }() - os.Stdout = writer - os.Stderr = writer - log.SetOutput(writer) - out := make(chan string) - wg := new(sync.WaitGroup) - wg.Add(1) - go func() { - var buf bytes.Buffer - wg.Done() - io.Copy(&buf, reader) - out <- buf.String() - }() - wg.Wait() - f() - writer.Close() - return <-out -} - -func Test_App_Master_Process_Show_Startup_Message(t *testing.T) { - startupMessage := captureOutput(func() { - New(Config{Prefork: true}). - startupMessage(":3000", true, strings.Repeat(",11111,22222,33333,44444,55555,60000", 10)) - }) - fmt.Println(startupMessage) - utils.AssertEqual(t, true, strings.Contains(startupMessage, "https://127.0.0.1:3000")) - utils.AssertEqual(t, true, strings.Contains(startupMessage, "(bound on host 0.0.0.0 and port 3000)")) - utils.AssertEqual(t, true, strings.Contains(startupMessage, "Child PIDs")) - utils.AssertEqual(t, true, strings.Contains(startupMessage, "11111, 22222, 33333, 44444, 55555, 60000")) - utils.AssertEqual(t, true, strings.Contains(startupMessage, "Prefork ........ Enabled")) -} - -func Test_App_Master_Process_Show_Startup_MessageWithAppName(t *testing.T) { - app := New(Config{Prefork: true, AppName: "Test App v1.0.1"}) - startupMessage := captureOutput(func() { - app.startupMessage(":3000", true, strings.Repeat(",11111,22222,33333,44444,55555,60000", 10)) - }) - fmt.Println(startupMessage) - utils.AssertEqual(t, "Test App v1.0.1", app.Config().AppName) - utils.AssertEqual(t, true, strings.Contains(startupMessage, app.Config().AppName)) -} - func Test_App_Server(t *testing.T) { app := New() diff --git a/listen.go b/listen.go index 8f92007b91..8bd6f0dd4d 100644 --- a/listen.go +++ b/listen.go @@ -208,11 +208,12 @@ func (app *App) startupMessage(addr string, tls bool, pids string) { } centerValue := func(s string, width int) string { - pad := strconv.Itoa((width - len(s)) / 2) + pad := strconv.Itoa((width - len([]rune(s))) / 2) str := fmt.Sprintf("%"+pad+"s", " ") str += fmt.Sprintf("%s%s%s", colors.Cyan, s, colors.Black) str += fmt.Sprintf("%"+pad+"s", " ") - if len(str)-10 < width { + if len([]rune(str))-10 < width && len([]rune(str))%2 != 0 { + // add an ending space if the length of str is odd and str is not too long str += " " } return str diff --git a/listen_test.go b/listen_test.go index 5eb035f515..11d1908bd4 100644 --- a/listen_test.go +++ b/listen_test.go @@ -5,9 +5,14 @@ package fiber import ( + "bytes" "crypto/tls" "fmt" + "io" + "log" + "os" "strings" + "sync" "testing" "time" @@ -140,6 +145,69 @@ func Test_App_Listener_TLS_Listener(t *testing.T) { utils.AssertEqual(t, nil, app.Listener(ln)) } +func captureOutput(f func()) string { + reader, writer, err := os.Pipe() + if err != nil { + panic(err) + } + stdout := os.Stdout + stderr := os.Stderr + defer func() { + os.Stdout = stdout + os.Stderr = stderr + log.SetOutput(os.Stderr) + }() + os.Stdout = writer + os.Stderr = writer + log.SetOutput(writer) + out := make(chan string) + wg := new(sync.WaitGroup) + wg.Add(1) + go func() { + var buf bytes.Buffer + wg.Done() + io.Copy(&buf, reader) + out <- buf.String() + }() + wg.Wait() + f() + writer.Close() + return <-out +} + +func Test_App_Master_Process_Show_Startup_Message(t *testing.T) { + startupMessage := captureOutput(func() { + New(Config{Prefork: true}). + startupMessage(":3000", true, strings.Repeat(",11111,22222,33333,44444,55555,60000", 10)) + }) + fmt.Println(startupMessage) + utils.AssertEqual(t, true, strings.Contains(startupMessage, "https://127.0.0.1:3000")) + utils.AssertEqual(t, true, strings.Contains(startupMessage, "(bound on host 0.0.0.0 and port 3000)")) + utils.AssertEqual(t, true, strings.Contains(startupMessage, "Child PIDs")) + utils.AssertEqual(t, true, strings.Contains(startupMessage, "11111, 22222, 33333, 44444, 55555, 60000")) + utils.AssertEqual(t, true, strings.Contains(startupMessage, "Prefork ........ Enabled")) +} + +func Test_App_Master_Process_Show_Startup_MessageWithAppName(t *testing.T) { + app := New(Config{Prefork: true, AppName: "Test App v1.0.1"}) + startupMessage := captureOutput(func() { + app.startupMessage(":3000", true, strings.Repeat(",11111,22222,33333,44444,55555,60000", 10)) + }) + fmt.Println(startupMessage) + utils.AssertEqual(t, "Test App v1.0.1", app.Config().AppName) + utils.AssertEqual(t, true, strings.Contains(startupMessage, app.Config().AppName)) +} + +func Test_App_Master_Process_Show_Startup_MessageWithAppNameNonAscii(t *testing.T) { + appName := "Serveur de vérification des données" + app := New(Config{Prefork: true, AppName: appName}) + startupMessage := captureOutput(func() { + app.startupMessage(":3000", false, "") + }) + fmt.Println(startupMessage) + utils.AssertEqual(t, true, strings.Contains(startupMessage, "│ Serveur de vérification des données │")) +} + func Test_App_print_Route(t *testing.T) { app := New(Config{EnablePrintRoutes: true}) app.Get("/", emptyHandler).Name("routeName")