github.com/nak3/source-to-image@v1.1.10-0.20180319140719-2ed55639898d/pkg/scm/git/url_test.go (about)

     1  package git
     2  
     3  import (
     4  	"net/url"
     5  	"reflect"
     6  	"runtime"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  type parseTest struct {
    12  	rawurl         string
    13  	expectedGitURL *URL
    14  	expectedError  bool
    15  }
    16  
    17  func TestParse(t *testing.T) {
    18  	var tests []parseTest
    19  
    20  	switch runtime.GOOS {
    21  	case "windows":
    22  		tests = append(tests,
    23  			parseTest{
    24  				rawurl:        "file://relative/path",
    25  				expectedError: true,
    26  			},
    27  			parseTest{
    28  				rawurl:        "file:///relative/path",
    29  				expectedError: true,
    30  			},
    31  			parseTest{
    32  				rawurl: "file:///c:/absolute/path?query#fragment",
    33  				expectedGitURL: &URL{
    34  					URL: url.URL{
    35  						Scheme:   "file",
    36  						Path:     "/c:/absolute/path",
    37  						RawQuery: "query",
    38  						Fragment: "fragment",
    39  					},
    40  					Type: URLTypeURL,
    41  				},
    42  			},
    43  		)
    44  
    45  	default:
    46  		tests = append(tests,
    47  			parseTest{
    48  				rawurl:        "file://relative/path",
    49  				expectedError: true,
    50  			},
    51  			parseTest{
    52  				rawurl: "file:///absolute/path?query#fragment",
    53  				expectedGitURL: &URL{
    54  					URL: url.URL{
    55  						Scheme:   "file",
    56  						Path:     "/absolute/path",
    57  						RawQuery: "query",
    58  						Fragment: "fragment",
    59  					},
    60  					Type: URLTypeURL,
    61  				},
    62  			},
    63  		)
    64  	}
    65  
    66  	tests = append(tests,
    67  		// http://
    68  		parseTest{
    69  			rawurl: "http://user:pass@github.com:443/user/repo.git?query#fragment",
    70  			expectedGitURL: &URL{
    71  				URL: url.URL{
    72  					Scheme:   "http",
    73  					User:     url.UserPassword("user", "pass"),
    74  					Host:     "github.com:443",
    75  					Path:     "/user/repo.git",
    76  					RawQuery: "query",
    77  					Fragment: "fragment",
    78  				},
    79  				Type: URLTypeURL,
    80  			},
    81  		},
    82  		parseTest{
    83  			rawurl: "http://user@1.2.3.4:443/repo?query#fragment",
    84  			expectedGitURL: &URL{
    85  				URL: url.URL{
    86  					Scheme:   "http",
    87  					User:     url.User("user"),
    88  					Host:     "1.2.3.4:443",
    89  					Path:     "/repo",
    90  					RawQuery: "query",
    91  					Fragment: "fragment",
    92  				},
    93  				Type: URLTypeURL,
    94  			},
    95  		},
    96  		parseTest{
    97  			rawurl: "http://[::ffff:1.2.3.4]:443",
    98  			expectedGitURL: &URL{
    99  				URL: url.URL{
   100  					Scheme: "http",
   101  					Host:   "[::ffff:1.2.3.4]:443",
   102  				},
   103  				Type: URLTypeURL,
   104  			},
   105  		},
   106  		parseTest{
   107  			rawurl: "http://github.com/openshift/origin",
   108  			expectedGitURL: &URL{
   109  				URL: url.URL{
   110  					Scheme: "http",
   111  					Host:   "github.com",
   112  					Path:   "/openshift/origin",
   113  				},
   114  				Type: URLTypeURL,
   115  			},
   116  		},
   117  
   118  		// transport::opaque
   119  		parseTest{
   120  			rawurl: "http::http://github.com/openshift/origin",
   121  			expectedGitURL: &URL{
   122  				URL: url.URL{
   123  					Scheme: "http",
   124  					Opaque: ":http://github.com/openshift/origin",
   125  				},
   126  				Type: URLTypeURL,
   127  			},
   128  		},
   129  
   130  		// git@host ...
   131  		parseTest{
   132  			rawurl: "user@github.com:/user/repo.git#fragment",
   133  			expectedGitURL: &URL{
   134  				URL: url.URL{
   135  					User:     url.User("user"),
   136  					Host:     "github.com",
   137  					Path:     "/user/repo.git",
   138  					Fragment: "fragment",
   139  				},
   140  				Type: URLTypeSCP,
   141  			},
   142  		},
   143  		parseTest{
   144  			rawurl: "user@github.com:user/repo.git#fragment",
   145  			expectedGitURL: &URL{
   146  				URL: url.URL{
   147  					User:     url.User("user"),
   148  					Host:     "github.com",
   149  					Path:     "user/repo.git",
   150  					Fragment: "fragment",
   151  				},
   152  				Type: URLTypeSCP,
   153  			},
   154  		},
   155  		parseTest{
   156  			rawurl: "user@1.2.3.4:repo#fragment",
   157  			expectedGitURL: &URL{
   158  				URL: url.URL{
   159  					User:     url.User("user"),
   160  					Host:     "1.2.3.4",
   161  					Path:     "repo",
   162  					Fragment: "fragment",
   163  				},
   164  				Type: URLTypeSCP,
   165  			},
   166  		},
   167  		parseTest{
   168  			rawurl: "[::ffff:1.2.3.4]:",
   169  			expectedGitURL: &URL{
   170  				URL: url.URL{
   171  					Host: "[::ffff:1.2.3.4]",
   172  				},
   173  				Type: URLTypeSCP,
   174  			},
   175  		},
   176  		parseTest{
   177  			rawurl: "git@github.com:openshift/origin",
   178  			expectedGitURL: &URL{
   179  				URL: url.URL{
   180  					User: url.User("git"),
   181  					Host: "github.com",
   182  					Path: "openshift/origin",
   183  				},
   184  				Type: URLTypeSCP,
   185  			},
   186  		},
   187  
   188  		// path ...
   189  		parseTest{
   190  			rawurl: "/absolute#fragment",
   191  			expectedGitURL: &URL{
   192  				URL: url.URL{
   193  					Path:     "/absolute",
   194  					Fragment: "fragment",
   195  				},
   196  				Type: URLTypeLocal,
   197  			},
   198  		},
   199  		parseTest{
   200  			rawurl: "relative#fragment",
   201  			expectedGitURL: &URL{
   202  				URL: url.URL{
   203  					Path:     "relative",
   204  					Fragment: "fragment",
   205  				},
   206  				Type: URLTypeLocal,
   207  			},
   208  		},
   209  	)
   210  
   211  	for _, test := range tests {
   212  		parsedURL, err := Parse(test.rawurl)
   213  		if test.expectedError != (err != nil) {
   214  			t.Errorf("%s: Parse() returned err: %v", test.rawurl, err)
   215  		}
   216  		if err != nil {
   217  			continue
   218  		}
   219  
   220  		if !reflect.DeepEqual(parsedURL, test.expectedGitURL) {
   221  			t.Errorf("%s: Parse() returned\n\t%#v\nWanted\n\t%#v", test.rawurl, parsedURL, test.expectedGitURL)
   222  		}
   223  
   224  		if parsedURL.String() != test.rawurl {
   225  			t.Errorf("%s: String() returned %s", test.rawurl, parsedURL.String())
   226  		}
   227  
   228  		if parsedURL.StringNoFragment() != strings.SplitN(test.rawurl, "#", 2)[0] {
   229  			t.Errorf("%s: StringNoFragment() returned %s", test.rawurl, parsedURL.StringNoFragment())
   230  		}
   231  	}
   232  }
   233  
   234  func TestStringNoFragment(t *testing.T) {
   235  	u := MustParse("part#fragment")
   236  	if u.StringNoFragment() != "part" {
   237  		t.Errorf("StringNoFragment() returned %s", u.StringNoFragment())
   238  	}
   239  	if !reflect.DeepEqual(u, MustParse("part#fragment")) {
   240  		t.Errorf("StringNoFragment() modified its argument")
   241  	}
   242  }
   243  
   244  type localPathTest struct {
   245  	url      *URL
   246  	expected string
   247  }
   248  
   249  func TestLocalPath(t *testing.T) {
   250  	var tests []localPathTest
   251  
   252  	switch runtime.GOOS {
   253  	case "windows":
   254  		tests = append(tests,
   255  			localPathTest{
   256  				url:      MustParse("file:///c:/foo/bar"),
   257  				expected: `c:\foo\bar`,
   258  			},
   259  			localPathTest{
   260  				url:      MustParse(`c:\foo\bar`),
   261  				expected: `c:\foo\bar`,
   262  			},
   263  			localPathTest{
   264  				url:      MustParse(`\foo\bar`),
   265  				expected: `\foo\bar`,
   266  			},
   267  			localPathTest{
   268  				url:      MustParse(`foo\bar`),
   269  				expected: `foo\bar`,
   270  			},
   271  			localPathTest{
   272  				url:      MustParse(`foo`),
   273  				expected: `foo`,
   274  			},
   275  		)
   276  
   277  	default:
   278  		tests = append(tests,
   279  			localPathTest{
   280  				url:      MustParse("file:///foo/bar"),
   281  				expected: "/foo/bar",
   282  			},
   283  			localPathTest{
   284  				url:      MustParse("/foo/bar"),
   285  				expected: "/foo/bar",
   286  			},
   287  		)
   288  	}
   289  
   290  	for i, test := range tests {
   291  		if test.url.LocalPath() != test.expected {
   292  			t.Errorf("%d: LocalPath() returned %s", i, test.url.LocalPath())
   293  		}
   294  	}
   295  }