From 2986d7a7ed7155a0c9f50c8bf17b23f003ed8be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Michon?= Date: Thu, 11 Jul 2019 15:50:17 +0200 Subject: [PATCH] Add tests --- appdetect/git.go | 36 ++++++++++++++++++------------------ appdetect/git_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 appdetect/git_test.go diff --git a/appdetect/git.go b/appdetect/git.go index 942f1d9e7..9f61a641b 100644 --- a/appdetect/git.go +++ b/appdetect/git.go @@ -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 diff --git a/appdetect/git_test.go b/appdetect/git_test.go new file mode 100644 index 000000000..c9211fa83 --- /dev/null +++ b/appdetect/git_test.go @@ -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) + }) + } +}