diff --git a/trellis/trellis.go b/trellis/trellis.go index 5e058be6..83e229cc 100644 --- a/trellis/trellis.go +++ b/trellis/trellis.go @@ -21,8 +21,6 @@ const ( GlobPattern = "group_vars/*/wordpress_sites.yml" ) -const cliConfigFile = "cli.yml" - type Options struct { Detector Detector ConfigDir string @@ -183,15 +181,15 @@ func (t *Trellis) LoadProject() error { return errors.New("No Trellis project detected in the current directory or any of its parent directories.") } - if err = t.LoadProjectCliConfig(); err != nil { - return err - } - t.Path = path t.Virtualenv = NewVirtualenv(t.ConfigPath()) os.Chdir(t.Path) + if err = t.LoadProjectCliConfig(); err != nil { + return err + } + if t.CliConfig.VirtualenvIntegration { if t.Virtualenv.Initialized() { t.VenvInitialized = true diff --git a/trellis/trellis_test.go b/trellis/trellis_test.go index e75f17f1..b51a62e7 100644 --- a/trellis/trellis_test.go +++ b/trellis/trellis_test.go @@ -354,7 +354,7 @@ func TestLoadGlobalCliConfig(t *testing.T) { tempDir := t.TempDir() t.Setenv("TRELLIS_CONFIG_DIR", tempDir) - configFilePath := app_paths.ConfigPath(cliConfigFile) + configFilePath := app_paths.ConfigPath("cli.yml") configContents := ` ask_vault_pass: true ` @@ -382,12 +382,12 @@ func TestLoadProjectCliConfig(t *testing.T) { tempDir := t.TempDir() t.Setenv("TRELLIS_CONFIG_DIR", tempDir) - configFilePath := app_paths.ConfigPath(cliConfigFile) + globalConfigFilePath := app_paths.ConfigPath("cli.yml") configContents := ` ask_vault_pass: true ` - if err := os.WriteFile(configFilePath, []byte(configContents), 0666); err != nil { + if err := os.WriteFile(globalConfigFilePath, []byte(configContents), 0666); err != nil { t.Fatal(err) } @@ -401,7 +401,7 @@ ask_vault_pass: true ask_vault_pass: false ` - if err := os.WriteFile(filepath.Join(tp.ConfigPath(), cliConfigFile), []byte(projectConfigContents), 0666); err != nil { + if err := os.WriteFile(filepath.Join(tp.Path, "trellis.cli.yml"), []byte(projectConfigContents), 0666); err != nil { t.Fatal(err) } @@ -413,3 +413,34 @@ ask_vault_pass: false t.Errorf("expected project CLI config to override AskVaultPass to false") } } + +func TestProjectCliConfigIsLoadedFromProjectRoot(t *testing.T) { + defer LoadFixtureProject(t)() + + tp := NewTrellis() + + configFilePath := filepath.Join(tp.Path, "trellis.cli.yml") + + projectConfigContents := ` +ask_vault_pass: true +` + + if err := os.WriteFile(configFilePath, []byte(projectConfigContents), 0666); err != nil { + t.Fatal(err) + } + + defer os.Remove(configFilePath) + + // Change directory outside the `trellis` directory to test that + // `trellis.cli.yml` is still properly loaded + defer TestChdir(t, "..")() + + err := tp.LoadProject() + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if tp.CliConfig.AskVaultPass != true { + t.Errorf("expected load project to load project CLI config file") + } +}