Skip to content

Commit

Permalink
Fix CLI config file loading
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
swalkinshaw committed Aug 29, 2024
1 parent f589442 commit f52dc72
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
10 changes: 4 additions & 6 deletions trellis/trellis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ const (
GlobPattern = "group_vars/*/wordpress_sites.yml"
)

const cliConfigFile = "cli.yml"

type Options struct {
Detector Detector
ConfigDir string
Expand Down Expand Up @@ -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
Expand Down
39 changes: 35 additions & 4 deletions trellis/trellis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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")
}
}

0 comments on commit f52dc72

Please sign in to comment.