...

Source file src/golang.org/x/exp/cmd/gorelease/path_test.go

Documentation: golang.org/x/exp/cmd/gorelease

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"runtime"
     9  	"testing"
    10  )
    11  
    12  func TestHasPathPrefix(t *testing.T) {
    13  	for _, test := range []struct {
    14  		desc, path, prefix string
    15  		want               bool
    16  	}{
    17  		{
    18  			desc:   "empty_prefix",
    19  			path:   "a/b",
    20  			prefix: "",
    21  			want:   true,
    22  		}, {
    23  			desc:   "partial_prefix",
    24  			path:   "a/b",
    25  			prefix: "a",
    26  			want:   true,
    27  		}, {
    28  			desc:   "full_prefix",
    29  			path:   "a/b",
    30  			prefix: "a/b",
    31  			want:   true,
    32  		}, {
    33  			desc:   "partial_component",
    34  			path:   "aa/b",
    35  			prefix: "a",
    36  			want:   false,
    37  		},
    38  	} {
    39  		t.Run(test.desc, func(t *testing.T) {
    40  			if got := hasPathPrefix(test.path, test.prefix); got != test.want {
    41  				t.Errorf("hasPathPrefix(%q, %q): got %v, want %v", test.path, test.prefix, got, test.want)
    42  			}
    43  		})
    44  	}
    45  }
    46  
    47  func TestHasFilePathPrefix(t *testing.T) {
    48  	type test struct {
    49  		desc, path, prefix string
    50  		want               bool
    51  	}
    52  	var tests []test
    53  	if runtime.GOOS == "windows" {
    54  		tests = []test{
    55  			{
    56  				desc:   "empty_prefix",
    57  				path:   `c:\a\b`,
    58  				prefix: "",
    59  				want:   true,
    60  			}, {
    61  				desc:   "drive_prefix",
    62  				path:   `c:\a\b`,
    63  				prefix: `c:\`,
    64  				want:   true,
    65  			}, {
    66  				desc:   "partial_prefix",
    67  				path:   `c:\a\b`,
    68  				prefix: `c:\a`,
    69  				want:   true,
    70  			}, {
    71  				desc:   "full_prefix",
    72  				path:   `c:\a\b`,
    73  				prefix: `c:\a\b`,
    74  				want:   true,
    75  			}, {
    76  				desc:   "partial_component",
    77  				path:   `c:\aa\b`,
    78  				prefix: `c:\a`,
    79  				want:   false,
    80  			},
    81  		}
    82  	} else {
    83  		tests = []test{
    84  			{
    85  				desc:   "empty_prefix",
    86  				path:   "/a/b",
    87  				prefix: "",
    88  				want:   true,
    89  			}, {
    90  				desc:   "partial_prefix",
    91  				path:   "/a/b",
    92  				prefix: "/a",
    93  				want:   true,
    94  			}, {
    95  				desc:   "full_prefix",
    96  				path:   "/a/b",
    97  				prefix: "/a/b",
    98  				want:   true,
    99  			}, {
   100  				desc:   "partial_component",
   101  				path:   "/aa/b",
   102  				prefix: "/a",
   103  				want:   false,
   104  			},
   105  		}
   106  	}
   107  	for _, test := range tests {
   108  		t.Run(test.desc, func(t *testing.T) {
   109  			if got := hasFilePathPrefix(test.path, test.prefix); got != test.want {
   110  				t.Errorf("hasFilePathPrefix(%q, %q): got %v, want %v", test.path, test.prefix, got, test.want)
   111  			}
   112  		})
   113  	}
   114  }
   115  
   116  func TestTrimFilePathPrefix(t *testing.T) {
   117  	type test struct {
   118  		desc, path, prefix, want string
   119  	}
   120  	var tests []test
   121  	if runtime.GOOS == "windows" {
   122  		tests = []test{
   123  			// Note: these two cases in which the result preserves the leading \
   124  			// don't come up in reality in gorelease. That's because prefix is
   125  			// always far to the right of the path parts (ex github.com/foo/bar
   126  			// in C:\Users\foo\AppData\Local\Temp\...\github.com\foo\bar).
   127  			{
   128  				desc:   "empty_prefix",
   129  				path:   `c:\a\b`,
   130  				prefix: "",
   131  				want:   `\a\b`,
   132  			}, {
   133  				desc:   "partial_component",
   134  				path:   `c:\aa\b`,
   135  				prefix: `c:\a`,
   136  				want:   `\aa\b`,
   137  			},
   138  
   139  			{
   140  				desc:   "drive_prefix",
   141  				path:   `c:\a\b`,
   142  				prefix: `c:\`,
   143  				want:   `a\b`,
   144  			}, {
   145  				desc:   "partial_prefix",
   146  				path:   `c:\a\b`,
   147  				prefix: `c:\a`,
   148  				want:   `b`,
   149  			}, {
   150  				desc:   "full_prefix",
   151  				path:   `c:\a\b`,
   152  				prefix: `c:\a\b`,
   153  				want:   "",
   154  			},
   155  		}
   156  	} else {
   157  		tests = []test{
   158  			{
   159  				desc:   "empty_prefix",
   160  				path:   "/a/b",
   161  				prefix: "",
   162  				want:   "/a/b",
   163  			}, {
   164  				desc:   "partial_prefix",
   165  				path:   "/a/b",
   166  				prefix: "/a",
   167  				want:   "b",
   168  			}, {
   169  				desc:   "full_prefix",
   170  				path:   "/a/b",
   171  				prefix: "/a/b",
   172  				want:   "",
   173  			}, {
   174  				desc:   "partial_component",
   175  				path:   "/aa/b",
   176  				prefix: "/a",
   177  				want:   "/aa/b",
   178  			},
   179  		}
   180  	}
   181  	for _, test := range tests {
   182  		t.Run(test.desc, func(t *testing.T) {
   183  			if got := trimFilePathPrefix(test.path, test.prefix); got != test.want {
   184  				t.Errorf("hasFilePathPrefix(%q, %q): got %v, want %v", test.path, test.prefix, got, test.want)
   185  			}
   186  		})
   187  	}
   188  }
   189  
   190  func TestTrimPathPrefix(t *testing.T) {
   191  	for _, test := range []struct {
   192  		desc, path, prefix, want string
   193  	}{
   194  		{
   195  			desc:   "empty_prefix",
   196  			path:   "a/b",
   197  			prefix: "",
   198  			want:   "a/b",
   199  		}, {
   200  			desc:   "abs_empty_prefix",
   201  			path:   "/a/b",
   202  			prefix: "",
   203  			want:   "/a/b",
   204  		}, {
   205  			desc:   "partial_prefix",
   206  			path:   "a/b",
   207  			prefix: "a",
   208  			want:   "b",
   209  		}, {
   210  			desc:   "full_prefix",
   211  			path:   "a/b",
   212  			prefix: "a/b",
   213  			want:   "",
   214  		}, {
   215  			desc:   "partial_component",
   216  			path:   "aa/b",
   217  			prefix: "a",
   218  			want:   "aa/b",
   219  		},
   220  	} {
   221  		t.Run(test.desc, func(t *testing.T) {
   222  			if got := trimPathPrefix(test.path, test.prefix); got != test.want {
   223  				t.Errorf("trimPathPrefix(%q, %q): got %q, want %q", test.path, test.prefix, got, test.want)
   224  			}
   225  		})
   226  	}
   227  }
   228  

View as plain text