12 func bufLogger() (*log.Logger, *bufio.Reader) {
14 logger := log.New(w, "", 0)
15 return logger, bufio.NewReader(r)
18 func TestReadAllOrWarnFail(t *testing.T) {
19 logger, rcv := bufLogger()
20 rep := Reporter{Logger: logger}
22 done := make(chan bool)
26 msg, err = rcv.ReadBytes('\n')
30 // The special file /proc/self/mem can be opened for
31 // reading, but reading from byte 0 returns an error.
32 f, err := os.Open("/proc/self/mem")
34 t.Fatalf("Opening /proc/self/mem: %s", err)
36 if x, err := rep.readAllOrWarn(f); err == nil {
37 t.Fatalf("Expected error, got %v", x)
43 } else if matched, err := regexp.MatchString("^read /proc/self/mem: .*", string(msg)); err != nil || !matched {
44 t.Fatalf("Expected error message about unreadable file, got \"%s\"", msg)
48 func TestReadAllOrWarnSuccess(t *testing.T) {
49 rep := Reporter{Logger: log.New(os.Stderr, "", 0)}
51 f, err := os.Open("./crunchstat_test.go")
53 t.Fatalf("Opening ./crunchstat_test.go: %s", err)
55 data, err := rep.readAllOrWarn(f)
57 t.Fatalf("got error %s", err)
59 if matched, err := regexp.MatchString("^package crunchstat\n", string(data)); err != nil || !matched {
60 t.Fatalf("data failed regexp: err %v, matched %v", err, matched)