File: logutil_test.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (39 lines) | stat: -rw-r--r-- 898 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package logutil

import (
	"os"
	"path/filepath"
	"regexp"
	"testing"

	"src.elv.sh/pkg/must"
)

func TestLogger(t *testing.T) {
	logger := GetLogger("foo ")

	r, w := must.Pipe()
	SetOutput(w)
	logger.Println("out 1")
	w.Close()
	wantOut1 := must.OK1(regexp.Compile("^foo .*out 1\n$"))
	if out := must.ReadAllAndClose(r); !wantOut1.Match(out) {
		t.Errorf("got out %q, want one matching %q", out, wantOut1)
	}

	outPath := filepath.Join(t.TempDir(), "out")
	must.OK(SetOutputFile(outPath))
	logger.Println("out 2")
	must.OK(SetOutputFile(""))
	wantOut2 := must.OK1(regexp.Compile("^foo .*out 2\n$"))
	if out := must.ReadAllAndClose(must.OK1(os.Open(outPath))); !wantOut2.Match(out) {
		t.Errorf("got out %q, want one matching %q", out, wantOut2)
	}
}

func TestSetOutput_Error(t *testing.T) {
	err := SetOutputFile("/bad/file/path")
	if err == nil {
		t.Errorf("want non-nil error, got nil")
	}
}