Skip to content

Commit

Permalink
Add support for path.* named values
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Aug 24, 2019
1 parent 1ba6a7a commit 41c6ac9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
10 changes: 9 additions & 1 deletion docs/guides/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ Like Terraform, it supports the `--var`,` --var-file` options, automatic loading

## Named Values

[Named values](https://www.terraform.io/docs/configuration/expressions.html#references-to-named-values) are supported only for [input variables](https://www.terraform.io/docs/configuration/variables.html) and [workspaces](https://www.terraform.io/docs/state/workspaces.html). Expressions that contain anything else are excluded from the inspection.
[Named values](https://www.terraform.io/docs/configuration/expressions.html#references-to-named-values) are supported partially. The following named values are available:

- `var.<NAME>`
- `path.module`
- `path.root`
- `path.cwd`
- `terraform.workspace`

Expressions that reference named values not included above are excluded from the inspection.

## Built-in Functions

Expand Down
2 changes: 2 additions & 0 deletions tflint/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ func isEvaluable(expr hcl.Expression) bool {
// noop
case addrs.TerraformAttr:
// noop
case addrs.PathAttr:
// noop
default:
return false
}
Expand Down
83 changes: 77 additions & 6 deletions tflint/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ import (
)

func Test_EvaluateExpr_string(t *testing.T) {
dir, err := ioutil.TempDir("", "string")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

cases := []struct {
Name string
Content string
Expand Down Expand Up @@ -144,14 +150,24 @@ resource "null_resource" "test" {
}`,
Expected: "Hello World",
},
{
Name: "path.root",
Content: `
resource "null_resource" "test" {
key = path.root
}`,
Expected: filepath.ToSlash(dir),
},
{
Name: "path.module",
Content: `
resource "null_resource" "test" {
key = path.module
}`,
Expected: filepath.ToSlash(dir),
},
}

dir, err := ioutil.TempDir("", "string")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

for _, tc := range cases {
err := ioutil.WriteFile(dir+"/resource.tf", []byte(tc.Content), os.ModePerm)
if err != nil {
Expand Down Expand Up @@ -184,6 +200,53 @@ resource "null_resource" "test" {
}
}

func Test_EvaluateExpr_pathCwd(t *testing.T) {
dir, err := ioutil.TempDir("", "string")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

content := `
resource "null_resource" "test" {
key = path.cwd
}`
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
expected := filepath.ToSlash(cwd)

err = ioutil.WriteFile(dir+"/resource.tf", []byte(content), os.ModePerm)
if err != nil {
t.Fatal(err)
}

cfg, err := loadConfigHelper(dir)
if err != nil {
t.Fatal(err)
}
attribute, err := extractAttributeHelper("key", cfg)
if err != nil {
t.Fatal(err)
}

runner, err := NewRunner(EmptyConfig(), map[string]Annotations{}, cfg, map[string]*terraform.InputValue{})
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}

var ret string
err = runner.EvaluateExpr(attribute.Expr, &ret)
if err != nil {
t.Fatalf("Unexpected error: `%s`", err)
}

if expected != ret {
t.Fatalf("expected value is `%s`, but get `%s`", expected, ret)
}
}

func Test_EvaluateExpr_integer(t *testing.T) {
cases := []struct {
Name string
Expand Down Expand Up @@ -833,6 +896,14 @@ resource "null_resource" "test" {
}`,
Expected: false,
},
{
Name: "path attributes",
Content: `
resource "null_resource" "test" {
key = path.cwd
}`,
Expected: true,
},
}

dir, err := ioutil.TempDir("", "isEvaluable")
Expand Down

0 comments on commit 41c6ac9

Please sign in to comment.