github.com/replicatedhq/ship@v0.55.0/pkg/util/github_test.go (about)

     1  package util
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestIsGithubURL(t *testing.T) {
    10  	tests := []struct {
    11  		name string
    12  		url  string
    13  		want bool
    14  	}{
    15  		{
    16  			name: "empty",
    17  			url:  "",
    18  			want: false,
    19  		},
    20  		{
    21  			name: "random nonsense",
    22  			url:  "a string of random nonsense",
    23  			want: false,
    24  		},
    25  		{
    26  			name: "mocked github url with tree",
    27  			url:  "github.com/OWNER/REPO/tree/REF/SUBDIR",
    28  			want: true,
    29  		},
    30  		{
    31  			name: "ship repo in pkg dir on master",
    32  			url:  "https://github.com/replicatedhq/ship/tree/master/pkg",
    33  			want: true,
    34  		},
    35  		{
    36  			name: "ship repo in pkg/specs dir on master",
    37  			url:  "https://github.com/replicatedhq/ship/tree/master/pkg/specs",
    38  			want: true,
    39  		},
    40  		{
    41  			name: "ship repo in pkg/specs dir at hash with www",
    42  			url:  "https://www.github.com/replicatedhq/ship/tree/atestsha/pkg/specs",
    43  			want: true,
    44  		},
    45  		{
    46  			name: "ship repo in root dir on master with www",
    47  			url:  "https://www.github.com/replicatedhq/ship/tree/master",
    48  			want: true,
    49  		},
    50  		{
    51  			name: "github repo with no tree",
    52  			url:  "github.com/replicatedhq/ship",
    53  			want: true,
    54  		},
    55  		{
    56  			name: "github repo with no tree with www",
    57  			url:  "https://www.github.com/replicatedhq/ship",
    58  			want: true,
    59  		},
    60  		{
    61  			name: "github repo with no tree with subdir",
    62  			url:  "https://github.com/replicatedhq/ship/pkg/specs",
    63  			want: true,
    64  		},
    65  		{
    66  			name: "github repo with no https or tree with subdir",
    67  			url:  "github.com/replicatedhq/ship/pkg/specs",
    68  			want: true,
    69  		},
    70  		{
    71  			name: "bitbucket repo",
    72  			url:  "bitbucket.org/ww/goautoneg",
    73  			want: false,
    74  		},
    75  		{
    76  			name: "already configured go-getter string",
    77  			url:  "github.com/replicatedhq/ship?ref=master//pkg/specs",
    78  			want: false,
    79  		},
    80  	}
    81  	for _, tt := range tests {
    82  		t.Run(tt.name, func(t *testing.T) {
    83  			if got := IsGithubURL(tt.url); got != tt.want {
    84  				t.Errorf("IsGithubURL(%s) = %v, want %v", tt.url, got, tt.want)
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  func TestParseGithubURL(t *testing.T) {
    91  	tests := []struct {
    92  		name    string
    93  		path    string
    94  		want    GithubURL
    95  		wanterr bool
    96  	}{
    97  		{
    98  			name:    "empty",
    99  			path:    "",
   100  			wanterr: true,
   101  		},
   102  		{
   103  			name:    "helm chart",
   104  			path:    "stable/mysql",
   105  			wanterr: true,
   106  		},
   107  		{
   108  			name:    "random nonsense",
   109  			path:    "a string of random nonsense",
   110  			wanterr: true,
   111  		},
   112  		{
   113  			name:    "bitbucket repo",
   114  			path:    "bitbucket.org/ww/goautoneg",
   115  			wanterr: true,
   116  		},
   117  		{
   118  			name:    "already configured go-getter string",
   119  			path:    "github.com/replicatedhq/ship?ref=master//pkg/specs",
   120  			wanterr: true,
   121  		},
   122  		{
   123  			name: "mocked github url with tree",
   124  			path: "github.com/OWNER/REPO/tree/REF/SUBDIR",
   125  			want: GithubURL{
   126  				Owner:  "OWNER",
   127  				Repo:   "REPO",
   128  				Ref:    "REF",
   129  				Subdir: "SUBDIR",
   130  			},
   131  		},
   132  		{
   133  			name: "ship repo in pkg dir on master",
   134  			path: "https://github.com/replicatedhq/ship/tree/master/pkg",
   135  			want: GithubURL{
   136  				Owner:  "replicatedhq",
   137  				Repo:   "ship",
   138  				Ref:    "master",
   139  				Subdir: "pkg",
   140  			},
   141  		},
   142  		{
   143  			name: "ship repo in pkg/specs dir on master",
   144  			path: "https://github.com/replicatedhq/ship/tree/master/pkg/specs",
   145  			want: GithubURL{
   146  				Owner:  "replicatedhq",
   147  				Repo:   "ship",
   148  				Ref:    "master",
   149  				Subdir: "pkg/specs",
   150  			},
   151  		},
   152  		{
   153  			name: "ship repo in pkg/specs dir at hash with www",
   154  			path: "https://www.github.com/replicatedhq/ship/tree/atestsha/pkg/specs",
   155  			want: GithubURL{
   156  				Owner:  "replicatedhq",
   157  				Repo:   "ship",
   158  				Ref:    "atestsha",
   159  				Subdir: "pkg/specs",
   160  			},
   161  		},
   162  		{
   163  			name: "ship repo in root dir on master with www",
   164  			path: "https://www.github.com/replicatedhq/ship/tree/master",
   165  			want: GithubURL{
   166  				Owner:  "replicatedhq",
   167  				Repo:   "ship",
   168  				Ref:    "master",
   169  				Subdir: "",
   170  			},
   171  		},
   172  		{
   173  			name: "github repo with no tree",
   174  			path: "https://github.com/replicatedhq/ship",
   175  			want: GithubURL{
   176  				Owner:  "replicatedhq",
   177  				Repo:   "ship",
   178  				Ref:    "default",
   179  				Subdir: "",
   180  			},
   181  		},
   182  		{
   183  			name: "github repo with no tree with www",
   184  			path: "https://www.github.com/replicatedhq/ship",
   185  			want: GithubURL{
   186  				Owner:  "replicatedhq",
   187  				Repo:   "ship",
   188  				Ref:    "default",
   189  				Subdir: "",
   190  			},
   191  		},
   192  		{
   193  			name: "github repo with no tree with subdir",
   194  			path: "https://github.com/replicatedhq/ship/pkg/specs",
   195  			want: GithubURL{
   196  				Owner:  "replicatedhq",
   197  				Repo:   "ship",
   198  				Ref:    "default",
   199  				Subdir: "pkg/specs",
   200  			},
   201  		},
   202  		{
   203  			name: "github repo with no https or tree with subdir",
   204  			path: "github.com/replicatedhq/ship/pkg/specs",
   205  			want: GithubURL{
   206  				Owner:  "replicatedhq",
   207  				Repo:   "ship",
   208  				Ref:    "default",
   209  				Subdir: "pkg/specs",
   210  			},
   211  		},
   212  		{
   213  			name: "ship repo in pkg/specs/chart.go file at hash with www",
   214  			path: "https://www.github.com/replicatedhq/ship/blob/atestsha/pkg/specs/chart.go",
   215  			want: GithubURL{
   216  				Owner:  "replicatedhq",
   217  				Repo:   "ship",
   218  				Ref:    "atestsha",
   219  				Subdir: "pkg/specs/chart.go",
   220  				IsBlob: true,
   221  			},
   222  		},
   223  	}
   224  	for _, tt := range tests {
   225  		t.Run(tt.name, func(t *testing.T) {
   226  			got, err := ParseGithubURL(tt.path, "default")
   227  			if err != nil {
   228  				if tt.wanterr {
   229  					return
   230  				}
   231  				t.Errorf("got unexpected error %s parsing %q", err.Error(), tt.path)
   232  			}
   233  
   234  			if got != tt.want {
   235  				t.Errorf("untreeGithub(%q) = %v, want %v", tt.path, got, tt.want)
   236  			}
   237  		})
   238  	}
   239  }
   240  
   241  func TestGithubURL_URL(t *testing.T) {
   242  	tests := []struct {
   243  		name      string
   244  		githubURL GithubURL
   245  		want      string
   246  	}{
   247  		{
   248  			name: "github tree with no subdir",
   249  			githubURL: GithubURL{
   250  				Owner: "o",
   251  				Repo:  "r",
   252  				Ref:   "master",
   253  			},
   254  			want: "github.com/o/r/tree/master/",
   255  		},
   256  		{
   257  			name: "github tree with a subdir",
   258  			githubURL: GithubURL{
   259  				Owner:  "o",
   260  				Repo:   "r",
   261  				Ref:    "master",
   262  				Subdir: "something/maybe",
   263  			},
   264  			want: "github.com/o/r/tree/master/something/maybe",
   265  		},
   266  		{
   267  			name: "github blob to a file",
   268  			githubURL: GithubURL{
   269  				Owner:  "o",
   270  				Repo:   "r",
   271  				Ref:    "master",
   272  				Subdir: "file.yaml",
   273  				IsBlob: true,
   274  			},
   275  			want: "github.com/o/r/blob/master/file.yaml",
   276  		},
   277  	}
   278  	for _, tt := range tests {
   279  		t.Run(tt.name, func(t *testing.T) {
   280  			req := require.New(t)
   281  			got := tt.githubURL.URL()
   282  			req.Equal(tt.want, got)
   283  		})
   284  	}
   285  }