github.com/artpar/rclone@v1.67.3/backend/alias/alias_internal_test.go (about)

     1  package alias
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"path"
     7  	"path/filepath"
     8  	"sort"
     9  	"testing"
    10  
    11  	_ "github.com/artpar/rclone/backend/local" // pull in test backend
    12  	"github.com/artpar/rclone/fs"
    13  	"github.com/artpar/rclone/fs/config"
    14  	"github.com/artpar/rclone/fs/config/configfile"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  var (
    19  	remoteName = "TestAlias"
    20  )
    21  
    22  func prepare(t *testing.T, root string) {
    23  	configfile.Install()
    24  
    25  	// Configure the remote
    26  	config.FileSet(remoteName, "type", "alias")
    27  	config.FileSet(remoteName, "remote", root)
    28  }
    29  
    30  func TestNewFS(t *testing.T) {
    31  	type testEntry struct {
    32  		remote string
    33  		size   int64
    34  		isDir  bool
    35  	}
    36  	for testi, test := range []struct {
    37  		remoteRoot string
    38  		fsRoot     string
    39  		fsList     string
    40  		wantOK     bool
    41  		entries    []testEntry
    42  	}{
    43  		{"", "", "", true, []testEntry{
    44  			{"four", -1, true},
    45  			{"one%.txt", 6, false},
    46  			{"three", -1, true},
    47  			{"two.html", 7, false},
    48  		}},
    49  		{"", "four", "", true, []testEntry{
    50  			{"five", -1, true},
    51  			{"under four.txt", 9, false},
    52  		}},
    53  		{"", "", "four", true, []testEntry{
    54  			{"four/five", -1, true},
    55  			{"four/under four.txt", 9, false},
    56  		}},
    57  		{"four", "..", "", true, []testEntry{
    58  			{"five", -1, true},
    59  			{"under four.txt", 9, false},
    60  		}},
    61  		{"", "../../three", "", true, []testEntry{
    62  			{"underthree.txt", 9, false},
    63  		}},
    64  		{"four", "../../five", "", true, []testEntry{
    65  			{"underfive.txt", 6, false},
    66  		}},
    67  	} {
    68  		what := fmt.Sprintf("test %d remoteRoot=%q, fsRoot=%q, fsList=%q", testi, test.remoteRoot, test.fsRoot, test.fsList)
    69  
    70  		remoteRoot, err := filepath.Abs(filepath.FromSlash(path.Join("test/files", test.remoteRoot)))
    71  		require.NoError(t, err, what)
    72  		prepare(t, remoteRoot)
    73  		f, err := fs.NewFs(context.Background(), fmt.Sprintf("%s:%s", remoteName, test.fsRoot))
    74  		require.NoError(t, err, what)
    75  		gotEntries, err := f.List(context.Background(), test.fsList)
    76  		require.NoError(t, err, what)
    77  
    78  		sort.Sort(gotEntries)
    79  
    80  		require.Equal(t, len(test.entries), len(gotEntries), what)
    81  		for i, gotEntry := range gotEntries {
    82  			what := fmt.Sprintf("%s, entry=%d", what, i)
    83  			wantEntry := test.entries[i]
    84  			_, isDir := gotEntry.(fs.Directory)
    85  
    86  			require.Equal(t, wantEntry.remote, gotEntry.Remote(), what)
    87  			if !isDir {
    88  				require.Equal(t, wantEntry.size, gotEntry.Size(), what)
    89  			}
    90  			require.Equal(t, wantEntry.isDir, isDir, what)
    91  		}
    92  	}
    93  }
    94  
    95  func TestNewFSNoRemote(t *testing.T) {
    96  	prepare(t, "")
    97  	f, err := fs.NewFs(context.Background(), fmt.Sprintf("%s:", remoteName))
    98  
    99  	require.Error(t, err)
   100  	require.Nil(t, f)
   101  }
   102  
   103  func TestNewFSInvalidRemote(t *testing.T) {
   104  	prepare(t, "not_existing_test_remote:")
   105  	f, err := fs.NewFs(context.Background(), fmt.Sprintf("%s:", remoteName))
   106  
   107  	require.Error(t, err)
   108  	require.Nil(t, f)
   109  }