github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/tfdiags/consolidate_warnings_test.go (about) 1 package tfdiags 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 "github.com/hashicorp/hcl/v2" 9 ) 10 11 func TestConsolidateWarnings(t *testing.T) { 12 var diags Diagnostics 13 14 for i := 0; i < 4; i++ { 15 diags = diags.Append(&hcl.Diagnostic{ 16 Severity: hcl.DiagWarning, 17 Summary: "Warning 1", 18 Detail: fmt.Sprintf("This one has a subject %d", i), 19 Subject: &hcl.Range{ 20 Filename: "foo.tf", 21 Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, 22 End: hcl.Pos{Line: 1, Column: 1, Byte: 0}, 23 }, 24 }) 25 diags = diags.Append(&hcl.Diagnostic{ 26 Severity: hcl.DiagError, 27 Summary: "Error 1", 28 Detail: fmt.Sprintf("This one has a subject %d", i), 29 Subject: &hcl.Range{ 30 Filename: "foo.tf", 31 Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, 32 End: hcl.Pos{Line: 1, Column: 1, Byte: 0}, 33 }, 34 }) 35 diags = diags.Append(Sourceless( 36 Warning, 37 "Warning 2", 38 fmt.Sprintf("This one is sourceless %d", i), 39 )) 40 diags = diags.Append(SimpleWarning("Warning 3")) 41 } 42 43 diags = diags.Append(&hcl.Diagnostic{ 44 Severity: hcl.DiagWarning, 45 Summary: "Warning 4", 46 Detail: "Only one of this one", 47 Subject: &hcl.Range{ 48 Filename: "foo.tf", 49 Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, 50 End: hcl.Pos{Line: 1, Column: 1, Byte: 0}, 51 }, 52 }) 53 54 // We're using ForRPC here to force the diagnostics to be of a consistent 55 // type that we can easily assert against below. 56 got := diags.ConsolidateWarnings(2).ForRPC() 57 want := Diagnostics{ 58 // First set 59 &rpcFriendlyDiag{ 60 Severity_: Warning, 61 Summary_: "Warning 1", 62 Detail_: "This one has a subject 0", 63 Subject_: &SourceRange{ 64 Filename: "foo.tf", 65 Start: SourcePos{Line: 1, Column: 1, Byte: 0}, 66 End: SourcePos{Line: 1, Column: 1, Byte: 0}, 67 }, 68 }, 69 &rpcFriendlyDiag{ 70 Severity_: Error, 71 Summary_: "Error 1", 72 Detail_: "This one has a subject 0", 73 Subject_: &SourceRange{ 74 Filename: "foo.tf", 75 Start: SourcePos{Line: 1, Column: 1, Byte: 0}, 76 End: SourcePos{Line: 1, Column: 1, Byte: 0}, 77 }, 78 }, 79 &rpcFriendlyDiag{ 80 Severity_: Warning, 81 Summary_: "Warning 2", 82 Detail_: "This one is sourceless 0", 83 }, 84 &rpcFriendlyDiag{ 85 Severity_: Warning, 86 Summary_: "Warning 3", 87 }, 88 89 // Second set (consolidation begins; note additional paragraph in Warning 1 detail) 90 &rpcFriendlyDiag{ 91 Severity_: Warning, 92 Summary_: "Warning 1", 93 Detail_: "This one has a subject 1\n\n(and 2 more similar warnings elsewhere)", 94 Subject_: &SourceRange{ 95 Filename: "foo.tf", 96 Start: SourcePos{Line: 1, Column: 1, Byte: 0}, 97 End: SourcePos{Line: 1, Column: 1, Byte: 0}, 98 }, 99 }, 100 &rpcFriendlyDiag{ 101 Severity_: Error, 102 Summary_: "Error 1", 103 Detail_: "This one has a subject 1", 104 Subject_: &SourceRange{ 105 Filename: "foo.tf", 106 Start: SourcePos{Line: 1, Column: 1, Byte: 0}, 107 End: SourcePos{Line: 1, Column: 1, Byte: 0}, 108 }, 109 }, 110 &rpcFriendlyDiag{ 111 Severity_: Warning, 112 Summary_: "Warning 2", 113 Detail_: "This one is sourceless 1", 114 }, 115 &rpcFriendlyDiag{ 116 Severity_: Warning, 117 Summary_: "Warning 3", 118 }, 119 120 // Third set (no more Warning 1, because it's consolidated) 121 &rpcFriendlyDiag{ 122 Severity_: Error, 123 Summary_: "Error 1", 124 Detail_: "This one has a subject 2", 125 Subject_: &SourceRange{ 126 Filename: "foo.tf", 127 Start: SourcePos{Line: 1, Column: 1, Byte: 0}, 128 End: SourcePos{Line: 1, Column: 1, Byte: 0}, 129 }, 130 }, 131 &rpcFriendlyDiag{ 132 Severity_: Warning, 133 Summary_: "Warning 2", 134 Detail_: "This one is sourceless 2", 135 }, 136 &rpcFriendlyDiag{ 137 Severity_: Warning, 138 Summary_: "Warning 3", 139 }, 140 141 // Fourth set (still no warning 1) 142 &rpcFriendlyDiag{ 143 Severity_: Error, 144 Summary_: "Error 1", 145 Detail_: "This one has a subject 3", 146 Subject_: &SourceRange{ 147 Filename: "foo.tf", 148 Start: SourcePos{Line: 1, Column: 1, Byte: 0}, 149 End: SourcePos{Line: 1, Column: 1, Byte: 0}, 150 }, 151 }, 152 &rpcFriendlyDiag{ 153 Severity_: Warning, 154 Summary_: "Warning 2", 155 Detail_: "This one is sourceless 3", 156 }, 157 &rpcFriendlyDiag{ 158 Severity_: Warning, 159 Summary_: "Warning 3", 160 }, 161 162 // Special straggler warning gets to show up unconsolidated, because 163 // there is only one of it. 164 &rpcFriendlyDiag{ 165 Severity_: Warning, 166 Summary_: "Warning 4", 167 Detail_: "Only one of this one", 168 Subject_: &SourceRange{ 169 Filename: "foo.tf", 170 Start: SourcePos{Line: 1, Column: 1, Byte: 0}, 171 End: SourcePos{Line: 1, Column: 1, Byte: 0}, 172 }, 173 }, 174 } 175 176 if diff := cmp.Diff(want, got); diff != "" { 177 t.Errorf("wrong result\n%s", diff) 178 } 179 }