test: add tests for glob matching (#319)

This helps demonstrate how our globbing works.
This commit is contained in:
Jonas Chevalier 2024-06-14 12:57:51 +02:00 committed by GitHub
parent 251012657a
commit 6b591255b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 1 deletions

41
format/glob_test.go Normal file
View File

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

View File

@ -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}"
];