github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/tfdiags/hcl_test.go (about) 1 package tfdiags 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 "github.com/google/go-cmp/cmp/cmpopts" 9 "github.com/hashicorp/hcl/v2" 10 "github.com/zclconf/go-cty/cty" 11 ) 12 13 func TestDiagnosticsToHCL(t *testing.T) { 14 var diags Diagnostics 15 diags = diags.Append(Sourceless( 16 Error, 17 "A sourceless diagnostic", 18 "...that has a detail", 19 )) 20 diags = diags.Append(fmt.Errorf("a diagnostic promoted from an error")) 21 diags = diags.Append(SimpleWarning("A diagnostic from a simple warning")) 22 diags = diags.Append(&hcl.Diagnostic{ 23 Severity: hcl.DiagWarning, 24 Summary: "A diagnostic from HCL", 25 Detail: "...that has a detail and source information", 26 Subject: &hcl.Range{ 27 Filename: "test.tf", 28 Start: hcl.Pos{Line: 1, Column: 2, Byte: 1}, 29 End: hcl.Pos{Line: 1, Column: 3, Byte: 2}, 30 }, 31 Context: &hcl.Range{ 32 Filename: "test.tf", 33 Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, 34 End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, 35 }, 36 EvalContext: &hcl.EvalContext{}, 37 Expression: &fakeHCLExpression{}, 38 }) 39 40 got := diags.ToHCL() 41 want := hcl.Diagnostics{ 42 { 43 Severity: hcl.DiagError, 44 Summary: "A sourceless diagnostic", 45 Detail: "...that has a detail", 46 }, 47 { 48 Severity: hcl.DiagError, 49 Summary: "a diagnostic promoted from an error", 50 }, 51 { 52 Severity: hcl.DiagWarning, 53 Summary: "A diagnostic from a simple warning", 54 }, 55 { 56 Severity: hcl.DiagWarning, 57 Summary: "A diagnostic from HCL", 58 Detail: "...that has a detail and source information", 59 Subject: &hcl.Range{ 60 Filename: "test.tf", 61 Start: hcl.Pos{Line: 1, Column: 2, Byte: 1}, 62 End: hcl.Pos{Line: 1, Column: 3, Byte: 2}, 63 }, 64 Context: &hcl.Range{ 65 Filename: "test.tf", 66 Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, 67 End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, 68 }, 69 EvalContext: &hcl.EvalContext{}, 70 Expression: &fakeHCLExpression{}, 71 }, 72 } 73 74 if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(hcl.EvalContext{})); diff != "" { 75 t.Errorf("incorrect result\n%s", diff) 76 } 77 } 78 79 // We have this here just to give us something easy to compare in the test 80 // above, because we only care that the expression passes through, not about 81 // how exactly it is shaped. 82 type fakeHCLExpression struct { 83 } 84 85 func (e *fakeHCLExpression) Range() hcl.Range { 86 return hcl.Range{} 87 } 88 89 func (e *fakeHCLExpression) StartRange() hcl.Range { 90 return hcl.Range{} 91 } 92 93 func (e *fakeHCLExpression) Variables() []hcl.Traversal { 94 return nil 95 } 96 97 func (e *fakeHCLExpression) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { 98 return cty.DynamicVal, nil 99 }