github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/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  
    20  	ctx := context.Background()
    21  	fi := filter.GetConfig(ctx)
    22  	fi.Opt.MaxSize = 10
    23  	defer func() {
    24  		fi.Opt.MaxSize = -1
    25  	}()
    26  
    27  	files := []fstest.Item{
    28  		r.WriteObject(context.Background(), "a.txt", "hello world", t1),
    29  		r.WriteObject(context.Background(), "zend.txt", "hello", t1),
    30  		r.WriteObject(context.Background(), "sub dir/hello world", "hello world", t1),
    31  		r.WriteObject(context.Background(), "sub dir/hello world2", "hello world", t1),
    32  		r.WriteObject(context.Background(), "sub dir/ignore dir/.ignore", "-", t1),
    33  		r.WriteObject(context.Background(), "sub dir/ignore dir/should be ignored", "to ignore", t1),
    34  		r.WriteObject(context.Background(), "sub dir/sub sub dir/hello world3", "hello world", t1),
    35  	}
    36  	r.CheckRemoteItems(t, files...)
    37  	var items fs.DirEntries
    38  	var err error
    39  
    40  	// Turn the DirEntry into a name, ending with a / if it is a
    41  	// dir
    42  	str := func(i int) string {
    43  		item := items[i]
    44  		name := item.Remote()
    45  		switch item.(type) {
    46  		case fs.Object:
    47  		case fs.Directory:
    48  			name += "/"
    49  		default:
    50  			t.Fatalf("Unknown type %+v", item)
    51  		}
    52  		return name
    53  	}
    54  
    55  	items, err = list.DirSorted(context.Background(), r.Fremote, true, "")
    56  	require.NoError(t, err)
    57  	require.Len(t, items, 3)
    58  	assert.Equal(t, "a.txt", str(0))
    59  	assert.Equal(t, "sub dir/", str(1))
    60  	assert.Equal(t, "zend.txt", str(2))
    61  
    62  	items, err = list.DirSorted(context.Background(), r.Fremote, false, "")
    63  	require.NoError(t, err)
    64  	require.Len(t, items, 2)
    65  	assert.Equal(t, "sub dir/", str(0))
    66  	assert.Equal(t, "zend.txt", str(1))
    67  
    68  	items, err = list.DirSorted(context.Background(), r.Fremote, true, "sub dir")
    69  	require.NoError(t, err)
    70  	require.Len(t, items, 4)
    71  	assert.Equal(t, "sub dir/hello world", str(0))
    72  	assert.Equal(t, "sub dir/hello world2", str(1))
    73  	assert.Equal(t, "sub dir/ignore dir/", str(2))
    74  	assert.Equal(t, "sub dir/sub sub dir/", str(3))
    75  
    76  	items, err = list.DirSorted(context.Background(), r.Fremote, false, "sub dir")
    77  	require.NoError(t, err)
    78  	require.Len(t, items, 2)
    79  	assert.Equal(t, "sub dir/ignore dir/", str(0))
    80  	assert.Equal(t, "sub dir/sub sub dir/", str(1))
    81  
    82  	// testing ignore file
    83  	fi.Opt.ExcludeFile = []string{".ignore"}
    84  
    85  	items, err = list.DirSorted(context.Background(), r.Fremote, false, "sub dir")
    86  	require.NoError(t, err)
    87  	require.Len(t, items, 1)
    88  	assert.Equal(t, "sub dir/sub sub dir/", str(0))
    89  
    90  	items, err = list.DirSorted(context.Background(), r.Fremote, false, "sub dir/ignore dir")
    91  	require.NoError(t, err)
    92  	require.Len(t, items, 0)
    93  
    94  	items, err = list.DirSorted(context.Background(), r.Fremote, true, "sub dir/ignore dir")
    95  	require.NoError(t, err)
    96  	require.Len(t, items, 2)
    97  	assert.Equal(t, "sub dir/ignore dir/.ignore", str(0))
    98  	assert.Equal(t, "sub dir/ignore dir/should be ignored", str(1))
    99  
   100  	fi.Opt.ExcludeFile = nil
   101  	items, err = list.DirSorted(context.Background(), r.Fremote, false, "sub dir/ignore dir")
   102  	require.NoError(t, err)
   103  	require.Len(t, items, 2)
   104  	assert.Equal(t, "sub dir/ignore dir/.ignore", str(0))
   105  	assert.Equal(t, "sub dir/ignore dir/should be ignored", str(1))
   106  }