github.com/magnusbaeck/logstash-filter-verifier/v2@v2.0.0-pre.1/testcase/discover_test.go (about) 1 // Copyright (c) 2015 Magnus Bäck <magnus@noun.se> 2 3 package testcase 4 5 import ( 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "sort" 10 "strings" 11 "testing" 12 ) 13 14 // TestDiscoverTests_File tests passing the path to a directory with 15 // files to DiscoverTests. 16 func TestDiscoverTests_Directory(t *testing.T) { 17 cases := []struct { 18 files []string 19 dirs []string 20 expected []string 21 }{ 22 { 23 files: []string{}, 24 dirs: []string{}, 25 expected: []string{}, 26 }, 27 { 28 files: []string{}, 29 dirs: []string{"dir1", "dir2"}, 30 expected: []string{}, 31 }, 32 { 33 files: []string{"otherfile.txt", ".dotfile"}, 34 dirs: []string{}, 35 expected: []string{}, 36 }, 37 { 38 files: []string{"test1.json", "test2.json", "test1.yaml", "test2.yaml", "test1.yml", "test2.yml"}, 39 dirs: []string{}, 40 expected: []string{"test1.json", "test2.json", "test1.yaml", "test2.yaml", "test1.yml", "test2.yml"}, 41 }, 42 { 43 files: []string{"otherfile.txt", "test1.json", "test2.json", "test1.yaml", "test2.yaml", "test1.yml", "test2.yml"}, 44 dirs: []string{}, 45 expected: []string{"test1.json", "test2.json", "test1.yaml", "test2.yaml", "test1.yml", "test2.yml"}, 46 }, 47 } 48 for cnum, c := range cases { 49 tempdir, err := ioutil.TempDir("", "") 50 if err != nil { 51 t.Errorf(err.Error()) 52 break 53 } 54 defer os.RemoveAll(tempdir) 55 56 for _, f := range c.files { 57 if strings.Contains(f, "/") { 58 t.Errorf("This test doesn't support subdirectories: %s", f) 59 break 60 } 61 if err = ioutil.WriteFile(filepath.Join(tempdir, f), []byte(`{"type": "test"}`), 0600); err != nil { 62 t.Fatalf(err.Error()) 63 } 64 } 65 66 for _, d := range c.dirs { 67 if strings.Contains(d, "/") { 68 t.Errorf("This test doesn't support subdirectories: %s", d) 69 break 70 } 71 if err := os.Mkdir(filepath.Join(tempdir, d), 0755); err != nil { 72 t.Error(err.Error()) 73 break 74 } 75 } 76 77 testcases, err := DiscoverTests(tempdir) 78 if err != nil { 79 t.Errorf("Test %d: DiscoverTests() unexpectedly returned an error: %s", cnum, err) 80 break 81 } 82 83 filenames := make([]string, len(testcases)) 84 for i, tcs := range testcases { 85 filenames[i] = filepath.Base(tcs.File) 86 } 87 sort.Strings(filenames) 88 89 sexpected := make([]string, len(c.expected)) 90 copy(sexpected, c.expected) 91 sort.Strings(sexpected) 92 93 if len(filenames) != len(sexpected) { 94 t.Errorf("Test %d:\nExpected:\n%v\nGot:\n%v", cnum, sexpected, filenames) 95 break 96 } 97 for i := range sexpected { 98 if sexpected[i] != filenames[i] { 99 t.Errorf("Test %d: Expected item %d to be %q, got %q instead.", cnum, i, sexpected[i], filenames[i]) 100 } 101 } 102 } 103 } 104 105 // TestDiscoverTests_File tests passing the path to a single file to 106 // DiscoverTests. 107 func TestDiscoverTests_File(t *testing.T) { 108 filenames := []string{ 109 "filename.json", 110 "filename.yml", 111 "filename.yaml", 112 } 113 for _, filename := range filenames { 114 tempdir, err := ioutil.TempDir("", "") 115 if err != nil { 116 t.Fatal(err.Error()) 117 } 118 defer os.RemoveAll(tempdir) 119 120 inputpath := filepath.Join(tempdir, filename) 121 122 // As it happens a valid JSON file is also a valid YAML file so 123 // the file we create can have the same contents regardless of 124 // the file format. 125 if err = ioutil.WriteFile(inputpath, []byte(`{"type": "test"}`), 0600); err != nil { 126 t.Fatal(err.Error()) 127 } 128 129 testcases, err := DiscoverTests(inputpath) 130 if err != nil { 131 t.Fatalf("DiscoverTests() unexpectedly returned an error: %s", err) 132 } 133 134 if len(testcases) != 1 { 135 t.Fatalf("DiscoverTests() unexpectedly returned %d test cases: %+v", len(testcases), testcases) 136 } 137 138 if testcases[0].File != inputpath { 139 t.Fatalf("DiscoverTests() unexpectedly returned %d test cases: %+v", len(testcases), testcases) 140 } 141 } 142 }