Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test help to verify that a test panicked #2194

Merged
merged 2 commits into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions pkg/skaffold/apiversion/apiversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@ limitations under the License.
package apiversion

import (
"reflect"
"testing"

"github.com/GoogleContainerTools/skaffold/testutil"
"github.com/blang/semver"
)

func TestMustParse(t *testing.T) {
_ = MustParse("skaffold/v1alpha4")
MustParse("skaffold/v1alpha4")
}

func TestMustParse_panic(t *testing.T) {
defer func() {
if recover() == nil {
t.Errorf("Should have panicked")
}
}()
_ = MustParse("invalid version")
defer testutil.EnsureTestPanicked(t)

MustParse("invalid version")
}

func TestParseVersion(t *testing.T) {
Expand Down Expand Up @@ -83,13 +80,8 @@ func TestParseVersion(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Parse(tt.args.v)
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Parse() = %v, want %v", got, tt.want)
}

testutil.CheckErrorAndDeepEqual(t, tt.wantErr, err, tt.want, got)
})
}
}
14 changes: 5 additions & 9 deletions pkg/skaffold/yamltags/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,13 @@ func TestValidateStructOneOf(t *testing.T) {
}

func TestValidateStructInvalid(t *testing.T) {
invalidYamlTags := struct {
A string `yamltags:"invalidTag"`
}{A: "whatever"}
defer testutil.EnsureTestPanicked(t)

defer func() {
if recover() == nil {
t.Errorf("should have panicked")
}
}()
invalidTags := struct {
A string `yamltags:"invalidTag"`
}{}

_ = ValidateStruct(invalidYamlTags)
ValidateStruct(invalidTags)
}

func TestIsZeroValue(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ func CheckErrorContains(t *testing.T, message string, err error) {
}
}

func EnsureTestPanicked(t *testing.T) {
if recover() == nil {
t.Errorf("should have panicked")
}
}

// Chdir changes current directory for a test
func Chdir(t *testing.T, dir string) func() {
t.Helper()
Expand Down