github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/service/systemd/export_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package systemd
     5  
     6  import (
     7  	"go.uber.org/mock/gomock"
     8  )
     9  
    10  // TODO (manadart 2018-04-04)
    11  // This, and the shims and mocks in shims.go and shims_mock.go, should be
    12  // phased out.
    13  // The more elegant approach would be to create types that implement the
    14  // methods in the shims by wrapping the calls that are being patched below.
    15  // Then, those types should be passed as dependencies to the objects that
    16  // use them, and can be replaced by mocks in testing.
    17  // See the DBusAPI factory method passed to NewService as an example.
    18  
    19  var (
    20  	Serialize = serialize
    21  )
    22  
    23  type patcher interface {
    24  	PatchValue(interface{}, interface{})
    25  }
    26  
    27  func PatchNewChan(patcher patcher) chan string {
    28  	ch := make(chan string, 1)
    29  	patcher.PatchValue(&newChan, func() chan string { return ch })
    30  	return ch
    31  }
    32  
    33  func PatchExec(patcher patcher, ctrl *gomock.Controller) *MockShimExec {
    34  	mock := NewMockShimExec(ctrl)
    35  	patcher.PatchValue(&runCommands, mock.RunCommands)
    36  	return mock
    37  }