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

     1  // +build windows
     2  
     3  package path
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestWindowsJoin(t *testing.T) {
    12  	p := NewWindowsPath()
    13  
    14  	tests := map[string]struct {
    15  		args     []string
    16  		expected string
    17  	}{
    18  		"the same result": {
    19  			args:     []string{"dir"},
    20  			expected: "dir",
    21  		},
    22  		"joins absolute and relative": {
    23  			args:     []string{"c:\\path\\to", "dir"},
    24  			expected: "c:\\path\\to\\dir",
    25  		},
    26  		"joins absolute two absolutes": {
    27  			args:     []string{"d:/path/to", "/dir/path"},
    28  			expected: "d:\\path\\to\\dir\\path",
    29  		},
    30  		"cleans paths": {
    31  			args:     []string{"path\\..\\to", "dir/with/my/../path"},
    32  			expected: "to\\dir\\with\\path",
    33  		},
    34  		"does normalize separators": {
    35  			args:     []string{"path/to/windows/dir"},
    36  			expected: "path\\to\\windows\\dir",
    37  		},
    38  	}
    39  
    40  	for name, test := range tests {
    41  		t.Run(name, func(t *testing.T) {
    42  			assert.Equal(t, test.expected, p.Join(test.args...))
    43  		})
    44  	}
    45  }
    46  
    47  func TestWindowsIsAbs(t *testing.T) {
    48  	p := NewWindowsPath()
    49  
    50  	tests := map[string]struct {
    51  		arg      string
    52  		expected bool
    53  	}{
    54  		"relative path": {
    55  			arg:      "dir",
    56  			expected: false,
    57  		},
    58  		"unix absolute path": {
    59  			arg:      "/path/to/dir",
    60  			expected: false,
    61  		},
    62  		"unclean unix absolute path": {
    63  			arg:      "/path/../to/dir",
    64  			expected: false,
    65  		},
    66  		"windows absolute path": {
    67  			arg:      "c:\\path\\to\\dir",
    68  			expected: true,
    69  		},
    70  		"unclean windows absolute path": {
    71  			arg:      "c:\\path\\..\\to\\..\\dir",
    72  			expected: true,
    73  		},
    74  	}
    75  
    76  	for name, test := range tests {
    77  		t.Run(name, func(t *testing.T) {
    78  			assert.Equal(t, test.expected, p.IsAbs(test.arg))
    79  		})
    80  	}
    81  }
    82  
    83  func TestWindowsIsRoot(t *testing.T) {
    84  	p := NewWindowsPath()
    85  
    86  	tests := map[string]struct {
    87  		arg      string
    88  		expected bool
    89  	}{
    90  		"relative path": {
    91  			arg:      "dir",
    92  			expected: false,
    93  		},
    94  		"absolute path without drive": {
    95  			arg:      "/path/to/dir",
    96  			expected: false,
    97  		},
    98  		"root path without drive": {
    99  			arg:      "/",
   100  			expected: false,
   101  		},
   102  		"root path with drive": {
   103  			arg:      "c:/",
   104  			expected: true,
   105  		},
   106  		"absolute path with drive": {
   107  			arg:      "c:/path/to/dir",
   108  			expected: false,
   109  		},
   110  	}
   111  
   112  	for name, test := range tests {
   113  		t.Run(name, func(t *testing.T) {
   114  			assert.Equal(t, test.expected, p.IsRoot(test.arg))
   115  		})
   116  	}
   117  }
   118  
   119  func TestWindowsContains(t *testing.T) {
   120  	p := NewWindowsPath()
   121  
   122  	tests := map[string]struct {
   123  		basepath   string
   124  		targetpath string
   125  		expected   bool
   126  	}{
   127  		"root path": {
   128  			basepath:   "/",
   129  			targetpath: "/path/to/dir",
   130  			expected:   true,
   131  		},
   132  		"unclean root path": {
   133  			basepath:   "/other/..",
   134  			targetpath: "/path/../to/dir",
   135  			expected:   true,
   136  		},
   137  		"absolute path": {
   138  			basepath:   "/other",
   139  			targetpath: "/path/to/dir",
   140  			expected:   false,
   141  		},
   142  		"unclean absolute path": {
   143  			basepath:   "/other/../my/path",
   144  			targetpath: "/path/../to/dir",
   145  			expected:   false,
   146  		},
   147  		"relative path": {
   148  			basepath:   "other",
   149  			targetpath: "path/to/dir",
   150  			expected:   false,
   151  		},
   152  		"invalid absolute path": {
   153  			basepath:   "c:\\other",
   154  			targetpath: "\\path\\to\\dir",
   155  			expected:   false,
   156  		},
   157  		"windows absolute path": {
   158  			basepath:   "c:\\path",
   159  			targetpath: "c:\\path\\to\\dir",
   160  			expected:   true,
   161  		},
   162  		"the same path without drive": {
   163  			basepath:   "/path/to/dir",
   164  			targetpath: "/path/to/dir",
   165  			expected:   true,
   166  		},
   167  		"the same path with one having the drive": {
   168  			basepath:   "c:/path/to/dir",
   169  			targetpath: "/path/to/dir",
   170  			expected:   false,
   171  		},
   172  		"the same path with the drive": {
   173  			basepath:   "c:/path/to/dir",
   174  			targetpath: "c:\\path\\to\\dir\\",
   175  			expected:   true,
   176  		},
   177  	}
   178  
   179  	for name, test := range tests {
   180  		t.Run(name, func(t *testing.T) {
   181  			assert.Equal(t, test.expected, p.Contains(test.basepath, test.targetpath))
   182  		})
   183  	}
   184  }