github.com/eliastor/durgaform@v0.0.0-20220816172711-d0ab2d17673e/internal/configs/parser_config_dir_test.go (about) 1 package configs 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "path/filepath" 7 "testing" 8 9 "github.com/hashicorp/hcl/v2" 10 ) 11 12 // TestParseLoadConfigDirSuccess is a simple test that just verifies that 13 // a number of test configuration directories (in testdata/valid-modules) 14 // can be parsed without raising any diagnostics. 15 // 16 // It also re-tests the individual files in testdata/valid-files as if 17 // they were single-file modules, to ensure that they can be bundled into 18 // modules correctly. 19 // 20 // This test does not verify that reading these modules produces the correct 21 // module element contents. More detailed assertions may be made on some subset 22 // of these configuration files in other tests. 23 func TestParserLoadConfigDirSuccess(t *testing.T) { 24 dirs, err := ioutil.ReadDir("testdata/valid-modules") 25 if err != nil { 26 t.Fatal(err) 27 } 28 29 for _, info := range dirs { 30 name := info.Name() 31 t.Run(name, func(t *testing.T) { 32 parser := NewParser(nil) 33 path := filepath.Join("testdata/valid-modules", name) 34 35 mod, diags := parser.LoadConfigDir(path) 36 if len(diags) != 0 && len(mod.ActiveExperiments) != 0 { 37 // As a special case to reduce churn while we're working 38 // through experimental features, we'll ignore the warning 39 // that an experimental feature is active if the module 40 // intentionally opted in to that feature. 41 // If you want to explicitly test for the feature warning 42 // to be generated, consider using testdata/warning-files 43 // instead. 44 filterDiags := make(hcl.Diagnostics, 0, len(diags)) 45 for _, diag := range diags { 46 if diag.Severity != hcl.DiagWarning { 47 continue 48 } 49 match := false 50 for exp := range mod.ActiveExperiments { 51 allowedSummary := fmt.Sprintf("Experimental feature %q is active", exp.Keyword()) 52 if diag.Summary == allowedSummary { 53 match = true 54 break 55 } 56 } 57 if !match { 58 filterDiags = append(filterDiags, diag) 59 } 60 } 61 diags = filterDiags 62 } 63 if len(diags) != 0 { 64 t.Errorf("unexpected diagnostics") 65 for _, diag := range diags { 66 t.Logf("- %s", diag) 67 } 68 } 69 70 if mod.SourceDir != path { 71 t.Errorf("wrong SourceDir value %q; want %s", mod.SourceDir, path) 72 } 73 }) 74 } 75 76 // The individual files in testdata/valid-files should also work 77 // when loaded as modules. 78 files, err := ioutil.ReadDir("testdata/valid-files") 79 if err != nil { 80 t.Fatal(err) 81 } 82 83 for _, info := range files { 84 name := info.Name() 85 t.Run(fmt.Sprintf("%s as module", name), func(t *testing.T) { 86 src, err := ioutil.ReadFile(filepath.Join("testdata/valid-files", name)) 87 if err != nil { 88 t.Fatal(err) 89 } 90 91 parser := testParser(map[string]string{ 92 "mod/" + name: string(src), 93 }) 94 95 _, diags := parser.LoadConfigDir("mod") 96 if diags.HasErrors() { 97 t.Errorf("unexpected error diagnostics") 98 for _, diag := range diags { 99 t.Logf("- %s", diag) 100 } 101 } 102 }) 103 } 104 105 } 106 107 // TestParseLoadConfigDirFailure is a simple test that just verifies that 108 // a number of test configuration directories (in testdata/invalid-modules) 109 // produce diagnostics when parsed. 110 // 111 // It also re-tests the individual files in testdata/invalid-files as if 112 // they were single-file modules, to ensure that their errors are still 113 // detected when loading as part of a module. 114 // 115 // This test does not verify that reading these modules produces any 116 // diagnostics in particular. More detailed assertions may be made on some subset 117 // of these configuration files in other tests. 118 func TestParserLoadConfigDirFailure(t *testing.T) { 119 dirs, err := ioutil.ReadDir("testdata/invalid-modules") 120 if err != nil { 121 t.Fatal(err) 122 } 123 124 for _, info := range dirs { 125 name := info.Name() 126 t.Run(name, func(t *testing.T) { 127 parser := NewParser(nil) 128 path := filepath.Join("testdata/invalid-modules", name) 129 130 _, diags := parser.LoadConfigDir(path) 131 if !diags.HasErrors() { 132 t.Errorf("no errors; want at least one") 133 for _, diag := range diags { 134 t.Logf("- %s", diag) 135 } 136 } 137 }) 138 } 139 140 // The individual files in testdata/valid-files should also work 141 // when loaded as modules. 142 files, err := ioutil.ReadDir("testdata/invalid-files") 143 if err != nil { 144 t.Fatal(err) 145 } 146 147 for _, info := range files { 148 name := info.Name() 149 t.Run(fmt.Sprintf("%s as module", name), func(t *testing.T) { 150 src, err := ioutil.ReadFile(filepath.Join("testdata/invalid-files", name)) 151 if err != nil { 152 t.Fatal(err) 153 } 154 155 parser := testParser(map[string]string{ 156 "mod/" + name: string(src), 157 }) 158 159 _, diags := parser.LoadConfigDir("mod") 160 if !diags.HasErrors() { 161 t.Errorf("no errors; want at least one") 162 for _, diag := range diags { 163 t.Logf("- %s", diag) 164 } 165 } 166 }) 167 } 168 169 } 170 171 func TestIsEmptyDir(t *testing.T) { 172 val, err := IsEmptyDir(filepath.Join("testdata", "valid-files")) 173 if err != nil { 174 t.Fatalf("err: %s", err) 175 } 176 if val { 177 t.Fatal("should not be empty") 178 } 179 } 180 181 func TestIsEmptyDir_noExist(t *testing.T) { 182 val, err := IsEmptyDir(filepath.Join("testdata", "nopenopenope")) 183 if err != nil { 184 t.Fatalf("err: %s", err) 185 } 186 if !val { 187 t.Fatal("should be empty") 188 } 189 } 190 191 func TestIsEmptyDir_noConfigs(t *testing.T) { 192 val, err := IsEmptyDir(filepath.Join("testdata", "dir-empty")) 193 if err != nil { 194 t.Fatalf("err: %s", err) 195 } 196 if !val { 197 t.Fatal("should be empty") 198 } 199 }