Skip to content

Commit

Permalink
Merge pull request #5 from imos/fix-no-tests
Browse files Browse the repository at this point in the history
Mark tests with no test cases as success
  • Loading branch information
imos authored May 20, 2019
2 parents 49af2c9 + d651870 commit e5b8314
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
15 changes: 14 additions & 1 deletion pkg/pytest/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/exec"
"strings"
"sync"
"syscall"
"time"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -135,7 +136,8 @@ func executeInternal(
})

// Wait for the command.
if err := cmd.Wait(); err != nil {
err = cmd.Wait()
if err != nil {
fmt.Fprintf(os.Stderr, "[DEBUG] failed to wait a command: %s: %s\n",
strings.Join(args, " "), err)
cmd.Process.Kill()
Expand All @@ -148,6 +150,17 @@ func executeInternal(
result.Status = xpytest_proto.TestResult_TIMEOUT
} else if cmd.ProcessState.Success() {
result.Status = xpytest_proto.TestResult_SUCCESS
} else if hasNoTests := func() bool {
// NOTE: pytest fails with code=5 if there are no tests:
// https://docs.pytest.org/en/latest/usage.html
if err, ok := err.(*exec.ExitError); ok {
if s, ok := err.Sys().(syscall.WaitStatus); ok {
return s.ExitStatus() == 5
}
}
return false
}(); hasNoTests {
result.Status = xpytest_proto.TestResult_SUCCESS
} else {
result.Status = xpytest_proto.TestResult_FAILED
}
Expand Down
15 changes: 14 additions & 1 deletion pkg/pytest/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func TestExecute(t *testing.T) {

func TestExecuteWithFailure(t *testing.T) {
ctx := context.Background()
r, err := pytest.Execute(ctx, []string{"false"}, time.Second, nil)
r, err := pytest.Execute(
ctx, []string{"bash", "-c", "exit 1"}, time.Second, nil)
if err != nil {
t.Fatalf("failed to execute: %s", err)
}
Expand All @@ -33,6 +34,18 @@ func TestExecuteWithFailure(t *testing.T) {
}
}

func TestExecuteWithNoTests(t *testing.T) {
ctx := context.Background()
r, err := pytest.Execute(
ctx, []string{"bash", "-c", "exit 5"}, time.Second, nil)
if err != nil {
t.Fatalf("failed to execute: %s", err)
}
if r.Status != xpytest_proto.TestResult_SUCCESS {
t.Fatalf("unexpected status: %s", r.Status)
}
}

func TestExecuteWithTimeout(t *testing.T) {
ctx := context.Background()
r, err := pytest.Execute(
Expand Down

0 comments on commit e5b8314

Please sign in to comment.