Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func runE(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, args []string)
configFile = os.Getenv("TREEFMT_CONFIG")
}

filenames := []string{"treefmt.toml", ".treefmt.toml"}
filenames := []string{"treefmt.toml", ".treefmt.toml", ".config/treefmt.toml"}

// look in PRJ_ROOT if set
if prjRoot := os.Getenv("PRJ_ROOT"); configFile == "" && prjRoot != "" {
Expand Down
72 changes: 72 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,78 @@ func TestConfigFile(t *testing.T) {
}
}

func TestDotConfigDir(t *testing.T) {
t.Run("basic", func(t *testing.T) {
as := require.New(t)

tempDir := test.TempExamples(t)

// remove the sample treefmt.toml
as.NoError(os.Remove(filepath.Join(tempDir, "treefmt.toml")), "failed to remove sample treefmt.toml")

// ensure the test config is created in the .config subdirectory
configPath := filepath.Join(tempDir, ".config/treefmt.toml")

// ensure we are executing from the root of the temp dir
test.ChangeWorkDir(t, tempDir)

treefmt(t,
withConfig(configPath, &config.Config{
FormatterConfigs: map[string]*config.Formatter{
"echo": {
Command: "echo",
Includes: []string{"*"},
},
},
}),
withNoError(t),
withStats(t, map[stats.Type]int{
stats.Traversed: 33,
stats.Matched: 33,
stats.Formatted: 33,
stats.Changed: 0,
}),
)
})

t.Run("project root ending in .config", func(t *testing.T) {
as := require.New(t)

// create a project root whose name ends with .config
tempDir := filepath.Join(t.TempDir(), "foo.config")
as.NoError(os.Mkdir(tempDir, 0o755))

test.TempExamplesInDir(t, tempDir)

// remove the sample treefmt.toml
as.NoError(os.Remove(filepath.Join(tempDir, "treefmt.toml")), "failed to remove sample treefmt.toml")

// place config in the .config subdirectory
configPath := filepath.Join(tempDir, ".config/treefmt.toml")

// ensure we are executing from the root of the temp dir
test.ChangeWorkDir(t, tempDir)

treefmt(t,
withConfig(configPath, &config.Config{
FormatterConfigs: map[string]*config.Formatter{
"echo": {
Command: "echo",
Includes: []string{"*"},
},
},
}),
withNoError(t),
withStats(t, map[stats.Type]int{
stats.Traversed: 33,
stats.Matched: 33,
stats.Formatted: 33,
stats.Changed: 0,
}),
)
})
}

func TestCache(t *testing.T) {
tempDir := test.TempExamples(t)
configPath := filepath.Join(tempDir, "treefmt.toml")
Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"slices"
Expand Down Expand Up @@ -356,6 +357,11 @@ func determineTreeRoot(v *viper.Viper, cfg *Config, logger *log.Logger) error {
)

cfg.TreeRoot = filepath.Dir(v.ConfigFileUsed())

// if the config file was located inside a `.config` directory, we need to go up one level
if path.Base(cfg.TreeRoot) == ".config" {
Comment thread
brianmcgee marked this conversation as resolved.
cfg.TreeRoot = filepath.Dir(cfg.TreeRoot)
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func WriteConfig(t *testing.T, path string, cfg *config.Config) {
t.Fatalf("failed to stat old config file: %v", path)
}

// ensure any sub directories exist
err = os.MkdirAll(filepath.Dir(path), 0o755)
if err != nil {
t.Fatalf("failed to ensure sub directories for config file: %v", err)
}

f, err := os.Create(path)
if err != nil {
t.Fatalf("failed to create a new config file: %v", err)
Expand Down