github.com/argoproj/argo-cd/v3@v3.2.1/util/io/files/util_test.go (about)

     1  package files_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/argoproj/argo-cd/v3/util/io/files"
     9  )
    10  
    11  func TestRelativePath(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	type testcase struct {
    15  		name        string
    16  		fullpath    string
    17  		basepath    string
    18  		expected    string
    19  		expectedErr error
    20  	}
    21  	cases := []testcase{
    22  		{
    23  			name:        "will return relative path from file path",
    24  			fullpath:    "/home/test/app/readme.md",
    25  			basepath:    "/home/test",
    26  			expected:    "app/readme.md",
    27  			expectedErr: nil,
    28  		},
    29  		{
    30  			name:        "will return relative path from dir path",
    31  			fullpath:    "/home/test/app/",
    32  			basepath:    "/home/test",
    33  			expected:    "app",
    34  			expectedErr: nil,
    35  		},
    36  		{
    37  			name:        "will return . if fullpath and basepath are the same",
    38  			fullpath:    "/home/test/app/readme.md",
    39  			basepath:    "/home/test/app/readme.md",
    40  			expected:    ".",
    41  			expectedErr: nil,
    42  		},
    43  		{
    44  			name:        "will return error if basepath does not match",
    45  			fullpath:    "/home/test/app/readme.md",
    46  			basepath:    "/somewhere/else",
    47  			expected:    "",
    48  			expectedErr: files.ErrRelativeOutOfBound,
    49  		},
    50  		{
    51  			name:        "will return relative path from dir path",
    52  			fullpath:    "/home/test//app/",
    53  			basepath:    "/home/test",
    54  			expected:    "app",
    55  			expectedErr: nil,
    56  		},
    57  		{
    58  			name:        "will handle relative fullpath",
    59  			fullpath:    "./app/",
    60  			basepath:    "/home/test",
    61  			expected:    "",
    62  			expectedErr: files.ErrRelativeOutOfBound,
    63  		},
    64  		{
    65  			name:        "will handle relative basepath",
    66  			fullpath:    "/home/test/app/",
    67  			basepath:    "./test",
    68  			expected:    "",
    69  			expectedErr: files.ErrRelativeOutOfBound,
    70  		},
    71  		{
    72  			name:        "will handle relative paths",
    73  			fullpath:    "./test/app",
    74  			basepath:    "./test/app",
    75  			expected:    ".",
    76  			expectedErr: nil,
    77  		},
    78  	}
    79  	for _, c := range cases {
    80  		c := c
    81  		t.Run(c.name, func(t *testing.T) {
    82  			// given
    83  			t.Parallel()
    84  
    85  			// when
    86  			relativePath, err := files.RelativePath(c.fullpath, c.basepath)
    87  
    88  			// then
    89  			assert.Equal(t, c.expectedErr, err)
    90  			assert.Equal(t, c.expected, relativePath)
    91  		})
    92  	}
    93  }
    94  
    95  func TestInbound(t *testing.T) {
    96  	t.Parallel()
    97  
    98  	type testcase struct {
    99  		name      string
   100  		candidate string
   101  		basedir   string
   102  		expected  bool
   103  	}
   104  	cases := []testcase{
   105  		{
   106  			name:      "will return true if candidate is inbound",
   107  			candidate: "/home/test/app/readme.md",
   108  			basedir:   "/home/test",
   109  			expected:  true,
   110  		},
   111  		{
   112  			name:      "will return false if candidate is not inbound",
   113  			candidate: "/home/test/../readme.md",
   114  			basedir:   "/home/test",
   115  			expected:  false,
   116  		},
   117  		{
   118  			name:      "will return true if candidate is relative inbound",
   119  			candidate: "./readme.md",
   120  			basedir:   "/home/test",
   121  			expected:  true,
   122  		},
   123  		{
   124  			name:      "will return false if candidate is relative outbound",
   125  			candidate: "../readme.md",
   126  			basedir:   "/home/test",
   127  			expected:  false,
   128  		},
   129  		{
   130  			name:      "will return false if basedir is relative",
   131  			candidate: "/home/test/app/readme.md",
   132  			basedir:   "./test",
   133  			expected:  false,
   134  		},
   135  	}
   136  	for _, c := range cases {
   137  		c := c
   138  		t.Run(c.name, func(t *testing.T) {
   139  			// given
   140  			t.Parallel()
   141  
   142  			// when
   143  			inbound := files.Inbound(c.candidate, c.basedir)
   144  
   145  			// then
   146  			assert.Equal(t, c.expected, inbound)
   147  		})
   148  	}
   149  }