Skip to content

Commit 8364139

Browse files
authored
Implement Files() method (#122)
1 parent 18655e3 commit 8364139

File tree

7 files changed

+89
-5
lines changed

7 files changed

+89
-5
lines changed

helper/runner.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,30 @@ import (
1717

1818
// Runner is a mock that satisfies the Runner interface for plugin testing.
1919
type Runner struct {
20-
Files map[string]*hcl.File
20+
files map[string]*hcl.File
2121
Issues Issues
2222

2323
tfconfig *configs.Config
2424
config Config
2525
}
2626

27+
// NewLocalRunner initialises a new test runner.
28+
// Internal use only.
29+
func NewLocalRunner(files map[string]*hcl.File, issues Issues) *Runner {
30+
return &Runner{files: map[string]*hcl.File{}, Issues: issues}
31+
}
32+
33+
// AddLocalFile adds a new file to the current mapped files.
34+
// Internal use only.
35+
func (r *Runner) AddLocalFile(name string, file *hcl.File) bool {
36+
if _, exists := r.files[name]; exists {
37+
return false
38+
}
39+
40+
r.files[name] = file
41+
return true
42+
}
43+
2744
// Config is a pseudo TFLint config file object for testing from plugins.
2845
type Config struct {
2946
Rules []RuleConfig `hcl:"rule,block"`
@@ -134,7 +151,12 @@ func (r *Runner) Config() (*configs.Config, error) {
134151

135152
// File returns the hcl.File object
136153
func (r *Runner) File(filename string) (*hcl.File, error) {
137-
return r.Files[filename], nil
154+
return r.files[filename], nil
155+
}
156+
157+
// Files returns a map[string]hcl.File object
158+
func (r *Runner) Files() (map[string]*hcl.File, error) {
159+
return r.files, nil
138160
}
139161

140162
// RootProvider returns the provider configuration.
@@ -264,7 +286,7 @@ func (r *Runner) initFromFiles() error {
264286
},
265287
}
266288

267-
for _, file := range r.Files {
289+
for _, file := range r.files {
268290
content, diags := file.Body.Content(configFileSchema)
269291
if diags.HasErrors() {
270292
return diags

helper/runner_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,31 @@ func Test_EnsureNoError(t *testing.T) {
471471
t.Fatal("Expected to exec the passed proc, but doesn't")
472472
}
473473
}
474+
475+
func Test_Files(t *testing.T) {
476+
var sources = map[string]string{
477+
"main.tf": `
478+
resource "aws_instance" "foo" {
479+
instance_type = "t2.micro"
480+
}`,
481+
"outputs.tf": `
482+
output "dummy" {
483+
value = "test"
484+
}`,
485+
"providers.tf": `
486+
provider "aws" {
487+
region = "us-east-1"
488+
}`,
489+
}
490+
491+
runner := TestRunner(t, sources)
492+
493+
files, err := runner.Files()
494+
if err != nil {
495+
t.Fatalf("The response has an unexpected error: %s", err)
496+
}
497+
498+
if !cmp.Equal(len(sources), len(files)) {
499+
t.Fatalf("Sources and Files differ: %s", cmp.Diff(sources, files))
500+
}
501+
}

helper/testing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// TestRunner returns a mock Runner for testing.
1717
// You can pass the map of file names and their contents in the second argument.
1818
func TestRunner(t *testing.T, files map[string]string) *Runner {
19-
runner := &Runner{Files: map[string]*hcl.File{}, Issues: Issues{}}
19+
runner := NewLocalRunner(map[string]*hcl.File{}, Issues{})
2020
parser := hclparse.NewParser()
2121

2222
for name, src := range files {
@@ -32,7 +32,7 @@ func TestRunner(t *testing.T, files map[string]string) *Runner {
3232
}
3333
runner.config = config
3434
} else {
35-
runner.Files[name] = file
35+
runner.AddLocalFile(name, file)
3636
}
3737
}
3838

tflint/client/client.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,26 @@ func (c *Client) File(filename string) (*hcl.File, error) {
186186
return file, nil
187187
}
188188

189+
// Files calls the server-side Files method and returns a collection of hcl.File object.
190+
func (c *Client) Files() (map[string]*hcl.File, error) {
191+
var response FilesResponse
192+
if err := c.rpcClient.Call("Plugin.Files", FilesRequest{}, &response); err != nil {
193+
return nil, err
194+
}
195+
196+
files := make(map[string]*hcl.File)
197+
for filename, content := range response.Files {
198+
file, diags := parseConfig(content, filename, hcl.InitialPos)
199+
200+
if diags.HasErrors() {
201+
return nil, diags
202+
}
203+
204+
files[filename] = file
205+
}
206+
return files, nil
207+
}
208+
189209
// RootProvider calls the server-side RootProvider method and returns the provider configuration.
190210
func (c *Client) RootProvider(name string) (*configs.Provider, error) {
191211
log.Printf("[DEBUG] Accessing to the `%s` provider config in the root module", name)

tflint/client/rpc.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ type FileResponse struct {
7878
Range hcl.Range
7979
}
8080

81+
// FilesRequest is a request to the server-side Files method.
82+
type FilesRequest struct{}
83+
84+
// FilesResponse is a response to the server-side Files method.
85+
type FilesResponse struct {
86+
Files map[string][]byte
87+
Err error
88+
}
89+
8190
// RootProviderRequest is a request to the server-side RootProvider method.
8291
type RootProviderRequest struct {
8392
Name string

tflint/interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ type Runner interface {
5959
// When accessing resources, expressions, etc, it is recommended to use high-level APIs.
6060
File(string) (*hcl.File, error)
6161

62+
// Files returns a map[string]hcl.File object, where the key is the file name.
63+
// This is low level API for accessing information such as comments and syntax.
64+
Files() (map[string]*hcl.File, error)
65+
6266
// RootProvider returns the provider configuration in the root module.
6367
// It can be used by child modules to access the credentials defined in the root module.
6468
RootProvider(name string) (*configs.Provider, error)

tflint/server/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ type Server interface {
1111
Backend(*client.BackendRequest, *client.BackendResponse) error
1212
EvalExpr(*client.EvalExprRequest, *client.EvalExprResponse) error
1313
EmitIssue(*client.EmitIssueRequest, *interface{}) error
14+
Files(*client.FilesRequest, *client.FilesResponse) error
1415
}

0 commit comments

Comments
 (0)