github.com/release-engineering/exodus-rsync@v1.11.2/internal/walk/hash_error_test.go (about)

     1  package walk
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  )
     9  
    10  // Mock hash.Hash.
    11  //go:generate go run -modfile ../../go.tools.mod github.com/golang/mock/mockgen -package $GOPACKAGE -destination hash_mock.go hash Hash
    12  
    13  func TestHashError(t *testing.T) {
    14  	ctrl := gomock.NewController(t)
    15  
    16  	mockHash := NewMockHash(ctrl)
    17  
    18  	// make it fail
    19  	mockHash.EXPECT().Write(gomock.Any()).Return(0, fmt.Errorf("simulated error"))
    20  
    21  	_, err := fileHash("walk.go", mockHash)
    22  
    23  	// It should propagate the error.
    24  	if fmt.Sprint(err) != "simulated error" {
    25  		t.Errorf("did not get expected error, err = %v", err)
    26  	}
    27  }