Skip to content

Commit

Permalink
feat: add ability to specify standalone CLI path (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
r-best authored Jun 6, 2024
1 parent 0d81b71 commit 9c80346
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion client/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// Service is a process wrapper for 3rd party binaries. It will spawn an instance
// of the binary and manage the life-cycle and IO of the process.
type Service interface {
Setup()
Setup(pactCLIDir string)
Stop(pid int) (bool, error)
List() map[int]*exec.Cmd
Command() *exec.Cmd
Expand Down
13 changes: 7 additions & 6 deletions client/service_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"log"
"os"
"os/exec"
"path"
"sync"
"time"
)

// ServiceManager is the default implementation of the Service interface.
type ServiceManager struct {
PactCLIDir string
Cmd string
processMap processMap
Args []string
Expand All @@ -20,9 +22,11 @@ type ServiceManager struct {
}

// Setup the Management services.
func (s *ServiceManager) Setup() {
func (s *ServiceManager) Setup(pactCLIDir string) {
log.Println("[DEBUG] setting up a service manager")

s.PactCLIDir = pactCLIDir

s.commandCreatedChan = make(chan *exec.Cmd)
s.commandCompleteChan = make(chan *exec.Cmd)
s.processMap = processMap{processes: make(map[int]*exec.Cmd)}
Expand Down Expand Up @@ -97,7 +101,7 @@ func (s *ServiceManager) List() map[int]*exec.Cmd {

// Command creates an os command to be run
func (s *ServiceManager) Command() *exec.Cmd {
cmd := exec.Command(s.Cmd, s.Args...)
cmd := exec.Command(path.Join(s.PactCLIDir, s.Cmd), s.Args...)
env := os.Environ()
env = append(env, s.Env...)
cmd.Env = env
Expand All @@ -108,10 +112,7 @@ func (s *ServiceManager) Command() *exec.Cmd {
// Start a Service and log its output.
func (s *ServiceManager) Start() *exec.Cmd {
log.Println("[DEBUG] starting service")
cmd := exec.Command(s.Cmd, s.Args...)
env := os.Environ()
env = append(env, s.Env...)
cmd.Env = env
cmd := s.Command()

cmdReader, err := cmd.StdoutPipe()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion client/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func createServiceManager() *ServiceManager {
Args: cs,
Env: env,
}
mgr.Setup()
mgr.Setup("")
return mgr
}

Expand Down
14 changes: 7 additions & 7 deletions dsl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ type PactClient struct {
}

// newClient creates a new Pact client manager with the provided services
func newClient(mockServiceManager client.Service, verificationServiceManager client.Service, messageServiceManager client.Service, publishServiceManager client.Service) *PactClient {
mockServiceManager.Setup()
verificationServiceManager.Setup()
messageServiceManager.Setup()
publishServiceManager.Setup()
func newClient(pactCLIDir string, mockServiceManager client.Service, verificationServiceManager client.Service, messageServiceManager client.Service, publishServiceManager client.Service) *PactClient {
mockServiceManager.Setup(pactCLIDir)
verificationServiceManager.Setup(pactCLIDir)
messageServiceManager.Setup(pactCLIDir)
publishServiceManager.Setup(pactCLIDir)

return &PactClient{
pactMockSvcManager: mockServiceManager,
Expand All @@ -86,8 +86,8 @@ func newClient(mockServiceManager client.Service, verificationServiceManager cli
}

// NewClient creates a new Pact client manager with defaults
func NewClient() *PactClient {
return newClient(&client.MockService{}, &client.VerificationService{}, &client.MessageService{}, &client.PublishService{})
func NewClient(pactCLIDir string) *PactClient {
return newClient(pactCLIDir, &client.MockService{}, &client.VerificationService{}, &client.MessageService{}, &client.PublishService{})
}

// StartServer starts a remote Pact Mock Server.
Expand Down
2 changes: 1 addition & 1 deletion dsl/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func createMockClient(success bool) (*PactClient, *ServiceMock) {
}
}()

d := newClient(svc, svc, svc, svc)
d := newClient("", svc, svc, svc, svc)
d.TimeoutDuration = 100 * time.Millisecond
return d, svc
}
Expand Down
5 changes: 4 additions & 1 deletion dsl/pact.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ type Pact struct {
// Current server for the consumer.
Server *types.MockServer

// Directory of Pactflow standalone CLI (blank if CLI is installed on system)
PactCLIDir string

// Pact RPC Client.
pactClient Client

Expand Down Expand Up @@ -158,7 +161,7 @@ func (p *Pact) Setup(startMockServer bool) *Pact {
}

if p.pactClient == nil {
c := NewClient()
c := NewClient(p.PactCLIDir)
c.TimeoutDuration = p.ClientTimeout
p.pactClient = c
}
Expand Down
5 changes: 4 additions & 1 deletion dsl/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type PactName struct {

// Publisher is the API to send Pact files to a Pact Broker.
type Publisher struct {
// Directory of Pactflow standalone CLI (blank if CLI is installed on system)
PactCLIDir string

pactClient Client

// Log levels.
Expand All @@ -40,7 +43,7 @@ func (p *Publisher) Publish(request types.PublishRequest) error {
log.Println("[DEBUG] pact publisher: publish pact")

if p.pactClient == nil {
c := NewClient()
c := NewClient(p.PactCLIDir)
p.pactClient = c
}

Expand Down
2 changes: 1 addition & 1 deletion dsl/service_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type ServiceMock struct {
}

// Setup the Management services.
func (s *ServiceMock) Setup() {
func (s *ServiceMock) Setup(pactCLIDir string) {
s.ServicesSetupCalled = true
}

Expand Down

0 comments on commit 9c80346

Please sign in to comment.