Skip to content

Commit 2247db1

Browse files
committed
have env trim whitespace
1 parent ebae6a0 commit 2247db1

4 files changed

Lines changed: 35 additions & 19 deletions

File tree

internal/gitsemver/environment.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package gitsemver
22

3-
import "os"
3+
import (
4+
"os"
5+
"strings"
6+
)
47

5-
// Environment allows us to mock the OS environment
8+
// Environment allows us to mock the OS environment.
9+
// Returns values with leading and trailing space trimmed.
610
type Environment interface {
711
Getenv(string) string
812
LookupEnv(string) (string, bool)
@@ -12,9 +16,11 @@ type Environment interface {
1216
type OsEnvironment struct{}
1317

1418
func (OsEnvironment) Getenv(key string) string {
15-
return os.Getenv(key)
19+
return strings.TrimSpace(os.Getenv(key))
1620
}
1721

18-
func (OsEnvironment) LookupEnv(key string) (string, bool) {
19-
return os.LookupEnv(key)
22+
func (OsEnvironment) LookupEnv(key string) (v string, ok bool) {
23+
v, ok = os.LookupEnv(key)
24+
v = strings.TrimSpace(v)
25+
return
2026
}

internal/gitsemver/environment_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gitsemver_test
22

33
import (
44
"os"
5+
"strings"
56
"testing"
67

78
gitsemver "github.com/linkdata/gitsemver/internal/gitsemver"
@@ -17,7 +18,16 @@ func Test_OsEnvironment_Getenv(t *testing.T) {
1718
}
1819
expect := os.Getenv(VarName)
1920
actual := env.Getenv(VarName)
20-
if expect != actual {
21+
if strings.TrimSpace(expect) != actual {
2122
t.Error(actual)
2223
}
2324
}
25+
26+
func Test_OsEnvironment_Getenv_TrimsSpace(t *testing.T) {
27+
const varName = "MKENV_TEST_TRIM_SPACE"
28+
t.Setenv(varName, " \t trimmed value \n")
29+
env := gitsemver.OsEnvironment{}
30+
if got := env.Getenv(varName); got != "trimmed value" {
31+
t.Fatalf("expected trimmed value, got %q", got)
32+
}
33+
}

internal/gitsemver/gitsemver.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io"
77
"strconv"
8-
"strings"
98
)
109

1110
// GitSemVer holds git metadata used while computing a version.
@@ -58,7 +57,7 @@ func (vs *GitSemVer) IsReleaseBranch(branchName string) bool {
5857

5958
// GitLab gives us the default branch name directly.
6059
if defBranch, ok := vs.Env.LookupEnv("CI_DEFAULT_BRANCH"); ok {
61-
return branchName == strings.TrimSpace(defBranch)
60+
return branchName == defBranch
6261
}
6362

6463
// Fallback to common default branch names.
@@ -145,7 +144,7 @@ func (vs *GitSemVer) examineTags(repo string) (err error) {
145144
// the closest semver tag if none match exactly. It also returns a bool
146145
// that is true if the tree hashes match and there are no uncommitted changes.
147146
func (vs *GitSemVer) GetTag(repo string) (tag string, match bool, err error) {
148-
if ciTag := strings.TrimSpace(vs.Env.Getenv("CI_COMMIT_TAG")); ciTag != "" {
147+
if ciTag := vs.Env.Getenv("CI_COMMIT_TAG"); ciTag != "" {
149148
if isSemverTag(ciTag) {
150149
return ciTag, true, nil
151150
}
@@ -179,9 +178,9 @@ type gitHubPullRequestEvent struct {
179178
}
180179

181180
func (vs *GitSemVer) getBranchGitHub(repo string) (branchName string, err error) {
182-
if branchName = strings.TrimSpace(vs.Env.Getenv("GITHUB_BASE_REF")); branchName == "" {
183-
if refName := strings.TrimSpace(vs.Env.Getenv("GITHUB_REF_NAME")); refName != "" {
184-
if strings.TrimSpace(vs.Env.Getenv("GITHUB_REF_TYPE")) == "tag" {
181+
if branchName = vs.Env.Getenv("GITHUB_BASE_REF"); branchName == "" {
182+
if refName := vs.Env.Getenv("GITHUB_REF_NAME"); refName != "" {
183+
if vs.Env.Getenv("GITHUB_REF_TYPE") == "tag" {
185184
var branches []string
186185
if branches, err = vs.Git.GetBranchesFromTag(repo, refName); err == nil {
187186
for _, branchName = range branches {
@@ -198,10 +197,10 @@ func (vs *GitSemVer) getBranchGitHub(repo string) (branchName string, err error)
198197
}
199198

200199
func (vs *GitSemVer) getBranchGitLab(repo string) (branchName string, err error) {
201-
if branchName = strings.TrimSpace(vs.Env.Getenv("CI_MERGE_REQUEST_TARGET_BRANCH_NAME")); branchName == "" {
202-
if branchName = strings.TrimSpace(vs.Env.Getenv("CI_EXTERNAL_PULL_REQUEST_TARGET_BRANCH_NAME")); branchName == "" {
203-
if branchName = strings.TrimSpace(vs.Env.Getenv("CI_COMMIT_REF_NAME")); branchName != "" {
204-
if strings.TrimSpace(vs.Env.Getenv("CI_COMMIT_TAG")) == branchName {
200+
if branchName = vs.Env.Getenv("CI_MERGE_REQUEST_TARGET_BRANCH_NAME"); branchName == "" {
201+
if branchName = vs.Env.Getenv("CI_EXTERNAL_PULL_REQUEST_TARGET_BRANCH_NAME"); branchName == "" {
202+
if branchName = vs.Env.Getenv("CI_COMMIT_REF_NAME"); branchName != "" {
203+
if vs.Env.Getenv("CI_COMMIT_TAG") == branchName {
205204
var branches []string
206205
if branches, err = vs.Git.GetBranchesFromTag(repo, branchName); err == nil {
207206
for _, branchName = range branches {
@@ -236,8 +235,8 @@ func (vs *GitSemVer) GetBranch(repo string) (branchName string, err error) {
236235
// otherwise the Git commit count is used. Returns an empty string if no reasonable build
237236
// counter can be found.
238237
func (vs *GitSemVer) GetBuild(repo string) (build string, err error) {
239-
if build = strings.TrimSpace(vs.Env.Getenv("CI_PIPELINE_IID")); build == "" {
240-
if build = strings.TrimSpace(vs.Env.Getenv("GITHUB_RUN_NUMBER")); build == "" {
238+
if build = vs.Env.Getenv("CI_PIPELINE_IID"); build == "" {
239+
if build = vs.Env.Getenv("GITHUB_RUN_NUMBER"); build == "" {
241240
build, err = vs.Git.GetBuild(repo)
242241
}
243242
}

internal/gitsemver/mockgitter_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import (
1010
type MockEnvironment map[string]string
1111

1212
func (me MockEnvironment) Getenv(key string) string {
13-
return me[key]
13+
return strings.TrimSpace(me[key])
1414
}
1515

1616
func (me MockEnvironment) LookupEnv(key string) (val string, ok bool) {
1717
val, ok = me[key]
18+
val = strings.TrimSpace(val)
1819
return
1920
}
2021

0 commit comments

Comments
 (0)