github.com/Hashicorp/terraform@v0.11.12-beta1/config/import_tree_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestImportTreeHCL2Experiment(t *testing.T) {
     8  	// Can only run this test if we're built with the experiment enabled.
     9  	// Enable this test by passing the following option to "go test":
    10  	//    -ldflags="-X github.com/hashicorp/terraform/config.enableHCL2Experiment=true"
    11  	// See the comment associated with this flag variable for more information.
    12  	if enableHCL2Experiment == "" {
    13  		t.Skip("HCL2 experiment is not enabled")
    14  	}
    15  
    16  	t.Run("HCL not opted in", func(t *testing.T) {
    17  		// .tf file without opt-in should use the old HCL parser
    18  		imp, err := loadTree("test-fixtures/hcl2-experiment-switch/not-opted-in.tf")
    19  		if err != nil {
    20  			t.Fatal(err)
    21  		}
    22  
    23  		tree, err := imp.ConfigTree()
    24  		if err != nil {
    25  			t.Fatalf("unexpected error loading not-opted-in.tf: %s", err)
    26  		}
    27  
    28  		cfg := tree.Config
    29  		if got, want := len(cfg.Locals), 1; got != want {
    30  			t.Fatalf("wrong number of locals %#v; want %#v", got, want)
    31  		}
    32  		if cfg.Locals[0].RawConfig.Raw == nil {
    33  			// Having RawConfig.Raw indicates the old loader
    34  			t.Fatal("local has no RawConfig.Raw")
    35  		}
    36  	})
    37  
    38  	t.Run("HCL opted in", func(t *testing.T) {
    39  		// .tf file with opt-in should use the new HCL2 parser
    40  		imp, err := loadTree("test-fixtures/hcl2-experiment-switch/opted-in.tf")
    41  		if err != nil {
    42  			t.Fatal(err)
    43  		}
    44  
    45  		tree, err := imp.ConfigTree()
    46  		if err != nil {
    47  			t.Fatalf("unexpected error loading opted-in.tf: %s", err)
    48  		}
    49  
    50  		cfg := tree.Config
    51  		if got, want := len(cfg.Locals), 1; got != want {
    52  			t.Fatalf("wrong number of locals %#v; want %#v", got, want)
    53  		}
    54  		if cfg.Locals[0].RawConfig.Body == nil {
    55  			// Having RawConfig.Body indicates the new loader
    56  			t.Fatal("local has no RawConfig.Body")
    57  		}
    58  	})
    59  
    60  	t.Run("JSON ineligible", func(t *testing.T) {
    61  		// .tf.json file should always use the old HCL parser
    62  		imp, err := loadTree("test-fixtures/hcl2-experiment-switch/not-eligible.tf.json")
    63  		if err != nil {
    64  			t.Fatal(err)
    65  		}
    66  
    67  		tree, err := imp.ConfigTree()
    68  		if err != nil {
    69  			t.Fatalf("unexpected error loading not-eligible.tf.json: %s", err)
    70  		}
    71  
    72  		cfg := tree.Config
    73  		if got, want := len(cfg.Locals), 1; got != want {
    74  			t.Fatalf("wrong number of locals %#v; want %#v", got, want)
    75  		}
    76  		if cfg.Locals[0].RawConfig.Raw == nil {
    77  			// Having RawConfig.Raw indicates the old loader
    78  			t.Fatal("local has no RawConfig.Raw")
    79  		}
    80  	})
    81  }