Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/git changed paths only #349

Closed
wants to merge 3 commits into from
Closed
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
363 changes: 133 additions & 230 deletions cache/cache.go

Large diffs are not rendered by default.

166 changes: 166 additions & 0 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package cache

import (
"os"
"strings"
"testing"
"time"

"github.com/adrg/xdg"
"github.com/stretchr/testify/require"
)

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

tempDir := t.TempDir()
xdgPrefix, err := xdg.CacheFile("")
as.NoError(err)

// normal open
cache, err := Open(tempDir, false)
path := cache.db.Path()

as.NoError(err)
as.True(
strings.HasPrefix(path, xdgPrefix),
"db path %s does not contain the xdg cache file prefix %s",
path, xdgPrefix,
)

// normal close
as.NoError(cache.Close())
_, err = os.Stat(path)
as.NoError(err, "db path %s should still exist after closing the cache", path)

// open a temp cache e.g. --no-cache
tempDir = t.TempDir()
cache, err = Open(tempDir, true)
as.NoError(err)

// close temp cache
as.NoError(cache.Close())
_, err = os.Stat(cache.db.Path())
as.ErrorIs(err, os.ErrNotExist, "temp db path %s should not exist after closing the cache")
}

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

cache, err := Open(t.TempDir(), false)
as.NoError(err)

now := time.Now()

testData := map[string]map[string]*Entry{
"paths": {
"foo": {Size: 0, Modified: now},
"bar": {Size: 1, Modified: now.Add(-1 * time.Second)},
"fizz/buzz": {Size: 1 << 16, Modified: now.Add(-1 * time.Minute)},
},
"formatters": {
"bla": {Size: 1 << 32, Modified: now.Add(-1 * time.Hour)},
"foo/bar/baz": {Size: 1 << 24, Modified: now.Add(-24 * time.Hour)},
},
}

putEntries := func(bucket *Bucket[Entry], err error) func(string) {
return func(name string) {
as.NoError(err)
for k, v := range testData[name] {
as.NoError(bucket.Put(k, v), "failed to put value")
}
}
}

getEntries := func(bucket *Bucket[Entry], err error) func(string) {
return func(name string) {
as.NoError(err)
as.Equal(len(testData[name]), bucket.Size())
for k, v := range testData[name] {
actual, err := bucket.Get(k)
as.NoError(err)
as.EqualExportedValues(*v, *actual)
}
}
}

modifyEntries := func(bucket *Bucket[Entry], err error) func(string) {
return func(name string) {
entries := testData[name]
idx := 0
for k := range entries {
switch idx {
case 0:
// delete the first entry
as.NoError(bucket.Delete(k))
delete(entries, k)
case 1:
// change the second
entries[k] = &Entry{Size: 123, Modified: now.Add(-2 * time.Hour)}
as.NoError(bucket.Put(k, entries[k]))
default:
break
}
}
}
}

clearEntries := func(bucket *Bucket[Entry], err error) {
as.NoError(err)
as.NoError(bucket.DeleteAll())
}

checkEmpty := func(bucket *Bucket[Entry], err error) {
as.NoError(err)
as.Equal(0, bucket.Size())
}

// insert the test data into the cache
err = cache.Update(func(tx *Tx) error {
putEntries(tx.Paths())("paths")
putEntries(tx.Formatters())("formatters")
return nil
})
as.NoError(err)

// read it back and check it matches
err = cache.View(func(tx *Tx) error {
getEntries(tx.Paths())("paths")
getEntries(tx.Formatters())("formatters")
return nil
})
as.NoError(err)

// delete and update some entries
err = cache.Update(func(tx *Tx) error {
modifyEntries(tx.Paths())
modifyEntries(tx.Formatters())
return nil
})
as.NoError(err)

// read them back and make sure they match the updated test data
err = cache.View(func(tx *Tx) error {
getEntries(tx.Paths())("paths")
getEntries(tx.Formatters())("formatters")
return nil
})
as.NoError(err)

// delete all
err = cache.Update(func(tx *Tx) error {
clearEntries(tx.Paths())
clearEntries(tx.Formatters())
return nil
})
as.NoError(err)

// check the cache is empty
err = cache.Update(func(tx *Tx) error {
checkEmpty(tx.Paths())
checkEmpty(tx.Formatters())
return nil
})
as.NoError(err)
}
21 changes: 13 additions & 8 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package cli

import (
"git.numtide.com/numtide/treefmt/cache"
"os"

"github.com/gobwas/glob"

"git.numtide.com/numtide/treefmt/format"
"git.numtide.com/numtide/treefmt/walk"
"git.numtide.com/numtide/treefmt/walker"
"github.com/alecthomas/kong"
"github.com/charmbracelet/log"
)
Expand All @@ -25,10 +26,11 @@ type Format struct {
Formatters []string `short:"f" help:"Specify formatters to apply. Defaults to all formatters."`
TreeRoot string `type:"existingdir" xor:"tree-root" help:"The root directory from which treefmt will start walking the filesystem (defaults to the directory containing the config file)."`
TreeRootFile string `type:"string" xor:"tree-root" help:"File to search for to find the project root (if --tree-root is not passed)."`
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."`
Version bool `name:"version" short:"V" help:"Print version."`
Init bool `name:"init" short:"i" help:"Create a new treefmt.toml."`
Walk walker.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."`
Version bool `name:"version" short:"V" help:"Print version."`
Init bool `name:"init" short:"i" help:"Create a new treefmt.toml."`

OnUnmatched log.Level `name:"on-unmatched" short:"u" default:"warn" help:"Log paths that did not match any formatters at the specified log level, with fatal exiting the process with an error. Possible values are <debug|info|warn|error|fatal>."`

Expand All @@ -40,9 +42,12 @@ type Format struct {
formatters map[string]*format.Formatter
globalExcludes []glob.Glob

filesCh chan *walk.File
formattedCh chan *walk.File
processedCh chan *walk.File
cache *cache.Cache

walker walker.Walker
fileCh chan *walker.File
formattedCh chan *walker.File
processedCh chan *walker.File
}

func (f *Format) configureLogging() {
Expand Down
Loading