github.com/opentofu/opentofu@v1.7.1/internal/tfdiags/hcl_test.go (about)

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