Skip to content

Commit

Permalink
Implement shelldocexitcode and shelldocwhatever options.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirko Boehm (Endocode) committed Jul 6, 2018
1 parent e731ac0 commit 1d8c3e2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmd/shelldoc/shelldoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ func TestHFailNoMatch(t *testing.T) {
require.Equal(t, returnFailure, results.returncode, "The expected return code is returnFailure.")
require.Equal(t, 1, results.failureCount, "There is one failing test in the sample.")
}

func TestExitCodesOptions(t *testing.T) {
results, err := performInteractions("../../pkg/tokenizer/samples/options.md")
require.NoError(t, err, "The HelloWorld example should execute without errors.")
require.Equal(t, returnSuccess, results.returncode, "The expected return code is returnFailure.")
}
17 changes: 16 additions & 1 deletion pkg/tokenizer/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package tokenizer
import (
"fmt"
"reflect"
"strconv"
"strings"

"github.com/endocode/shelldoc/pkg/shell"
Expand Down Expand Up @@ -120,11 +121,25 @@ func (interaction *Interaction) Execute(shell *shell.Shell) error {
// execute the command in the shell
output, rc, err := shell.ExecuteCommand(interaction.Cmd)
// compare the results
const ExitCodeOption = "shelldocexitcode"
const ExitCodeWhatever = "shelldocwhatever"
var expectedExitCode int
if expectedExitCodeOption, ok := interaction.Attributes[ExitCodeOption]; ok {
if value, err := strconv.Atoi(expectedExitCodeOption); err == nil {
expectedExitCode = value
} else {
return fmt.Errorf("argument to %s needs to be an integer, got \"%s\"", ExitCodeOption, expectedExitCodeOption)
}
}
expectedWhatever := false
if _, ok := interaction.Attributes[ExitCodeWhatever]; ok {
expectedWhatever = true
}
if err != nil {
interaction.ResultCode = ResultExecutionError
interaction.Comment = err.Error()
return fmt.Errorf("unable to execute command: %v", err)
} else if rc != 0 {
} else if expectedWhatever == false && rc != expectedExitCode {
interaction.ResultCode = ResultError
interaction.Comment = fmt.Sprintf("command exited with non-zero exit code %d", rc)
} else if interaction.evaluateResponse(output) {
Expand Down
15 changes: 15 additions & 0 deletions pkg/tokenizer/samples/options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Tests for shelldoc options in fenced code blocks

This one says the exit code of the command does not matter:

```shell {shelldocwhatever}
> false
```

This one specifies that the exit code should be 2:

```shell {shelldocexitcode=2}
> (exit 2)
```

More options may follow.

0 comments on commit 1d8c3e2

Please sign in to comment.