Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when enabling plugins by CLI #1377

Merged
merged 1 commit into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions integrationtest/inspection/enable-plugin-by-cli/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"issues": [
{
"rule": {
"name": "aws_instance_example_type",
"severity": "error",
"link": ""
},
"message": "instance type is t2.micro",
"range": {
"filename": "template.tf",
"start": {
"line": 2,
"column": 19
},
"end": {
"line": 2,
"column": 29
}
},
"callers": []
}
],
"errors": []
}
3 changes: 3 additions & 0 deletions integrationtest/inspection/enable-plugin-by-cli/template.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_instance" "foo" {
instance_type = "t2.micro"
}
5 changes: 5 additions & 0 deletions integrationtest/inspection/inspection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func TestIntegration(t *testing.T) {
Command: "tflint --only aws_s3_bucket_with_config_example --format json",
Dir: "rule-config",
},
{
Name: "enable plugin by CLI",
Command: "tflint --enable-plugin testing --format json",
Dir: "enable-plugin-by-cli",
},
}

dir, _ := os.Getwd()
Expand Down
3 changes: 3 additions & 0 deletions tflint/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ func (c *PluginConfig) Content(schema *hclext.BodySchema) (*hclext.BodyContent,
if schema == nil {
schema = &hclext.BodySchema{}
}
if c.Body == nil {
return &hclext.BodyContent{}, hcl.Diagnostics{}
}
return hclext.Content(c.Body, schema)
}

Expand Down
22 changes: 19 additions & 3 deletions tflint/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ func TestPluginContent(t *testing.T) {
tests := []struct {
Name string
Config string
CLI bool
Arg *hclext.BodySchema
Want *hclext.BodyContent
DiagCount int
Expand Down Expand Up @@ -822,6 +823,15 @@ plugin "test" {
},
DiagCount: 0,
},
{
Name: "enabled by CLI",
CLI: true,
Arg: &hclext.BodySchema{
Attributes: []hclext.AttributeSchema{{Name: "foo"}},
},
Want: &hclext.BodyContent{},
DiagCount: 0,
},
{
Name: "required attribute is not found",
Config: `
Expand Down Expand Up @@ -857,9 +867,15 @@ plugin "test" {
if err != nil {
t.Fatal(err)
}
plugin, exists := config.Plugins["test"]
if !exists {
t.Fatal("plugin `test` should be declared")
var plugin *PluginConfig
if test.CLI {
plugin = &PluginConfig{Name: "test", Body: nil}
} else {
var exists bool
plugin, exists = config.Plugins["test"]
if !exists {
t.Fatal("plugin `test` should be declared")
}
}

content, diags := plugin.Content(test.Arg)
Expand Down