Skip to content

Commit

Permalink
test: add tests for glob matching (#319)
Browse files Browse the repository at this point in the history
This helps demonstrate how our globbing works.
  • Loading branch information
zimbatm authored Jun 14, 2024
1 parent 2510126 commit 6b59125
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions format/glob_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package format_test

import (
"testing"

"git.numtide.com/numtide/treefmt/format"

"github.com/gobwas/glob"
"github.com/stretchr/testify/require"
)

func TestGlobs(t *testing.T) {
r := require.New(t)

var (
globs []glob.Glob
err error
)

// File extension
globs, err = format.CompileGlobs([]string{"*.txt"})
r.NoError(err)
r.True(format.PathMatches("test/foo/bar.txt", globs))
r.False(format.PathMatches("test/foo/bar.txtz", globs))
r.False(format.PathMatches("test/foo/bar.flob", globs))

// Prefix matching
globs, err = format.CompileGlobs([]string{"test/*"})
r.NoError(err)
r.True(format.PathMatches("test/bar.txt", globs))
r.True(format.PathMatches("test/foo/bar.txt", globs))
r.False(format.PathMatches("/test/foo/bar.txt", globs))

// Exact matches
// File extension
globs, err = format.CompileGlobs([]string{"LICENSE"})
r.NoError(err)
r.True(format.PathMatches("LICENSE", globs))
r.False(format.PathMatches("test/LICENSE", globs))
r.False(format.PathMatches("LICENSE.txt", globs))
}
2 changes: 1 addition & 1 deletion nix/treefmt.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
global.excludes = [
"LICENSE"
# let's not mess with the test folder
"test/**"
"test/*"
# unsupported extensions
"*.{gif,png,svg,tape,mts,lock,mod,sum,toml,env,envrc,gitignore}"
];
Expand Down

0 comments on commit 6b59125

Please sign in to comment.