fix error with windows filepath parse from URL

This commit is contained in:
Vladimir Kuznetsov 2020-04-28 19:10:34 +00:00 committed by Fedor Korotkiy
parent cd75697ac3
commit 53d176f38a

View file

@ -7,8 +7,10 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -70,7 +72,14 @@ func newEnv(t *testing.T, config *Config) (e *env, cancel func()) {
} }
cfg := zap.NewDevelopmentConfig() cfg := zap.NewDevelopmentConfig()
if runtime.GOOS == "windows" {
cfg.OutputPaths = []string{filepath.Join("winfile://", env.RootDir, "test.log")}
err = zap.RegisterSink("winfile", newWinFileSink)
require.NoError(t, err)
} else {
cfg.OutputPaths = []string{filepath.Join(env.RootDir, "test.log")} cfg.OutputPaths = []string{filepath.Join(env.RootDir, "test.log")}
}
if logToStderr { if logToStderr {
cfg.OutputPaths = append(cfg.OutputPaths, "stderr") cfg.OutputPaths = append(cfg.OutputPaths, "stderr")
@ -168,3 +177,13 @@ func newEnv(t *testing.T, config *Config) (e *env, cancel func()) {
goleak.VerifyNone(t) goleak.VerifyNone(t)
} }
} }
func newWinFileSink(u *url.URL) (zap.Sink, error) {
if len(u.Opaque) > 0 {
// Remove leading slash left by url.Parse()
return os.OpenFile(u.Opaque[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
}
// if url.URL is empty, don't panic slice index error
return os.OpenFile(u.Opaque, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
}