github.com/databricks/cli@v0.203.0/libs/filer/workspace_root_path_test.go (about)

     1  package filer
     2  
     3  import (
     4  	"path"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func testRootPath(t *testing.T, uncleanRoot string) {
    11  	cleanRoot := path.Clean(uncleanRoot)
    12  	rp := NewWorkspaceRootPath(uncleanRoot)
    13  
    14  	remotePath, err := rp.Join("a/b/c")
    15  	assert.NoError(t, err)
    16  	assert.Equal(t, cleanRoot+"/a/b/c", remotePath)
    17  
    18  	remotePath, err = rp.Join("a/b/../d")
    19  	assert.NoError(t, err)
    20  	assert.Equal(t, cleanRoot+"/a/d", remotePath)
    21  
    22  	remotePath, err = rp.Join("a/../c")
    23  	assert.NoError(t, err)
    24  	assert.Equal(t, cleanRoot+"/c", remotePath)
    25  
    26  	remotePath, err = rp.Join("a/b/c/.")
    27  	assert.NoError(t, err)
    28  	assert.Equal(t, cleanRoot+"/a/b/c", remotePath)
    29  
    30  	remotePath, err = rp.Join("a/b/c/d/./../../f/g")
    31  	assert.NoError(t, err)
    32  	assert.Equal(t, cleanRoot+"/a/b/f/g", remotePath)
    33  
    34  	remotePath, err = rp.Join(".//a/..//./b/..")
    35  	assert.NoError(t, err)
    36  	assert.Equal(t, cleanRoot, remotePath)
    37  
    38  	remotePath, err = rp.Join("a/b/../..")
    39  	assert.NoError(t, err)
    40  	assert.Equal(t, cleanRoot, remotePath)
    41  
    42  	remotePath, err = rp.Join("")
    43  	assert.NoError(t, err)
    44  	assert.Equal(t, cleanRoot, remotePath)
    45  
    46  	remotePath, err = rp.Join(".")
    47  	assert.NoError(t, err)
    48  	assert.Equal(t, cleanRoot, remotePath)
    49  
    50  	remotePath, err = rp.Join("/")
    51  	assert.NoError(t, err)
    52  	assert.Equal(t, cleanRoot, remotePath)
    53  
    54  	_, err = rp.Join("..")
    55  	assert.ErrorContains(t, err, `relative path escapes root: ..`)
    56  
    57  	_, err = rp.Join("a/../..")
    58  	assert.ErrorContains(t, err, `relative path escapes root: a/../..`)
    59  
    60  	_, err = rp.Join("./../.")
    61  	assert.ErrorContains(t, err, `relative path escapes root: ./../.`)
    62  
    63  	_, err = rp.Join("/./.././..")
    64  	assert.ErrorContains(t, err, `relative path escapes root: /./.././..`)
    65  
    66  	_, err = rp.Join("./../.")
    67  	assert.ErrorContains(t, err, `relative path escapes root: ./../.`)
    68  
    69  	_, err = rp.Join("./..")
    70  	assert.ErrorContains(t, err, `relative path escapes root: ./..`)
    71  
    72  	_, err = rp.Join("./../../..")
    73  	assert.ErrorContains(t, err, `relative path escapes root: ./../../..`)
    74  
    75  	_, err = rp.Join("./../a/./b../../..")
    76  	assert.ErrorContains(t, err, `relative path escapes root: ./../a/./b../../..`)
    77  
    78  	_, err = rp.Join("../..")
    79  	assert.ErrorContains(t, err, `relative path escapes root: ../..`)
    80  }
    81  
    82  func TestRootPathClean(t *testing.T) {
    83  	testRootPath(t, "/some/root/path")
    84  }
    85  
    86  func TestRootPathUnclean(t *testing.T) {
    87  	testRootPath(t, "/some/root/path/")
    88  	testRootPath(t, "/some/root/path/.")
    89  	testRootPath(t, "/some/root/../path/")
    90  }