allow init to reset -plugin-dir

Remove the recorded -plugin-dir during init if the flag is not provided.
This commit is contained in:
James Bardin 2017-12-21 11:21:07 -05:00
parent f43d66e143
commit 79e985366f
2 changed files with 71 additions and 1 deletions

View File

@ -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)

View File

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