github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/configs/parser_config_test.go (about) 1 package configs 2 3 import ( 4 "io/ioutil" 5 "path/filepath" 6 "testing" 7 8 "github.com/hashicorp/hcl/v2" 9 ) 10 11 // TestParseLoadConfigFileSuccess is a simple test that just verifies that 12 // a number of test configuration files (in testdata/valid-files) can 13 // be parsed without raising any diagnostics. 14 // 15 // This test does not verify that reading these files produces the correct 16 // file element contents. More detailed assertions may be made on some subset 17 // of these configuration files in other tests. 18 func TestParserLoadConfigFileSuccess(t *testing.T) { 19 files, err := ioutil.ReadDir("testdata/valid-files") 20 if err != nil { 21 t.Fatal(err) 22 } 23 24 for _, info := range files { 25 name := info.Name() 26 t.Run(name, func(t *testing.T) { 27 src, err := ioutil.ReadFile(filepath.Join("testdata/valid-files", name)) 28 if err != nil { 29 t.Fatal(err) 30 } 31 32 parser := testParser(map[string]string{ 33 name: string(src), 34 }) 35 36 _, diags := parser.LoadConfigFile(name) 37 if diags.HasErrors() { 38 t.Errorf("unexpected error diagnostics") 39 for _, diag := range diags { 40 t.Logf("- %s", diag) 41 } 42 } 43 }) 44 } 45 } 46 47 // TestParseLoadConfigFileFailure is a simple test that just verifies that 48 // a number of test configuration files (in testdata/invalid-files) 49 // produce errors as expected. 50 // 51 // This test does not verify specific error messages, so more detailed 52 // assertions should be made on some subset of these configuration files in 53 // other tests. 54 func TestParserLoadConfigFileFailure(t *testing.T) { 55 files, err := ioutil.ReadDir("testdata/invalid-files") 56 if err != nil { 57 t.Fatal(err) 58 } 59 60 for _, info := range files { 61 name := info.Name() 62 t.Run(name, func(t *testing.T) { 63 src, err := ioutil.ReadFile(filepath.Join("testdata/invalid-files", name)) 64 if err != nil { 65 t.Fatal(err) 66 } 67 68 parser := testParser(map[string]string{ 69 name: string(src), 70 }) 71 72 _, diags := parser.LoadConfigFile(name) 73 if !diags.HasErrors() { 74 t.Errorf("LoadConfigFile succeeded; want errors") 75 } 76 for _, diag := range diags { 77 t.Logf("- %s", diag) 78 } 79 }) 80 } 81 } 82 83 // This test uses a subset of the same fixture files as 84 // TestParserLoadConfigFileFailure, but additionally verifies that each 85 // file produces the expected diagnostic summary. 86 func TestParserLoadConfigFileFailureMessages(t *testing.T) { 87 tests := []struct { 88 Filename string 89 WantSeverity hcl.DiagnosticSeverity 90 WantDiag string 91 }{ 92 { 93 "invalid-files/data-resource-lifecycle.tf", 94 hcl.DiagError, 95 "Unsupported lifecycle block", 96 }, 97 { 98 "invalid-files/variable-type-unknown.tf", 99 hcl.DiagError, 100 "Invalid type specification", 101 }, 102 { 103 "invalid-files/unexpected-attr.tf", 104 hcl.DiagError, 105 "Unsupported argument", 106 }, 107 { 108 "invalid-files/unexpected-block.tf", 109 hcl.DiagError, 110 "Unsupported block type", 111 }, 112 { 113 "invalid-files/resource-count-and-for_each.tf", 114 hcl.DiagError, 115 `Invalid combination of "count" and "for_each"`, 116 }, 117 { 118 "invalid-files/data-count-and-for_each.tf", 119 hcl.DiagError, 120 `Invalid combination of "count" and "for_each"`, 121 }, 122 { 123 "invalid-files/resource-lifecycle-badbool.tf", 124 hcl.DiagError, 125 "Unsuitable value type", 126 }, 127 { 128 "valid-files/resources-ignorechanges-all-legacy.tf", 129 hcl.DiagWarning, 130 "Deprecated ignore_changes wildcard", 131 }, 132 { 133 "valid-files/resources-ignorechanges-all-legacy.tf.json", 134 hcl.DiagWarning, 135 "Deprecated ignore_changes wildcard", 136 }, 137 } 138 139 for _, test := range tests { 140 t.Run(test.Filename, func(t *testing.T) { 141 src, err := ioutil.ReadFile(filepath.Join("testdata", test.Filename)) 142 if err != nil { 143 t.Fatal(err) 144 } 145 146 parser := testParser(map[string]string{ 147 test.Filename: string(src), 148 }) 149 150 _, diags := parser.LoadConfigFile(test.Filename) 151 if len(diags) != 1 { 152 t.Errorf("Wrong number of diagnostics %d; want 1", len(diags)) 153 for _, diag := range diags { 154 t.Logf("- %s", diag) 155 } 156 return 157 } 158 if diags[0].Severity != test.WantSeverity { 159 t.Errorf("Wrong diagnostic severity %#v; want %#v", diags[0].Severity, test.WantSeverity) 160 } 161 if diags[0].Summary != test.WantDiag { 162 t.Errorf("Wrong diagnostic summary\ngot: %s\nwant: %s", diags[0].Summary, test.WantDiag) 163 } 164 }) 165 } 166 }