Skip to content

Commit e29f559

Browse files
authored
Update/trailingslash (gabrie30#178)
1 parent d7f79c8 commit e29f559

File tree

5 files changed

+99
-6
lines changed

5 files changed

+99
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
1212
### Removed
1313
### Fixed
1414
- Gitlab token length requirements; thanks @dschafhauser
15+
- Appending trailing slashes on urls and filepaths; thanks @dschafhauser
1516
### Security
1617

1718
## [1.7.5] - 12/11/21

cmd/clone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var cloneCmd = &cobra.Command{
3030
func cloneFunc(cmd *cobra.Command, argz []string) {
3131

3232
if cmd.Flags().Changed("path") {
33-
absolutePath := configs.EnsureTrailingSlash((cmd.Flag("path").Value.String()))
33+
absolutePath := configs.EnsureTrailingSlashOnFilePath((cmd.Flag("path").Value.String()))
3434
os.Setenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO", absolutePath)
3535
}
3636

cmd/root.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ func getOrSetDefaults(envVar string) {
109109
} else {
110110
s := viper.GetString(envVar)
111111
// envs that need a trailing slash
112-
if envVar == "GHORG_SCM_BASE_URL" || envVar == "GHORG_ABSOLUTE_PATH_TO_CLONE_TO" {
113-
os.Setenv(envVar, configs.EnsureTrailingSlash(s))
112+
if envVar == "GHORG_SCM_BASE_URL" {
113+
os.Setenv(envVar, configs.EnsureTrailingSlashOnURL(s))
114+
} else if envVar == "GHORG_ABSOLUTE_PATH_TO_CLONE_TO" {
115+
os.Setenv(envVar, configs.EnsureTrailingSlashOnFilePath(s))
114116
} else {
115117
os.Setenv(envVar, s)
116118
}

configs/configs.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,25 @@ func isZero(value interface{}) bool {
6161
return value == reflect.Zero(reflect.TypeOf(value)).Interface()
6262
}
6363

64+
// EnsureTrailingSlashOnURL takes a url and ensures a single / is appened
65+
func EnsureTrailingSlashOnURL(s string) string {
66+
trailing := "/"
67+
68+
if !strings.HasSuffix(s, trailing) {
69+
s = s + trailing
70+
}
71+
72+
return s
73+
}
74+
6475
func GetAbsolutePathToCloneTo() string {
6576
path := HomeDir()
6677
path = filepath.Join(path, "ghorg")
67-
return EnsureTrailingSlash(path)
78+
return EnsureTrailingSlashOnFilePath(path)
6879
}
6980

70-
// EnsureTrailingSlash takes a string and ensures a single / is appened
71-
func EnsureTrailingSlash(s string) string {
81+
// EnsureTrailingSlashOnFilePath takes a filepath and ensures a single / is appened
82+
func EnsureTrailingSlashOnFilePath(s string) string {
7283
trailing := GetCorrectFilePathSeparator()
7384

7485
if !strings.HasSuffix(s, trailing) {

configs/configs_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package configs_test
22

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

78
"github.com/gabrie30/ghorg/configs"
@@ -95,3 +96,81 @@ func TestVerifyConfigsSetCorrectly(t *testing.T) {
9596

9697
})
9798
}
99+
100+
func TestVerifyTrialingSlashes(t *testing.T) {
101+
102+
t.Run("When cloning github", func(tt *testing.T) {
103+
os.Setenv("GHORG_SCM_TYPE", "github")
104+
os.Setenv("GHORG_GITHUB_TOKEN", "")
105+
106+
err := configs.VerifyTokenSet()
107+
if err != configs.ErrNoGitHubToken {
108+
tt.Errorf("Expected ErrNoGitHubTokenError, got: %v", err)
109+
}
110+
111+
})
112+
113+
t.Run("When cloning gitlab", func(tt *testing.T) {
114+
os.Setenv("GHORG_SCM_TYPE", "gitlab")
115+
os.Setenv("GHORG_GITLAB_TOKEN", "")
116+
117+
err := configs.VerifyTokenSet()
118+
if err != configs.ErrNoGitLabToken {
119+
tt.Errorf("Expected ErrNoGitLabTokenError, got: %v", err)
120+
}
121+
122+
})
123+
124+
t.Run("When cloning bitbucket with no username", func(tt *testing.T) {
125+
os.Setenv("GHORG_SCM_TYPE", "bitbucket")
126+
os.Setenv("GHORG_BITBUCKET_USERNAME", "")
127+
os.Setenv("GHORG_BITBUCKET_APP_PASSWORD", "12345678912345678901")
128+
err := configs.VerifyTokenSet()
129+
if err != configs.ErrNoBitbucketUsername {
130+
tt.Errorf("Expected ErrNoBitbucketUsername, got: %v", err)
131+
}
132+
133+
})
134+
135+
t.Run("When cloning bitbucket with username but no app password", func(tt *testing.T) {
136+
os.Setenv("GHORG_SCM_TYPE", "bitbucket")
137+
os.Setenv("GHORG_BITBUCKET_USERNAME", "bitbucketuser")
138+
os.Setenv("GHORG_BITBUCKET_APP_PASSWORD", "")
139+
err := configs.VerifyTokenSet()
140+
if err != configs.ErrNoBitbucketAppPassword {
141+
tt.Errorf("Expected ErrNoBitbucketAppPassword, got: %v", err)
142+
}
143+
144+
})
145+
}
146+
147+
func TestTrailingSlashes(t *testing.T) {
148+
t.Run("URL's with no trailing slash should get one", func(tt *testing.T) {
149+
got := configs.EnsureTrailingSlashOnURL("github.com")
150+
want := "github.com/"
151+
if got != want {
152+
tt.Errorf("Expected %v, got: %v", want, got)
153+
}
154+
155+
})
156+
157+
t.Run("URL's with a trailing slash should only have one", func(tt *testing.T) {
158+
got := configs.EnsureTrailingSlashOnURL("github.com/")
159+
want := "github.com/"
160+
if got != want {
161+
tt.Errorf("Expected %v, got: %v", want, got)
162+
}
163+
164+
})
165+
166+
t.Run("Filepaths should be correctly appeneded", func(tt *testing.T) {
167+
got := configs.EnsureTrailingSlashOnFilePath("foo")
168+
want := "foo/"
169+
if runtime.GOOS == "windows" {
170+
want = "foo\\"
171+
}
172+
if got != want {
173+
tt.Errorf("Expected %v, got: %v", want, got)
174+
}
175+
})
176+
}

0 commit comments

Comments
 (0)