go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/internal/testutils/test_utils.go (about)

     1  // Copyright 2021 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testutils
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.chromium.org/luci/server/caching"
    21  	"go.chromium.org/luci/server/caching/cachingtest"
    22  
    23  	. "github.com/smartystreets/goconvey/convey"
    24  )
    25  
    26  // shouldResembleMatcher is a gomock.Matcher that performs
    27  // convey.ShouldResemble(actual, expected).
    28  type shouldResembleMatcher struct {
    29  	expected any
    30  }
    31  
    32  // NewShouldResemberMatcher constrcuts a gomock.Matcher that performs
    33  // convey.ShouldResemble(actual, expected).
    34  func NewShouldResemberMatcher(expected any) shouldResembleMatcher {
    35  	return shouldResembleMatcher{
    36  		expected: expected,
    37  	}
    38  }
    39  
    40  // Matches implements gomock.Matcher
    41  func (e shouldResembleMatcher) Matches(actual any) bool {
    42  	ShouldResemble(actual, e.expected)
    43  	return true
    44  }
    45  
    46  // String implements gomock.Matcher
    47  func (e shouldResembleMatcher) String() string {
    48  	return "ShouldResemble"
    49  }
    50  
    51  // SetUpTestGlobalCache sets up GlobalCache in the context.
    52  func SetUpTestGlobalCache(ctx context.Context) context.Context {
    53  	caches := make(map[string]caching.BlobCache)
    54  	return caching.WithGlobalCache(ctx, func(namespace string) caching.BlobCache {
    55  		cache, ok := caches[namespace]
    56  		if !ok {
    57  			cache = cachingtest.NewBlobCache()
    58  			caches[namespace] = cache
    59  		}
    60  		return cache
    61  	})
    62  }