Skip to content

Commit

Permalink
Have FileLinter report Load error as Violation instead of exiting pro…
Browse files Browse the repository at this point in the history
…gram
  • Loading branch information
Larry Hitchon committed Jun 15, 2018
1 parent abe333a commit d27395c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
21 changes: 19 additions & 2 deletions linter/file_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,20 @@ func (fl FileLinter) Validate(ruleSet assertion.RuleSet, options Options) (asser
variables := []Variable{}
filesScanned := []string{}

loadViolations := []assertion.Violation{}

for _, filename := range fl.Filenames {
include, err := assertion.ShouldIncludeFile(ruleSet.Files, filename)
if err == nil && include {
assertion.Debugf("Processing %s\n", filename)
filesScanned = append(filesScanned, filename)
loaded, err := fl.Loader.Load(filename)
if err != nil {
return assertion.ValidationReport{}, err
fmt.Printf("Error loading %s: %s\n", filename, err)
loadViolations = append(loadViolations, makeLoadViolation(filename, err))
continue
}
assertion.Debugf("Found variables %v\n", loaded.Variables)
filesScanned = append(filesScanned, filename)
resources = append(resources, loaded.Resources...)
variables = append(variables, loaded.Variables...)
}
Expand All @@ -62,9 +66,22 @@ func (fl FileLinter) Validate(ruleSet assertion.RuleSet, options Options) (asser
return report, err
}
report.FilesScanned = filesScanned
report.Violations = append(report.Violations, loadViolations...)
return report, nil
}

func makeLoadViolation(filename string, err error) assertion.Violation {
return assertion.Violation{
RuleID: "FILE_LOAD",
ResourceID: filename,
ResourceType: "file",
Status: "FAILURE",
RuleMessage: "Unable to load file",
AssertionMessage: err.Error(),
Filename: filename,
}
}

// Search evaluates a JMESPath expression against resources in a collection of filenames
func (fl FileLinter) Search(ruleSet assertion.RuleSet, searchExpression string, w io.Writer) {
for _, filename := range fl.Filenames {
Expand Down
24 changes: 24 additions & 0 deletions linter/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,27 @@ func TestTerraformProvider(t *testing.T) {
t.Errorf("Violations: %v", report.Violations)
}
}

func TestTerraformParseError(t *testing.T) {
options := Options{
Tags: []string{},
RuleIDs: []string{},
}
filenames := []string{
"./testdata/resources/terraform_provider.tf",
"./testdata/resources/terraform_syntax_error.tf",
}
linter := FileLinter{Filenames: filenames, ValueSource: TestingValueSource{}, Loader: TerraformResourceLoader{}}
ruleSet := loadRulesForTest("./testdata/rules/terraform_provider.yml", t)
report, err := linter.Validate(ruleSet, options)
if err != nil {
t.Error("Expecting TestTerraformParseError to not return an error:" + err.Error())
}
if len(report.Violations) != 1 {
t.Errorf("TestTerraformParseError returned %d violations, expecting 1", len(report.Violations))
t.Errorf("Violations: %v", report.Violations)
}
if report.Violations[0].RuleID != "FILE_LOAD" {
t.Errorf("TestTerraformParseError returned RuleID = %s, expecting FILE_LOAD", report.Violations)
}
}
3 changes: 3 additions & 0 deletions linter/testdata/resources/terraform_syntax_error.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "template_file" "example" }
template = "/example/template.json"
}

0 comments on commit d27395c

Please sign in to comment.