diff --git a/sync/lib/rsync.go b/sync/lib/rsync.go index 030e595..c088cb3 100644 --- a/sync/lib/rsync.go +++ b/sync/lib/rsync.go @@ -27,9 +27,12 @@ func ExtractFoldersPathFromRsyncURL(url string) (string, error) { return "", fmt.Errorf("%q is not an rsync URL", url) } - filePath := strings.TrimPrefix(url, RsyncProtoPrefix) - parts := strings.Split(filePath, "/") - return strings.Join(parts[0:len(parts)-1], "/"), nil + fp := strings.Split(strings.TrimPrefix(url, RsyncProtoPrefix), "/") + if wantedFileExtensionRE.Match([]byte(fp[len(fp)-1])) { + fp = fp[:len(fp)-1] + } + + return strings.Join(fp, "/"), nil } func ExtractFilePathFromRsyncURL(url string) (string, error) { diff --git a/sync/lib/rsync_test.go b/sync/lib/rsync_test.go new file mode 100644 index 0000000..f3fd08a --- /dev/null +++ b/sync/lib/rsync_test.go @@ -0,0 +1,49 @@ +package syncpki + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestExtractFoldersPathFromRsyncURL(t *testing.T) { + tests := []struct { + name string + url string + wantFail bool + expected string + }{ + { + name: "Valid URL", + url: "rsync://r.magellan.ipxo.com/repo", + wantFail: false, + expected: "r.magellan.ipxo.com/repo", + }, + { + name: "Invalid URL", + url: "xxxx://r.magellan.ipxo.com/repo", + wantFail: true, + }, + { + name: "Valid URL with file", + url: "rsync://r.magellan.ipxo.com/repo/foo.roa", + wantFail: false, + expected: "r.magellan.ipxo.com/repo", + }, + } + + for _, test := range tests { + res, err := ExtractFoldersPathFromRsyncURL(test.url) + if test.wantFail && err == nil { + t.Errorf("unexpected success for %q", test.name) + continue + } + + if !test.wantFail && err != nil { + t.Errorf("unexpected error for %q: %v", test.name, err) + continue + } + + assert.Equal(t, test.expected, res, test.name) + } +}