github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/addrs/check_rule_diagnostic_test.go (about)

     1  package addrs
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/hcl/v2"
     7  
     8  	"github.com/terramate-io/tf/tfdiags"
     9  )
    10  
    11  func TestCheckRuleDiagnosticExtra_WrapsExtra(t *testing.T) {
    12  	var originals tfdiags.Diagnostics
    13  	originals = originals.Append(&hcl.Diagnostic{
    14  		Severity: hcl.DiagError,
    15  		Summary:  "original error",
    16  		Detail:   "this is an error",
    17  		Extra:    "extra",
    18  	})
    19  
    20  	overridden := tfdiags.OverrideAll(originals, tfdiags.Warning, func() tfdiags.DiagnosticExtraWrapper {
    21  		return &CheckRuleDiagnosticExtra{}
    22  	})
    23  
    24  	if overridden[0].ExtraInfo().(*CheckRuleDiagnosticExtra).wrapped.(string) != "extra" {
    25  		t.Errorf("unexpected extra info: %v", overridden[0].ExtraInfo())
    26  	}
    27  }
    28  
    29  func TestCheckRuleDiagnosticExtra_Unwraps(t *testing.T) {
    30  	var originals tfdiags.Diagnostics
    31  	originals = originals.Append(&hcl.Diagnostic{
    32  		Severity: hcl.DiagError,
    33  		Summary:  "original error",
    34  		Detail:   "this is an error",
    35  		Extra:    "extra",
    36  	})
    37  
    38  	overridden := tfdiags.OverrideAll(originals, tfdiags.Warning, func() tfdiags.DiagnosticExtraWrapper {
    39  		return &CheckRuleDiagnosticExtra{}
    40  	})
    41  
    42  	result := tfdiags.ExtraInfo[string](overridden[0])
    43  	if result != "extra" {
    44  		t.Errorf("unexpected extra info: %v", result)
    45  	}
    46  }
    47  
    48  func TestCheckRuleDiagnosticExtra_DoNotConsolidate(t *testing.T) {
    49  	var diags tfdiags.Diagnostics
    50  	diags = diags.Append(&hcl.Diagnostic{
    51  		Severity: hcl.DiagError,
    52  		Summary:  "original error",
    53  		Detail:   "this is an error",
    54  		Extra: &CheckRuleDiagnosticExtra{
    55  			CheckRule: NewCheckRule(AbsOutputValue{
    56  				Module: RootModuleInstance,
    57  				OutputValue: OutputValue{
    58  					Name: "output",
    59  				},
    60  			}, OutputPrecondition, 0),
    61  		},
    62  	})
    63  	diags = diags.Append(&hcl.Diagnostic{
    64  		Severity: hcl.DiagError,
    65  		Summary:  "original error",
    66  		Detail:   "this is an error",
    67  		Extra: &CheckRuleDiagnosticExtra{
    68  			CheckRule: NewCheckRule(AbsCheck{
    69  				Module: RootModuleInstance,
    70  				Check: Check{
    71  					Name: "check",
    72  				},
    73  			}, CheckAssertion, 0),
    74  		},
    75  	})
    76  
    77  	if tfdiags.DoNotConsolidateDiagnostic(diags[0]) {
    78  		t.Errorf("first diag should be consolidated but was not")
    79  	}
    80  
    81  	if !tfdiags.DoNotConsolidateDiagnostic(diags[1]) {
    82  		t.Errorf("second diag should not be consolidated but was")
    83  	}
    84  
    85  }
    86  
    87  func TestDiagnosticOriginatesFromCheckRule_Passes(t *testing.T) {
    88  	var diags tfdiags.Diagnostics
    89  	diags = diags.Append(&hcl.Diagnostic{
    90  		Severity: hcl.DiagError,
    91  		Summary:  "original error",
    92  		Detail:   "this is an error",
    93  	})
    94  	diags = diags.Append(&hcl.Diagnostic{
    95  		Severity: hcl.DiagError,
    96  		Summary:  "original error",
    97  		Detail:   "this is an error",
    98  		Extra:    &CheckRuleDiagnosticExtra{},
    99  	})
   100  
   101  	if _, ok := DiagnosticOriginatesFromCheckRule(diags[0]); ok {
   102  		t.Errorf("first diag did not originate from check rule but thinks it did")
   103  	}
   104  
   105  	if _, ok := DiagnosticOriginatesFromCheckRule(diags[1]); !ok {
   106  		t.Errorf("second diag did originate from check rule but this it did not")
   107  	}
   108  }