From f52dc72d7a4c2da096de0a07fc17f3cd56d772bc Mon Sep 17 00:00:00 2001 From: Scott Walkinshaw Date: Wed, 28 Aug 2024 19:56:58 -0400 Subject: [PATCH] Fix CLI config file loading Fixes a bug where the project CLI config was only loaded if the CLI command was run from within the `trellis` directory. The project CLI config file was being loaded _before_ the project `Path` was set which meant it wasn't found and loaded. Now it's loaded after the `Path` is set. --- trellis/trellis.go | 10 ++++------ trellis/trellis_test.go | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 10 deletions(-) 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") + } +}