From 6277d633e0858bd9bb157a48104ed4365febf328 Mon Sep 17 00:00:00 2001 From: Lucas Clemente Date: Thu, 3 Aug 2017 20:42:19 +0200 Subject: [PATCH] Factor out inttest logging setup into a separate package --- integrationtests/chrome/chrome_suite_test.go | 2 + .../integrationtests_suite_test.go | 33 ++------------- integrationtests/tools/testlog/testlog.go | 41 +++++++++++++++++++ 3 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 integrationtests/tools/testlog/testlog.go diff --git a/integrationtests/chrome/chrome_suite_test.go b/integrationtests/chrome/chrome_suite_test.go index 00184d8b..b8820916 100644 --- a/integrationtests/chrome/chrome_suite_test.go +++ b/integrationtests/chrome/chrome_suite_test.go @@ -18,6 +18,8 @@ import ( "github.com/lucas-clemente/quic-go/internal/utils" "github.com/lucas-clemente/quic-go/protocol" + _ "github.com/lucas-clemente/quic-go/integrationtests/tools/testlog" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/onsi/gomega/gexec" diff --git a/integrationtests/integrationtests_suite_test.go b/integrationtests/integrationtests_suite_test.go index 55a00878..3b5a3da1 100644 --- a/integrationtests/integrationtests_suite_test.go +++ b/integrationtests/integrationtests_suite_test.go @@ -1,14 +1,11 @@ package integrationtests import ( - "flag" "fmt" "io" "io/ioutil" - "log" "net" "net/http" - "os" "path/filepath" "runtime" @@ -16,7 +13,8 @@ import ( "github.com/lucas-clemente/quic-go/h2quic" "github.com/lucas-clemente/quic-go/internal/testdata" - "github.com/lucas-clemente/quic-go/internal/utils" + + _ "github.com/lucas-clemente/quic-go/integrationtests/tools/testlog" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -35,9 +33,6 @@ var ( port string clientPath string serverPath string - - logFileName string // the log file set in the ginkgo flags - logFile *os.File ) func TestIntegration(t *testing.T) { @@ -47,40 +42,18 @@ func TestIntegration(t *testing.T) { var _ = BeforeSuite(setupHTTPHandlers) -// read the logfile command line flag -// to set call ginkgo -- -logfile=log.txt -func init() { - flag.StringVar(&logFileName, "logfile", "", "log file") -} - var _ = BeforeEach(func() { - // set custom time format for logs - utils.SetLogTimeFormat("15:04:05.000") _, thisfile, _, ok := runtime.Caller(0) if !ok { Fail("Failed to get current path") } clientPath = filepath.Join(thisfile, fmt.Sprintf("../../../quic-clients/client-%s-debug", runtime.GOOS)) serverPath = filepath.Join(thisfile, fmt.Sprintf("../../../quic-clients/server-%s-debug", runtime.GOOS)) - - if len(logFileName) > 0 { - var err error - logFile, err = os.Create("./log.txt") - Expect(err).ToNot(HaveOccurred()) - log.SetOutput(logFile) - utils.SetLogLevel(utils.LogLevelDebug) - } }) var _ = JustBeforeEach(startQuicServer) -var _ = AfterEach(func() { - stopQuicServer() - - if len(logFileName) > 0 { - _ = logFile.Close() - } -}) +var _ = AfterEach(stopQuicServer) func setupHTTPHandlers() { defer GinkgoRecover() diff --git a/integrationtests/tools/testlog/testlog.go b/integrationtests/tools/testlog/testlog.go new file mode 100644 index 00000000..7db09a44 --- /dev/null +++ b/integrationtests/tools/testlog/testlog.go @@ -0,0 +1,41 @@ +package testlog + +import ( + "flag" + "log" + "os" + + "github.com/lucas-clemente/quic-go/internal/utils" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var ( + logFileName string // the log file set in the ginkgo flags + logFile *os.File +) + +// read the logfile command line flag +// to set call ginkgo -- -logfile=log.txt +func init() { + flag.StringVar(&logFileName, "logfile", "", "log file") +} + +var _ = BeforeEach(func() { + log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds) + + if len(logFileName) > 0 { + var err error + logFile, err = os.Create("./log.txt") + Expect(err).ToNot(HaveOccurred()) + log.SetOutput(logFile) + utils.SetLogLevel(utils.LogLevelDebug) + } +}) + +var _ = AfterEach(func() { + if len(logFileName) > 0 { + _ = logFile.Close() + } +})