Skip to content

Commit

Permalink
feat: run command in working dir (diggerhq#261)
Browse files Browse the repository at this point in the history
* working directory of a run command should be in project dir
  • Loading branch information
Spartakovic authored May 16, 2023
1 parent 85c74b9 commit 9de2faf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 5 additions & 4 deletions pkg/digger/digger.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,17 +358,18 @@ type DiggerExecutor struct {
}

type CommandRun interface {
Run(shell string, command string) (string, string, error)
Run(workingDir string, shell string, command string) (string, string, error)
}

type CommandRunner struct {
}

func (c CommandRunner) Run(shell string, command string) (string, string, error) {
func (c CommandRunner) Run(workingDir string, shell string, command string) (string, string, error) {
if shell == "" {
shell = "bash"
}
cmd := exec.Command(shell, "-c", command)
cmd.Dir = workingDir

var stdout, stderr bytes.Buffer
mwout := io.MultiWriter(os.Stdout, &stdout)
Expand Down Expand Up @@ -442,7 +443,7 @@ func (d DiggerExecutor) Plan(prNumber int) error {
d.CIService.PublishComment(prNumber, comment)
}
if step.Action == "run" {
stdout, stderr, err := d.CommandRunner.Run(step.Shell, step.Value)
stdout, stderr, err := d.CommandRunner.Run(d.ProjectPath, step.Shell, step.Value)
log.Printf("Running %v for **%v**\n%v%v", step.Value, d.ProjectLock.LockId(), stdout, stderr)
if err != nil {
return fmt.Errorf("error running command: %v", err)
Expand Down Expand Up @@ -506,7 +507,7 @@ func (d DiggerExecutor) Apply(prNumber int) error {
}
}
if step.Action == "run" {
stdout, stderr, err := d.CommandRunner.Run(step.Shell, step.Value)
stdout, stderr, err := d.CommandRunner.Run(d.ProjectPath, step.Shell, step.Value)
log.Printf("Running %v for **%v**\n%v%v", step.Value, d.ProjectLock.LockId(), stdout, stderr)
if err != nil {
return fmt.Errorf("error running command: %v", err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/digger/digger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type MockCommandRunner struct {
Commands []RunInfo
}

func (m *MockCommandRunner) Run(shell string, command string) (string, string, error) {
m.Commands = append(m.Commands, RunInfo{"Run", shell + " " + command, time.Now()})
func (m *MockCommandRunner) Run(workDir string, shell string, command string) (string, string, error) {
m.Commands = append(m.Commands, RunInfo{"Run", workDir + " " + shell + " " + command, time.Now()})
return "", "", nil
}

Expand Down Expand Up @@ -182,7 +182,7 @@ func TestCorrectCommandExecutionWhenApplying(t *testing.T) {

commandStrings := allCommandsInOrderWithParams(terraformExecutor, commandRunner, prManager, lock, planStorage)

assert.Equal(t, []string{"RetrievePlan #.tfplan", "IsMergeable 1", "Lock 1", "Init ", "Apply ", "LockId ", "PublishComment 1 <details>\n <summary>Apply for ****</summary>\n\n ```terraform\n\n ```\n</details>", "Run echo", "LockId "}, commandStrings)
assert.Equal(t, []string{"RetrievePlan #.tfplan", "IsMergeable 1", "Lock 1", "Init ", "Apply ", "LockId ", "PublishComment 1 <details>\n <summary>Apply for ****</summary>\n\n ```terraform\n\n ```\n</details>", "Run echo", "LockId "}, commandStrings)
}

func TestCorrectCommandExecutionWhenPlanning(t *testing.T) {
Expand Down Expand Up @@ -224,7 +224,7 @@ func TestCorrectCommandExecutionWhenPlanning(t *testing.T) {

commandStrings := allCommandsInOrderWithParams(terraformExecutor, commandRunner, prManager, lock, planStorage)

assert.Equal(t, []string{"Lock 1", "Init ", "Plan -out #.tfplan", "StorePlan #.tfplan", "LockId ", "PublishComment 1 <details>\n <summary>Plan for ****</summary>\n\n ```terraform\n\n ```\n</details>", "Run echo", "LockId "}, commandStrings)
assert.Equal(t, []string{"Lock 1", "Init ", "Plan -out #.tfplan", "StorePlan #.tfplan", "LockId ", "PublishComment 1 <details>\n <summary>Plan for ****</summary>\n\n ```terraform\n\n ```\n</details>", "Run echo", "LockId "}, commandStrings)
}

func allCommandsInOrderWithParams(terraformExecutor *MockTerraformExecutor, commandRunner *MockCommandRunner, prManager *MockPRManager, lock *MockProjectLock, planStorage *MockPlanStorage) []string {
Expand Down

0 comments on commit 9de2faf

Please sign in to comment.