From efdfe9e8081fad7ff88d8416a5c9635a586cc824 Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Wed, 1 May 2024 19:28:01 +0100 Subject: [PATCH] wip Signed-off-by: Brian McGee --- cache/cache.go | 8 +++----- cli/format.go | 14 +++----------- cli/format_test.go | 41 ++++++++++++++++++++--------------------- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index 39d3579..2b5e2e4 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -260,19 +260,17 @@ func ChangeSet(ctx context.Context, walker walk.Walker, filesCh chan<- *walk.Fil } // Update is used to record updated cache information for the specified list of paths. -func Update(files []*walk.File) (int, error) { +func Update(files []*walk.File) error { start := time.Now() defer func() { logger.Infof("finished processing %v paths in %v", len(files), time.Since(start)) }() if len(files) == 0 { - return 0, nil + return nil } - var changes int - - return changes, db.Update(func(tx *bolt.Tx) error { + return db.Update(func(tx *bolt.Tx) error { bucket := tx.Bucket([]byte(pathsBucket)) for _, f := range files { diff --git a/cli/format.go b/cli/format.go index d2773da..c638a2e 100644 --- a/cli/format.go +++ b/cli/format.go @@ -214,17 +214,9 @@ func updateCache(ctx context.Context) func() error { return func() error { batch := make([]*walk.File, 0, BatchSize) - 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 + if err := cache.Update(batch); err != nil { + return err } batch = batch[:0] return nil @@ -253,7 +245,7 @@ func updateCache(ctx context.Context) func() error { return err } - if Cli.FailOnChange && changes != 0 { + if Cli.FailOnChange && stats.Value(stats.Formatted) != 0 { return ErrFailOnChange } diff --git a/cli/format_test.go b/cli/format_test.go index aeabb92..8399f40 100644 --- a/cli/format_test.go +++ b/cli/format_test.go @@ -245,27 +245,26 @@ func TestChangeWorkingDirectory(t *testing.T) { assertStats(t, as, out, 31, 31, 31, 0) } -// -//func TestFailOnChange(t *testing.T) { -// as := require.New(t) -// -// tempDir := test.TempExamples(t) -// configPath := tempDir + "/echo.toml" -// -// // test without any excludes -// cfg := config2.Config{ -// Formatters: map[string]*config2.Formatter{ -// "echo": { -// Command: "echo", -// Includes: []string{"*"}, -// }, -// }, -// } -// -// test.WriteConfig(t, configPath, cfg) -// _, err := cmd(t, "--fail-on-change", "--config-file", configPath, "--tree-root", tempDir) -// as.ErrorIs(err, ErrFailOnChange) -//} +func TestFailOnChange(t *testing.T) { + as := require.New(t) + + tempDir := test.TempExamples(t) + configPath := tempDir + "/echo.toml" + + // test without any excludes + cfg := config2.Config{ + Formatters: map[string]*config2.Formatter{ + "touch": { + Command: "touch", + Includes: []string{"*"}, + }, + }, + } + + test.WriteConfig(t, configPath, cfg) + _, err := cmd(t, "--fail-on-change", "--config-file", configPath, "--tree-root", tempDir) + as.ErrorIs(err, ErrFailOnChange) +} func TestBustCacheOnFormatterChange(t *testing.T) { as := require.New(t)