Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
Brian McGee 2024-05-01 19:28:01 +01:00
parent 12e795acf1
commit efdfe9e808
Signed by: brianmcgee
GPG Key ID: D49016E76AD1E8C0
3 changed files with 26 additions and 37 deletions

8
cache/cache.go vendored
View File

@ -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 {

View File

@ -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
}

View File

@ -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)