Skip to content

Commit

Permalink
Use a null writer + null logger when running tests
Browse files Browse the repository at this point in the history
  • Loading branch information
film42 committed May 5, 2017
1 parent a6efab8 commit eac39ff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 2 additions & 4 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
"io"
"net/http"
"net"
"net/http"
"strings"
"testing"
)
Expand Down Expand Up @@ -50,7 +50,7 @@ func readMessage(reader io.Reader) string {
}

func TestMain(m *testing.M) {
InitLogger()
InitNullLogger()
m.Run()
}

Expand Down Expand Up @@ -163,7 +163,6 @@ func TestSampleProxyViaConnect(t *testing.T) {
t.Fatalf("Expected '%s' but got '%s'", expected_response, response)
}


// Mimic a regular http request
request := "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"
incoming.ClientWriter.Write([]byte(request))
Expand All @@ -173,7 +172,6 @@ func TestSampleProxyViaConnect(t *testing.T) {
t.Fatalf("Expected '%s' but got '%s'", expected_response, response)
}


incoming.CloseClient()
<-cleanedUp
}
14 changes: 14 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import (
"os"
)

type nullWriter struct{}

func (nullWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

type Logger struct {
Info *log.Logger
Warn *log.Logger
Expand All @@ -17,6 +23,14 @@ func InitLogger() {
logger = NewLogger()
}

func InitNullLogger() {
nw := nullWriter{}
InitLogger()
logger.Info.SetOutput(nw)
logger.Warn.SetOutput(nw)
logger.Fatal.SetOutput(nw)
}

func NewLogger() *Logger {
return &Logger{
Info: log.New(os.Stdout, "[info] ", log.Ldate|log.Ltime|log.Lshortfile),
Expand Down

0 comments on commit eac39ff

Please sign in to comment.