github.com/buildtool/build-tools@v0.2.29-0.20240322150259-6a1d0a553c23/pkg/ci/github_test.go (about) 1 // MIT License 2 // 3 // Copyright (c) 2018 buildtool 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 package ci 24 25 import ( 26 "os" 27 "path/filepath" 28 "testing" 29 30 "github.com/stretchr/testify/assert" 31 32 "github.com/buildtool/build-tools/pkg/vcs" 33 ) 34 35 func TestGithub_Name(t *testing.T) { 36 ci := &Github{} 37 assert.Equal(t, "Github", ci.Name()) 38 } 39 40 func TestGithub_BuildName(t *testing.T) { 41 ci := &Github{Common: &Common{}, CIBuildName: "/home/runner/work/name"} 42 43 assert.Equal(t, "name", ci.BuildName()) 44 } 45 46 func TestGithub_Override_ImageName(t *testing.T) { 47 ci := &Github{Common: &Common{}, CIBuildName: "/home/runner/work/name"} 48 ci.SetImageName("override") 49 assert.Equal(t, "override", ci.BuildName()) 50 } 51 52 func TestGithub_BuildName_Fallback(t *testing.T) { 53 name, _ := os.MkdirTemp(os.TempDir(), "build-tools") 54 defer func() { _ = os.RemoveAll(name) }() 55 oldpwd, _ := os.Getwd() 56 defer func() { _ = os.Chdir(oldpwd) }() 57 _ = os.Chdir(name) 58 59 ci := &Github{Common: &Common{}} 60 61 assert.Equal(t, filepath.Base(name), ci.BuildName()) 62 } 63 64 func TestGithub_BranchReplaceSlash(t *testing.T) { 65 ci := &Github{CIBranchName: "refs/heads/feature/xyz"} 66 67 assert.Equal(t, "feature_xyz", ci.BranchReplaceSlash()) 68 } 69 70 func TestGithub_Branch_Ref(t *testing.T) { 71 ci := &Github{CIBranchName: "refs/heads/feature1"} 72 73 assert.Equal(t, "feature1", ci.Branch()) 74 } 75 76 func TestGithub_Branch_Tag(t *testing.T) { 77 ci := &Github{CIBranchName: "refs/tags/feature1"} 78 79 assert.Equal(t, "feature1", ci.Branch()) 80 } 81 func TestGithub_Branch(t *testing.T) { 82 ci := &Github{CIBranchName: "feature1"} 83 84 assert.Equal(t, "feature1", ci.Branch()) 85 } 86 87 func TestGithub_Branch_Fallback(t *testing.T) { 88 ci := &Github{Common: &Common{VCS: vcs.NewMockVcs()}} 89 90 assert.Equal(t, "fallback-branch", ci.Branch()) 91 } 92 93 func TestGithub_Commit(t *testing.T) { 94 ci := &Github{CICommit: "sha"} 95 96 assert.Equal(t, "sha", ci.Commit()) 97 } 98 99 func TestGithub_Commit_Fallback(t *testing.T) { 100 ci := &Github{Common: &Common{VCS: vcs.NewMockVcs()}} 101 102 assert.Equal(t, "fallback-sha", ci.Commit()) 103 }