Skip to content

Commit

Permalink
assert that log file is created by writer
Browse files Browse the repository at this point in the history
  • Loading branch information
dimroc committed Apr 12, 2018
1 parent 9b29574 commit 08c8e44
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
package zap

import (
"crypto/rand"
"encoding/hex"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -44,20 +47,19 @@ func TestOpenNoPaths(t *testing.T) {
}

func TestOpen(t *testing.T) {
temp, err := ioutil.TempFile("", "zap-open-test")
require.NoError(t, err, "Couldn't create a temporary file for test.")
defer os.Remove(temp.Name())
tempName := tempFileName("", "zap-open-test")
assert.False(t, fileExists(tempName))

tests := []struct {
paths []string
error string
}{
{[]string{"stdout"}, ""},
{[]string{"stderr"}, ""},
{[]string{temp.Name()}, ""},
{[]string{tempName}, ""},
{[]string{"/foo/bar/baz"}, "open /foo/bar/baz: no such file or directory"},
{
paths: []string{"stdout", "/foo/bar/baz", temp.Name(), "/baz/quux"},
paths: []string{"stdout", "/foo/bar/baz", tempName, "/baz/quux"},
error: "open /foo/bar/baz: no such file or directory; open /baz/quux: no such file or directory",
},
}
Expand All @@ -74,6 +76,9 @@ func TestOpen(t *testing.T) {
assert.Equal(t, tt.error, err.Error(), "Unexpected error opening paths %v.", tt.paths)
}
}

assert.True(t, fileExists(tempName))
os.Remove(tempName)
}

func TestOpenFails(t *testing.T) {
Expand Down Expand Up @@ -123,3 +128,16 @@ func TestCombineWriteSyncers(t *testing.T) {
w := CombineWriteSyncers(tw)
w.Write([]byte("test"))
}

func tempFileName(prefix, suffix string) string {
randBytes := make([]byte, 16)
rand.Read(randBytes)
return filepath.Join(os.TempDir(), prefix+hex.EncodeToString(randBytes)+suffix)
}

func fileExists(name string) bool {
if _, err := os.Stat(name); os.IsNotExist(err) {
return false
}
return true
}

0 comments on commit 08c8e44

Please sign in to comment.