diff --git a/configs/parser_config_test.go b/configs/parser_config_test.go new file mode 100644 index 0000000000..affc946267 --- /dev/null +++ b/configs/parser_config_test.go @@ -0,0 +1,43 @@ +package configs + +import ( + "io/ioutil" + "path/filepath" + "testing" +) + +// TestParseLoadConfigFileSuccess is a simple test that just verifies that +// a number of test configuration files (in test-fixtures/valid-files) can +// be parsed without raising any diagnostics. +// +// This test does not verify that reading these files produces the correct +// file element contents. More detailed assertions may be made on some subset +// of these configuration files in other tests. +func TestParserLoadConfigFileSuccess(t *testing.T) { + files, err := ioutil.ReadDir("test-fixtures/valid-files") + if err != nil { + t.Fatal(err) + } + + for _, info := range files { + name := info.Name() + t.Run(name, func(t *testing.T) { + src, err := ioutil.ReadFile(filepath.Join("test-fixtures/valid-files", name)) + if err != nil { + t.Fatal(err) + } + + parser := testParser(map[string]string{ + name: string(src), + }) + + _, diags := parser.LoadConfigFile(name) + if len(diags) != 0 { + t.Errorf("unexpected diagnostics") + for _, diag := range diags { + t.Logf("- %s", diag) + } + } + }) + } +} diff --git a/configs/test-fixtures/valid-files/backend.tf b/configs/test-fixtures/valid-files/backend.tf new file mode 100644 index 0000000000..bd8e0f669a --- /dev/null +++ b/configs/test-fixtures/valid-files/backend.tf @@ -0,0 +1,10 @@ + +terraform { + backend "example" { + foo = "bar" + + baz { + bar = "foo" + } + } +} diff --git a/configs/test-fixtures/valid-files/data-sources.tf b/configs/test-fixtures/valid-files/data-sources.tf new file mode 100644 index 0000000000..f14dffdac6 --- /dev/null +++ b/configs/test-fixtures/valid-files/data-sources.tf @@ -0,0 +1,15 @@ +data "http" "example1" { +} + +data "http" "example2" { + url = "http://example.com/" + + request_headers = { + "Accept" = "application/json" + } + + count = 5 + depends_on = [ + data.http.example1, + ] +} diff --git a/configs/test-fixtures/valid-files/empty.tf b/configs/test-fixtures/valid-files/empty.tf new file mode 100644 index 0000000000..e69de29bb2 diff --git a/configs/test-fixtures/valid-files/empty.tf.json b/configs/test-fixtures/valid-files/empty.tf.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/configs/test-fixtures/valid-files/empty.tf.json @@ -0,0 +1 @@ +{} diff --git a/configs/test-fixtures/valid-files/locals.tf b/configs/test-fixtures/valid-files/locals.tf new file mode 100644 index 0000000000..f2ee4a7c4c --- /dev/null +++ b/configs/test-fixtures/valid-files/locals.tf @@ -0,0 +1,16 @@ + +locals { + # This block intentionally left blank +} + +locals { + foo = "foo" + bar = true +} + +locals { + baz = "oink" + dunno = "🤷" + rowing = "🚣‍♀️" + π = 3.14159265359 +} diff --git a/configs/test-fixtures/valid-files/locals.tf.json b/configs/test-fixtures/valid-files/locals.tf.json new file mode 100644 index 0000000000..525f69d6fd --- /dev/null +++ b/configs/test-fixtures/valid-files/locals.tf.json @@ -0,0 +1,10 @@ +{ + "locals": { + "foo": "foo", + "bar": true, + "baz": "oink", + "dunno": "🤷", + "rowing": "🚣‍♀️", + "π": 3.14159265359 + } +} diff --git a/configs/test-fixtures/valid-files/module-calls.tf b/configs/test-fixtures/valid-files/module-calls.tf new file mode 100644 index 0000000000..abd4235868 --- /dev/null +++ b/configs/test-fixtures/valid-files/module-calls.tf @@ -0,0 +1,26 @@ + +module "foo" { + source = "./foo" + # this block intentionally left (almost) blank +} + +module "bar" { + source = "hashicorp/bar/aws" + + boom = "🎆" + yes = true +} + +module "baz" { + source = "git::https://example.com/" + + a = 1 + + count = 12 + for_each = ["a", "b", "c"] + + depends_on = [ + module.bar, + ] +} + diff --git a/configs/test-fixtures/valid-files/outputs.tf b/configs/test-fixtures/valid-files/outputs.tf new file mode 100644 index 0000000000..7a8066686f --- /dev/null +++ b/configs/test-fixtures/valid-files/outputs.tf @@ -0,0 +1,25 @@ + +output "foo" { + value = "hello" +} + +output "bar" { + value = local.bar +} + +output "baz" { + value = "ssshhhhhhh" + sensitive = true +} + +output "cheeze_pizza" { + description = "Nothing special" + value = "🍕" +} + +output "π" { + value = 3.14159265359 + depends_on = [ + pizza.cheese, + ] +} diff --git a/configs/test-fixtures/valid-files/provider-configs.tf b/configs/test-fixtures/valid-files/provider-configs.tf new file mode 100644 index 0000000000..a07f08be88 --- /dev/null +++ b/configs/test-fixtures/valid-files/provider-configs.tf @@ -0,0 +1,15 @@ + +provider "foo" { +} + +provider "bar" { + version = ">= 1.0.2" + + other = 12 +} + +provider "bar" { + other = 13 + + alias = "bar" +} diff --git a/configs/test-fixtures/valid-files/required-providers.tf b/configs/test-fixtures/valid-files/required-providers.tf new file mode 100644 index 0000000000..271df4a57f --- /dev/null +++ b/configs/test-fixtures/valid-files/required-providers.tf @@ -0,0 +1,7 @@ + +terraform { + required_providers { + aws = "~> 1.0.0" + consul = "~> 1.2.0" + } +} diff --git a/configs/test-fixtures/valid-files/required-version.tf b/configs/test-fixtures/valid-files/required-version.tf new file mode 100644 index 0000000000..77c9a35706 --- /dev/null +++ b/configs/test-fixtures/valid-files/required-version.tf @@ -0,0 +1,4 @@ + +terraform { + required_version = "~> 0.12.0" +} diff --git a/configs/test-fixtures/valid-files/resources.tf b/configs/test-fixtures/valid-files/resources.tf new file mode 100644 index 0000000000..53fb745335 --- /dev/null +++ b/configs/test-fixtures/valid-files/resources.tf @@ -0,0 +1,42 @@ +resource "aws_security_group" "firewall" { + lifecycle { + create_before_destroy = true + prevent_destroy = true + ignore_changes = [ + description, + ] + } + + connection { + host = "127.0.0.1" + } + + provisioner "local-exec" { + command = "echo hello" + + connection { + host = "10.1.2.1" + } + } + + provisioner "local-exec" { + command = "echo hello" + } +} + +resource "aws_instance" "web" { + ami = "ami-1234" + security_groups = [ + "foo", + "bar", + ] + + network_interface { + device_index = 0 + description = "Main network interface" + } + + depends_on = [ + aws_security_group.firewall, + ] +} diff --git a/configs/test-fixtures/valid-files/variables.tf b/configs/test-fixtures/valid-files/variables.tf new file mode 100644 index 0000000000..185d9de555 --- /dev/null +++ b/configs/test-fixtures/valid-files/variables.tf @@ -0,0 +1,24 @@ + +variable "foo" { +} + +variable "bar" { + default = "hello" +} + +variable "baz" { + type = list +} + +variable "bar-baz" { + default = [] + type = list +} + +variable "cheeze_pizza" { + description = "Nothing special" +} + +variable "π" { + default = 3.14159265359 +} diff --git a/configs/test-fixtures/valid-files/variables.tf.json b/configs/test-fixtures/valid-files/variables.tf.json new file mode 100644 index 0000000000..3c97d61786 --- /dev/null +++ b/configs/test-fixtures/valid-files/variables.tf.json @@ -0,0 +1,21 @@ +{ + "variable": { + "foo": {}, + "bar": { + "default": "hello" + }, + "baz": { + "type": "list" + }, + "bar-baz": { + "default": [], + "type": "list" + }, + "cheese_pizza": { + "description": "Nothing special" + }, + "π": { + "default": 3.14159265359 + } + } +}