github.com/nilium/gitlab-runner@v12.5.0+incompatible/helpers/path/unix_path_test.go (about)

     1  package path
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestUnixJoin(t *testing.T) {
    10  	p := NewUnixPath()
    11  
    12  	tests := map[string]struct {
    13  		args     []string
    14  		expected string
    15  	}{
    16  		"the same result": {
    17  			args:     []string{"dir"},
    18  			expected: "dir",
    19  		},
    20  		"joins absolute and relative": {
    21  			args:     []string{"/path/to", "dir"},
    22  			expected: "/path/to/dir",
    23  		},
    24  		"joins absolute two absolutes": {
    25  			args:     []string{"/path/to", "/dir/path"},
    26  			expected: "/path/to/dir/path",
    27  		},
    28  		"cleans paths": {
    29  			args:     []string{"path/../to", "dir/with/my/../path"},
    30  			expected: "to/dir/with/path",
    31  		},
    32  		"does not normalize separators": {
    33  			args:     []string{"path\\to\\windows\\dir"},
    34  			expected: "path\\to\\windows\\dir",
    35  		},
    36  	}
    37  
    38  	for name, test := range tests {
    39  		t.Run(name, func(t *testing.T) {
    40  			assert.Equal(t, test.expected, p.Join(test.args...))
    41  		})
    42  	}
    43  }
    44  
    45  func TestUnixIsAbs(t *testing.T) {
    46  	p := NewUnixPath()
    47  
    48  	tests := map[string]struct {
    49  		arg      string
    50  		expected bool
    51  	}{
    52  		"relative path": {
    53  			arg:      "dir",
    54  			expected: false,
    55  		},
    56  		"relative path with dots": {
    57  			arg:      "../dir",
    58  			expected: false,
    59  		},
    60  		"absolute path": {
    61  			arg:      "/path/to/dir",
    62  			expected: true,
    63  		},
    64  		"unclean absolute": {
    65  			arg:      "/path/../to/dir",
    66  			expected: true,
    67  		},
    68  	}
    69  
    70  	for name, test := range tests {
    71  		t.Run(name, func(t *testing.T) {
    72  			assert.Equal(t, test.expected, p.IsAbs(test.arg))
    73  		})
    74  	}
    75  }
    76  
    77  func TestUnixIsRoot(t *testing.T) {
    78  	p := NewUnixPath()
    79  
    80  	tests := map[string]struct {
    81  		arg      string
    82  		expected bool
    83  	}{
    84  		"relative path": {
    85  			arg: "dir", expected: false,
    86  		},
    87  		"absolute path": {
    88  			arg: "/path/to/dir", expected: false,
    89  		},
    90  		"root path": {
    91  			arg: "/", expected: true,
    92  		},
    93  		"unclean root": {
    94  			arg: "/path/..", expected: true,
    95  		},
    96  	}
    97  
    98  	for name, test := range tests {
    99  		t.Run(name, func(t *testing.T) {
   100  			assert.Equal(t, test.expected, p.IsRoot(test.arg))
   101  		})
   102  	}
   103  }
   104  
   105  func TestUnixContains(t *testing.T) {
   106  	p := NewUnixPath()
   107  
   108  	tests := map[string]struct {
   109  		basepath   string
   110  		targetpath string
   111  		expected   bool
   112  	}{
   113  		"root path": {
   114  			basepath:   "/",
   115  			targetpath: "/path/to/dir",
   116  			expected:   true,
   117  		},
   118  		"unclean root path": {
   119  			basepath:   "/other/..",
   120  			targetpath: "/path/../to/dir",
   121  			expected:   true,
   122  		},
   123  		"absolute path": {
   124  			basepath:   "/other",
   125  			targetpath: "/path/to/dir",
   126  			expected:   false,
   127  		},
   128  		"unclean absolute path": {
   129  			basepath:   "/other/../my/path",
   130  			targetpath: "/path/../to/dir",
   131  			expected:   false,
   132  		},
   133  		"relative path": {
   134  			basepath:   "other",
   135  			targetpath: "path/to/dir",
   136  			expected:   false,
   137  		},
   138  		"the same path": {
   139  			basepath:   "/path/to/dir",
   140  			targetpath: "/path/to/dir",
   141  			expected:   true,
   142  		},
   143  	}
   144  
   145  	for name, test := range tests {
   146  		t.Run(name, func(t *testing.T) {
   147  			assert.Equal(t, test.expected, p.Contains(test.basepath, test.targetpath))
   148  		})
   149  	}
   150  }