github.com/buildtool/build-tools@v0.2.29-0.20240322150259-6a1d0a553c23/pkg/config/vcs_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 config 24 25 import ( 26 "os" 27 "path/filepath" 28 "testing" 29 30 "github.com/apex/log" 31 "github.com/stretchr/testify/assert" 32 mocks "gitlab.com/unboundsoftware/apex-mocks" 33 34 "github.com/buildtool/build-tools/pkg/vcs" 35 ) 36 37 func TestIdentify(t *testing.T) { 38 os.Clearenv() 39 40 dir, _ := os.MkdirTemp("", "build-tools") 41 defer func() { _ = os.RemoveAll(dir) }() 42 43 logMock := mocks.New() 44 log.SetHandler(logMock) 45 log.SetLevel(log.DebugLevel) 46 result := vcs.Identify(dir) 47 assert.NotNil(t, result) 48 assert.Equal(t, "none", result.Name()) 49 assert.Equal(t, "", result.Commit()) 50 assert.Equal(t, "", result.Branch()) 51 logMock.Check(t, []string{}) 52 } 53 54 func TestGit_Identify(t *testing.T) { 55 dir, _ := os.MkdirTemp(os.TempDir(), "build-tools") 56 defer func() { _ = os.RemoveAll(dir) }() 57 58 hash, _ := InitRepoWithCommit(dir) 59 60 logMock := mocks.New() 61 log.SetHandler(logMock) 62 log.SetLevel(log.DebugLevel) 63 result := vcs.Identify(dir) 64 assert.NotNil(t, result) 65 assert.Equal(t, "Git", result.Name()) 66 assert.Equal(t, hash.String(), result.Commit()) 67 assert.Equal(t, "master", result.Branch()) 68 logMock.Check(t, []string{}) 69 } 70 71 func TestGit_Identify_Subdirectory(t *testing.T) { 72 dir, _ := os.MkdirTemp(os.TempDir(), "build-tools") 73 defer func() { _ = os.RemoveAll(dir) }() 74 75 hash, _ := InitRepoWithCommit(dir) 76 77 subdir := filepath.Join(dir, "subdir") 78 _ = os.Mkdir(subdir, 0777) 79 80 logMock := mocks.New() 81 log.SetHandler(logMock) 82 log.SetLevel(log.DebugLevel) 83 result := vcs.Identify(subdir) 84 assert.NotNil(t, result) 85 assert.Equal(t, "Git", result.Name()) 86 assert.Equal(t, hash.String(), result.Commit()) 87 assert.Equal(t, "master", result.Branch()) 88 logMock.Check(t, []string{}) 89 } 90 91 func TestGit_MissingRepo(t *testing.T) { 92 dir, _ := os.MkdirTemp(os.TempDir(), "build-tools") 93 defer func() { _ = os.RemoveAll(dir) }() 94 95 _ = os.Mkdir(filepath.Join(dir, ".git"), 0777) 96 97 logMock := mocks.New() 98 log.SetHandler(logMock) 99 log.SetLevel(log.DebugLevel) 100 result := vcs.Identify(dir) 101 assert.NotNil(t, result) 102 assert.Equal(t, "", result.Commit()) 103 assert.Equal(t, "", result.Branch()) 104 logMock.Check(t, []string{}) 105 } 106 107 func TestGit_NoCommit(t *testing.T) { 108 dir, _ := os.MkdirTemp(os.TempDir(), "build-tools") 109 defer func() { _ = os.RemoveAll(dir) }() 110 111 InitRepo(dir) 112 113 logMock := mocks.New() 114 log.SetHandler(logMock) 115 log.SetLevel(log.DebugLevel) 116 result := vcs.Identify(dir) 117 assert.NotNil(t, result) 118 assert.Equal(t, "", result.Commit()) 119 assert.Equal(t, "", result.Branch()) 120 logMock.Check(t, []string{"debug: Unable to fetch head: reference not found\n"}) 121 }