1package gou
2
3import (
4	"io/ioutil"
5	"os"
6	"strings"
7	"testing"
8	"time"
9)
10
11func TestSetupLogToFile(t *testing.T) {
12	tmpf, err := ioutil.TempFile("", "goutest")
13	if err != nil {
14		t.Fatalf("error creating log file: %v\n", err)
15	}
16	defer os.Remove(tmpf.Name())
17
18	SetupLoggingFile(tmpf, "debug")
19	logStr := "hihi"
20	Infof(logStr)
21
22	// Flush file buffer to disk
23	err = tmpf.Sync()
24	if err != nil {
25		t.Errorf("error syncing tmpf: %v", err)
26	}
27	time.Sleep(1 * time.Second)
28
29	// Read tmp file and confirm log message was written
30	bytes, err := ioutil.ReadFile(tmpf.Name())
31	if err != nil {
32		t.Errorf("error reading temp file[%s]: %v\n", tmpf.Name(), err)
33	}
34
35	logFileBytes := string(bytes)
36	if !strings.Contains(logFileBytes, logStr) {
37		t.Logf("logfile:\n%s", logFileBytes)
38		t.Errorf("%s not found in logfile %s\n", logStr, tmpf.Name())
39	}
40}
41