-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make --scm-base-url more fool-proof (#95)
- Loading branch information
Showing
3 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package scm | ||
|
||
import "strings" | ||
|
||
// ScmBaseDomain represent the base domain for a SCM provider. | ||
type ScmBaseDomain string | ||
|
||
var schemePrefixes = []string{"https://", "http://"} | ||
|
||
func (d *ScmBaseDomain) Set(value string) error { | ||
for _, prefix := range schemePrefixes { | ||
value = strings.TrimPrefix(value, prefix) | ||
} | ||
value = strings.TrimRight(value, "/") | ||
|
||
*d = ScmBaseDomain(value) | ||
return nil | ||
} | ||
|
||
func (d *ScmBaseDomain) String() string { | ||
if d == nil { | ||
return "" | ||
} | ||
return string(*d) | ||
} | ||
|
||
func (d *ScmBaseDomain) Type() string { | ||
return "string" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package scm | ||
|
||
import "testing" | ||
|
||
var tests = map[string]struct { | ||
input string | ||
expected string | ||
}{ | ||
"strip https": { | ||
input: "https://scm.com", | ||
expected: "scm.com", | ||
}, | ||
"strip http": { | ||
input: "http://example.scm.com", | ||
expected: "example.scm.com", | ||
}, | ||
"ignore": { | ||
input: "scm.com", | ||
expected: "scm.com", | ||
}, | ||
"empty": { | ||
input: "", | ||
expected: "", | ||
}, | ||
"trailing slash": { | ||
input: "https://scm.com/", | ||
expected: "scm.com", | ||
}, | ||
"sub path": { | ||
input: "https://scm.com/sub/domain", | ||
expected: "scm.com/sub/domain", | ||
}, | ||
} | ||
|
||
func TestScmBaseDomain(t *testing.T) { | ||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
var d ScmBaseDomain | ||
err := d.Set(test.input) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
s := d.String() | ||
if s != test.expected { | ||
t.Errorf("expected %s, got %s", test.expected, s) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestScmBaseDomainNil(t *testing.T) { | ||
var d ScmBaseDomain | ||
if d.String() != "" { | ||
t.Error("expected default value of to be \"\"") | ||
} | ||
} |