Skip to content

Commit

Permalink
Move withcontext test to only run on linux as the sleep command is a …
Browse files Browse the repository at this point in the history
…unix command
  • Loading branch information
dylanhitt committed Jul 4, 2022
1 parent 2be09d1 commit ac313c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
20 changes: 19 additions & 1 deletion command_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package cmd

import (
"bytes"
"github.com/stretchr/testify/assert"
"context"
"io/ioutil"
"os"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestCommand_ExecuteStderr(t *testing.T) {
Expand Down Expand Up @@ -138,3 +140,19 @@ func TestWithEnvironmentVariables(t *testing.T) {

assertEqualWithLineBreak(t, "value", c.Stdout())
}

func TestCommand_WithContext(t *testing.T) {
// ensure legacy timeout is honored
cmd := NewCommand("sleep 3;", WithTimeout(1*time.Second))
err := cmd.Execute()
assert.NotNil(t, err)
assert.Equal(t, "Command timed out after 1s", err.Error())

// set context timeout to 2 seconds to ensure
// context takes precedence over timeout
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
err = cmd.ExecuteContext(ctx)
assert.NotNil(t, err)
assert.Equal(t, "context deadline exceeded", err.Error())
}
17 changes: 0 additions & 17 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"bytes"
"context"
"fmt"
"os"
"runtime"
Expand Down Expand Up @@ -131,19 +130,3 @@ func TestCommand_SetOptions(t *testing.T) {
assert.Equal(t, time.Duration(1000000000), c.Timeout)
assertEqualWithLineBreak(t, "test", writer.String())
}

func TestCommand_WithContext(t *testing.T) {
// ensure legacy timeout is honored
cmd := NewCommand("sleep 3;", WithTimeout(1*time.Second))
err := cmd.Execute()
assert.NotNil(t, err)
assert.Equal(t, "Command timed out after 1s", err.Error())

// set context timeout to 2 seconds to ensure
// context takes precedence over timeout
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
err = cmd.ExecuteContext(ctx)
assert.NotNil(t, err)
assert.Equal(t, "context deadline exceeded", err.Error())
}

0 comments on commit ac313c5

Please sign in to comment.