Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
rsync: Fix path extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Geiselhardt-Herms authored and BarbarossaTM committed Apr 19, 2023
1 parent 7c3a145 commit 73c9cc2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
9 changes: 6 additions & 3 deletions sync/lib/rsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
49 changes: 49 additions & 0 deletions sync/lib/rsync_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 73c9cc2

Please sign in to comment.