Skip to content

Commit

Permalink
Switch to counterfeiter and testify (#85)
Browse files Browse the repository at this point in the history
- Generate fakes using counterfeiter.
- Use testify for assertions.
  • Loading branch information
Kristian authored Mar 14, 2019
1 parent 59dfa29 commit 4baae67
Show file tree
Hide file tree
Showing 12 changed files with 1,049 additions and 423 deletions.
48 changes: 15 additions & 33 deletions check_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package resource_test

import (
"reflect"
"testing"

"github.com/golang/mock/gomock"
"github.com/telia-oss/github-pr-resource"
"github.com/telia-oss/github-pr-resource/mocks"
"github.com/stretchr/testify/assert"
resource "github.com/telia-oss/github-pr-resource"
"github.com/telia-oss/github-pr-resource/fakes"
)

var (
Expand Down Expand Up @@ -141,29 +140,20 @@ func TestCheck(t *testing.T) {

for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
github := new(fakes.FakeGithub)
github.ListOpenPullRequestsReturns(tc.pullRequests, nil)

github := mocks.NewMockGithub(ctrl)
github.EXPECT().ListOpenPullRequests().Times(1).Return(tc.pullRequests, nil)

if len(tc.files) > 0 {
// TODO: Figure out how to do this in a loop with variables. As is, it will break when adding new tests.
gomock.InOrder(
github.EXPECT().ListModifiedFiles(gomock.Any()).Times(1).Return(tc.files[0], nil),
github.EXPECT().ListModifiedFiles(gomock.Any()).Times(1).Return(tc.files[1], nil),
)
for i, file := range tc.files {
github.ListModifiedFilesReturnsOnCall(i, file, nil)
}

input := resource.CheckRequest{Source: tc.source, Version: tc.version}
output, err := resource.Check(input, github)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if got, want := output, tc.expected; !reflect.DeepEqual(got, want) {
t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want)
if assert.NoError(t, err) {
assert.Equal(t, tc.expected, output)
}
assert.Equal(t, 1, github.ListOpenPullRequestsCallCount())
})
}
}
Expand Down Expand Up @@ -214,9 +204,7 @@ func TestContainsSkipCI(t *testing.T) {
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
got := resource.ContainsSkipCI(tc.message)
if got != tc.want {
t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, tc.want)
}
assert.Equal(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -282,11 +270,8 @@ func TestFilterPath(t *testing.T) {
for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
got, err := resource.FilterPath(tc.files, tc.pattern)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("\ngot:\n%v\nwant:\n%s\n", got, tc.want)
if assert.NoError(t, err) {
assert.Equal(t, tc.want, got)
}
})
}
Expand Down Expand Up @@ -353,11 +338,8 @@ func TestFilterIgnorePath(t *testing.T) {
for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
got, err := resource.FilterIgnorePath(tc.files, tc.pattern)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("\ngot:\n%v\nwant:\n%s\n", got, tc.want)
if assert.NoError(t, err) {
assert.Equal(t, tc.want, got)
}
})
}
Expand Down
56 changes: 19 additions & 37 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
"time"

"github.com/telia-oss/github-pr-resource"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
resource "github.com/telia-oss/github-pr-resource"
)

var (
Expand Down Expand Up @@ -113,17 +114,13 @@ func TestCheckE2E(t *testing.T) {
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
github, err := resource.NewGithubClient(&tc.source)
if err != nil {
t.Fatalf("failed to create github client: %s", err)
}
require.NoError(t, err)

input := resource.CheckRequest{Source: tc.source, Version: tc.version}
output, err := resource.Check(input, github)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if got, want := output, tc.expected; !reflect.DeepEqual(got, want) {
t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want)

if assert.NoError(t, err) {
assert.Equal(t, tc.expected, output)
}
})
}
Expand Down Expand Up @@ -200,49 +197,34 @@ func TestGetAndPutE2E(t *testing.T) {
t.Run(tc.description, func(t *testing.T) {
// Create temporary directory
dir, err := ioutil.TempDir("", "github-pr-resource")
if err != nil {
t.Fatalf("failed to create temporary directory")
}
require.NoError(t, err)
defer os.RemoveAll(dir)

github, err := resource.NewGithubClient(&tc.source)
if err != nil {
t.Fatalf("failed to create github client: %s", err)
}
require.NoError(t, err)

git, err := resource.NewGitClient(&tc.source, dir, ioutil.Discard)
if err != nil {
t.Fatalf("failed to create git client: %s", err)
}
require.NoError(t, err)

// Get (output and files)
getRequest := resource.GetRequest{Source: tc.source, Version: tc.version, Params: tc.getParameters}
getOutput, err := resource.Get(getRequest, github, git, dir)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if got, want := getOutput.Version, tc.version; !reflect.DeepEqual(got, want) {
t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want)
}

require.NoError(t, err)
assert.Equal(t, tc.version, getOutput.Version)

version := readTestFile(t, filepath.Join(dir, ".git", "resource", "version.json"))
if got, want := version, tc.versionString; got != want {
t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want)
}
assert.Equal(t, tc.versionString, version)

metadata := readTestFile(t, filepath.Join(dir, ".git", "resource", "metadata.json"))
if got, want := metadata, tc.metadataString; got != want {
t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want)
}
assert.Equal(t, tc.metadataString, metadata)

// Put
putRequest := resource.PutRequest{Source: tc.source, Params: tc.putParameters}
putOutput, err := resource.Put(putRequest, github, dir)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if got, want := putOutput.Version, tc.version; !reflect.DeepEqual(got, want) {
t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want)
}

require.NoError(t, err)
assert.Equal(t, tc.version, putOutput.Version)
})
}
}
Expand Down
Loading

0 comments on commit 4baae67

Please sign in to comment.