diff --git a/command/init_test.go b/command/init_test.go index 05ad34093..fcf0e2ec0 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -926,6 +926,70 @@ func TestInit_providerLockFile(t *testing.T) { } } +func TestInit_pluginDirReset(t *testing.T) { + td, err := ioutil.TempDir("", "tf") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(td) + defer testChdir(t, td)() + + ui := new(cli.MockUi) + c := &InitCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + }, + providerInstaller: &mockProviderInstaller{}, + } + + // make our vendor paths + pluginPath := []string{"a", "b", "c"} + for _, p := range pluginPath { + if err := os.MkdirAll(p, 0755); err != nil { + t.Fatal(err) + } + } + + // run once and save the -plugin-dir + args := []string{"-plugin-dir", "a"} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: \n%s", ui.ErrorWriter) + } + + pluginDirs, err := c.loadPluginPath() + if err != nil { + t.Fatal(err) + } + + if len(pluginDirs) != 1 || pluginDirs[0] != "a" { + t.Fatalf(`expected plugin dir ["a"], got %q`, pluginDirs) + } + + ui = new(cli.MockUi) + c = &InitCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + }, + providerInstaller: &mockProviderInstaller{}, + } + + // make sure we remove the plugin-dir record + if code := c.Run(nil); code != 0 { + t.Fatalf("bad: \n%s", ui.ErrorWriter) + } + + pluginDirs, err = c.loadPluginPath() + if err != nil { + t.Fatal(err) + } + + if len(pluginDirs) != 0 { + t.Fatalf("expected no plugin dirs got %q", pluginDirs) + } +} + // Test user-supplied -plugin-dir func TestInit_pluginDirProviders(t *testing.T) { td := tempDir(t) diff --git a/command/plugins.go b/command/plugins.go index 5eba6b698..2dce2510f 100644 --- a/command/plugins.go +++ b/command/plugins.go @@ -113,7 +113,13 @@ func (r *multiVersionProviderResolver) ResolveProviders( // store the user-supplied path for plugin discovery func (m *Meta) storePluginPath(pluginPath []string) error { + path := filepath.Join(m.DataDir(), PluginPathFile) + if len(pluginPath) == 0 { + err := os.Remove(path) + if !os.IsNotExist(err) { + return err + } return nil } @@ -125,7 +131,7 @@ func (m *Meta) storePluginPath(pluginPath []string) error { // if this fails, so will WriteFile os.MkdirAll(m.DataDir(), 0755) - return ioutil.WriteFile(filepath.Join(m.DataDir(), PluginPathFile), js, 0644) + return ioutil.WriteFile(path, js, 0644) } // Load the user-defined plugin search path into Meta.pluginPath if the file