diff --git a/api/query/autocomplete_medium_test.go b/api/query/autocomplete_medium_test.go index 714ed9cc1a8..2a892793444 100644 --- a/api/query/autocomplete_medium_test.go +++ b/api/query/autocomplete_medium_test.go @@ -66,7 +66,7 @@ func TestAutocompleteHandler(t *testing.T) { defer mockCtrl.Finish() // TODO(markdittmer): Should this be hitting GCS instead? - store := shared.NewMockReadable(mockCtrl) + store := sharedtest.NewMockReadable(mockCtrl) rs := []*sharedtest.MockReadCloser{ sharedtest.NewMockReadCloser(t, summaryBytes[0]), sharedtest.NewMockReadCloser(t, summaryBytes[1]), diff --git a/api/query/query_test.go b/api/query/query_test.go index 6dbf489b822..9540bb661af 100644 --- a/api/query/query_test.go +++ b/api/query/query_test.go @@ -13,6 +13,7 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" "github.com/web-platform-tests/wpt.fyi/shared" + "github.com/web-platform-tests/wpt.fyi/shared/sharedtest" ) func TestGetMemcacheKey(t *testing.T) { @@ -44,7 +45,7 @@ func TestLoadSummaries_success(t *testing.T) { getMemcacheKey(testRuns[1]), } - cachedStore := shared.NewMockCachedStore(mockCtrl) + cachedStore := sharedtest.NewMockCachedStore(mockCtrl) sh := unstructuredSearchHandler{queryHandler{dataSource: cachedStore}} summaryBytes := [][]byte{ []byte(`{"/a/b/c":[1,2]}`), @@ -94,7 +95,7 @@ func TestLoadSummaries_fail(t *testing.T) { getMemcacheKey(testRuns[1]), } - cachedStore := shared.NewMockCachedStore(mockCtrl) + cachedStore := sharedtest.NewMockCachedStore(mockCtrl) sh := unstructuredSearchHandler{queryHandler{dataSource: cachedStore}} summaryBytes := [][]byte{ []byte(`{"/a/b/c":[1,2]}`), diff --git a/api/query/search_medium_test.go b/api/query/search_medium_test.go index 6a524a9b6b8..9242ef5f6ac 100644 --- a/api/query/search_medium_test.go +++ b/api/query/search_medium_test.go @@ -67,7 +67,7 @@ func TestUnstructuredSearchHandler(t *testing.T) { defer mockCtrl.Finish() // TODO(markdittmer): Should this be hitting GCS instead? - store := shared.NewMockReadable(mockCtrl) + store := sharedtest.NewMockReadable(mockCtrl) rs := []*sharedtest.MockReadCloser{ sharedtest.NewMockReadCloser(t, summaryBytes[0]), sharedtest.NewMockReadCloser(t, summaryBytes[1]), @@ -187,7 +187,7 @@ func TestStructuredSearchHandler_equivalentToUnstructured(t *testing.T) { defer mockCtrl.Finish() // TODO(markdittmer): Should this be hitting GCS instead? - store := shared.NewMockReadable(mockCtrl) + store := sharedtest.NewMockReadable(mockCtrl) rs := []*sharedtest.MockReadCloser{ sharedtest.NewMockReadCloser(t, summaryBytes[0]), sharedtest.NewMockReadCloser(t, summaryBytes[1]), diff --git a/api/receiver/receive_results_test.go b/api/receiver/receive_results_test.go index 2150274d158..d21597e6335 100644 --- a/api/receiver/receive_results_test.go +++ b/api/receiver/receive_results_test.go @@ -18,7 +18,7 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" - "github.com/web-platform-tests/wpt.fyi/shared" + "github.com/web-platform-tests/wpt.fyi/shared/sharedtest" "google.golang.org/appengine/taskqueue" ) @@ -92,7 +92,7 @@ func TestHandleResultsUpload_success(t *testing.T) { mockAE := NewMockAppEngineAPI(mockCtrl) f := &os.File{} task := &taskqueue.Task{Name: "task"} - mockAE.EXPECT().Context().Return(shared.NewTestContext()).AnyTimes() + mockAE.EXPECT().Context().Return(sharedtest.NewTestContext()).AnyTimes() gomock.InOrder( mockAE.EXPECT().IsAdmin().Return(false), mockAE.EXPECT().authenticateUploader("blade-runner", "123").Return(true), @@ -130,7 +130,7 @@ func TestHandleResultsUpload_extra_params(t *testing.T) { "os_version": "", } task := &taskqueue.Task{Name: "task"} - mockAE.EXPECT().Context().Return(shared.NewTestContext()).AnyTimes() + mockAE.EXPECT().Context().Return(sharedtest.NewTestContext()).AnyTimes() gomock.InOrder( mockAE.EXPECT().IsAdmin().Return(false), mockAE.EXPECT().authenticateUploader("blade-runner", "123").Return(true), @@ -171,7 +171,7 @@ func TestHandleURLPayload_single(t *testing.T) { } mockAE := NewMockAppEngineAPI(mockCtrl) - mockAE.EXPECT().Context().Return(shared.NewTestContext()).AnyTimes() + mockAE.EXPECT().Context().Return(sharedtest.NewTestContext()).AnyTimes() gomock.InOrder( mockAE.EXPECT().fetchWithTimeout("http://wpt.fyi/test.json.gz", DownloadTimeout).Return(f, nil), mockAE.EXPECT().uploadToGCS(matchRegex(`^/wptd-results-buffer/blade-runner/.*\.json$`), f, true).Return(nil), @@ -189,7 +189,7 @@ func TestHandleURLPayload_multiple(t *testing.T) { urls := []string{"http://wpt.fyi/foo.json.gz", "http://wpt.fyi/bar.json.gz"} mockAE := NewMockAppEngineAPI(mockCtrl) - mockAE.EXPECT().Context().Return(shared.NewTestContext()).AnyTimes() + mockAE.EXPECT().Context().Return(sharedtest.NewTestContext()).AnyTimes() gomock.InOrder( mockAE.EXPECT().fetchWithTimeout(urls[0], DownloadTimeout).Return(f, nil), mockAE.EXPECT().uploadToGCS(matchRegex(`^/wptd-results-buffer/blade-runner/.*/0\.json$`), f, true).Return(nil), @@ -211,7 +211,7 @@ func TestHandleURLPayload_retry_fetching(t *testing.T) { errTimeout := fmt.Errorf("server timed out") mockAE := NewMockAppEngineAPI(mockCtrl) - mockAE.EXPECT().Context().Return(shared.NewTestContext()).AnyTimes() + mockAE.EXPECT().Context().Return(sharedtest.NewTestContext()).AnyTimes() gomock.InOrder( mockAE.EXPECT().fetchWithTimeout("http://wpt.fyi/test.json.gz", DownloadTimeout).Return(nil, errTimeout), mockAE.EXPECT().fetchWithTimeout("http://wpt.fyi/test.json.gz", DownloadTimeout).Return(nil, errTimeout), @@ -230,7 +230,7 @@ func TestHandleURLPayload_fail_fetching(t *testing.T) { errTimeout := fmt.Errorf("server timed out") mockAE := NewMockAppEngineAPI(mockCtrl) - mockAE.EXPECT().Context().Return(shared.NewTestContext()).AnyTimes() + mockAE.EXPECT().Context().Return(sharedtest.NewTestContext()).AnyTimes() gomock.InOrder( mockAE.EXPECT().fetchWithTimeout("http://wpt.fyi/test.json.gz", DownloadTimeout).Return(nil, errTimeout), mockAE.EXPECT().fetchWithTimeout("http://wpt.fyi/test.json.gz", DownloadTimeout).Return(nil, errTimeout), @@ -250,7 +250,7 @@ func TestHandleURLPayload_fail_uploading(t *testing.T) { errGCS := fmt.Errorf("failed to upload to GCS") mockAE := NewMockAppEngineAPI(mockCtrl) - mockAE.EXPECT().Context().Return(shared.NewTestContext()).AnyTimes() + mockAE.EXPECT().Context().Return(sharedtest.NewTestContext()).AnyTimes() gomock.InOrder( mockAE.EXPECT().fetchWithTimeout("http://wpt.fyi/test.json.gz", DownloadTimeout).Return(f, nil), mockAE.EXPECT().uploadToGCS(matchRegex(`^/wptd-results-buffer/blade-runner/.*\.json$`), f, true).Return(errGCS), diff --git a/shared/models_test.go b/shared/models_test.go index 6fdfa122dfb..eceea25c97f 100644 --- a/shared/models_test.go +++ b/shared/models_test.go @@ -1,17 +1,18 @@ // +build medium -package shared +package shared_test import ( "testing" "github.com/stretchr/testify/assert" + "github.com/web-platform-tests/wpt.fyi/shared" "github.com/web-platform-tests/wpt.fyi/shared/sharedtest" "google.golang.org/appengine/datastore" ) func TestTestRunIDs_LoadTestRuns(t *testing.T) { - testRuns := make(TestRuns, 2) + testRuns := make(shared.TestRuns, 2) testRuns[0].BrowserName = "chrome" testRuns[0].BrowserVersion = "63.0" testRuns[0].OSName = "linux" diff --git a/shared/request_caching_test.go b/shared/request_caching_test.go index 8d770637f7f..c6b9e7ca092 100644 --- a/shared/request_caching_test.go +++ b/shared/request_caching_test.go @@ -4,7 +4,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -package shared +package shared_test import ( "context" @@ -16,6 +16,7 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" + "github.com/web-platform-tests/wpt.fyi/shared" "github.com/web-platform-tests/wpt.fyi/shared/sharedtest" ) @@ -36,9 +37,15 @@ func (okHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func TestNoCaching404(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() - cache := NewMockReadWritable(mockCtrl) + cache := sharedtest.NewMockReadWritable(mockCtrl) cache.EXPECT().NewReadCloser("/some/url").Return(ioutil.NopCloser(failReader{}), nil) - h := NewCachingHandler(context.Background(), http.NotFoundHandler(), cache, AlwaysCachable, URLAsCacheKey, CacheStatusOK) + h := shared.NewCachingHandler( + context.Background(), + http.NotFoundHandler(), + cache, + shared.AlwaysCachable, + shared.URLAsCacheKey, + shared.CacheStatusOK) w := httptest.NewRecorder() r := httptest.NewRequest("GET", "/some/url", nil) h.ServeHTTP(w, r) @@ -48,11 +55,17 @@ func TestNoCaching404(t *testing.T) { func TestCaching200(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() - cache := NewMockReadWritable(mockCtrl) + cache := sharedtest.NewMockReadWritable(mockCtrl) cache.EXPECT().NewReadCloser("/some/url").Return(ioutil.NopCloser(failReader{}), nil) wc := sharedtest.NewMockWriteCloser(t) cache.EXPECT().NewWriteCloser("/some/url").Return(wc, nil) - h := NewCachingHandler(context.Background(), okHandler{}, cache, AlwaysCachable, URLAsCacheKey, CacheStatusOK) + h := shared.NewCachingHandler( + context.Background(), + okHandler{}, + cache, + shared.AlwaysCachable, + shared.URLAsCacheKey, + shared.CacheStatusOK) w := httptest.NewRecorder() r := httptest.NewRequest("GET", "/some/url", nil) h.ServeHTTP(w, r) diff --git a/shared/sharedtest/README.md b/shared/sharedtest/README.md new file mode 100644 index 00000000000..156338b80fc --- /dev/null +++ b/shared/sharedtest/README.md @@ -0,0 +1,14 @@ +# sharedtest + +`sharedtest` is a folder for shared test code. + +Note that when authoring tests for `shared`, while also relying on utilities +available in `sharedtest`, you'll need to put the test in the `shared_test` +package, which is a Golang convention for "black box" testing of the `shared` +package. This is because we would otherwise have a circular dependency of + + shared + sharedtest + shared (test) + +where `shared (test)` are `_test.go` files in the `shared` package. diff --git a/shared/storage_mock.go b/shared/sharedtest/storage_mock.go similarity index 98% rename from shared/storage_mock.go rename to shared/sharedtest/storage_mock.go index be3bb24b5c3..2044937d911 100644 --- a/shared/storage_mock.go +++ b/shared/sharedtest/storage_mock.go @@ -1,13 +1,14 @@ // Code generated by MockGen. DO NOT EDIT. // Source: shared/storage.go -// Package shared is a generated GoMock package. -package shared +// Package sharedtest is a generated GoMock package. +package sharedtest import ( - gomock "github.com/golang/mock/gomock" io "io" reflect "reflect" + + gomock "github.com/golang/mock/gomock" ) // MockReadable is a mock of Readable interface diff --git a/shared/sharedtest/util.go b/shared/sharedtest/util.go index 1270eb6d5ca..83db3e05502 100644 --- a/shared/sharedtest/util.go +++ b/shared/sharedtest/util.go @@ -8,7 +8,6 @@ import ( "context" "github.com/golang/mock/gomock" - "github.com/web-platform-tests/wpt.fyi/shared" "google.golang.org/appengine" "google.golang.org/appengine/aetest" @@ -66,3 +65,10 @@ func SameProductSpec(spec string) gomock.Matcher { spec: spec, } } + +// NewTestContext creates a new context.Context for small tests. +func NewTestContext() context.Context { + ctx := context.Background() + ctx = context.WithValue(ctx, shared.DefaultLoggerCtxKey(), shared.NewNilLogger()) + return ctx +} diff --git a/shared/storage_test.go b/shared/storage_test.go index 5c66c134b11..43ddd548d50 100644 --- a/shared/storage_test.go +++ b/shared/storage_test.go @@ -4,7 +4,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -package shared +package shared_test import ( "errors" @@ -12,6 +12,7 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" + "github.com/web-platform-tests/wpt.fyi/shared" "github.com/web-platform-tests/wpt.fyi/shared/sharedtest" "google.golang.org/appengine/memcache" ) @@ -22,9 +23,9 @@ func TestGet_cacheHit(t *testing.T) { var cacheID, storeID interface{} - cache := NewMockReadWritable(mockCtrl) - store := NewMockReadable(mockCtrl) - cs := NewByteCachedStore(NewTestContext(), cache, store) + cache := sharedtest.NewMockReadWritable(mockCtrl) + store := sharedtest.NewMockReadable(mockCtrl) + cs := shared.NewByteCachedStore(sharedtest.NewTestContext(), cache, store) data := []byte("{}") cr := sharedtest.NewMockReadCloser(t, data) @@ -42,9 +43,9 @@ func TestGet_cacheMiss(t *testing.T) { var cacheID, storeID interface{} - cache := NewMockReadWritable(mockCtrl) - store := NewMockReadable(mockCtrl) - cs := NewByteCachedStore(NewTestContext(), cache, store) + cache := sharedtest.NewMockReadWritable(mockCtrl) + store := sharedtest.NewMockReadable(mockCtrl) + cs := shared.NewByteCachedStore(sharedtest.NewTestContext(), cache, store) data := []byte("{}") cw := sharedtest.NewMockWriteCloser(t) @@ -66,9 +67,9 @@ func TestGet_missing(t *testing.T) { var cacheID, storeID interface{} - cache := NewMockReadWritable(mockCtrl) - store := NewMockReadable(mockCtrl) - cs := NewByteCachedStore(NewTestContext(), cache, store) + cache := sharedtest.NewMockReadWritable(mockCtrl) + store := sharedtest.NewMockReadable(mockCtrl) + cs := shared.NewByteCachedStore(sharedtest.NewTestContext(), cache, store) errMissing := errors.New("Failed to fetch from store") cache.EXPECT().NewReadCloser(&cacheID).Return(nil, memcache.ErrCacheMiss) diff --git a/shared/util.go b/shared/util.go index 0bbed20d77a..98b80b06d14 100644 --- a/shared/util.go +++ b/shared/util.go @@ -195,13 +195,6 @@ func NewRequestContext(r *http.Request) context.Context { return ctx } -// NewTestContext creates a new context.Context for small tests. -func NewTestContext() context.Context { - ctx := context.Background() - ctx = context.WithValue(ctx, DefaultLoggerCtxKey(), NewNilLogger()) - return ctx -} - // NewSetFromStringSlice is a helper for the inability to cast []string to []interface{} func NewSetFromStringSlice(items []string) mapset.Set { if items == nil {