github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/fs/filter/glob_test.go (about)

     1  package filter
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestGlobToRegexp(t *testing.T) {
    11  	for _, test := range []struct {
    12  		in    string
    13  		want  string
    14  		error string
    15  	}{
    16  		{``, `(^|/)$`, ``},
    17  		{`potato`, `(^|/)potato$`, ``},
    18  		{`potato,sausage`, `(^|/)potato,sausage$`, ``},
    19  		{`/potato`, `^potato$`, ``},
    20  		{`potato?sausage`, `(^|/)potato[^/]sausage$`, ``},
    21  		{`potat[oa]`, `(^|/)potat[oa]$`, ``},
    22  		{`potat[a-z]or`, `(^|/)potat[a-z]or$`, ``},
    23  		{`potat[[:alpha:]]or`, `(^|/)potat[[:alpha:]]or$`, ``},
    24  		{`'.' '+' '(' ')' '|' '^' '$'`, `(^|/)'\.' '\+' '\(' '\)' '\|' '\^' '\$'$`, ``},
    25  		{`*.jpg`, `(^|/)[^/]*\.jpg$`, ``},
    26  		{`a{b,c,d}e`, `(^|/)a(b|c|d)e$`, ``},
    27  		{`potato**`, `(^|/)potato.*$`, ``},
    28  		{`potato**sausage`, `(^|/)potato.*sausage$`, ``},
    29  		{`*.p[lm]`, `(^|/)[^/]*\.p[lm]$`, ``},
    30  		{`[\[\]]`, `(^|/)[\[\]]$`, ``},
    31  		{`***potato`, `(^|/)`, `too many stars`},
    32  		{`***`, `(^|/)`, `too many stars`},
    33  		{`ab]c`, `(^|/)`, `mismatched ']'`},
    34  		{`ab[c`, `(^|/)`, `mismatched '[' and ']'`},
    35  		{`ab{{cd`, `(^|/)`, `can't nest`},
    36  		{`ab{}}cd`, `(^|/)`, `mismatched '{' and '}'`},
    37  		{`ab}c`, `(^|/)`, `mismatched '{' and '}'`},
    38  		{`ab{c`, `(^|/)`, `mismatched '{' and '}'`},
    39  		{`*.{jpg,png,gif}`, `(^|/)[^/]*\.(jpg|png|gif)$`, ``},
    40  		{`[a--b]`, `(^|/)`, `bad glob pattern`},
    41  		{`a\*b`, `(^|/)a\*b$`, ``},
    42  		{`a\\b`, `(^|/)a\\b$`, ``},
    43  	} {
    44  		for _, ignoreCase := range []bool{false, true} {
    45  			gotRe, err := globToRegexp(test.in, ignoreCase)
    46  			if test.error == "" {
    47  				prefix := ""
    48  				if ignoreCase {
    49  					prefix = "(?i)"
    50  				}
    51  				got := gotRe.String()
    52  				require.NoError(t, err, test.in)
    53  				assert.Equal(t, prefix+test.want, got, test.in)
    54  			} else {
    55  				require.Error(t, err, test.in)
    56  				assert.Contains(t, err.Error(), test.error, test.in)
    57  				assert.Nil(t, gotRe)
    58  			}
    59  		}
    60  	}
    61  }
    62  
    63  func TestGlobToDirGlobs(t *testing.T) {
    64  	for _, test := range []struct {
    65  		in   string
    66  		want []string
    67  	}{
    68  		{`*`, []string{"/**"}},
    69  		{`/*`, []string{"/"}},
    70  		{`*.jpg`, []string{"/**"}},
    71  		{`/*.jpg`, []string{"/"}},
    72  		{`//*.jpg`, []string{"/"}},
    73  		{`///*.jpg`, []string{"/"}},
    74  		{`/a/*.jpg`, []string{"/a/", "/"}},
    75  		{`/a//*.jpg`, []string{"/a/", "/"}},
    76  		{`/a///*.jpg`, []string{"/a/", "/"}},
    77  		{`/a/b/*.jpg`, []string{"/a/b/", "/a/", "/"}},
    78  		{`a/*.jpg`, []string{"a/"}},
    79  		{`a/b/*.jpg`, []string{"a/b/", "a/"}},
    80  		{`*/*/*.jpg`, []string{"*/*/", "*/"}},
    81  		{`a/b/`, []string{"a/b/", "a/"}},
    82  		{`a/b`, []string{"a/"}},
    83  		{`a/b/*.{jpg,png,gif}`, []string{"a/b/", "a/"}},
    84  		{`/a/{jpg,png,gif}/*.{jpg,png,gif}`, []string{"/a/{jpg,png,gif}/", "/a/", "/"}},
    85  		{`a/{a,a*b,a**c}/d/`, []string{"/**"}},
    86  		{`/a/{a,a*b,a/c,d}/d/`, []string{"/**"}},
    87  		{`**`, []string{"**/"}},
    88  		{`a**`, []string{"a**/"}},
    89  		{`a**b`, []string{"a**/"}},
    90  		{`a**b**c**d`, []string{"a**b**c**/", "a**b**/", "a**/"}},
    91  		{`a**b/c**d`, []string{"a**b/c**/", "a**b/", "a**/"}},
    92  		{`/A/a**b/B/c**d/C/`, []string{"/A/a**b/B/c**d/C/", "/A/a**b/B/c**d/", "/A/a**b/B/c**/", "/A/a**b/B/", "/A/a**b/", "/A/a**/", "/A/", "/"}},
    93  		{`/var/spool/**/ncw`, []string{"/var/spool/**/", "/var/spool/", "/var/", "/"}},
    94  		{`var/spool/**/ncw/`, []string{"var/spool/**/ncw/", "var/spool/**/", "var/spool/", "var/"}},
    95  		{"/file1.jpg", []string{`/`}},
    96  		{"/file2.png", []string{`/`}},
    97  		{"/*.jpg", []string{`/`}},
    98  		{"/*.png", []string{`/`}},
    99  		{"/potato", []string{`/`}},
   100  		{"/sausage1", []string{`/`}},
   101  		{"/sausage2*", []string{`/`}},
   102  		{"/sausage3**", []string{`/sausage3**/`, "/"}},
   103  		{"/a/*.jpg", []string{`/a/`, "/"}},
   104  	} {
   105  		_, err := globToRegexp(test.in, false)
   106  		assert.NoError(t, err)
   107  		got := globToDirGlobs(test.in)
   108  		assert.Equal(t, test.want, got, test.in)
   109  	}
   110  }