github.com/jlmeeker/kismatic@v1.10.1-0.20180612190640-57f9005a1f1a/pkg/inspector/check/file_content_test.go (about) 1 package check 2 3 import ( 4 "io/ioutil" 5 "testing" 6 ) 7 8 func TestFileContentCheckFileDoesntExist(t *testing.T) { 9 c := FileContentCheck{ 10 File: "doesntExist", 11 SearchString: "foo", 12 } 13 ok, err := c.Check() 14 if err == nil { 15 t.Errorf("expected an error but didn't get one") 16 } 17 if ok { 18 t.Errorf("check returned true for a non-existent file") 19 } 20 } 21 22 func TestFileContentCheck(t *testing.T) { 23 f, err := ioutil.TempFile("", "file-regex-check") 24 if err != nil { 25 t.Fatalf("error creating temp file: %v", err) 26 } 27 fileConts := "hello world\n" 28 f.WriteString(fileConts) 29 c := FileContentCheck{ 30 File: f.Name(), 31 SearchString: "^hello w.*", 32 } 33 ok, err := c.Check() 34 if err != nil { 35 t.Errorf("Unexpected error when running check: %v", err) 36 } 37 if !ok { 38 t.Errorf("Expected check OK, but check failed. Search string was: %q\nFile contents: \n%s\n", c.SearchString, fileConts) 39 } 40 }