Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 Fix padding around app name in startup message when containing non-ascii characters #1987

Merged
merged 4 commits into from Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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