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

     1  package operations_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/rclone/rclone/fs"
     8  	"github.com/rclone/rclone/fs/filter"
     9  	"github.com/rclone/rclone/fs/list"
    10  	"github.com/rclone/rclone/fstest"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  // TestListDirSorted is integration testing code in fs/list/list.go
    16  // which can't be tested there due to import loops.
    17  func TestListDirSorted(t *testing.T) {
    18  	r := fstest.NewRun(t)
    19  	defer r.Finalise()
    20  
    21  	filter.Active.Opt.MaxSize = 10
    22  	defer func() {
    23  		filter.Active.Opt.MaxSize = -1
    24  	}()
    25  
    26  	files := []fstest.Item{
    27  		r.WriteObject(context.Background(), "a.txt", "hello world", t1),
    28  		r.WriteObject(context.Background(), "zend.txt", "hello", t1),
    29  		r.WriteObject(context.Background(), "sub dir/hello world", "hello world", t1),
    30  		r.WriteObject(context.Background(), "sub dir/hello world2", "hello world", t1),
    31  		r.WriteObject(context.Background(), "sub dir/ignore dir/.ignore", "-", t1),
    32  		r.WriteObject(context.Background(), "sub dir/ignore dir/should be ignored", "to ignore", t1),
    33  		r.WriteObject(context.Background(), "sub dir/sub sub dir/hello world3", "hello world", t1),
    34  	}
    35  	fstest.CheckItems(t, r.Fremote, files...)
    36  	var items fs.DirEntries
    37  	var err error
    38  
    39  	// Turn the DirEntry into a name, ending with a / if it is a
    40  	// dir
    41  	str := func(i int) string {
    42  		item := items[i]
    43  		name := item.Remote()
    44  		switch item.(type) {
    45  		case fs.Object:
    46  		case fs.Directory:
    47  			name += "/"
    48  		default:
    49  			t.Fatalf("Unknown type %+v", item)
    50  		}
    51  		return name
    52  	}
    53  
    54  	items, err = list.DirSorted(context.Background(), r.Fremote, true, "")
    55  	require.NoError(t, err)
    56  	require.Len(t, items, 3)
    57  	assert.Equal(t, "a.txt", str(0))
    58  	assert.Equal(t, "sub dir/", str(1))
    59  	assert.Equal(t, "zend.txt", str(2))
    60  
    61  	items, err = list.DirSorted(context.Background(), r.Fremote, false, "")
    62  	require.NoError(t, err)
    63  	require.Len(t, items, 2)
    64  	assert.Equal(t, "sub dir/", str(0))
    65  	assert.Equal(t, "zend.txt", str(1))
    66  
    67  	items, err = list.DirSorted(context.Background(), r.Fremote, true, "sub dir")
    68  	require.NoError(t, err)
    69  	require.Len(t, items, 4)
    70  	assert.Equal(t, "sub dir/hello world", str(0))
    71  	assert.Equal(t, "sub dir/hello world2", str(1))
    72  	assert.Equal(t, "sub dir/ignore dir/", str(2))
    73  	assert.Equal(t, "sub dir/sub sub dir/", str(3))
    74  
    75  	items, err = list.DirSorted(context.Background(), r.Fremote, false, "sub dir")
    76  	require.NoError(t, err)
    77  	require.Len(t, items, 2)
    78  	assert.Equal(t, "sub dir/ignore dir/", str(0))
    79  	assert.Equal(t, "sub dir/sub sub dir/", str(1))
    80  
    81  	// testing ignore file
    82  	filter.Active.Opt.ExcludeFile = ".ignore"
    83  
    84  	items, err = list.DirSorted(context.Background(), r.Fremote, false, "sub dir")
    85  	require.NoError(t, err)
    86  	require.Len(t, items, 1)
    87  	assert.Equal(t, "sub dir/sub sub dir/", str(0))
    88  
    89  	items, err = list.DirSorted(context.Background(), r.Fremote, false, "sub dir/ignore dir")
    90  	require.NoError(t, err)
    91  	require.Len(t, items, 0)
    92  
    93  	items, err = list.DirSorted(context.Background(), r.Fremote, true, "sub dir/ignore dir")
    94  	require.NoError(t, err)
    95  	require.Len(t, items, 2)
    96  	assert.Equal(t, "sub dir/ignore dir/.ignore", str(0))
    97  	assert.Equal(t, "sub dir/ignore dir/should be ignored", str(1))
    98  
    99  	filter.Active.Opt.ExcludeFile = ""
   100  	items, err = list.DirSorted(context.Background(), r.Fremote, false, "sub dir/ignore dir")
   101  	require.NoError(t, err)
   102  	require.Len(t, items, 2)
   103  	assert.Equal(t, "sub dir/ignore dir/.ignore", str(0))
   104  	assert.Equal(t, "sub dir/ignore dir/should be ignored", str(1))
   105  }