aboutsummaryrefslogtreecommitdiff
path: root/internal/rtree
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2020-03-28 14:18:07 +0100
committerStefan Majewsky <majewsky@gmx.net>2020-03-28 14:18:07 +0100
commitfdffeee7dc2a4869c85f0c298e3d12924846724b (patch)
tree8fe00aeba084b491a96aa4139bf57ec4247ff676 /internal/rtree
parent6ed52c386210e21564f9bcc3f29772fe05bf5a43 (diff)
downloadgofu-fdffeee7dc2a4869c85f0c298e3d12924846724b.tar.gz
add test coverage for ExpandRemoteURL, add ContractRemoteURL
Diffstat (limited to 'internal/rtree')
-rw-r--r--internal/rtree/remote.go19
-rw-r--r--internal/rtree/remote_test.go68
2 files changed, 87 insertions, 0 deletions
diff --git a/internal/rtree/remote.go b/internal/rtree/remote.go
index 0c4e880..b2b3340 100644
--- a/internal/rtree/remote.go
+++ b/internal/rtree/remote.go
@@ -49,6 +49,25 @@ func ExpandRemoteURL(remoteURL string) string {
return best.Replacement + strings.TrimPrefix(remoteURL, best.Alias)
}
+//ContractRemoteURL takes the canonical URL for a given remote and shortens it
+//as much as possible by substituting an alias from the system-wide and
+//user-global Git config. This function is pretty much the reverse of
+//ExpandRemoteURL().
+func ContractRemoteURL(remoteURL string) string {
+ var best *RemoteAlias
+ for _, current := range RemoteAliases {
+ if strings.HasPrefix(remoteURL, current.Replacement) {
+ if best == nil || len(best.Replacement) < len(current.Replacement) {
+ best = current
+ }
+ }
+ }
+ if best == nil {
+ return remoteURL
+ }
+ return best.Alias + strings.TrimPrefix(remoteURL, best.Replacement)
+}
+
//This regex recognizes the scp-like syntax for git remotes
//(i.e. "[user@]example.org:path/to/repo") as specified by the "GIT URLS"
//section of man:git-clone(1).
diff --git a/internal/rtree/remote_test.go b/internal/rtree/remote_test.go
new file mode 100644
index 0000000..3841c5b
--- /dev/null
+++ b/internal/rtree/remote_test.go
@@ -0,0 +1,68 @@
+/*******************************************************************************
+*
+* Copyright 2020 Stefan Majewsky <majewsky@gmx.net>
+*
+* This program is free software: you can redistribute it and/or modify it under
+* the terms of the GNU General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later
+* version.
+*
+* This program is distributed in the hope that it will be useful, but WITHOUT ANY
+* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License along with
+* this program. If not, see <http://www.gnu.org/licenses/>.
+*
+*******************************************************************************/
+
+package rtree
+
+import "testing"
+
+var remoteAliasesForExpansionTest = []*RemoteAlias{
+ {Alias: "gh:f:", Replacement: "https://github.com/foo/"},
+ {Alias: "gh:", Replacement: "https://github.com/"},
+ {Alias: "gh:b:", Replacement: "https://github.com/bar/"},
+ {Alias: "gl:", Replacement: "git://gitlab.com/"},
+ {Alias: "test:", Replacement: "test:test:"},
+}
+
+var testExpansions = map[string]string{
+ "gh:foo/bar": "https://github.com/foo/bar",
+ "gh:f:bar": "https://github.com/foo/bar",
+ "gh:b:foo": "https://github.com/bar/foo",
+ "gh:gh:foo/bar": "https://github.com/gh:foo/bar", //do not expand multiple times
+ "test:foo": "test:test:foo", //do not expand recursively
+ "https://github.com/foo/bar": "https://github.com/foo/bar", //no expansion at all
+}
+
+func TestExpandRemoteURL(t *testing.T) {
+ RemoteAliases = remoteAliasesForExpansionTest
+ for input, expected := range testExpansions {
+ actual := ExpandRemoteURL(input)
+ if actual != expected {
+ t.Errorf("expected %q to expand into %q, but got %q", input, expected, actual)
+ }
+ }
+}
+
+//Most of those are just reversed from `testExpansions`.
+var testContractions = map[string]string{
+ "https://github.com/foo/bar": "gh:f:bar",
+ "https://github.com/bar/foo": "gh:b:foo",
+ "https://github.com/qux/foobar": "gh:qux/foobar",
+ "https://github.com/gh:foo/bar": "gh:gh:foo/bar",
+ "test:test:foo": "test:foo",
+ "git://somewhereelse.com/foo/bar": "git://somewhereelse.com/foo/bar",
+}
+
+func TestContractRemoteURL(t *testing.T) {
+ RemoteAliases = remoteAliasesForExpansionTest
+ for input, expected := range testContractions {
+ actual := ContractRemoteURL(input)
+ if actual != expected {
+ t.Errorf("expected %q to contract into %q, but got %q", input, expected, actual)
+ }
+ }
+}