github.com/tonkpils/snag@v1.2.1-0.20160221223445-7f8829737a1d/gitglob_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func tempLoc() string {
    13  	return filepath.Join(os.TempDir(), "snag/test/stuff")
    14  }
    15  
    16  func createFiles(t *testing.T, names []string) {
    17  	for _, name := range names {
    18  		f, err := os.Create(filepath.Join(tempLoc(), name))
    19  		require.NoError(t, err, "Error creating temp file %s", err)
    20  		f.Close()
    21  	}
    22  }
    23  
    24  func deleteFiles(t *testing.T, names []string) {
    25  	for _, name := range names {
    26  		err := os.Remove(filepath.Join(tempLoc(), name))
    27  		require.NoError(t, err, "Error removing temp file %s", err)
    28  	}
    29  }
    30  
    31  func TestGlobMatch(t *testing.T) {
    32  	v := tempLoc()
    33  	require.NoError(t, os.MkdirAll(v, 0777))
    34  	defer os.RemoveAll(os.TempDir() + "/snag")
    35  
    36  	// blank line
    37  	assert.False(t, globMatch("", v))
    38  
    39  	// a comment
    40  	assert.False(t, globMatch("#a comment", v))
    41  
    42  	// regular match no slash
    43  	assert.True(t, globMatch("gitglob.go", "gitglob.go"))
    44  
    45  	// negation no slash
    46  	assert.False(t, globMatch("!gitglob.go", "gitglob.go"))
    47  
    48  	// match with slash
    49  	tmpFiles := []string{"foo.txt"}
    50  	createFiles(t, tmpFiles)
    51  	assert.True(t, globMatch(tempLoc()+"/foo.txt", v+"/foo.txt"))
    52  	deleteFiles(t, tmpFiles)
    53  
    54  	// negate match with slash
    55  	tmpFiles = []string{"foo.txt"}
    56  	createFiles(t, tmpFiles)
    57  	assert.False(t, globMatch("!"+tempLoc()+"/foo.txt", v+"/foo.txt"))
    58  	deleteFiles(t, tmpFiles)
    59  
    60  	// directory
    61  	assert.True(t, globMatch(tempLoc(), v))
    62  
    63  	// directory with trailing slash
    64  	assert.True(t, globMatch(tempLoc()+"/", v))
    65  
    66  	// star matching
    67  	tmpFiles = []string{"foo.txt"}
    68  	createFiles(t, tmpFiles)
    69  	assert.True(t, globMatch(tempLoc()+"/*.txt", v+"/foo.txt"))
    70  	assert.False(t, globMatch(tempLoc()+"/*.txt", v+"/somedir/foo.txt"))
    71  	deleteFiles(t, tmpFiles)
    72  
    73  	// double star prefix
    74  	assert.True(t, globMatch("**/foo.txt", v+"/hello/foo.txt"))
    75  	assert.True(t, globMatch("**/foo.txt", v+"/some/dirs/foo.txt"))
    76  
    77  	// double star suffix
    78  	assert.True(t, globMatch(tempLoc()+"/hello/**", v+"/hello/foo.txt"))
    79  	assert.False(t, globMatch(tempLoc()+"/hello/**", v+"/some/dirs/foo.txt"))
    80  
    81  	// double star in path
    82  	assert.True(t, globMatch(tempLoc()+"/hello/**/world.txt", v+"/hello/world.txt"))
    83  	assert.True(t, globMatch(tempLoc()+"/hello/**/world.txt", v+"/hello/stuff/world.txt"))
    84  	assert.False(t, globMatch(tempLoc()+"/hello/**/world.txt", v+"/some/dirs/foo.txt"))
    85  
    86  	// negate doubl start patterns
    87  	assert.False(t, globMatch("!**/foo.txt", v+"/hello/foo.txt"))
    88  	assert.False(t, globMatch("!"+tempLoc()+"/hello/**", v+"/hello/foo.txt"))
    89  	assert.False(t, globMatch("!"+tempLoc()+"/hello/**/world.txt", v+"/hello/world.txt"))
    90  }