github.com/stecky/yet-another-chart-releaser@v0.4.4/pkg/git/git_test.go (about)

     1  // Copyright © 2021 Steven Barnes <stecky@users.noreply.github.com>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package git
    16  
    17  import (
    18  	"os"
    19  	"os/exec"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestGit_GetPushURL(t *testing.T) {
    26  	curDir, _ := os.Getwd()
    27  	repoPath := t.TempDir()
    28  	repoDirErr := os.Chdir(repoPath)
    29  	if repoDirErr != nil {
    30  		t.Error(repoDirErr.Error())
    31  	}
    32  
    33  	t.Cleanup(func() {
    34  		chdirErr := os.Chdir(curDir)
    35  		if chdirErr != nil {
    36  			t.Error(chdirErr.Error())
    37  		}
    38  	})
    39  
    40  	_, initErr := exec.Command("git", "init").Output()
    41  	if initErr != nil {
    42  		t.Error(initErr.Error())
    43  	}
    44  
    45  	tests := []struct {
    46  		name    string
    47  		repo    string
    48  		remote  string
    49  		url     string
    50  		token   string
    51  		pushUrl string
    52  	}{
    53  		{
    54  			name:    "Public GitHub",
    55  			repo:    "publicrepo",
    56  			remote:  "public",
    57  			url:     "https://github.com/org/publicrepo",
    58  			token:   "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",
    59  			pushUrl: "https://x-access-token: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@github.com/org/publicrepo",
    60  		},
    61  		{
    62  			name:    "GitHub Enterprise",
    63  			repo:    "privaterepo",
    64  			remote:  "enterprise",
    65  			url:     "https://github.example.com/org/privaterepo",
    66  			token:   "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",
    67  			pushUrl: "https://x-access-token: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@github.example.com/org/privaterepo",
    68  		},
    69  	}
    70  
    71  	for _, tt := range tests {
    72  		t.Run(tt.name, func(t *testing.T) {
    73  			_, addErr := exec.Command("git", "remote", "add", tt.remote, tt.url).Output()
    74  			if addErr != nil {
    75  				t.Error(addErr.Error())
    76  			}
    77  
    78  			g := Git{}
    79  			pushUrl, pushErr := g.GetPushURL(tt.remote, tt.token)
    80  
    81  			require.Empty(t, pushErr)
    82  			require.EqualValues(t, pushUrl, tt.pushUrl)
    83  		})
    84  	}
    85  }