github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/configs/experiments_test.go (about)

     1  package configs
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/hashicorp/hcl/v2"
     8  
     9  	"github.com/muratcelep/terraform/not-internal/experiments"
    10  )
    11  
    12  func TestExperimentsConfig(t *testing.T) {
    13  	// The experiment registrations are global, so we need to do some special
    14  	// patching in order to get a predictable set for our tests.
    15  	current := experiments.Experiment("current")
    16  	concluded := experiments.Experiment("concluded")
    17  	currentExperiments := experiments.NewSet(current)
    18  	concludedExperiments := map[experiments.Experiment]string{
    19  		concluded: "Reticulate your splines.",
    20  	}
    21  	defer experiments.OverrideForTesting(t, currentExperiments, concludedExperiments)()
    22  
    23  	t.Run("current", func(t *testing.T) {
    24  		parser := NewParser(nil)
    25  		mod, diags := parser.LoadConfigDir("testdata/experiments/current")
    26  		if got, want := len(diags), 1; got != want {
    27  			t.Fatalf("wrong number of diagnostics %d; want %d", got, want)
    28  		}
    29  		got := diags[0]
    30  		want := &hcl.Diagnostic{
    31  			Severity: hcl.DiagWarning,
    32  			Summary:  `Experimental feature "current" is active`,
    33  			Detail:   "Experimental features are subject to breaking changes in future minor or patch releases, based on feedback.\n\nIf you have feedback on the design of this feature, please open a GitHub issue to discuss it.",
    34  			Subject: &hcl.Range{
    35  				Filename: "testdata/experiments/current/current_experiment.tf",
    36  				Start:    hcl.Pos{Line: 2, Column: 18, Byte: 29},
    37  				End:      hcl.Pos{Line: 2, Column: 25, Byte: 36},
    38  			},
    39  		}
    40  		if diff := cmp.Diff(want, got); diff != "" {
    41  			t.Errorf("wrong warning\n%s", diff)
    42  		}
    43  		if got, want := len(mod.ActiveExperiments), 1; got != want {
    44  			t.Errorf("wrong number of experiments %d; want %d", got, want)
    45  		}
    46  		if !mod.ActiveExperiments.Has(current) {
    47  			t.Errorf("module does not indicate current experiment as active")
    48  		}
    49  	})
    50  	t.Run("concluded", func(t *testing.T) {
    51  		parser := NewParser(nil)
    52  		_, diags := parser.LoadConfigDir("testdata/experiments/concluded")
    53  		if got, want := len(diags), 1; got != want {
    54  			t.Fatalf("wrong number of diagnostics %d; want %d", got, want)
    55  		}
    56  		got := diags[0]
    57  		want := &hcl.Diagnostic{
    58  			Severity: hcl.DiagError,
    59  			Summary:  `Experiment has concluded`,
    60  			Detail:   `Experiment "concluded" is no longer available. Reticulate your splines.`,
    61  			Subject: &hcl.Range{
    62  				Filename: "testdata/experiments/concluded/concluded_experiment.tf",
    63  				Start:    hcl.Pos{Line: 2, Column: 18, Byte: 29},
    64  				End:      hcl.Pos{Line: 2, Column: 27, Byte: 38},
    65  			},
    66  		}
    67  		if diff := cmp.Diff(want, got); diff != "" {
    68  			t.Errorf("wrong error\n%s", diff)
    69  		}
    70  	})
    71  	t.Run("concluded", func(t *testing.T) {
    72  		parser := NewParser(nil)
    73  		_, diags := parser.LoadConfigDir("testdata/experiments/unknown")
    74  		if got, want := len(diags), 1; got != want {
    75  			t.Fatalf("wrong number of diagnostics %d; want %d", got, want)
    76  		}
    77  		got := diags[0]
    78  		want := &hcl.Diagnostic{
    79  			Severity: hcl.DiagError,
    80  			Summary:  `Unknown experiment keyword`,
    81  			Detail:   `There is no current experiment with the keyword "unknown".`,
    82  			Subject: &hcl.Range{
    83  				Filename: "testdata/experiments/unknown/unknown_experiment.tf",
    84  				Start:    hcl.Pos{Line: 2, Column: 18, Byte: 29},
    85  				End:      hcl.Pos{Line: 2, Column: 25, Byte: 36},
    86  			},
    87  		}
    88  		if diff := cmp.Diff(want, got); diff != "" {
    89  			t.Errorf("wrong error\n%s", diff)
    90  		}
    91  	})
    92  	t.Run("invalid", func(t *testing.T) {
    93  		parser := NewParser(nil)
    94  		_, diags := parser.LoadConfigDir("testdata/experiments/invalid")
    95  		if got, want := len(diags), 1; got != want {
    96  			t.Fatalf("wrong number of diagnostics %d; want %d", got, want)
    97  		}
    98  		got := diags[0]
    99  		want := &hcl.Diagnostic{
   100  			Severity: hcl.DiagError,
   101  			Summary:  `Invalid expression`,
   102  			Detail:   `A static list expression is required.`,
   103  			Subject: &hcl.Range{
   104  				Filename: "testdata/experiments/invalid/invalid_experiments.tf",
   105  				Start:    hcl.Pos{Line: 2, Column: 17, Byte: 28},
   106  				End:      hcl.Pos{Line: 2, Column: 24, Byte: 35},
   107  			},
   108  		}
   109  		if diff := cmp.Diff(want, got); diff != "" {
   110  			t.Errorf("wrong error\n%s", diff)
   111  		}
   112  	})
   113  }