Skip to content

Commit

Permalink
Merge pull request #22 from asahasrabuddhe/handle-error-code
Browse files Browse the repository at this point in the history
Handle error code
  • Loading branch information
asahasrabuddhe committed Aug 7, 2019
2 parents b02e0c5 + 5e4da1a commit 222f1c4
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.coverprofile
coverage.html
vendor
17 changes: 12 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
language: go
dist: trusty
go:
- 1.8.x
- master
- 1.11.x
- 1.12.x
addons:
apt:
packages:
- zsh
- zsh
before_script:
- go get github.com/alecthomas/gometalinter
- gometalinter --install
- go get github.com/sirupsen/logrus
- go get github.com/stretchr/testify
- go get gopkg.in/urfave/cli.v2
- go get gopkg.in/yaml.v2
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin latest

script:
- golangci-lint run
- make all
8 changes: 2 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ALL_PACKAGES := $(PACKAGE) $(PACKAGE)/cmd/...

GIT ?= git
GO ?= go
GOMETALINTER ?= gometalinter
GOLANGCI_LINT?= golangci-lint
PYTHON ?= python

VERSION_VAR := $(PACKAGE).VersionString
Expand Down Expand Up @@ -49,11 +49,7 @@ coverage.coverprofile:

.PHONY: lint
lint:
$(GOMETALINTER) --errors --tests -D gocyclo --deadline=1m ./...

.PHONY: lintbomb
lintbomb:
$(GOMETALINTER) --tests -D gocyclo --deadline=1m ./...
$(GOLANGCI_LINT) run

.PHONY: build
build: deps
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ func main() {
}
```

<!-- {
"error": "we can handle errors too"
} -->
``` go
package main

import (
"fmt"
"os"
)

func main() {
fmt.Errorf("we can handle errors too")
}
```

### Java

If a code example has a declared language of `java` and a line matching `^public
Expand Down
16 changes: 8 additions & 8 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ clone_folder: c:\gopath\src\github.com\urfave\gfmrun

environment:
GOPATH: c:\gopath
GOVERSION: 1.8
GOVERSION: 1.11
GFMRUN_DEBUG: 1

install:
- set PATH=%GOPATH%\bin;c:\go\bin;c:\MinGW\bin;%PATH%
- go version
- go env
- go get -v -t ./...
- go get github.com/alecthomas/gometalinter
- gometalinter --install
- set PATH=%GOPATH%\bin;c:\go\bin;c:\MinGW\bin;%PATH%
- go version
- go env
- go get -v -t ./...
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin latest
- golangci-lint run

build_script:
- mingw32-make
- mingw32-make
2 changes: 1 addition & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func cliListFrobs(ctx *cli.Context) error {

known := map[string]bool{}

for name, _ := range DefaultFrobs {
for name := range DefaultFrobs {
for _, alias := range langs.Lookup(name).Aliases {
known[alias] = true
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/gfmrun/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package main

import (
"log"
"os"

"github.com/urfave/gfmrun"
)

func main() {
gfmrun.NewCLI().Run(os.Args)
err := gfmrun.NewCLI().Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
4 changes: 2 additions & 2 deletions cmd/gfmrun/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"gopkg.in/urfave/cli.v2"
)

func TestMain(t *testing.T) {
func TestMain(m *testing.M) {
stdout := os.Stdout
stderr := os.Stderr
os.Stdout, _ = os.Create(os.DevNull)
os.Stderr, _ = os.Create(os.DevNull)
cli.OsExiter = func(i int) { return }
cli.OsExiter = func(i int) { }

defer func() {
os.Stdout = stdout
Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/urfave/gfmrun

go 1.12

require (
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.2.2
gopkg.in/urfave/cli.v2 v2.0.0-20190806201727-b62605953717
gopkg.in/yaml.v2 v2.2.2
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/urfave/cli.v2 v2.0.0-20190806201727-b62605953717 h1:OvXt/p4cdwNl+mwcWMq/AxaKFkhdxcjx+tx+qf4EOvY=
gopkg.in/urfave/cli.v2 v2.0.0-20190806201727-b62605953717/go.mod h1:cKXr3E0k4aosgycml1b5z33BVV6hai1Kh7uDgFOkbcs=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
32 changes: 29 additions & 3 deletions runnable.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ func (rn *Runnable) ExpectedOutput() *regexp.Regexp {
return nil
}

func (rn *Runnable) ExpectedError() *regexp.Regexp {
rn.parseTags()

if v, ok := rn.Tags["error"]; ok {
if s, ok := v.(string); ok {
return regexp.MustCompile(s)
}
}

return nil
}

func (rn *Runnable) IsValidOS() bool {
rn.parseTags()
v, ok := rn.Tags["os"]
Expand Down Expand Up @@ -283,7 +295,6 @@ func (rn *Runnable) executeCommands(env []string, commands []*command) *runResul
var err error
interruptable := false
interrupted := false
dur := defaultKillDuration

rn.log.WithFields(logrus.Fields{
"runnable": rn.GoString(),
Expand All @@ -305,7 +316,7 @@ func (rn *Runnable) executeCommands(env []string, commands []*command) *runResul
"command": c.Args,
}).Debug("running runnable command")

interruptable, dur = rn.Interruptable()
interruptable, dur := rn.Interruptable()

if c.Main && interruptable {
rn.log.WithFields(logrus.Fields{
Expand Down Expand Up @@ -385,16 +396,31 @@ func (rn *Runnable) executeCommands(env []string, commands []*command) *runResul
}
}

expectedError := rn.ExpectedError()

if expectedError != nil {
if !expectedError.MatchString(res.Stderr) {
res.Error = fmt.Errorf("expected error does not match actual: %q ~= %q",
expectedError, res.Stderr)
} else {
rn.log.WithFields(logrus.Fields{
"expected": fmt.Sprintf("%q", expectedError.String()),
"actual": fmt.Sprintf("%q", res.Stdout),
}).Debug("output matched")
}
}

if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.Success() {
res.Retcode = 0
return res
}

res.Error = err
if interrupted && interruptable {
if interrupted && interruptable || expectedError != nil {
res.Error = nil
}

return res
}

Expand Down

0 comments on commit 222f1c4

Please sign in to comment.