Skip to content

Commit

Permalink
feat: support changing work directory (#15)
Browse files Browse the repository at this point in the history
Closes #10

Signed-off-by: Brian McGee <brian@bmcgee.ie>

Reviewed-on: https://git.numtide.com/numtide/treefmt/pulls/15
Reviewed-by: Jonas Chevalier <zimbatm@noreply.git.numtide.com>
Co-authored-by: Brian McGee <brian@bmcgee.ie>
Co-committed-by: Brian McGee <brian@bmcgee.ie>
  • Loading branch information
brianmcgee authored and Brian McGee committed Jan 3, 2024
1 parent 84629f7 commit a3ca782
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
16 changes: 9 additions & 7 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package cli

import (
"github.com/alecthomas/kong"
"github.com/charmbracelet/log"
)

var Cli = Options{}

type Options struct {
AllowMissingFormatter bool `default:"false" help:"Do not exit with error if a configured formatter is missing"`
ClearCache bool `short:"c" help:"Reset the evaluation cache. Use in case the cache is not precise enough"`
ConfigFile string `type:"existingfile" default:"./treefmt.toml"`
FailOnChange bool `help:"Exit with error if any changes were made. Useful for CI"`
Formatters []string `help:"Specify formatters to apply. Defaults to all formatters"`
TreeRoot string `type:"existingdir" default:"."`
Verbosity int `name:"verbose" short:"v" type:"counter" default:"0" env:"LOG_LEVEL" help:"Set the verbosity of logs e.g. -vv"`
AllowMissingFormatter bool `default:"false" help:"Do not exit with error if a configured formatter is missing"`
WorkingDirectory kong.ChangeDirFlag `default:"." short:"C" help:"Run as if treefmt was started in the specified working directory instead of the current working directory"`
ClearCache bool `short:"c" help:"Reset the evaluation cache. Use in case the cache is not precise enough"`
ConfigFile string `type:"existingfile" default:"./treefmt.toml"`
FailOnChange bool `help:"Exit with error if any changes were made. Useful for CI"`
Formatters []string `help:"Specify formatters to apply. Defaults to all formatters"`
TreeRoot string `type:"existingdir" default:"."`
Verbosity int `name:"verbose" short:"v" type:"counter" default:"0" env:"LOG_LEVEL" help:"Set the verbosity of logs e.g. -vv"`

Format Format `cmd:"" default:"."`
}
Expand Down
34 changes: 34 additions & 0 deletions internal/cli/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,40 @@ func TestCache(t *testing.T) {
as.Contains(string(out), "0 files changed")
}

func TestChangeWorkingDirectory(t *testing.T) {
as := require.New(t)

// capture current cwd, so we can replace it after the test is finished
cwd, err := os.Getwd()
as.NoError(err)

t.Cleanup(func() {
// return to the previous working directory
as.NoError(os.Chdir(cwd))
})

tempDir := test.TempExamples(t)
configPath := tempDir + "/treefmt.toml"

// test without any excludes
config := format.Config{
Formatters: map[string]*format.Formatter{
"echo": {
Command: "echo",
Includes: []string{"*"},
},
},
}

test.WriteConfig(t, configPath, config)

// by default, we look for ./treefmt.toml and use the cwd for the tree root
// this should fail if the working directory hasn't been changed first
out, err := cmd(t, "-C", tempDir)
as.NoError(err)
as.Contains(string(out), fmt.Sprintf("%d files changed", 29))
}

func TestFailOnChange(t *testing.T) {
as := require.New(t)

Expand Down

0 comments on commit a3ca782

Please sign in to comment.