9 func TestReadAllOrWarnFail(t *testing.T) {
10 logChan = make(chan string)
13 // The special file /proc/self/mem can be opened for
14 // reading, but reading from byte 0 returns an error.
15 f, err := os.Open("/proc/self/mem")
17 t.Fatalf("Opening /proc/self/mem: %s", err)
19 if x, err := ReadAllOrWarn(f); err == nil {
20 t.Fatalf("Expected error, got %v", x)
23 if _, ok := <-logChan; !ok {
24 t.Fatalf("Expected error message about nonexistent file")
26 if msg, ok := <-logChan; ok {
27 t.Fatalf("Expected channel to close, got %s", msg)
31 func TestReadAllOrWarnSuccess(t *testing.T) {
32 logChan = make(chan string)
35 f, err := os.Open("./crunchstat_test.go")
37 t.Fatalf("Opening ./crunchstat_test.go: %s", err)
39 data, err := ReadAllOrWarn(f)
41 t.Fatalf("got error %s", err)
43 if matched, err := regexp.MatchString("^package main\n", string(data)); err != nil || !matched {
44 t.Fatalf("data failed regexp: %s", err)
47 if msg, ok := <-logChan; ok {
48 t.Fatalf("Expected channel to close, got %s", msg)