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: return error if plugin is not a file #1169

Merged
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
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