-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package controllers | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestLibgit2ErrorTidy(t *testing.T) { | ||
// this is what GitLab sends if the deploy key doesn't have write access | ||
gitlabMessage := `remote: | ||
remote: ======================================================================== | ||
remote: | ||
remote: This deploy key does not have write access to this project. | ||
remote: | ||
remote: ======================================================================== | ||
remote: | ||
` | ||
expectedReformat := "remote: This deploy key does not have write access to this project." | ||
|
||
err := errors.New(gitlabMessage) | ||
err = libgit2PushError(err) | ||
reformattedMessage := err.Error() | ||
if reformattedMessage != expectedReformat { | ||
t.Errorf("expected %q, got %q", expectedReformat, reformattedMessage) | ||
} | ||
} | ||
|
||
func TestLibgit2Multiline(t *testing.T) { | ||
// this is a hypothetical error message, in which the useful | ||
// content spans more than one line | ||
multilineMessage := `remote: | ||
remote: ======================================================================== | ||
remote: | ||
remote: This deploy key does not have write access to this project. | ||
remote: You will need to create a new deploy key. | ||
remote: | ||
remote: ======================================================================== | ||
remote: | ||
` | ||
expectedReformat := "remote: This deploy key does not have write access to this project. You will need to create a new deploy key." | ||
|
||
err := errors.New(multilineMessage) | ||
err = libgit2PushError(err) | ||
reformattedMessage := err.Error() | ||
if reformattedMessage != expectedReformat { | ||
t.Errorf("expected %q, got %q", expectedReformat, reformattedMessage) | ||
} | ||
} | ||
|
||
func TestLibgit2ErrorUnchanged(t *testing.T) { | ||
// this is (roughly) what GitHub sends if the deploy key doesn't have write access | ||
regularMessage := `remote: ERROR: deploy key does not have permissions` | ||
expectedReformat := regularMessage | ||
err := errors.New(regularMessage) | ||
err = libgit2PushError(err) | ||
reformattedMessage := err.Error() | ||
if reformattedMessage != expectedReformat { | ||
t.Errorf("expected %q, got %q", expectedReformat, reformattedMessage) | ||
} | ||
} | ||
|
||
func TestGoGitErrorReplace(t *testing.T) { | ||
// this is what go-git uses as the error message is if the remote | ||
// sends a blank first line | ||
unknownMessage := `unknown error: remote: ` | ||
err := errors.New(unknownMessage) | ||
err = gogitPushError(err) | ||
reformattedMessage := err.Error() | ||
if reformattedMessage == unknownMessage { | ||
t.Errorf("expected rewritten error, got %q", reformattedMessage) | ||
} | ||
} | ||
|
||
func TestGoGitErrorUnchanged(t *testing.T) { | ||
// this is (roughly) what GitHub sends if the deploy key doesn't | ||
// have write access; go-git passes this on verbatim | ||
regularMessage := `remote: ERROR: deploy key does not have write access` | ||
expectedReformat := regularMessage | ||
err := errors.New(regularMessage) | ||
err = gogitPushError(err) | ||
reformattedMessage := err.Error() | ||
// test that it's been rewritten, without checking the exact content | ||
if len(reformattedMessage) > len(expectedReformat) { | ||
t.Errorf("expected %q, got %q", expectedReformat, reformattedMessage) | ||
} | ||
} |