Skip to content

Commit

Permalink
Remove global test variables in favor of local. (#766)
Browse files Browse the repository at this point in the history
This refactor removes the global test variables used by all tests,
replacing them with local variables that are independent in each test.

This is better style, easier to read and be confident about correctness,
and allows tests to be parallelized in the future.

Resolves #762.
  • Loading branch information
chriskingnet authored and dmitshur committed Dec 2, 2017
1 parent b527232 commit df88dd9
Show file tree
Hide file tree
Showing 70 changed files with 898 additions and 505 deletions.
38 changes: 28 additions & 10 deletions github/activity_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestActivityService_ListEvents(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -39,7 +39,7 @@ func TestActivityService_ListEvents(t *testing.T) {
}

func TestActivityService_ListRepositoryEvents(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/events", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -63,12 +63,15 @@ func TestActivityService_ListRepositoryEvents(t *testing.T) {
}

func TestActivityService_ListRepositoryEvents_invalidOwner(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()

_, _, err := client.Activity.ListRepositoryEvents(context.Background(), "%", "%", nil)
testURLParseError(t, err)
}

func TestActivityService_ListIssueEventsForRepository(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/issues/events", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -92,12 +95,15 @@ func TestActivityService_ListIssueEventsForRepository(t *testing.T) {
}

func TestActivityService_ListIssueEventsForRepository_invalidOwner(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()

_, _, err := client.Activity.ListIssueEventsForRepository(context.Background(), "%", "%", nil)
testURLParseError(t, err)
}

func TestActivityService_ListEventsForRepoNetwork(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/networks/o/r/events", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -121,12 +127,15 @@ func TestActivityService_ListEventsForRepoNetwork(t *testing.T) {
}

func TestActivityService_ListEventsForRepoNetwork_invalidOwner(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()

_, _, err := client.Activity.ListEventsForRepoNetwork(context.Background(), "%", "%", nil)
testURLParseError(t, err)
}

func TestActivityService_ListEventsForOrganization(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/orgs/o/events", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -150,12 +159,15 @@ func TestActivityService_ListEventsForOrganization(t *testing.T) {
}

func TestActivityService_ListEventsForOrganization_invalidOrg(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()

_, _, err := client.Activity.ListEventsForOrganization(context.Background(), "%", nil)
testURLParseError(t, err)
}

func TestActivityService_ListEventsPerformedByUser_all(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/users/u/events", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -179,7 +191,7 @@ func TestActivityService_ListEventsPerformedByUser_all(t *testing.T) {
}

func TestActivityService_ListEventsPerformedByUser_publicOnly(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/users/u/events/public", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -199,12 +211,15 @@ func TestActivityService_ListEventsPerformedByUser_publicOnly(t *testing.T) {
}

func TestActivityService_ListEventsPerformedByUser_invalidUser(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()

_, _, err := client.Activity.ListEventsPerformedByUser(context.Background(), "%", false, nil)
testURLParseError(t, err)
}

func TestActivityService_ListEventsReceivedByUser_all(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/users/u/received_events", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -228,7 +243,7 @@ func TestActivityService_ListEventsReceivedByUser_all(t *testing.T) {
}

func TestActivityService_ListEventsReceivedByUser_publicOnly(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/users/u/received_events/public", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -248,12 +263,15 @@ func TestActivityService_ListEventsReceivedByUser_publicOnly(t *testing.T) {
}

func TestActivityService_ListEventsReceivedByUser_invalidUser(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()

_, _, err := client.Activity.ListEventsReceivedByUser(context.Background(), "%", false, nil)
testURLParseError(t, err)
}

func TestActivityService_ListUserEventsForOrganization(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/users/u/events/orgs/o", func(w http.ResponseWriter, r *http.Request) {
Expand Down
18 changes: 9 additions & 9 deletions github/activity_notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func TestActivityService_ListNotification(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/notifications", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -49,7 +49,7 @@ func TestActivityService_ListNotification(t *testing.T) {
}

func TestActivityService_ListRepositoryNotification(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/notifications", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -69,7 +69,7 @@ func TestActivityService_ListRepositoryNotification(t *testing.T) {
}

func TestActivityService_MarkNotificationsRead(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/notifications", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -87,7 +87,7 @@ func TestActivityService_MarkNotificationsRead(t *testing.T) {
}

func TestActivityService_MarkRepositoryNotificationsRead(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/notifications", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -105,7 +105,7 @@ func TestActivityService_MarkRepositoryNotificationsRead(t *testing.T) {
}

func TestActivityService_GetThread(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/notifications/threads/1", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -125,7 +125,7 @@ func TestActivityService_GetThread(t *testing.T) {
}

func TestActivityService_MarkThreadRead(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/notifications/threads/1", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -140,7 +140,7 @@ func TestActivityService_MarkThreadRead(t *testing.T) {
}

func TestActivityService_GetThreadSubscription(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/notifications/threads/1/subscription", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -160,7 +160,7 @@ func TestActivityService_GetThreadSubscription(t *testing.T) {
}

func TestActivityService_SetThreadSubscription(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

input := &Subscription{Subscribed: Bool(true)}
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestActivityService_SetThreadSubscription(t *testing.T) {
}

func TestActivityService_DeleteThreadSubscription(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/notifications/threads/1/subscription", func(w http.ResponseWriter, r *http.Request) {
Expand Down
26 changes: 19 additions & 7 deletions github/activity_star_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestActivityService_ListStargazers(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/stargazers", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -40,7 +40,7 @@ func TestActivityService_ListStargazers(t *testing.T) {
}

func TestActivityService_ListStarred_authenticatedUser(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/user/starred", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -61,7 +61,7 @@ func TestActivityService_ListStarred_authenticatedUser(t *testing.T) {
}

func TestActivityService_ListStarred_specifiedUser(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/users/u/starred", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -88,12 +88,15 @@ func TestActivityService_ListStarred_specifiedUser(t *testing.T) {
}

func TestActivityService_ListStarred_invalidUser(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()

_, _, err := client.Activity.ListStarred(context.Background(), "%", nil)
testURLParseError(t, err)
}

func TestActivityService_IsStarred_hasStar(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -111,7 +114,7 @@ func TestActivityService_IsStarred_hasStar(t *testing.T) {
}

func TestActivityService_IsStarred_noStar(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -129,12 +132,15 @@ func TestActivityService_IsStarred_noStar(t *testing.T) {
}

func TestActivityService_IsStarred_invalidID(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()

_, _, err := client.Activity.IsStarred(context.Background(), "%", "%")
testURLParseError(t, err)
}

func TestActivityService_Star(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -148,12 +154,15 @@ func TestActivityService_Star(t *testing.T) {
}

func TestActivityService_Star_invalidID(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()

_, err := client.Activity.Star(context.Background(), "%", "%")
testURLParseError(t, err)
}

func TestActivityService_Unstar(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -167,6 +176,9 @@ func TestActivityService_Unstar(t *testing.T) {
}

func TestActivityService_Unstar_invalidID(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()

_, err := client.Activity.Unstar(context.Background(), "%", "%")
testURLParseError(t, err)
}
2 changes: 1 addition & 1 deletion github/activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func TestActivityService_List(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/feeds", func(w http.ResponseWriter, r *http.Request) {
Expand Down
16 changes: 8 additions & 8 deletions github/activity_watching_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestActivityService_ListWatchers(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/subscribers", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -39,7 +39,7 @@ func TestActivityService_ListWatchers(t *testing.T) {
}

func TestActivityService_ListWatched_authenticatedUser(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/user/subscriptions", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -62,7 +62,7 @@ func TestActivityService_ListWatched_authenticatedUser(t *testing.T) {
}

func TestActivityService_ListWatched_specifiedUser(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/users/u/subscriptions", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -85,7 +85,7 @@ func TestActivityService_ListWatched_specifiedUser(t *testing.T) {
}

func TestActivityService_GetRepositorySubscription_true(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -105,7 +105,7 @@ func TestActivityService_GetRepositorySubscription_true(t *testing.T) {
}

func TestActivityService_GetRepositorySubscription_false(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -125,7 +125,7 @@ func TestActivityService_GetRepositorySubscription_false(t *testing.T) {
}

func TestActivityService_GetRepositorySubscription_error(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -140,7 +140,7 @@ func TestActivityService_GetRepositorySubscription_error(t *testing.T) {
}

func TestActivityService_SetRepositorySubscription(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

input := &Subscription{Subscribed: Bool(true)}
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestActivityService_SetRepositorySubscription(t *testing.T) {
}

func TestActivityService_DeleteRepositorySubscription(t *testing.T) {
setup()
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading

0 comments on commit df88dd9

Please sign in to comment.