Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneM committed Jul 11, 2019
1 parent 60a257d commit 2986d7a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
36 changes: 18 additions & 18 deletions appdetect/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,29 @@ func ScalingoRepo(directory string, remoteName string) (string, error) {
for _, remote := range remotes {
if remote.Config().Name == remoteName ||
remote.Config().Name == altRemoteName {
// The Git remote URL may look like:
// - agora-fr1: git@host:appName.git
// - osc-fr1: ssh://git@host:port/appName.git
// - GitHub: git@github.com:owner/appName.git
//
// The following line extract the application name from it.
splittedURL := strings.Split(strings.TrimSuffix(remote.Config().URLs[0], ".git"), ":")
if len(splittedURL) < 2 {
return "", errgo.Notef(err, "fail to parse remote URL")
}
appName := splittedURL[len(splittedURL)-1]
// appName may contain "port/appName" or "owner/appName". We keep the part
// after the slash.
i := strings.LastIndex(appName, "/")
if i != -1 {
appName = appName[i+1:]
}
return appName, nil
return getAppNameFromGitRemote(remote.Config().URLs[0]), nil
}
}
return "", errgo.Newf("Scalingo Git remote hasn't been found")
}

// getAppNameFromGitRemote parses a Git remote and return the app name extracted
// out of it. The Git remote URL may look like:
// - agora-fr1: git@host:appName.git
// - SSH on a custom port: ssh://git@host:port/appName.git
// - GitHub: git@github.com:owner/appName.git
func getAppNameFromGitRemote(url string) string {
splittedURL := strings.Split(strings.TrimSuffix(url, ".git"), ":")
appName := splittedURL[len(splittedURL)-1]
// appName may contain "port/appName" or "owner/appName". We keep the part
// after the slash.
i := strings.LastIndex(appName, "/")
if i != -1 {
appName = appName[i+1:]
}
return appName
}

func ScalingoRepoAutoComplete(dir string) []string {
var repos []string

Expand Down
34 changes: 34 additions & 0 deletions appdetect/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package appdetect

import (
"testing"

"github.com/bmizerany/assert"
)

func TestGetAppNameFromGitRemote(t *testing.T) {
tests := map[string]struct {
url string
expectedAppName string
}{
"Given a Git remote like on agora-fr1": {
url: "git@scalingo.com:my-app.git",
expectedAppName: "my-app",
},
"Given a Git remote if SSH on a cutom port": {
url: "ssh://git@ssh.osc-fr1.scalingo.com:2200/my-app.git",
expectedAppName: "my-app",
},
"Given a Git remote like on GitHub": {
url: "git@github.com:my-owner/my-app.git",
expectedAppName: "my-app",
},
}

for msg, test := range tests {
t.Run(msg, func(t *testing.T) {
appName := getAppNameFromGitRemote(test.url)
assert.Equal(t, test.expectedAppName, appName)
})
}
}

0 comments on commit 2986d7a

Please sign in to comment.