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

Don't query the TF registry for module sources starting with "." #1062

Merged
merged 4 commits into from
Sep 5, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ internal/schemas/schemas_gen.go
internal/schemas/data/
terraform-ls
terraform-ls.exe
go.work*
21 changes: 3 additions & 18 deletions internal/hooks/module_source_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func (h *Hooks) RegistryModuleSources(ctx context.Context, value cty.Value) ([]d
candidates := make([]decoder.Candidate, 0)
prefix := value.AsString()

if isModuleSourceLocal(prefix) {
// We're dealing with a local module source here, no need to search the registry
if strings.HasPrefix(prefix, ".") {
// We're likely dealing with a local module source here; no need to search the registry
// A search for "." will not return any results
return candidates, nil
}

Expand All @@ -71,19 +72,3 @@ func (h *Hooks) RegistryModuleSources(ctx context.Context, value cty.Value) ([]d

return candidates, nil
}

var moduleSourceLocalPrefixes = []string{
"./",
"../",
".\\",
"..\\",
}

func isModuleSourceLocal(raw string) bool {
for _, prefix := range moduleSourceLocalPrefixes {
if strings.HasPrefix(raw, prefix) {
return true
}
}
return false
}
60 changes: 57 additions & 3 deletions internal/hooks/module_source_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -105,7 +104,7 @@ func TestHooks_RegistryModuleSources(t *testing.T) {
h := &Hooks{
ModStore: s.Modules,
AlgoliaClient: searchClient,
Logger: log.New(ioutil.Discard, "", 0),
Logger: log.New(io.Discard, "", 0),
}

tests := []struct {
Expand Down Expand Up @@ -183,7 +182,7 @@ func TestHooks_RegistryModuleSourcesCtxCancel(t *testing.T) {
h := &Hooks{
ModStore: s.Modules,
AlgoliaClient: searchClient,
Logger: log.New(ioutil.Discard, "", 0),
Logger: log.New(io.Discard, "", 0),
}

_, err = h.RegistryModuleSources(ctx, cty.StringVal("aws"))
Expand All @@ -197,6 +196,61 @@ func TestHooks_RegistryModuleSourcesCtxCancel(t *testing.T) {
}
}

func TestHooks_RegistryModuleSourcesIgnore(t *testing.T) {
ctx := context.Background()

s, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}

searchClient := buildSearchClientMock(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprintf("unexpected request: %q", r.RequestURI), 400)
}))

h := &Hooks{
ModStore: s.Modules,
AlgoliaClient: searchClient,
Logger: log.New(io.Discard, "", 0),
}

tests := []struct {
name string
value cty.Value
want []decoder.Candidate
}{
{
"search dot",
cty.StringVal("."),
[]decoder.Candidate{},
},
{
"search dot dot",
cty.StringVal(".."),
[]decoder.Candidate{},
},
{
"local module",
cty.StringVal("../aws"),
[]decoder.Candidate{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
candidates, err := h.RegistryModuleSources(ctx, tt.value)

if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(tt.want, candidates); diff != "" {
t.Fatalf("mismatched candidates: %s", diff)
}
})
}
}

func buildSearchClientMock(t *testing.T, handler http.HandlerFunc) *search.Client {
searchServer := httptest.NewTLSServer(handler)
t.Cleanup(searchServer.Close)
Expand Down