generated from terraform-linters/tflint-ruleset-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from fatbasstard/fvb/empty-file-rule
- Loading branch information
Showing
7 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# formatter_empty_file | ||
|
||
This rule ensures there are no empty Terraform files. | ||
|
||
## Example | ||
|
||
``` | ||
$ tflint | ||
1 issue(s) found: | ||
empty_file.tf:0:0: Warning - file has no content (formatter_empty_file) | ||
``` | ||
|
||
## Why | ||
|
||
Having empty files in a project is confusing and gives unused maintenance and confusing, e.g. when browsing through folders. | ||
|
||
## How To Fix | ||
|
||
Remove the empty files from the project. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package rules | ||
|
||
import ( | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/terraform-linters/tflint-plugin-sdk/tflint" | ||
"github.com/thaim/tflint-ruleset-formatter/project" | ||
) | ||
|
||
// FormatterEmptyFileRule checks if a Terraform file actually has any content | ||
type FormatterEmptyFileRule struct { | ||
tflint.DefaultRule | ||
} | ||
|
||
func NewFormatterEmptyFileRule() *FormatterEmptyFileRule { | ||
return &FormatterEmptyFileRule{} | ||
} | ||
|
||
func (r *FormatterEmptyFileRule) Name() string { | ||
return "formatter_empty_file" | ||
} | ||
|
||
func (r *FormatterEmptyFileRule) Enabled() bool { | ||
return true | ||
} | ||
|
||
func (r *FormatterEmptyFileRule) Severity() tflint.Severity { | ||
return tflint.WARNING | ||
} | ||
|
||
func (r *FormatterEmptyFileRule) Link() string { | ||
return project.ReferenceLink(r.Name()) | ||
} | ||
|
||
func (r *FormatterEmptyFileRule) Check(runner tflint.Runner) error { | ||
files, err := runner.GetFiles() | ||
if err != nil { | ||
return err | ||
} | ||
for name, file := range files { | ||
if err := r.checkEmpty(runner, name, file); err != nil { | ||
return err | ||
} | ||
|
||
} | ||
return nil | ||
} | ||
|
||
func (r *FormatterEmptyFileRule) checkEmpty(runner tflint.Runner, filename string, file *hcl.File) error { | ||
if len(file.Bytes) == 0 { | ||
fileRange := hcl.Range{ | ||
Filename: filename, | ||
Start: hcl.Pos{Line: 0, Column: 0}, | ||
End: hcl.Pos{Line: 0, Column: 0}, | ||
} | ||
|
||
return runner.EmitIssue( | ||
r, | ||
"file has no content", | ||
fileRange, | ||
) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package rules | ||
|
||
import ( | ||
"testing" | ||
|
||
hcl "github.com/hashicorp/hcl/v2" | ||
"github.com/terraform-linters/tflint-plugin-sdk/helper" | ||
) | ||
|
||
func Test_FormatterEmptyFile(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Content string | ||
Expected helper.Issues | ||
}{ | ||
{ | ||
Name: "file has no content", | ||
Content: ``, | ||
Expected: helper.Issues{ | ||
{ | ||
Rule: NewFormatterEmptyFileRule(), | ||
Message: "file has no content", | ||
Range: hcl.Range{ | ||
Filename: "resource.tf", | ||
Start: hcl.Pos{Line: 0, Column: 0}, | ||
End: hcl.Pos{Line: 0, Column: 0}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
rule := NewFormatterEmptyFileRule() | ||
|
||
for _, tc := range cases { | ||
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content}) | ||
|
||
if err := rule.Check(runner); err != nil { | ||
t.Fatalf("Unexpected error occurred: %s", err) | ||
} | ||
|
||
helper.AssertIssues(t, tc.Expected, runner.Issues) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.