github.com/jonsyu1/godel@v0.0.0-20171017211503-64567a0cf169/apps/distgo/pkg/git/git_test.go (about) 1 // Copyright 2016 Palantir Technologies, Inc. 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 // http://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_test 16 17 import ( 18 "io/ioutil" 19 "path" 20 "testing" 21 22 "github.com/nmiyake/pkg/dirs" 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/require" 25 26 "github.com/palantir/godel/apps/distgo/pkg/git" 27 "github.com/palantir/godel/apps/distgo/pkg/git/gittest" 28 ) 29 30 func TestProjectInfo(t *testing.T) { 31 tmp, cleanup, err := dirs.TempDir("", "") 32 defer cleanup() 33 require.NoError(t, err) 34 35 for i, currCase := range []struct { 36 gitOperations func(gitDir string) 37 want git.ProjectInfo 38 }{ 39 { 40 gitOperations: func(gitDir string) { 41 }, 42 want: git.ProjectInfo{ 43 Version: "unspecified$", 44 Branch: "unspecified$", 45 Revision: "1$", 46 }, 47 }, 48 { 49 gitOperations: func(gitDir string) { 50 gittest.CommitRandomFile(t, gitDir, "Second commit") 51 err = ioutil.WriteFile(path.Join(gitDir, "foo"), []byte("foo"), 0644) 52 require.NoError(t, err) 53 }, 54 want: git.ProjectInfo{ 55 Version: "unspecified$", 56 Branch: "unspecified$", 57 Revision: "2$", 58 }, 59 }, 60 { 61 gitOperations: func(gitDir string) { 62 gittest.CreateGitTag(t, gitDir, "0.0.1") 63 }, 64 want: git.ProjectInfo{ 65 Version: "0.0.1$", 66 Branch: "0.0.1$", 67 Revision: "0$", 68 }, 69 }, 70 { 71 gitOperations: func(gitDir string) { 72 gittest.CreateGitTag(t, gitDir, "v0.0.1") 73 }, 74 want: git.ProjectInfo{ 75 Version: "0.0.1$", 76 Branch: "0.0.1$", 77 Revision: "0$", 78 }, 79 }, 80 { 81 gitOperations: func(gitDir string) { 82 gittest.CreateGitTag(t, gitDir, "0.0.1") 83 err = ioutil.WriteFile(path.Join(gitDir, "foo"), []byte("foo"), 0644) 84 require.NoError(t, err) 85 }, 86 want: git.ProjectInfo{ 87 Version: "0.0.1.dirty$", 88 Branch: "0.0.1$", 89 Revision: "0$", 90 }, 91 }, 92 { 93 gitOperations: func(gitDir string) { 94 gittest.CreateGitTag(t, gitDir, "0.0.1") 95 gittest.CommitRandomFile(t, gitDir, "Second commit") 96 }, 97 want: git.ProjectInfo{ 98 Version: "0.0.1-1-g[a-f0-9]{7}$", 99 Branch: "0.0.1$", 100 Revision: "1$", 101 }, 102 }, 103 { 104 gitOperations: func(gitDir string) { 105 gittest.CreateGitTag(t, gitDir, "0.0.1") 106 gittest.CommitRandomFile(t, gitDir, "Second commit") 107 err = ioutil.WriteFile(path.Join(gitDir, "foo"), []byte("foo"), 0644) 108 require.NoError(t, err) 109 }, 110 want: git.ProjectInfo{ 111 Version: "0.0.1-1-g[a-f0-9]{7}.dirty$", 112 }, 113 }, 114 { 115 gitOperations: func(gitDir string) { 116 gittest.CreateGitTag(t, gitDir, "0.0.1") 117 118 gittest.CreateBranch(t, gitDir, "hotfix-branch") 119 gittest.CommitRandomFile(t, gitDir, "hotfix commit") 120 gittest.CreateGitTag(t, gitDir, "0.0.1-hotfix") 121 122 gittest.RunGitCommand(t, gitDir, "checkout", "master") 123 gittest.Merge(t, gitDir, "hotfix-branch") 124 }, 125 want: git.ProjectInfo{ 126 Version: "^0.0.1-1-g[a-f0-9]{7}$", 127 }, 128 }, 129 { 130 gitOperations: func(gitDir string) { 131 gittest.CreateGitTag(t, gitDir, "0.0.1") 132 133 gittest.CreateBranch(t, gitDir, "hotfix-branch") 134 gittest.CommitRandomFile(t, gitDir, "hotfix commit") 135 gittest.CreateGitTag(t, gitDir, "0.0.1-hotfix") 136 137 gittest.RunGitCommand(t, gitDir, "checkout", "master") 138 gittest.Merge(t, gitDir, "hotfix-branch") 139 140 gittest.CreateGitTag(t, gitDir, "0.0.2") 141 }, 142 want: git.ProjectInfo{ 143 Version: "^0.0.2$", 144 }, 145 }, 146 } { 147 currTmp, err := ioutil.TempDir(tmp, "") 148 require.NoError(t, err) 149 150 gittest.InitGitDir(t, currTmp) 151 currCase.gitOperations(currTmp) 152 153 got, err := git.NewProjectInfo(currTmp) 154 require.NoError(t, err) 155 156 assert.Regexp(t, currCase.want.Version, got.Version, "Case %d", i) 157 assert.Regexp(t, currCase.want.Branch, got.Branch, "Case %d", i) 158 assert.Regexp(t, currCase.want.Revision, got.Revision, "Case %d", i) 159 } 160 } 161 162 func TestIsSnapshotVersion(t *testing.T) { 163 for i, currCase := range []struct { 164 version string 165 isSnapshot bool 166 }{ 167 {"0.1.0-2-g0f9fa0a", true}, 168 {"0.1.0-rc1-2-g0f9fa0a", true}, 169 {"0.0.1", false}, 170 {"0.0.1-rc1", false}, 171 {"0.0.1-rc1.dirty", false}, 172 } { 173 assert.Equal(t, currCase.isSnapshot, git.IsSnapshotVersion(currCase.version), "Case %d", i) 174 } 175 }