github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/fs/march/march_test.go (about)

     1  // Internal tests for march
     2  
     3  package march
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/ncw/rclone/fs"
    11  	"github.com/ncw/rclone/fstest/mockdir"
    12  	"github.com/ncw/rclone/fstest/mockobject"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestNewMatchEntries(t *testing.T) {
    17  	var (
    18  		a = mockobject.Object("path/a")
    19  		A = mockobject.Object("path/A")
    20  		B = mockobject.Object("path/B")
    21  		c = mockobject.Object("path/c")
    22  	)
    23  
    24  	es := newMatchEntries(fs.DirEntries{a, A, B, c}, nil)
    25  	assert.Equal(t, es, matchEntries{
    26  		{name: "A", leaf: "A", entry: A},
    27  		{name: "B", leaf: "B", entry: B},
    28  		{name: "a", leaf: "a", entry: a},
    29  		{name: "c", leaf: "c", entry: c},
    30  	})
    31  
    32  	es = newMatchEntries(fs.DirEntries{a, A, B, c}, []matchTransformFn{strings.ToLower})
    33  	assert.Equal(t, es, matchEntries{
    34  		{name: "a", leaf: "A", entry: A},
    35  		{name: "a", leaf: "a", entry: a},
    36  		{name: "b", leaf: "B", entry: B},
    37  		{name: "c", leaf: "c", entry: c},
    38  	})
    39  }
    40  
    41  func TestMatchListings(t *testing.T) {
    42  	var (
    43  		a    = mockobject.Object("a")
    44  		A    = mockobject.Object("A")
    45  		b    = mockobject.Object("b")
    46  		c    = mockobject.Object("c")
    47  		d    = mockobject.Object("d")
    48  		dirA = mockdir.New("A")
    49  		dirb = mockdir.New("b")
    50  	)
    51  
    52  	for _, test := range []struct {
    53  		what       string
    54  		input      fs.DirEntries // pairs of input src, dst
    55  		srcOnly    fs.DirEntries
    56  		dstOnly    fs.DirEntries
    57  		matches    []matchPair // pairs of output
    58  		transforms []matchTransformFn
    59  	}{
    60  		{
    61  			what: "only src or dst",
    62  			input: fs.DirEntries{
    63  				a, nil,
    64  				b, nil,
    65  				c, nil,
    66  				d, nil,
    67  			},
    68  			srcOnly: fs.DirEntries{
    69  				a, b, c, d,
    70  			},
    71  		},
    72  		{
    73  			what: "typical sync #1",
    74  			input: fs.DirEntries{
    75  				a, nil,
    76  				b, b,
    77  				nil, c,
    78  				nil, d,
    79  			},
    80  			srcOnly: fs.DirEntries{
    81  				a,
    82  			},
    83  			dstOnly: fs.DirEntries{
    84  				c, d,
    85  			},
    86  			matches: []matchPair{
    87  				{b, b},
    88  			},
    89  		},
    90  		{
    91  			what: "typical sync #2",
    92  			input: fs.DirEntries{
    93  				a, a,
    94  				b, b,
    95  				nil, c,
    96  				d, d,
    97  			},
    98  			dstOnly: fs.DirEntries{
    99  				c,
   100  			},
   101  			matches: []matchPair{
   102  				{a, a},
   103  				{b, b},
   104  				{d, d},
   105  			},
   106  		},
   107  		{
   108  			what: "One duplicate",
   109  			input: fs.DirEntries{
   110  				A, A,
   111  				a, a,
   112  				a, nil,
   113  				b, b,
   114  			},
   115  			matches: []matchPair{
   116  				{A, A},
   117  				{a, a},
   118  				{b, b},
   119  			},
   120  		},
   121  		{
   122  			what: "Two duplicates",
   123  			input: fs.DirEntries{
   124  				a, a,
   125  				a, a,
   126  				a, nil,
   127  			},
   128  			matches: []matchPair{
   129  				{a, a},
   130  			},
   131  		},
   132  		{
   133  			what: "Case insensitive duplicate - no transform",
   134  			input: fs.DirEntries{
   135  				a, a,
   136  				A, A,
   137  			},
   138  			matches: []matchPair{
   139  				{A, A},
   140  				{a, a},
   141  			},
   142  		},
   143  		{
   144  			what: "Case insensitive duplicate - transform to lower case",
   145  			input: fs.DirEntries{
   146  				a, a,
   147  				A, A,
   148  			},
   149  			matches: []matchPair{
   150  				{A, A},
   151  			},
   152  			transforms: []matchTransformFn{strings.ToLower},
   153  		},
   154  		{
   155  			what: "File and directory are not duplicates - srcOnly",
   156  			input: fs.DirEntries{
   157  				dirA, nil,
   158  				A, nil,
   159  			},
   160  			srcOnly: fs.DirEntries{
   161  				dirA,
   162  				A,
   163  			},
   164  		},
   165  		{
   166  			what: "File and directory are not duplicates - matches",
   167  			input: fs.DirEntries{
   168  				dirA, dirA,
   169  				A, A,
   170  			},
   171  			matches: []matchPair{
   172  				{dirA, dirA},
   173  				{A, A},
   174  			},
   175  		},
   176  		{
   177  			what: "Sync with directory #1",
   178  			input: fs.DirEntries{
   179  				dirA, nil,
   180  				A, nil,
   181  				b, b,
   182  				nil, c,
   183  				nil, d,
   184  			},
   185  			srcOnly: fs.DirEntries{
   186  				dirA,
   187  				A,
   188  			},
   189  			dstOnly: fs.DirEntries{
   190  				c, d,
   191  			},
   192  			matches: []matchPair{
   193  				{b, b},
   194  			},
   195  		},
   196  		{
   197  			what: "Sync with 2 directories",
   198  			input: fs.DirEntries{
   199  				dirA, dirA,
   200  				A, nil,
   201  				nil, dirb,
   202  				nil, b,
   203  			},
   204  			srcOnly: fs.DirEntries{
   205  				A,
   206  			},
   207  			dstOnly: fs.DirEntries{
   208  				dirb,
   209  				b,
   210  			},
   211  			matches: []matchPair{
   212  				{dirA, dirA},
   213  			},
   214  		},
   215  	} {
   216  		t.Run(fmt.Sprintf("TestMatchListings-%s", test.what), func(t *testing.T) {
   217  			var srcList, dstList fs.DirEntries
   218  			for i := 0; i < len(test.input); i += 2 {
   219  				src, dst := test.input[i], test.input[i+1]
   220  				if src != nil {
   221  					srcList = append(srcList, src)
   222  				}
   223  				if dst != nil {
   224  					dstList = append(dstList, dst)
   225  				}
   226  			}
   227  			srcOnly, dstOnly, matches := matchListings(srcList, dstList, test.transforms)
   228  			assert.Equal(t, test.srcOnly, srcOnly, test.what, "srcOnly differ")
   229  			assert.Equal(t, test.dstOnly, dstOnly, test.what, "dstOnly differ")
   230  			assert.Equal(t, test.matches, matches, test.what, "matches differ")
   231  			// now swap src and dst
   232  			dstOnly, srcOnly, matches = matchListings(dstList, srcList, test.transforms)
   233  			assert.Equal(t, test.srcOnly, srcOnly, test.what, "srcOnly differ")
   234  			assert.Equal(t, test.dstOnly, dstOnly, test.what, "dstOnly differ")
   235  			assert.Equal(t, test.matches, matches, test.what, "matches differ")
   236  		})
   237  	}
   238  }