From af8aa6f991df679244ffc27e69dc585eda53c351 Mon Sep 17 00:00:00 2001 From: Mate Herber Date: Mon, 9 Nov 2020 17:36:03 +0100 Subject: [PATCH] Using errormapper from bitrise-init (#128) * Removed code redundancy --- Gopkg.lock | 11 +- errormapper/errormapper_test.go | 225 ------------------ gitclone/git.go | 2 +- gitclone/steperror.go | 8 +- gitclone/steperror_test.go | 4 +- .../bitrise-init/errormapper}/errormapper.go | 4 +- 6 files changed, 16 insertions(+), 238 deletions(-) delete mode 100644 errormapper/errormapper_test.go rename {errormapper => vendor/github.com/bitrise-io/bitrise-init/errormapper}/errormapper.go (97%) diff --git a/Gopkg.lock b/Gopkg.lock index 95c6edf2..6ba00d25 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -3,11 +3,14 @@ [[projects]] branch = "master" - digest = "1:a0459feb3fd004baba94a39946c947565818583196ad122f6c07df5abe796bb2" + digest = "1:d5f275bbb8c47042361fa47b7f628137a93d0abdef56786584a064aaaa9bd771" name = "github.com/bitrise-io/bitrise-init" - packages = ["step"] + packages = [ + "errormapper", + "step", + ] pruneopts = "UT" - revision = "f6de811405dbad793749fa61d70ad2637e2252e7" + revision = "31fd00edb66aeab944734b9806357b81961f1288" [[projects]] branch = "master" @@ -91,12 +94,14 @@ analyzer-name = "dep" analyzer-version = 1 input-imports = [ + "github.com/bitrise-io/bitrise-init/errormapper", "github.com/bitrise-io/bitrise-init/step", "github.com/bitrise-io/envman/envman", "github.com/bitrise-io/go-steputils/stepconf", "github.com/bitrise-io/go-steputils/tools", "github.com/bitrise-io/go-utils/command", "github.com/bitrise-io/go-utils/command/git", + "github.com/bitrise-io/go-utils/errorutil", "github.com/bitrise-io/go-utils/log", "github.com/bitrise-io/go-utils/pathutil", "github.com/bitrise-io/go-utils/retry", diff --git a/errormapper/errormapper_test.go b/errormapper/errormapper_test.go deleted file mode 100644 index 6b3afac8..00000000 --- a/errormapper/errormapper_test.go +++ /dev/null @@ -1,225 +0,0 @@ -package errormapper - -import ( - "fmt" - "reflect" - "testing" - - "github.com/bitrise-io/bitrise-init/step" -) - -func Test_newDetailedErrorRecommendation(t *testing.T) { - type args struct { - detailedError DetailedError - } - tests := []struct { - name string - args args - want step.Recommendation - }{ - { - name: "newDetailedErrorRecommendation with nil", - args: args{ - detailedError: DetailedError{ - Title: "TestTitle", - Description: "TestDesciption", - }, - }, - want: step.Recommendation{ - DetailedErrorRecKey: DetailedError{ - Title: "TestTitle", - Description: "TestDesciption", - }, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := NewDetailedErrorRecommendation(tt.args.detailedError); !reflect.DeepEqual(got, tt.want) { - t.Errorf("newDetailedErrorRecommendation() = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_getParamAt(t *testing.T) { - type args struct { - index int - params []string - } - tests := []struct { - name string - args args - want string - }{ - { - name: "getParamsAt(0, nil)", - args: args{ - index: 0, - params: nil, - }, - want: UnknownParam, - }, - { - name: "getParamsAt(0, [])", - args: args{ - index: 0, - params: []string{}, - }, - want: UnknownParam, - }, - { - name: "getParamsAt(-1, ['1', '2', '3', '4', '5'])", - args: args{ - index: -1, - params: []string{"1", "2", "3", "4", "5"}, - }, - want: UnknownParam, - }, - { - name: "getParamsAt(5, ['1', '2', '3', '4', '5'])", - args: args{ - index: 5, - params: []string{"1", "2", "3", "4", "5"}, - }, - want: UnknownParam, - }, - { - name: "getParamsAt(0, ['1', '2', '3', '4', '5'])", - args: args{ - index: 0, - params: []string{"1", "2", "3", "4", "5"}, - }, - want: "1", - }, - { - name: "getParamsAt(4, ['1', '2', '3', '4', '5'])", - args: args{ - index: 4, - params: []string{"1", "2", "3", "4", "5"}, - }, - want: "5", - }, - { - name: "getParamsAt(2, ['1', '2', '3', '4', '5'])", - args: args{ - index: 2, - params: []string{"1", "2", "3", "4", "5"}, - }, - want: "3", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := GetParamAt(tt.args.index, tt.args.params); got != tt.want { - t.Errorf("getParamAt() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestPatternErrorMatcher_Run(t *testing.T) { - type fields struct { - defaultBuilder DetailedErrorBuilder - patternToBuilder PatternToDetailedErrorBuilder - } - type args struct { - msg string - } - tests := []struct { - name string - fields fields - args args - want step.Recommendation - }{ - { - name: "Run with defaultBuilder", - fields: fields{ - defaultBuilder: func(params ...string) DetailedError { - return DetailedError{ - Title: "T", - Description: "D", - } - }, - patternToBuilder: map[string]DetailedErrorBuilder{}, - }, - args: args{ - msg: "Test", - }, - want: step.Recommendation{ - DetailedErrorRecKey: DetailedError{ - Title: "T", - Description: "D", - }, - }, - }, - { - name: "Run with patternBuilder", - fields: fields{ - defaultBuilder: func(params ...string) DetailedError { - return DetailedError{ - Title: "DefaultTitle", - Description: "DefaultDesc", - } - }, - patternToBuilder: map[string]DetailedErrorBuilder{ - "Test": func(params ...string) DetailedError { - return DetailedError{ - Title: "PatternTitle", - Description: "PatternDesc", - } - }, - }, - }, - args: args{ - msg: "Test", - }, - want: step.Recommendation{ - DetailedErrorRecKey: DetailedError{ - Title: "PatternTitle", - Description: "PatternDesc", - }, - }, - }, - { - name: "Run with patternBuilder with param", - fields: fields{ - defaultBuilder: func(params ...string) DetailedError { - return DetailedError{ - Title: "DefaultTitle", - Description: "DefaultDesc", - } - }, - patternToBuilder: map[string]DetailedErrorBuilder{ - "Test (.+)!": func(params ...string) DetailedError { - p := GetParamAt(0, params) - return DetailedError{ - Title: "PatternTitle", - Description: fmt.Sprintf("PatternDesc: '%s'", p), - } - }, - }, - }, - args: args{ - msg: "Test WithPatternParam!", - }, - want: step.Recommendation{ - DetailedErrorRecKey: DetailedError{ - Title: "PatternTitle", - Description: "PatternDesc: 'WithPatternParam'", - }, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &PatternErrorMatcher{ - DefaultBuilder: tt.fields.defaultBuilder, - PatternToBuilder: tt.fields.patternToBuilder, - } - if got := m.Run(tt.args.msg); !reflect.DeepEqual(got, tt.want) { - t.Errorf("PatternErrorMatcher.Run() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/gitclone/git.go b/gitclone/git.go index 63b6b5cb..422f7baa 100644 --- a/gitclone/git.go +++ b/gitclone/git.go @@ -13,6 +13,7 @@ import ( "strconv" "strings" + "github.com/bitrise-io/bitrise-init/errormapper" "github.com/bitrise-io/bitrise-init/step" "github.com/bitrise-io/go-utils/command" "github.com/bitrise-io/go-utils/command/git" @@ -21,7 +22,6 @@ import ( "github.com/bitrise-io/go-utils/pathutil" "github.com/bitrise-io/go-utils/retry" "github.com/bitrise-io/go-utils/sliceutil" - "github.com/bitrise-steplib/steps-git-clone/errormapper" ) const ( diff --git a/gitclone/steperror.go b/gitclone/steperror.go index 840c36f2..a01a4fb5 100644 --- a/gitclone/steperror.go +++ b/gitclone/steperror.go @@ -3,8 +3,8 @@ package gitclone import ( "fmt" + "github.com/bitrise-io/bitrise-init/errormapper" "github.com/bitrise-io/bitrise-init/step" - "github.com/bitrise-steplib/steps-git-clone/errormapper" ) func mapRecommendation(tag, errMsg string) step.Recommendation { @@ -85,7 +85,7 @@ func newFetchFailedGenericDetailedError(params ...string) errormapper.DetailedEr } } -func newFetchFailedSSHAccessErrorDetailedError(params ...string) errormapper.DetailedError { +func newFetchFailedSSHAccessErrorDetailedError(...string) errormapper.DetailedError { return errormapper.DetailedError{ Title: "We couldn’t access your repository.", Description: "Please abort the process, double-check your SSH key and try again.", @@ -100,7 +100,7 @@ func newFetchFailedCouldNotFindGitRepoDetailedError(params ...string) errormappe } } -func newFetchFailedHTTPAccessErrorDetailedError(params ...string) errormapper.DetailedError { +func newFetchFailedHTTPAccessErrorDetailedError(...string) errormapper.DetailedError { return errormapper.DetailedError{ Title: "We couldn’t access your repository.", Description: "Please abort the process and try again, by providing the repository with SSH URL.", @@ -115,7 +115,7 @@ func newFetchFailedCouldConnectErrorDetailedError(params ...string) errormapper. } } -func newFetchFailedSamlSSOEnforcedDetailedError(params ...string) errormapper.DetailedError { +func newFetchFailedSamlSSOEnforcedDetailedError(...string) errormapper.DetailedError { return errormapper.DetailedError{ Title: "To access this repository, you need to use SAML SSO.", Description: `Please abort the process, update your SSH settings and try again. You can find out more about using SAML SSO in the Github docs.`, diff --git a/gitclone/steperror_test.go b/gitclone/steperror_test.go index 8d0bcebb..929da86d 100644 --- a/gitclone/steperror_test.go +++ b/gitclone/steperror_test.go @@ -5,12 +5,10 @@ import ( "reflect" "testing" + "github.com/bitrise-io/bitrise-init/errormapper" "github.com/bitrise-io/bitrise-init/step" - "github.com/bitrise-steplib/steps-git-clone/errormapper" ) -var mapRecommendationMock func(tag, errMsg string) step.Recommendation - func Test_mapRecommendation(t *testing.T) { type args struct { tag string diff --git a/errormapper/errormapper.go b/vendor/github.com/bitrise-io/bitrise-init/errormapper/errormapper.go similarity index 97% rename from errormapper/errormapper.go rename to vendor/github.com/bitrise-io/bitrise-init/errormapper/errormapper.go index 50c888fa..c5baa77c 100644 --- a/errormapper/errormapper.go +++ b/vendor/github.com/bitrise-io/bitrise-init/errormapper/errormapper.go @@ -53,7 +53,7 @@ func (m *PatternErrorMatcher) Run(msg string) step.Recommendation { re := regexp.MustCompile(pattern) if re.MatchString(msg) { // [search_string, match1, match2, ...] - matches := re.FindStringSubmatch((msg)) + matches := re.FindStringSubmatch(msg) // Drop the first item, which is always the search_string itself // [search_string] -> [] // [search_string, match1, ...] -> [match1, ...] @@ -65,4 +65,4 @@ func (m *PatternErrorMatcher) Run(msg string) step.Recommendation { detail := m.DefaultBuilder(msg) return NewDetailedErrorRecommendation(detail) -} +} \ No newline at end of file