Skip to content

Commit

Permalink
fix: return error if plugin is not a file (#1169)
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-barker-coles committed Jul 23, 2021
1 parent 18c0b52 commit 4983b6b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
23 changes: 16 additions & 7 deletions plugin/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,24 @@ func getPluginDir() (string, error) {
// Only in the case of Windows, the pattern with the `.exe` is also considered,
// and if it exists, the extension is added to the argument.
func findPluginPath(path string) (string, error) {
_, err := os.Stat(path)
if os.IsNotExist(err) && runtime.GOOS != "windows" {
return "", os.ErrNotExist
} else if !os.IsNotExist(err) {
return path, nil
if runtime.GOOS != "windows" {
return checkPluginExistance(path)
}

returnPath, err := checkPluginExistance(path)
if os.IsNotExist(err) {
return checkPluginExistance(path + ".exe")
}

if _, err := os.Stat(path + ".exe"); !os.IsNotExist(err) {
return path + ".exe", nil
return returnPath, err
}

func checkPluginExistance(path string) (string, error) {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return "", os.ErrNotExist
} else if !os.IsNotExist(err) && !info.IsDir() {
return path, nil
}

return "", os.ErrNotExist
Expand Down
34 changes: 34 additions & 0 deletions plugin/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,40 @@ func Test_Discovery_notFound(t *testing.T) {
}
}

func Test_Discovery_plugin_name_is_directory(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(cwd)

err = os.Chdir(filepath.Join(cwd, "test-fixtures", "plugin_name_is_directory"))
if err != nil {
t.Fatal(err)
}

original := PluginRoot
PluginRoot = filepath.Join(cwd, "test-fixtures", "plugin_name_is_directory")
defer func() { PluginRoot = original }()

_, err = Discovery(&tflint.Config{
Plugins: map[string]*tflint.PluginConfig{
"foo": {
Name: "foo",
Enabled: true,
},
},
})

if err == nil {
t.Fatal("The error should have occurred, but didn't")
}
expected := fmt.Sprintf("Plugin `foo` not found in %s", PluginRoot)
if err.Error() != expected {
t.Fatalf("The error message is not matched: want=%s, got=%s", expected, err.Error())
}
}

func Test_Discovery_notFoundForAutoInstallation(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
Expand Down
Empty file.

0 comments on commit 4983b6b

Please sign in to comment.