github.com/skippbox/kompose-origin@v0.0.0-20160524133224-16a9dca7bac2/lookup/file_test.go (about) 1 package lookup 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "path/filepath" 7 "testing" 8 ) 9 10 type input struct { 11 file string 12 relativeTo string 13 } 14 15 func TestLookupError(t *testing.T) { 16 abs, err := filepath.Abs(".") 17 if err != nil { 18 t.Fatalf("Failed to get absolute directory: %s", err) 19 } 20 invalids := map[input]string{ 21 input{"", ""}: fmt.Sprintf("read %s: is a directory", abs), 22 input{"", "/tmp/"}: "read /tmp: is a directory", 23 input{"file", "/does/not/exists/"}: "open /does/not/exists/file: no such file or directory", 24 input{"file", "/does/not/something"}: "open /does/not/file: no such file or directory", 25 input{"file", "/does/not/exists/another"}: "open /does/not/exists/file: no such file or directory", 26 input{"/does/not/exists/file", "/tmp/"}: "open /does/not/exists/file: no such file or directory", 27 input{"does/not/exists/file", "/tmp/"}: "open /tmp/does/not/exists/file: no such file or directory", 28 } 29 30 fileConfigLookup := FileConfigLookup{} 31 32 for invalid, expectedError := range invalids { 33 _, _, err := fileConfigLookup.Lookup(invalid.file, invalid.relativeTo) 34 if err == nil || err.Error() != expectedError { 35 t.Fatalf("Expected error with '%s', got '%v'", expectedError, err) 36 } 37 } 38 } 39 40 func TestLookupOK(t *testing.T) { 41 tmpFolder, err := ioutil.TempDir("", "lookup-tests") 42 if err != nil { 43 t.Fatal(err) 44 } 45 tmpFile1 := filepath.Join(tmpFolder, "file1") 46 tmpFile2 := filepath.Join(tmpFolder, "file2") 47 if err = ioutil.WriteFile(tmpFile1, []byte("content1"), 0755); err != nil { 48 t.Fatal(err) 49 } 50 if err = ioutil.WriteFile(tmpFile2, []byte("content2"), 0755); err != nil { 51 t.Fatal(err) 52 } 53 54 fileConfigLookup := FileConfigLookup{} 55 56 valids := map[input]string{ 57 input{"file1", tmpFolder + "/"}: "content1", 58 input{"file2", tmpFolder + "/"}: "content2", 59 input{tmpFile1, tmpFolder}: "content1", 60 input{tmpFile1, "/does/not/exists"}: "content1", 61 input{"file2", tmpFile1}: "content2", 62 } 63 64 for valid, expectedContent := range valids { 65 out, _, err := fileConfigLookup.Lookup(valid.file, valid.relativeTo) 66 if err != nil || string(out) != expectedContent { 67 t.Fatalf("Expected %s to contains '%s', got %s, %v.", valid.file, expectedContent, out, err) 68 } 69 } 70 }