Compare commits
4 Commits
a99b9513be
...
cb8565d683
Author | SHA1 | Date | |
---|---|---|---|
cb8565d683 | |||
da82b80f29 | |||
d53f98ea05 | |||
d4ab015bc6 |
6
build/build.go
Normal file
6
build/build.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package build
|
||||||
|
|
||||||
|
var (
|
||||||
|
Name = "treefmt"
|
||||||
|
Version = "v0.0.1+dev"
|
||||||
|
)
|
4
internal/cache/cache.go → cache/cache.go
vendored
4
internal/cache/cache.go → cache/cache.go
vendored
@ -9,9 +9,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.numtide.com/numtide/treefmt/internal/walk"
|
"git.numtide.com/numtide/treefmt/format"
|
||||||
|
"git.numtide.com/numtide/treefmt/walk"
|
||||||
|
|
||||||
"git.numtide.com/numtide/treefmt/internal/format"
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
|
|
||||||
"github.com/adrg/xdg"
|
"github.com/adrg/xdg"
|
@ -1,7 +1,7 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.numtide.com/numtide/treefmt/internal/walk"
|
"git.numtide.com/numtide/treefmt/walk"
|
||||||
"github.com/alecthomas/kong"
|
"github.com/alecthomas/kong"
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
)
|
)
|
||||||
@ -11,6 +11,7 @@ var Cli = Format{}
|
|||||||
type Format struct {
|
type Format struct {
|
||||||
AllowMissingFormatter bool `default:"false" help:"Do not exit with error if a configured formatter is missing"`
|
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"`
|
WorkingDirectory kong.ChangeDirFlag `default:"." short:"C" help:"Run as if treefmt was started in the specified working directory instead of the current working directory"`
|
||||||
|
NoCache bool `help:"Ignore the evaluation cache entirely. Useful for CI"`
|
||||||
ClearCache bool `short:"c" help:"Reset the evaluation cache. Use in case the cache is not precise enough"`
|
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"`
|
ConfigFile string `type:"existingfile" default:"./treefmt.toml"`
|
||||||
FailOnChange bool `help:"Exit with error if any changes were made. Useful for CI."`
|
FailOnChange bool `help:"Exit with error if any changes were made. Useful for CI."`
|
||||||
@ -18,6 +19,7 @@ type Format struct {
|
|||||||
TreeRoot string `type:"existingdir" default:"."`
|
TreeRoot string `type:"existingdir" default:"."`
|
||||||
Walk walk.Type `enum:"auto,git,filesystem" default:"auto" help:"The method used to traverse the files within --tree-root. Currently supports 'auto', 'git' or 'filesystem'."`
|
Walk walk.Type `enum:"auto,git,filesystem" default:"auto" help:"The method used to traverse the files within --tree-root. Currently supports 'auto', 'git' or 'filesystem'."`
|
||||||
Verbosity int `name:"verbose" short:"v" type:"counter" default:"0" env:"LOG_LEVEL" help:"Set the verbosity of logs e.g. -vv."`
|
Verbosity int `name:"verbose" short:"v" type:"counter" default:"0" env:"LOG_LEVEL" help:"Set the verbosity of logs e.g. -vv."`
|
||||||
|
Version bool `name:"version" short:"V" help:"Print version"`
|
||||||
|
|
||||||
Paths []string `name:"paths" arg:"" type:"path" optional:"" help:"Paths to format. Defaults to formatting the whole tree."`
|
Paths []string `name:"paths" arg:"" type:"path" optional:"" help:"Paths to format. Defaults to formatting the whole tree."`
|
||||||
Stdin bool `help:"Format the context passed in via stdin"`
|
Stdin bool `help:"Format the context passed in via stdin"`
|
||||||
@ -27,8 +29,10 @@ func (f *Format) Configure() {
|
|||||||
log.SetReportTimestamp(false)
|
log.SetReportTimestamp(false)
|
||||||
|
|
||||||
if f.Verbosity == 0 {
|
if f.Verbosity == 0 {
|
||||||
|
log.SetLevel(log.WarnLevel)
|
||||||
|
} else if f.Verbosity == 1 {
|
||||||
log.SetLevel(log.InfoLevel)
|
log.SetLevel(log.InfoLevel)
|
||||||
} else if f.Verbosity > 0 {
|
} else if f.Verbosity > 1 {
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(log.DebugLevel)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,18 +5,17 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.numtide.com/numtide/treefmt/internal/walk"
|
"git.numtide.com/numtide/treefmt/cache"
|
||||||
|
"git.numtide.com/numtide/treefmt/config"
|
||||||
"git.numtide.com/numtide/treefmt/internal/config"
|
format2 "git.numtide.com/numtide/treefmt/format"
|
||||||
|
"git.numtide.com/numtide/treefmt/walk"
|
||||||
"git.numtide.com/numtide/treefmt/internal/cache"
|
|
||||||
"git.numtide.com/numtide/treefmt/internal/format"
|
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
@ -47,7 +46,7 @@ func (f *Format) Run() error {
|
|||||||
return fmt.Errorf("%w: failed to read config file", err)
|
return fmt.Errorf("%w: failed to read config file", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
globalExcludes, err := format.CompileGlobs(cfg.Global.Excludes)
|
globalExcludes, err := format2.CompileGlobs(cfg.Global.Excludes)
|
||||||
|
|
||||||
// create optional formatter filter set
|
// create optional formatter filter set
|
||||||
formatterSet := make(map[string]bool)
|
formatterSet := make(map[string]bool)
|
||||||
@ -68,7 +67,7 @@ func (f *Format) Run() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
formatters := make(map[string]*format.Formatter)
|
formatters := make(map[string]*format2.Formatter)
|
||||||
|
|
||||||
// detect broken dependencies
|
// detect broken dependencies
|
||||||
for name, formatterCfg := range cfg.Formatters {
|
for name, formatterCfg := range cfg.Formatters {
|
||||||
@ -115,8 +114,8 @@ func (f *Format) Run() error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
formatter, err := format.NewFormatter(name, formatterCfg, globalExcludes)
|
formatter, err := format2.NewFormatter(name, formatterCfg, globalExcludes)
|
||||||
if errors.Is(err, format.ErrCommandNotFound) && Cli.AllowMissingFormatter {
|
if errors.Is(err, format2.ErrCommandNotFound) && Cli.AllowMissingFormatter {
|
||||||
l.Debugf("formatter not found: %v", name)
|
l.Debugf("formatter not found: %v", name)
|
||||||
continue
|
continue
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
@ -147,7 +146,7 @@ func (f *Format) Run() error {
|
|||||||
//
|
//
|
||||||
completedCh := make(chan string, 1024)
|
completedCh := make(chan string, 1024)
|
||||||
|
|
||||||
ctx = format.SetCompletedChannel(ctx, completedCh)
|
ctx = format2.SetCompletedChannel(ctx, completedCh)
|
||||||
|
|
||||||
//
|
//
|
||||||
eg, ctx := errgroup.WithContext(ctx)
|
eg, ctx := errgroup.WithContext(ctx)
|
||||||
@ -170,6 +169,20 @@ func (f *Format) Run() error {
|
|||||||
|
|
||||||
var changes int
|
var changes int
|
||||||
|
|
||||||
|
processBatch := func() error {
|
||||||
|
if Cli.NoCache {
|
||||||
|
changes += len(batch)
|
||||||
|
} else {
|
||||||
|
count, err := cache.Update(batch)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
changes += count
|
||||||
|
}
|
||||||
|
batch = batch[:0]
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
LOOP:
|
LOOP:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -181,22 +194,17 @@ func (f *Format) Run() error {
|
|||||||
}
|
}
|
||||||
batch = append(batch, path)
|
batch = append(batch, path)
|
||||||
if len(batch) == batchSize {
|
if len(batch) == batchSize {
|
||||||
count, err := cache.Update(batch)
|
if err = processBatch(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
changes += count
|
|
||||||
batch = batch[:0]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// final flush
|
// final flush
|
||||||
count, err := cache.Update(batch)
|
if err = processBatch(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
changes += count
|
|
||||||
|
|
||||||
if Cli.FailOnChange && changes != 0 {
|
if Cli.FailOnChange && changes != 0 {
|
||||||
return ErrFailOnChange
|
return ErrFailOnChange
|
||||||
@ -253,6 +261,22 @@ func (f *Format) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
defer close(pathsCh)
|
defer close(pathsCh)
|
||||||
|
|
||||||
|
if Cli.NoCache {
|
||||||
|
return walker.Walk(ctx, func(path string, info fs.FileInfo, err error) error {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
default:
|
||||||
|
// ignore symlinks and directories
|
||||||
|
if !(info.IsDir() || info.Mode()&os.ModeSymlink == os.ModeSymlink) {
|
||||||
|
pathsCh <- path
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return cache.ChangeSet(ctx, walker, pathsCh)
|
return cache.ChangeSet(ctx, walker, pathsCh)
|
||||||
})
|
})
|
||||||
|
|
@ -8,15 +8,15 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.numtide.com/numtide/treefmt/internal/config"
|
config2 "git.numtide.com/numtide/treefmt/config"
|
||||||
|
"git.numtide.com/numtide/treefmt/format"
|
||||||
|
"git.numtide.com/numtide/treefmt/test"
|
||||||
|
|
||||||
"git.numtide.com/numtide/treefmt/internal/test"
|
|
||||||
"github.com/go-git/go-billy/v5/osfs"
|
"github.com/go-git/go-billy/v5/osfs"
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
"github.com/go-git/go-git/v5/plumbing/cache"
|
"github.com/go-git/go-git/v5/plumbing/cache"
|
||||||
"github.com/go-git/go-git/v5/storage/filesystem"
|
"github.com/go-git/go-git/v5/storage/filesystem"
|
||||||
|
|
||||||
"git.numtide.com/numtide/treefmt/internal/format"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -26,8 +26,8 @@ func TestAllowMissingFormatter(t *testing.T) {
|
|||||||
tempDir := t.TempDir()
|
tempDir := t.TempDir()
|
||||||
configPath := tempDir + "/treefmt.toml"
|
configPath := tempDir + "/treefmt.toml"
|
||||||
|
|
||||||
test.WriteConfig(t, configPath, config.Config{
|
test.WriteConfig(t, configPath, config2.Config{
|
||||||
Formatters: map[string]*config.Formatter{
|
Formatters: map[string]*config2.Formatter{
|
||||||
"foo-fmt": {
|
"foo-fmt": {
|
||||||
Command: "foo-fmt",
|
Command: "foo-fmt",
|
||||||
},
|
},
|
||||||
@ -47,8 +47,8 @@ func TestDependencyCycle(t *testing.T) {
|
|||||||
tempDir := t.TempDir()
|
tempDir := t.TempDir()
|
||||||
configPath := tempDir + "/treefmt.toml"
|
configPath := tempDir + "/treefmt.toml"
|
||||||
|
|
||||||
test.WriteConfig(t, configPath, config.Config{
|
test.WriteConfig(t, configPath, config2.Config{
|
||||||
Formatters: map[string]*config.Formatter{
|
Formatters: map[string]*config2.Formatter{
|
||||||
"a": {Command: "echo", Before: "b"},
|
"a": {Command: "echo", Before: "b"},
|
||||||
"b": {Command: "echo", Before: "c"},
|
"b": {Command: "echo", Before: "c"},
|
||||||
"c": {Command: "echo", Before: "a"},
|
"c": {Command: "echo", Before: "a"},
|
||||||
@ -68,8 +68,8 @@ func TestSpecifyingFormatters(t *testing.T) {
|
|||||||
tempDir := test.TempExamples(t)
|
tempDir := test.TempExamples(t)
|
||||||
configPath := tempDir + "/treefmt.toml"
|
configPath := tempDir + "/treefmt.toml"
|
||||||
|
|
||||||
test.WriteConfig(t, configPath, config.Config{
|
test.WriteConfig(t, configPath, config2.Config{
|
||||||
Formatters: map[string]*config.Formatter{
|
Formatters: map[string]*config2.Formatter{
|
||||||
"elm": {
|
"elm": {
|
||||||
Command: "echo",
|
Command: "echo",
|
||||||
Includes: []string{"*.elm"},
|
Includes: []string{"*.elm"},
|
||||||
@ -117,8 +117,8 @@ func TestIncludesAndExcludes(t *testing.T) {
|
|||||||
configPath := tempDir + "/echo.toml"
|
configPath := tempDir + "/echo.toml"
|
||||||
|
|
||||||
// test without any excludes
|
// test without any excludes
|
||||||
cfg := config.Config{
|
cfg := config2.Config{
|
||||||
Formatters: map[string]*config.Formatter{
|
Formatters: map[string]*config2.Formatter{
|
||||||
"echo": {
|
"echo": {
|
||||||
Command: "echo",
|
Command: "echo",
|
||||||
Includes: []string{"*"},
|
Includes: []string{"*"},
|
||||||
@ -129,7 +129,7 @@ func TestIncludesAndExcludes(t *testing.T) {
|
|||||||
test.WriteConfig(t, configPath, cfg)
|
test.WriteConfig(t, configPath, cfg)
|
||||||
out, err := cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
out, err := cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
as.Contains(string(out), fmt.Sprintf("%d files changed", 29))
|
as.Contains(string(out), fmt.Sprintf("%d files changed", 30))
|
||||||
|
|
||||||
// globally exclude nix files
|
// globally exclude nix files
|
||||||
cfg.Global.Excludes = []string{"*.nix"}
|
cfg.Global.Excludes = []string{"*.nix"}
|
||||||
@ -137,7 +137,7 @@ func TestIncludesAndExcludes(t *testing.T) {
|
|||||||
test.WriteConfig(t, configPath, cfg)
|
test.WriteConfig(t, configPath, cfg)
|
||||||
out, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
out, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
as.Contains(string(out), fmt.Sprintf("%d files changed", 28))
|
as.Contains(string(out), fmt.Sprintf("%d files changed", 29))
|
||||||
|
|
||||||
// add haskell files to the global exclude
|
// add haskell files to the global exclude
|
||||||
cfg.Global.Excludes = []string{"*.nix", "*.hs"}
|
cfg.Global.Excludes = []string{"*.nix", "*.hs"}
|
||||||
@ -145,7 +145,7 @@ func TestIncludesAndExcludes(t *testing.T) {
|
|||||||
test.WriteConfig(t, configPath, cfg)
|
test.WriteConfig(t, configPath, cfg)
|
||||||
out, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
out, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
as.Contains(string(out), fmt.Sprintf("%d files changed", 22))
|
as.Contains(string(out), fmt.Sprintf("%d files changed", 23))
|
||||||
|
|
||||||
echo := cfg.Formatters["echo"]
|
echo := cfg.Formatters["echo"]
|
||||||
|
|
||||||
@ -155,7 +155,7 @@ func TestIncludesAndExcludes(t *testing.T) {
|
|||||||
test.WriteConfig(t, configPath, cfg)
|
test.WriteConfig(t, configPath, cfg)
|
||||||
out, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
out, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
as.Contains(string(out), fmt.Sprintf("%d files changed", 20))
|
as.Contains(string(out), fmt.Sprintf("%d files changed", 21))
|
||||||
|
|
||||||
// remove go files from the echo formatter
|
// remove go files from the echo formatter
|
||||||
echo.Excludes = []string{"*.py", "*.go"}
|
echo.Excludes = []string{"*.py", "*.go"}
|
||||||
@ -163,7 +163,7 @@ func TestIncludesAndExcludes(t *testing.T) {
|
|||||||
test.WriteConfig(t, configPath, cfg)
|
test.WriteConfig(t, configPath, cfg)
|
||||||
out, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
out, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
as.Contains(string(out), fmt.Sprintf("%d files changed", 19))
|
as.Contains(string(out), fmt.Sprintf("%d files changed", 20))
|
||||||
|
|
||||||
// adjust the includes for echo to only include elm files
|
// adjust the includes for echo to only include elm files
|
||||||
echo.Includes = []string{"*.elm"}
|
echo.Includes = []string{"*.elm"}
|
||||||
@ -189,8 +189,8 @@ func TestCache(t *testing.T) {
|
|||||||
configPath := tempDir + "/echo.toml"
|
configPath := tempDir + "/echo.toml"
|
||||||
|
|
||||||
// test without any excludes
|
// test without any excludes
|
||||||
cfg := config.Config{
|
cfg := config2.Config{
|
||||||
Formatters: map[string]*config.Formatter{
|
Formatters: map[string]*config2.Formatter{
|
||||||
"echo": {
|
"echo": {
|
||||||
Command: "echo",
|
Command: "echo",
|
||||||
Includes: []string{"*"},
|
Includes: []string{"*"},
|
||||||
@ -201,11 +201,34 @@ func TestCache(t *testing.T) {
|
|||||||
test.WriteConfig(t, configPath, cfg)
|
test.WriteConfig(t, configPath, cfg)
|
||||||
out, err := cmd(t, "--config-file", configPath, "--tree-root", tempDir)
|
out, err := cmd(t, "--config-file", configPath, "--tree-root", tempDir)
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
as.Contains(string(out), fmt.Sprintf("%d files changed", 29))
|
as.Contains(string(out), fmt.Sprintf("%d files changed", 30))
|
||||||
|
|
||||||
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir)
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir)
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
as.Contains(string(out), "0 files changed")
|
as.Contains(string(out), "0 files changed")
|
||||||
|
|
||||||
|
// clear cache
|
||||||
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir, "-c")
|
||||||
|
as.NoError(err)
|
||||||
|
as.Contains(string(out), fmt.Sprintf("%d files changed", 30))
|
||||||
|
|
||||||
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir)
|
||||||
|
as.NoError(err)
|
||||||
|
as.Contains(string(out), "0 files changed")
|
||||||
|
|
||||||
|
// clear cache
|
||||||
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir, "-c")
|
||||||
|
as.NoError(err)
|
||||||
|
as.Contains(string(out), fmt.Sprintf("%d files changed", 30))
|
||||||
|
|
||||||
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir)
|
||||||
|
as.NoError(err)
|
||||||
|
as.Contains(string(out), "0 files changed")
|
||||||
|
|
||||||
|
// no cache
|
||||||
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir, "--no-cache")
|
||||||
|
as.NoError(err)
|
||||||
|
as.Contains(string(out), fmt.Sprintf("%d files changed", 30))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestChangeWorkingDirectory(t *testing.T) {
|
func TestChangeWorkingDirectory(t *testing.T) {
|
||||||
@ -224,8 +247,8 @@ func TestChangeWorkingDirectory(t *testing.T) {
|
|||||||
configPath := tempDir + "/treefmt.toml"
|
configPath := tempDir + "/treefmt.toml"
|
||||||
|
|
||||||
// test without any excludes
|
// test without any excludes
|
||||||
cfg := config.Config{
|
cfg := config2.Config{
|
||||||
Formatters: map[string]*config.Formatter{
|
Formatters: map[string]*config2.Formatter{
|
||||||
"echo": {
|
"echo": {
|
||||||
Command: "echo",
|
Command: "echo",
|
||||||
Includes: []string{"*"},
|
Includes: []string{"*"},
|
||||||
@ -239,7 +262,7 @@ func TestChangeWorkingDirectory(t *testing.T) {
|
|||||||
// this should fail if the working directory hasn't been changed first
|
// this should fail if the working directory hasn't been changed first
|
||||||
out, err := cmd(t, "-C", tempDir)
|
out, err := cmd(t, "-C", tempDir)
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
as.Contains(string(out), fmt.Sprintf("%d files changed", 29))
|
as.Contains(string(out), fmt.Sprintf("%d files changed", 30))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFailOnChange(t *testing.T) {
|
func TestFailOnChange(t *testing.T) {
|
||||||
@ -249,8 +272,8 @@ func TestFailOnChange(t *testing.T) {
|
|||||||
configPath := tempDir + "/echo.toml"
|
configPath := tempDir + "/echo.toml"
|
||||||
|
|
||||||
// test without any excludes
|
// test without any excludes
|
||||||
cfg := config.Config{
|
cfg := config2.Config{
|
||||||
Formatters: map[string]*config.Formatter{
|
Formatters: map[string]*config2.Formatter{
|
||||||
"echo": {
|
"echo": {
|
||||||
Command: "echo",
|
Command: "echo",
|
||||||
Includes: []string{"*"},
|
Includes: []string{"*"},
|
||||||
@ -285,8 +308,8 @@ func TestBustCacheOnFormatterChange(t *testing.T) {
|
|||||||
as.NoError(os.Setenv("PATH", binPath+":"+os.Getenv("PATH")))
|
as.NoError(os.Setenv("PATH", binPath+":"+os.Getenv("PATH")))
|
||||||
|
|
||||||
// start with 2 formatters
|
// start with 2 formatters
|
||||||
cfg := config.Config{
|
cfg := config2.Config{
|
||||||
Formatters: map[string]*config.Formatter{
|
Formatters: map[string]*config2.Formatter{
|
||||||
"python": {
|
"python": {
|
||||||
Command: "black",
|
Command: "black",
|
||||||
Includes: []string{"*.py"},
|
Includes: []string{"*.py"},
|
||||||
@ -330,7 +353,7 @@ func TestBustCacheOnFormatterChange(t *testing.T) {
|
|||||||
as.Contains(string(out), "0 files changed")
|
as.Contains(string(out), "0 files changed")
|
||||||
|
|
||||||
// add go formatter
|
// add go formatter
|
||||||
cfg.Formatters["go"] = &config.Formatter{
|
cfg.Formatters["go"] = &config2.Formatter{
|
||||||
Command: "gofmt",
|
Command: "gofmt",
|
||||||
Options: []string{"-w"},
|
Options: []string{"-w"},
|
||||||
Includes: []string{"*.go"},
|
Includes: []string{"*.go"},
|
||||||
@ -380,8 +403,8 @@ func TestGitWorktree(t *testing.T) {
|
|||||||
configPath := filepath.Join(tempDir, "/treefmt.toml")
|
configPath := filepath.Join(tempDir, "/treefmt.toml")
|
||||||
|
|
||||||
// basic config
|
// basic config
|
||||||
cfg := config.Config{
|
cfg := config2.Config{
|
||||||
Formatters: map[string]*config.Formatter{
|
Formatters: map[string]*config2.Formatter{
|
||||||
"echo": {
|
"echo": {
|
||||||
Command: "echo",
|
Command: "echo",
|
||||||
Includes: []string{"*"},
|
Includes: []string{"*"},
|
||||||
@ -416,16 +439,16 @@ func TestGitWorktree(t *testing.T) {
|
|||||||
// add everything to the worktree
|
// add everything to the worktree
|
||||||
as.NoError(wt.AddGlob("."))
|
as.NoError(wt.AddGlob("."))
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
run(29)
|
run(30)
|
||||||
|
|
||||||
// remove python directory
|
// remove python directory
|
||||||
as.NoError(wt.RemoveGlob("python/*"))
|
as.NoError(wt.RemoveGlob("python/*"))
|
||||||
run(26)
|
run(27)
|
||||||
|
|
||||||
// walk with filesystem instead of git
|
// walk with filesystem instead of git
|
||||||
out, err := cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--walk", "filesystem")
|
out, err := cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--walk", "filesystem")
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
as.Contains(string(out), fmt.Sprintf("%d files changed", 55))
|
as.Contains(string(out), fmt.Sprintf("%d files changed", 57))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOrderingFormatters(t *testing.T) {
|
func TestOrderingFormatters(t *testing.T) {
|
||||||
@ -435,8 +458,8 @@ func TestOrderingFormatters(t *testing.T) {
|
|||||||
configPath := path.Join(tempDir, "treefmt.toml")
|
configPath := path.Join(tempDir, "treefmt.toml")
|
||||||
|
|
||||||
// missing child
|
// missing child
|
||||||
test.WriteConfig(t, configPath, config.Config{
|
test.WriteConfig(t, configPath, config2.Config{
|
||||||
Formatters: map[string]*config.Formatter{
|
Formatters: map[string]*config2.Formatter{
|
||||||
"hs-a": {
|
"hs-a": {
|
||||||
Command: "echo",
|
Command: "echo",
|
||||||
Includes: []string{"*.hs"},
|
Includes: []string{"*.hs"},
|
||||||
@ -449,8 +472,8 @@ func TestOrderingFormatters(t *testing.T) {
|
|||||||
as.ErrorContains(err, "formatter hs-a is before hs-b but config for hs-b was not found")
|
as.ErrorContains(err, "formatter hs-a is before hs-b but config for hs-b was not found")
|
||||||
|
|
||||||
// multiple roots
|
// multiple roots
|
||||||
test.WriteConfig(t, configPath, config.Config{
|
test.WriteConfig(t, configPath, config2.Config{
|
||||||
Formatters: map[string]*config.Formatter{
|
Formatters: map[string]*config2.Formatter{
|
||||||
"hs-a": {
|
"hs-a": {
|
||||||
Command: "echo",
|
Command: "echo",
|
||||||
Includes: []string{"*.hs"},
|
Includes: []string{"*.hs"},
|
||||||
@ -501,8 +524,8 @@ func TestPathsArg(t *testing.T) {
|
|||||||
as.NoError(os.Chdir(tempDir))
|
as.NoError(os.Chdir(tempDir))
|
||||||
|
|
||||||
// basic config
|
// basic config
|
||||||
cfg := config.Config{
|
cfg := config2.Config{
|
||||||
Formatters: map[string]*config.Formatter{
|
Formatters: map[string]*config2.Formatter{
|
||||||
"echo": {
|
"echo": {
|
||||||
Command: "echo",
|
Command: "echo",
|
||||||
Includes: []string{"*"},
|
Includes: []string{"*"},
|
||||||
@ -514,7 +537,7 @@ func TestPathsArg(t *testing.T) {
|
|||||||
// without any path args
|
// without any path args
|
||||||
out, err := cmd(t, "-C", tempDir)
|
out, err := cmd(t, "-C", tempDir)
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
as.Contains(string(out), fmt.Sprintf("%d files changed", 29))
|
as.Contains(string(out), fmt.Sprintf("%d files changed", 30))
|
||||||
|
|
||||||
// specify some explicit paths
|
// specify some explicit paths
|
||||||
out, err = cmd(t, "-C", tempDir, "-c", "elm/elm.json", "haskell/Nested/Foo.hs")
|
out, err = cmd(t, "-C", tempDir, "-c", "elm/elm.json", "haskell/Nested/Foo.hs")
|
||||||
@ -545,8 +568,8 @@ func TestStdIn(t *testing.T) {
|
|||||||
as.NoError(os.Chdir(tempDir))
|
as.NoError(os.Chdir(tempDir))
|
||||||
|
|
||||||
// basic config
|
// basic config
|
||||||
cfg := config.Config{
|
cfg := config2.Config{
|
||||||
Formatters: map[string]*config.Formatter{
|
Formatters: map[string]*config2.Formatter{
|
||||||
"echo": {
|
"echo": {
|
||||||
Command: "echo",
|
Command: "echo",
|
||||||
Includes: []string{"*"},
|
Includes: []string{"*"},
|
@ -7,7 +7,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.numtide.com/numtide/treefmt/internal/test"
|
"git.numtide.com/numtide/treefmt/test"
|
||||||
|
|
||||||
"github.com/alecthomas/kong"
|
"github.com/alecthomas/kong"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
@ -9,7 +9,7 @@ import (
|
|||||||
func TestReadConfigFile(t *testing.T) {
|
func TestReadConfigFile(t *testing.T) {
|
||||||
as := require.New(t)
|
as := require.New(t)
|
||||||
|
|
||||||
cfg, err := ReadFile("../../test/treefmt.toml")
|
cfg, err := ReadFile("../test/examples/treefmt.toml")
|
||||||
as.NoError(err, "failed to read config file")
|
as.NoError(err, "failed to read config file")
|
||||||
|
|
||||||
as.NotNil(cfg)
|
as.NotNil(cfg)
|
@ -7,7 +7,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.numtide.com/numtide/treefmt/internal/config"
|
"git.numtide.com/numtide/treefmt/config"
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"github.com/gobwas/glob"
|
"github.com/gobwas/glob"
|
17
main.go
17
main.go
@ -1,11 +1,26 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.numtide.com/numtide/treefmt/internal/cli"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"git.numtide.com/numtide/treefmt/build"
|
||||||
|
"git.numtide.com/numtide/treefmt/cli"
|
||||||
"github.com/alecthomas/kong"
|
"github.com/alecthomas/kong"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// This is to maintain compatibility with 1.0.0 which allows specifying the version with a `treefmt --version` flag
|
||||||
|
// on the 'default' command. With Kong it would be better to have `treefmt version` so it would be treated as a
|
||||||
|
// separate command. As it is, we would need to weaken some of the `existingdir` and `existingfile` checks kong is
|
||||||
|
// doing for us in the default format command.
|
||||||
|
for _, arg := range os.Args {
|
||||||
|
if arg == "--version" || arg == "-V" {
|
||||||
|
fmt.Printf("%s %s\n", build.Name, build.Version)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ctx := kong.Parse(&cli.Cli)
|
ctx := kong.Parse(&cli.Cli)
|
||||||
ctx.FatalIfErrorf(ctx.Run())
|
ctx.FatalIfErrorf(ctx.Run())
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
packages = rec {
|
packages = rec {
|
||||||
treefmt = inputs'.gomod2nix.legacyPackages.buildGoApplication rec {
|
treefmt = inputs'.gomod2nix.legacyPackages.buildGoApplication rec {
|
||||||
pname = "treefmt";
|
pname = "treefmt";
|
||||||
version = "0.0.1+dev";
|
version = "2.0.0+dev";
|
||||||
|
|
||||||
# ensure we are using the same version of go to build with
|
# ensure we are using the same version of go to build with
|
||||||
inherit (pkgs) go;
|
inherit (pkgs) go;
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.numtide.com/numtide/treefmt/internal/config"
|
"git.numtide.com/numtide/treefmt/config"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
cp "github.com/otiai10/copy"
|
cp "github.com/otiai10/copy"
|
||||||
@ -25,7 +25,7 @@ func WriteConfig(t *testing.T, path string, cfg config.Config) {
|
|||||||
|
|
||||||
func TempExamples(t *testing.T) string {
|
func TempExamples(t *testing.T) string {
|
||||||
tempDir := t.TempDir()
|
tempDir := t.TempDir()
|
||||||
require.NoError(t, cp.Copy("../../test/examples", tempDir), "failed to copy test data to temp dir")
|
require.NoError(t, cp.Copy("../test/examples", tempDir), "failed to copy test data to temp dir")
|
||||||
return tempDir
|
return tempDir
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue
Block a user