code.vegaprotocol.io/vega@v0.79.0/datanode/libs/testing/helpers.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package testing
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  
    23  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    24  	"code.vegaprotocol.io/vega/paths"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"google.golang.org/protobuf/proto"
    28  )
    29  
    30  func NewVegaPaths() (paths.Paths, func()) {
    31  	path := filepath.Join("/tmp", "vega-tests", vgrand.RandomStr(10))
    32  	err := os.MkdirAll(path, os.ModePerm)
    33  	if err != nil {
    34  		panic(err)
    35  	}
    36  	return paths.New(path), func() { _ = os.RemoveAll(path) }
    37  }
    38  
    39  // ProtosEq is a gomock matcher for comparing messages for equality.
    40  func ProtosEq(message proto.Message) ProtoMatcher {
    41  	return ProtoMatcher{message}
    42  }
    43  
    44  type ProtoMatcher struct {
    45  	expected proto.Message
    46  }
    47  
    48  func (m ProtoMatcher) Matches(x interface{}) bool {
    49  	msg, ok := x.(proto.Message)
    50  	if !ok {
    51  		return false
    52  	}
    53  	return proto.Equal(msg, m.expected)
    54  }
    55  
    56  func (m ProtoMatcher) String() string {
    57  	return fmt.Sprintf("is equal to %v (%T)", m.expected, m.expected)
    58  }
    59  
    60  type tHelper interface {
    61  	Helper()
    62  }
    63  
    64  // AssertProtoEqual is a testing assertion that two protos are the same.
    65  func AssertProtoEqual(t assert.TestingT, expected, actual proto.Message, msgAndArgs ...interface{}) bool {
    66  	if h, ok := t.(tHelper); ok {
    67  		h.Helper()
    68  	}
    69  
    70  	if !proto.Equal(expected, actual) {
    71  		return assert.Fail(t, fmt.Sprintf("Not equal: \n"+
    72  			"expected: %v\n"+
    73  			"actual  : %v", expected, actual), msgAndArgs...)
    74  	}
    75  
    76  	return true
    77  }