Skip to content

Commit

Permalink
shell: add exit code check functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
alileza committed Nov 7, 2019
1 parent 2036bd6 commit 2299eae
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 3 deletions.
20 changes: 20 additions & 0 deletions dictionary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,26 @@ handlers:
description: substring of the message
type: string

- name: exit_code_equal
description: check exit code is equal given value
handle: checkExitCodeEqual
expressions:
- $resource exit code equal to $exit_code
parameters:
- name: exit_code
description: exit code of the executed command
type: number

- name: exit_code_not_equal
description: check exit code is not equal given value
handle: checkExitCodeNotEqual
expressions:
- $resource exit code not equal to $exit_code
parameters:
- name: exit_code
description: exit code of the executed command
type: number

- name: cache
description: cache driver that interacts with a cache service
resources:
Expand Down
14 changes: 14 additions & 0 deletions docs/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,20 @@ Given $resource stderr should not contains $substring
```

#### **Exit Code Equal**
check exit code is equal given value
```gherkin
Given $resource exit code equal to $exit_code
```

#### **Exit Code Not Equal**
check exit code is not equal given value
```gherkin
Given $resource exit code not equal to $exit_code
```



## Cache
Expand Down
3 changes: 3 additions & 0 deletions examples/features/shell.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ Feature: shell features example

Scenario: Create file and check if file exist
Given "shell-cli" execute "touch helloworld"
Given "shell-cli" exit code equal to 0
Given "shell-cli" execute "rm some-not-exist-file"
Given "shell-cli" exit code not equal to 0
Then "shell-cli" execute "ls"
Then "shell-cli" stdout should contains "helloworld"
Then "shell-cli" execute "rm helloworld"
Expand Down
2 changes: 2 additions & 0 deletions handler/shell/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ func (h *Handler) Register(s *godog.Suite) {
s.Step(`^"([^"]*)" stdout should not contains "([^"]*)"$`, h.checkStdoutNotContains)
s.Step(`^"([^"]*)" stderr should contains "([^"]*)"$`, h.checkStderrContains)
s.Step(`^"([^"]*)" stderr should not contains "([^"]*)"$`, h.checkStderrNotContains)
s.Step(`^"([^"]*)" exit code equal to (\d+)$`, h.checkExitCodeEqual)
s.Step(`^"([^"]*)" exit code not equal to (\d+)$`, h.checkExitCodeNotEqual)
}
37 changes: 37 additions & 0 deletions handler/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Resource interface {
Exec(command string, arguments ...string) error
Stdout() (string, error)
Stderr() (string, error)
ExitCode() (int, error)
}

type Handler struct {
Expand Down Expand Up @@ -104,3 +105,39 @@ func (h *Handler) checkStderrNotContains(resourceName, message string) error {

return nil
}

func (h *Handler) checkExitCodeEqual(resourceName string, expectedExitCode int) error {
r, ok := h.r[resourceName]
if !ok {
return fmt.Errorf("%s not found", resourceName)
}

exitCode, err := r.ExitCode()
if err != nil {
return err
}

if exitCode != expectedExitCode {
return fmt.Errorf("expecting exit code to be %d, got %d", expectedExitCode, exitCode)
}

return nil
}

func (h *Handler) checkExitCodeNotEqual(resourceName string, unexpectedExitCode int) error {
r, ok := h.r[resourceName]
if !ok {
return fmt.Errorf("%s not found", resourceName)
}

exitCode, err := r.ExitCode()
if err != nil {
return err
}

if exitCode == unexpectedExitCode {
return fmt.Errorf("expecting exit code not to be %d, got %d", unexpectedExitCode, exitCode)
}

return nil
}
33 changes: 30 additions & 3 deletions resource/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package shell

import (
"errors"
"fmt"
"os/exec"
"strings"
"sync"
"syscall"

"github.com/tomatool/tomato/config"
)

type Shell struct {
prefix []string

stdout string
stderr string
stdout string
stderr string
exitCode int
}

func New(cfg *config.Resource) (*Shell, error) {
Expand Down Expand Up @@ -48,9 +51,24 @@ func (s *Shell) Exec(command string, arguments ...string) error {
}

cmd := exec.Command(arguments[0], arguments[1:]...)

cmd.Stdout = newWriter(&s.stdout)
cmd.Stderr = newWriter(&s.stderr)
return cmd.Run()

if err := cmd.Start(); err != nil {
return fmt.Errorf("shell: %v\nstdout: %s\nstderr: %s", err, s.stdout, s.stderr)
}

if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
s.exitCode = status.ExitStatus()
}
} else {
return fmt.Errorf("shell: %v\nstdout: %s\nstderr: %s", err, s.stdout, s.stderr)
}
}
return nil
}
func (s *Shell) Stdout() (string, error) {
defer func() { s.stdout = "" }()
Expand All @@ -70,6 +88,15 @@ func (s *Shell) Stderr() (string, error) {

return s.stderr, nil
}
func (s *Shell) ExitCode() (int, error) {
defer func() { s.exitCode = 0 }()

if s.exitCode == 0 {
return s.exitCode, nil
}

return s.exitCode, nil
}

type writer struct {
mtx sync.Mutex
Expand Down

0 comments on commit 2299eae

Please sign in to comment.