From 3216dc9b8d876f24ed561e81c2a1fb91926cb57a Mon Sep 17 00:00:00 2001 From: Grégoire Duchêne Date: Thu, 26 Mar 2020 22:00:40 +0000 Subject: Add a few test cases to lib --- lib/lib.go | 11 ++++++----- lib/lib_test.go | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 lib/lib_test.go (limited to 'lib') diff --git a/lib/lib.go b/lib/lib.go index b066e69..a35cb13 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -12,15 +12,16 @@ import ( ) func GetBody(pkg string) string { - dest := strings.TrimRight(os.Getenv("DEST"), "/") + "/" + getRepo(pkg) + dest := GetDest(os.Getenv("PREFIX"), os.Getenv("DEST"), pkg) return fmt.Sprintf(` go-import-redirect `, pkg, os.Getenv("VCS"), dest) } -func getRepo(pkg string) string { - prefix := strings.TrimRight(os.Getenv("PREFIX"), "/") - path := strings.TrimLeft(strings.TrimPrefix(pkg, prefix), "/") - return strings.Split(path, "/")[0] +func GetDest(srcPrefix, destPrefix, pkg string) string { + srcPrefix = strings.TrimRight(srcPrefix, "/") + destPrefix = strings.TrimRight(destPrefix, "/") + path := strings.TrimLeft(strings.TrimPrefix(pkg, srcPrefix), "/") + return destPrefix + "/" + strings.Split(path, "/")[0] } diff --git a/lib/lib_test.go b/lib/lib_test.go new file mode 100644 index 0000000..7b46bfb --- /dev/null +++ b/lib/lib_test.go @@ -0,0 +1,23 @@ +// Copyright (c) 2019, Grégoire Duchêne +// +// Use of this source code is governed by the ISC license that can be +// found in the LICENSE file. + +package lib + +import "testing" + +func TestGetDest(t *testing.T) { + cs := []struct{ srcPrefix, destPrefix, pkg, expected string }{ + {"src.example.com/x/", "https://example.com/git/", "src.example.com/x/foo", "https://example.com/git/foo"}, + {"src.example.com/x/", "https://example.com/git/", "src.example.com/x/foo/bar", "https://example.com/git/foo"}, + {"src.example.com/x", "https://example.com/git", "src.example.com/x/foo", "https://example.com/git/foo"}, + {"src.example.com/x", "https://example.com/git", "src.example.com/x/foo/bar", "https://example.com/git/foo"}, + } + for _, c := range cs { + actual := GetDest(c.srcPrefix, c.destPrefix, c.pkg) + if actual != c.expected { + t.Errorf("expected %s, got %s", c.expected, actual) + } + } +} -- cgit v1.2.3-70-g09d2