github.com/alaturka/gbreve@v0.0.0-20211031155621-c4ad29761ee9/net/usl/usl_test.go (about)

     1  package usl
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  )
     7  
     8  type testParse struct {
     9  	in  string
    10  	out map[string]string
    11  }
    12  
    13  func init() { //nolint:gochecknoinits
    14  	err := os.Chdir("/tmp")
    15  	if err != nil {
    16  		panic(err)
    17  	}
    18  }
    19  
    20  //nolint:funlen
    21  func TestParse(t *testing.T) {
    22  	t.Parallel()
    23  
    24  	tests := map[string][]testParse{
    25  		"Schemeless usual": {
    26  			{
    27  				"github.com/user/repo", map[string]string{
    28  					"source": "https://github.com/user/repo.git",
    29  
    30  					"class":  "git",
    31  					"id":     `https:%2F%2Fgithub.com%2Fuser%2Frepo.git`,
    32  					"inpath": "",
    33  					"name":   "user/repo",
    34  					"ref":    "",
    35  					"scheme": "https",
    36  				},
    37  			},
    38  			{
    39  				"github.com/user/repo@unstable", map[string]string{
    40  					"source": "https://github.com/user/repo.git",
    41  
    42  					"class":  "git",
    43  					"id":     `https:%2F%2Fgithub.com%2Fuser%2Frepo.git@unstable`,
    44  					"inpath": "",
    45  					"name":   "user/repo",
    46  					"ref":    "unstable",
    47  					"scheme": "https",
    48  				},
    49  			},
    50  			{
    51  				"github.com/user/repo/a/b@unstable", map[string]string{
    52  					"source": "https://github.com/user/repo.git",
    53  
    54  					"class":  "git",
    55  					"id":     `https:%2F%2Fgithub.com%2Fuser%2Frepo.git@unstable`,
    56  					"inpath": "a/b",
    57  					"name":   "user/repo",
    58  					"ref":    "unstable",
    59  					"scheme": "https",
    60  				},
    61  			},
    62  			{
    63  				"github.com/user/repo.git/a/b", map[string]string{
    64  					"source": "https://github.com/user/repo.git",
    65  
    66  					"class":  "git",
    67  					"id":     `https:%2F%2Fgithub.com%2Fuser%2Frepo.git`,
    68  					"inpath": "a/b",
    69  					"name":   "user/repo",
    70  					"ref":    "",
    71  					"scheme": "https",
    72  				},
    73  			},
    74  			{
    75  				"example.com", map[string]string{
    76  					"source": "https://example.com",
    77  
    78  					"class":  "",
    79  					"id":     `https:%2F%2Fexample.com`,
    80  					"inpath": "",
    81  					"name":   "",
    82  					"ref":    "",
    83  					"scheme": "https",
    84  				},
    85  			},
    86  			{
    87  				"example.com/", map[string]string{
    88  					"source": "https://example.com",
    89  
    90  					"class":  "",
    91  					"id":     `https:%2F%2Fexample.com`,
    92  					"inpath": "",
    93  					"name":   "",
    94  					"ref":    "",
    95  					"scheme": "https",
    96  				},
    97  			},
    98  		},
    99  		"Schemeless SSH": {
   100  			{
   101  				"github.com:user/repo", map[string]string{
   102  					"source": "git@github.com:user/repo.git",
   103  
   104  					"class":    "git",
   105  					"id":       `git@github.com:user%2Frepo.git`,
   106  					"inpath":   "",
   107  					"name":     "user/repo",
   108  					"ref":      "",
   109  					"scheme":   "ssh",
   110  					"username": "git",
   111  				},
   112  			},
   113  			{
   114  				"git@github.com:user/repo", map[string]string{
   115  					"source": "git@github.com:user/repo.git",
   116  
   117  					"class":    "git",
   118  					"id":       `git@github.com:user%2Frepo.git`,
   119  					"inpath":   "",
   120  					"name":     "user/repo",
   121  					"ref":      "",
   122  					"scheme":   "ssh",
   123  					"username": "git",
   124  				},
   125  			},
   126  			{
   127  				"user@example.com:a/b", map[string]string{
   128  					"source": "user@example.com:a/b",
   129  
   130  					"class":    "",
   131  					"id":       `user@example.com:a%2Fb`,
   132  					"inpath":   "",
   133  					"name":     "a/b",
   134  					"path":     "a/b",
   135  					"ref":      "",
   136  					"scheme":   "ssh",
   137  					"username": "user",
   138  				},
   139  			},
   140  		},
   141  		"File scheme": {
   142  			{
   143  				"file:///path/to/repo.git/a/b", map[string]string{
   144  					"source": "file:///path/to/repo",
   145  
   146  					"class":  "git",
   147  					"id":     `file:%2F%2F%2Fpath%2Fto%2Frepo`,
   148  					"inpath": "a/b",
   149  					"name":   "path/to/repo",
   150  					"ref":    "",
   151  					"scheme": "file",
   152  				},
   153  			},
   154  			{
   155  				"file:///path/to/repo.git/a/b@next", map[string]string{
   156  					"source": "file:///path/to/repo",
   157  
   158  					"class":  "git",
   159  					"id":     `file:%2F%2F%2Fpath%2Fto%2Frepo@next`,
   160  					"inpath": "a/b",
   161  					"name":   "path/to/repo",
   162  					"ref":    "next",
   163  					"scheme": "file",
   164  				},
   165  			},
   166  			{
   167  				"file://path/to/repo.git/a/b", map[string]string{
   168  					"source": "file://path/to/repo",
   169  
   170  					"class":  "git",
   171  					"id":     `file:%2F%2Fpath%2Fto%2Frepo`,
   172  					"inpath": "a/b",
   173  					"name":   "to/repo",
   174  					"ref":    "",
   175  					"scheme": "file",
   176  				},
   177  			},
   178  			{
   179  				"file:///path/to/repo.zip/a/b", map[string]string{
   180  					"source": "file:///path/to/repo.zip",
   181  
   182  					"class":  "zip",
   183  					"id":     `file:%2F%2F%2Fpath%2Fto%2Frepo.zip`,
   184  					"inpath": "a/b",
   185  					"name":   "path/to/repo",
   186  					"ref":    "",
   187  					"scheme": "file",
   188  				},
   189  			},
   190  		},
   191  		"HTTPS scheme": {
   192  			{
   193  				"https://github.com/user/repo", map[string]string{
   194  					"source": "https://github.com/user/repo.git",
   195  
   196  					"class":  "git",
   197  					"domain": "github.com",
   198  					"id":     `https:%2F%2Fgithub.com%2Fuser%2Frepo.git`,
   199  					"inpath": "",
   200  					"name":   "user/repo",
   201  					"port":   "",
   202  					"ref":    "",
   203  					"scheme": "https",
   204  				},
   205  			},
   206  			{
   207  				"https://example.com:8080/user/repo.git/a/b", map[string]string{
   208  					"source": "https://example.com:8080/user/repo.git",
   209  
   210  					"class":  "git",
   211  					"domain": "example.com",
   212  					"id":     `https:%2F%2Fexample.com:8080%2Fuser%2Frepo.git`,
   213  					"inpath": "a/b",
   214  					"name":   "user/repo",
   215  					"port":   "8080",
   216  					"ref":    "",
   217  					"scheme": "https",
   218  				},
   219  			},
   220  			{
   221  				"https://example.com", map[string]string{
   222  					"source": "https://example.com",
   223  
   224  					"class":  "",
   225  					"id":     `https:%2F%2Fexample.com`,
   226  					"inpath": "",
   227  					"name":   "",
   228  					"ref":    "",
   229  					"scheme": "https",
   230  				},
   231  			},
   232  			{
   233  				"https://example.com/", map[string]string{
   234  					"source": "https://example.com",
   235  
   236  					"class":  "",
   237  					"id":     `https:%2F%2Fexample.com`,
   238  					"inpath": "",
   239  					"name":   "",
   240  					"ref":    "",
   241  					"scheme": "https",
   242  				},
   243  			},
   244  		},
   245  		"SSH scheme": {
   246  			{
   247  				"ssh://git@github.com/user/repo", map[string]string{
   248  					"source": "git@github.com:user/repo.git",
   249  
   250  					"class":    "git",
   251  					"id":       `git@github.com:user%2Frepo.git`,
   252  					"inpath":   "",
   253  					"name":     "user/repo",
   254  					"ref":      "",
   255  					"scheme":   "ssh",
   256  					"username": "git",
   257  				},
   258  			},
   259  			{
   260  				"ssh://user:pass@example.com/a/b", map[string]string{
   261  					"source": "user@example.com:a/b",
   262  
   263  					"class":    "",
   264  					"id":       `user@example.com:a%2Fb`,
   265  					"inpath":   "",
   266  					"name":     "a/b",
   267  					"password": "pass",
   268  					"path":     "/a/b",
   269  					"ref":      "",
   270  					"scheme":   "ssh",
   271  					"username": "user",
   272  				},
   273  			},
   274  			{
   275  				"ssh://user:pass@example.com:22/a/b", map[string]string{
   276  					"source": "ssh://user:pass@example.com:22/a/b",
   277  
   278  					"class":    "",
   279  					"domain":   "example.com",
   280  					"host":     "example.com:22",
   281  					"inpath":   "",
   282  					"name":     "a/b",
   283  					"password": "pass",
   284  					"path":     "/a/b",
   285  					"port":     "22",
   286  					"ref":      "",
   287  					"scheme":   "ssh",
   288  					"username": "user",
   289  				},
   290  			},
   291  		},
   292  	}
   293  
   294  	for name, ts := range tests {
   295  		ts := ts // https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
   296  
   297  		t.Run(name, func(t *testing.T) {
   298  			t.Parallel()
   299  			for _, tc := range ts {
   300  				got, err := Parse(tc.in)
   301  
   302  				if err != nil {
   303  					t.Errorf("Parse(%q) = unexpected err %q", tc.in, err)
   304  					continue
   305  				}
   306  
   307  				m, _ := got.Map()
   308  
   309  				for ke, ve := range tc.out {
   310  					if va, ok := m[ke]; ok {
   311  						if ve != va {
   312  							t.Errorf("\t%40s    %-12s\twant: %-12s\tgot:  %-12s", tc.in, ke, ve, va)
   313  						}
   314  					}
   315  				}
   316  			}
   317  		})
   318  	}
   319  }
   320  
   321  //nolint:funlen
   322  func TestParseMayLocalPath(t *testing.T) {
   323  	t.Parallel()
   324  
   325  	tests := map[string][]testParse{
   326  		"Schemeless file": {
   327  			{
   328  				"/a/b", map[string]string{
   329  					"source": "file:///a/b",
   330  
   331  					"class":    "",
   332  					"basepath": "a/b",
   333  					"scheme":   "file",
   334  				},
   335  			},
   336  			{
   337  				"./a/b", map[string]string{
   338  					"source": "file:///tmp/a/b",
   339  
   340  					"class":    "",
   341  					"basepath": "tmp/a/b",
   342  					"scheme":   "file",
   343  				},
   344  			},
   345  			{
   346  				"../a/b", map[string]string{
   347  					"source": "file:///a/b",
   348  
   349  					"class":    "",
   350  					"basepath": "a/b",
   351  					"scheme":   "file",
   352  				},
   353  			},
   354  			{
   355  				"./a/b.git/x@next", map[string]string{
   356  					"source": "file:///tmp/a/b",
   357  
   358  					"class":    "git",
   359  					"basepath": "tmp/a/b",
   360  					"inpath":   "x",
   361  					"ref":      "next",
   362  					"scheme":   "file",
   363  				},
   364  			},
   365  			{
   366  				"./a/b.zip/x", map[string]string{
   367  					"source": "file:///tmp/a/b.zip",
   368  
   369  					"class":    "zip",
   370  					"basepath": "tmp/a/b",
   371  					"inpath":   "x",
   372  					"scheme":   "file",
   373  				},
   374  			},
   375  		},
   376  	}
   377  
   378  	for name, ts := range tests {
   379  		ts := ts // https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
   380  
   381  		t.Run(name, func(t *testing.T) {
   382  			t.Parallel()
   383  			for _, tc := range ts {
   384  				got, err := ParseMayLocalPath(tc.in)
   385  
   386  				if err != nil {
   387  					t.Errorf("Parse(%q) = unexpected err %q", tc.in, err)
   388  					continue
   389  				}
   390  
   391  				m, _ := got.Map()
   392  
   393  				for ke, ve := range tc.out {
   394  					if va, ok := m[ke]; ok {
   395  						if ve != va {
   396  							t.Errorf("\t%40s    %-12s\twant: %-12s\tgot:  %-12s", tc.in, ke, ve, va)
   397  						}
   398  					}
   399  				}
   400  			}
   401  		})
   402  	}
   403  }
   404  
   405  type testTemplate struct {
   406  	in     string
   407  	custom map[string]string
   408  	out    map[string]string
   409  }
   410  
   411  //nolint:funlen
   412  func TestTemplate(t *testing.T) {
   413  	t.Parallel()
   414  
   415  	tests := map[string][]testTemplate{
   416  		"Schemeless usual": {
   417  			{
   418  				"github.com/user/repo", map[string]string{
   419  					"custom": `{{ .source | pathescape }}`,
   420  				}, map[string]string{
   421  					"source": "https://github.com/user/repo.git",
   422  					"custom": `https:%2F%2Fgithub.com%2Fuser%2Frepo.git`,
   423  				},
   424  			},
   425  		},
   426  	}
   427  
   428  	for name, ts := range tests {
   429  		ts := ts // https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
   430  
   431  		t.Run(name, func(t *testing.T) {
   432  			t.Parallel()
   433  			for _, tc := range ts {
   434  				got, err := Parse(tc.in)
   435  
   436  				if err != nil {
   437  					t.Errorf("Parse(%q) = unexpected err %q", tc.in, err)
   438  					continue
   439  				}
   440  
   441  				m, _ := got.MapCustom(tc.custom)
   442  
   443  				for ke, ve := range tc.out {
   444  					if va, ok := m[ke]; ok {
   445  						if ve != va {
   446  							t.Errorf("\t%40s    %-12s\twant: %-12s\tgot:  %-12s", tc.in, ke, ve, va)
   447  						}
   448  					}
   449  				}
   450  			}
   451  		})
   452  	}
   453  }