Skip to content

Commit f0001de

Browse files
committed
Test that error rewriting works as intended
This commit adds a handful of tests to make sure errors in the problematic formats are rewritten, and other errors are left alone. I'm reluctant to test against the actual git providers, since that would introduce a dependency on them. Thus, these tests won't guard against the providers changing their messages. Signed-off-by: Michael Bridgen <[email protected]>
1 parent 12a339a commit f0001de

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

controllers/git_error_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Copyright 2020, 2021 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controllers
18+
19+
import (
20+
"errors"
21+
"testing"
22+
)
23+
24+
func TestLibgit2ErrorTidy(t *testing.T) {
25+
// this is what GitLab sends if the deploy key doesn't have write access
26+
gitlabMessage := `remote:
27+
remote: ========================================================================
28+
remote:
29+
remote: This deploy key does not have write access to this project.
30+
remote:
31+
remote: ========================================================================
32+
remote:
33+
`
34+
expectedReformat := "remote: This deploy key does not have write access to this project."
35+
36+
err := errors.New(gitlabMessage)
37+
err = libgit2PushError(err)
38+
reformattedMessage := err.Error()
39+
if reformattedMessage != expectedReformat {
40+
t.Errorf("expected %q, got %q", expectedReformat, reformattedMessage)
41+
}
42+
}
43+
44+
func TestLibgit2Multiline(t *testing.T) {
45+
// this is a hypothetical error message, in which the useful
46+
// content spans more than one line
47+
multilineMessage := `remote:
48+
remote: ========================================================================
49+
remote:
50+
remote: This deploy key does not have write access to this project.
51+
remote: You will need to create a new deploy key.
52+
remote:
53+
remote: ========================================================================
54+
remote:
55+
`
56+
expectedReformat := "remote: This deploy key does not have write access to this project. You will need to create a new deploy key."
57+
58+
err := errors.New(multilineMessage)
59+
err = libgit2PushError(err)
60+
reformattedMessage := err.Error()
61+
if reformattedMessage != expectedReformat {
62+
t.Errorf("expected %q, got %q", expectedReformat, reformattedMessage)
63+
}
64+
}
65+
66+
func TestLibgit2ErrorUnchanged(t *testing.T) {
67+
// this is (roughly) what GitHub sends if the deploy key doesn't have write access
68+
regularMessage := `remote: ERROR: deploy key does not have permissions`
69+
expectedReformat := regularMessage
70+
err := errors.New(regularMessage)
71+
err = libgit2PushError(err)
72+
reformattedMessage := err.Error()
73+
if reformattedMessage != expectedReformat {
74+
t.Errorf("expected %q, got %q", expectedReformat, reformattedMessage)
75+
}
76+
}
77+
78+
func TestGoGitErrorReplace(t *testing.T) {
79+
// this is what go-git uses as the error message is if the remote
80+
// sends a blank first line
81+
unknownMessage := `unknown error: remote: `
82+
err := errors.New(unknownMessage)
83+
err = gogitPushError(err)
84+
reformattedMessage := err.Error()
85+
if reformattedMessage == unknownMessage {
86+
t.Errorf("expected rewritten error, got %q", reformattedMessage)
87+
}
88+
}
89+
90+
func TestGoGitErrorUnchanged(t *testing.T) {
91+
// this is (roughly) what GitHub sends if the deploy key doesn't
92+
// have write access; go-git passes this on verbatim
93+
regularMessage := `remote: ERROR: deploy key does not have write access`
94+
expectedReformat := regularMessage
95+
err := errors.New(regularMessage)
96+
err = gogitPushError(err)
97+
reformattedMessage := err.Error()
98+
// test that it's been rewritten, without checking the exact content
99+
if len(reformattedMessage) > len(expectedReformat) {
100+
t.Errorf("expected %q, got %q", expectedReformat, reformattedMessage)
101+
}
102+
}

0 commit comments

Comments
 (0)