Skip to content

Commit

Permalink
🎨 Fix padding around app name in startup message when containing non-…
Browse files Browse the repository at this point in the history
…ascii characters (gofiber#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
  • Loading branch information
Maxi-Mega authored and trim21 committed Aug 15, 2022
1 parent d80e2e5 commit 06bc259
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 58 deletions.
56 changes: 0 additions & 56 deletions app_test.go
Expand Up @@ -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"

Expand Down Expand Up @@ -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()

Expand Down
5 changes: 3 additions & 2 deletions listen.go
Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions listen_test.go
Expand Up @@ -5,9 +5,14 @@
package fiber

import (
"bytes"
"crypto/tls"
"fmt"
"io"
"log"
"os"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 06bc259

Please sign in to comment.