mirror of https://github.com/hashicorp/terraform
Use the new HCL2 config loader when the opt-in comment #terraform:hcl2 is present in a .tf file. For now this is disabled for "normal" builds and enabled only if explicitly configured via a linker flag during build. This is because it's not yet in a good state to be released: the HCL2 loader produces RawConfig objects that the validator and interpolator can't yet deal with, and so using HCL2 for anything non-trivial currently causes Terraform to crash in real use.pull/16259/head
parent
b0215fcd0f
commit
d91327eaa0
@ -0,0 +1,81 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestImportTreeHCL2Experiment(t *testing.T) {
|
||||
// Can only run this test if we're built with the experiment enabled.
|
||||
// Enable this test by passing the following option to "go test":
|
||||
// -ldflags="-X github.com/hashicorp/terraform/config.enableHCL2Experiment=true"
|
||||
// See the comment associated with this flag variable for more information.
|
||||
if enableHCL2Experiment == "" {
|
||||
t.Skip("HCL2 experiment is not enabled")
|
||||
}
|
||||
|
||||
t.Run("HCL not opted in", func(t *testing.T) {
|
||||
// .tf file without opt-in should use the old HCL parser
|
||||
imp, err := loadTree("test-fixtures/hcl2-experiment-switch/not-opted-in.tf")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tree, err := imp.ConfigTree()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error loading not-opted-in.tf: %s", err)
|
||||
}
|
||||
|
||||
cfg := tree.Config
|
||||
if got, want := len(cfg.Locals), 1; got != want {
|
||||
t.Fatalf("wrong number of locals %#v; want %#v", got, want)
|
||||
}
|
||||
if cfg.Locals[0].RawConfig.Raw == nil {
|
||||
// Having RawConfig.Raw indicates the old loader
|
||||
t.Fatal("local has no RawConfig.Raw")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("HCL opted in", func(t *testing.T) {
|
||||
// .tf file with opt-in should use the new HCL2 parser
|
||||
imp, err := loadTree("test-fixtures/hcl2-experiment-switch/opted-in.tf")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tree, err := imp.ConfigTree()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error loading opted-in.tf: %s", err)
|
||||
}
|
||||
|
||||
cfg := tree.Config
|
||||
if got, want := len(cfg.Locals), 1; got != want {
|
||||
t.Fatalf("wrong number of locals %#v; want %#v", got, want)
|
||||
}
|
||||
if cfg.Locals[0].RawConfig.Body == nil {
|
||||
// Having RawConfig.Body indicates the new loader
|
||||
t.Fatal("local has no RawConfig.Body")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("JSON ineligible", func(t *testing.T) {
|
||||
// .tf.json file should always use the old HCL parser
|
||||
imp, err := loadTree("test-fixtures/hcl2-experiment-switch/not-eligible.tf.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tree, err := imp.ConfigTree()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error loading not-eligible.tf.json: %s", err)
|
||||
}
|
||||
|
||||
cfg := tree.Config
|
||||
if got, want := len(cfg.Locals), 1; got != want {
|
||||
t.Fatalf("wrong number of locals %#v; want %#v", got, want)
|
||||
}
|
||||
if cfg.Locals[0].RawConfig.Raw == nil {
|
||||
// Having RawConfig.Raw indicates the old loader
|
||||
t.Fatal("local has no RawConfig.Raw")
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
{
|
||||
"locals": {
|
||||
"foo": "baz"
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
|
||||
# The use of an equals to assign "locals" is something that would be rejected
|
||||
# by the HCL2 parser (equals is reserved for attributes only) and so we can
|
||||
# use it to verify that the old HCL parser was used.
|
||||
locals {
|
||||
foo = "bar"
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
#terraform:hcl2
|
||||
|
||||
locals {
|
||||
# This direct expression is something that would be rejected by the old HCL
|
||||
# parser, so we can use it as a marker that the HCL2 parser was used.
|
||||
foo = 1 + 2
|
||||
}
|
||||
Loading…
Reference in new issue