Skip to content

Commit 42f6e94

Browse files
authored
Fix panic when enabling plugins by CLI (#1377)
1 parent 0a3a2ae commit 42f6e94

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"issues": [
3+
{
4+
"rule": {
5+
"name": "aws_instance_example_type",
6+
"severity": "error",
7+
"link": ""
8+
},
9+
"message": "instance type is t2.micro",
10+
"range": {
11+
"filename": "template.tf",
12+
"start": {
13+
"line": 2,
14+
"column": 19
15+
},
16+
"end": {
17+
"line": 2,
18+
"column": 29
19+
}
20+
},
21+
"callers": []
22+
}
23+
],
24+
"errors": []
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resource "aws_instance" "foo" {
2+
instance_type = "t2.micro"
3+
}

integrationtest/inspection/inspection_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ func TestIntegration(t *testing.T) {
147147
Command: "tflint --only aws_s3_bucket_with_config_example --format json",
148148
Dir: "rule-config",
149149
},
150+
{
151+
Name: "enable plugin by CLI",
152+
Command: "tflint --enable-plugin testing --format json",
153+
Dir: "enable-plugin-by-cli",
154+
},
150155
}
151156

152157
dir, _ := os.Getwd()

tflint/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ func (c *PluginConfig) Content(schema *hclext.BodySchema) (*hclext.BodyContent,
335335
if schema == nil {
336336
schema = &hclext.BodySchema{}
337337
}
338+
if c.Body == nil {
339+
return &hclext.BodyContent{}, hcl.Diagnostics{}
340+
}
338341
return hclext.Content(c.Body, schema)
339342
}
340343

tflint/config_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ func TestPluginContent(t *testing.T) {
764764
tests := []struct {
765765
Name string
766766
Config string
767+
CLI bool
767768
Arg *hclext.BodySchema
768769
Want *hclext.BodyContent
769770
DiagCount int
@@ -822,6 +823,15 @@ plugin "test" {
822823
},
823824
DiagCount: 0,
824825
},
826+
{
827+
Name: "enabled by CLI",
828+
CLI: true,
829+
Arg: &hclext.BodySchema{
830+
Attributes: []hclext.AttributeSchema{{Name: "foo"}},
831+
},
832+
Want: &hclext.BodyContent{},
833+
DiagCount: 0,
834+
},
825835
{
826836
Name: "required attribute is not found",
827837
Config: `
@@ -857,9 +867,15 @@ plugin "test" {
857867
if err != nil {
858868
t.Fatal(err)
859869
}
860-
plugin, exists := config.Plugins["test"]
861-
if !exists {
862-
t.Fatal("plugin `test` should be declared")
870+
var plugin *PluginConfig
871+
if test.CLI {
872+
plugin = &PluginConfig{Name: "test", Body: nil}
873+
} else {
874+
var exists bool
875+
plugin, exists = config.Plugins["test"]
876+
if !exists {
877+
t.Fatal("plugin `test` should be declared")
878+
}
863879
}
864880

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

0 commit comments

Comments
 (0)