Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
support Silent error
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Jan 31, 2021
1 parent b2d6194 commit fb542e9
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ $ wget -qO- https://raw.githubusercontent.com/octolab/testit/master/bin/install

```bash
# use standard go tools
$ go get github.com/octolab/testit@latest
$ go get go.octolab.org/toolset/testit@latest
# or use egg tool
$ egg tools add github.com/octolab/testit@latest
$ egg tools add go.octolab.org/toolset/testit@latest
```

> [egg][] is an `extended go get`.
### Shell completions

```bash
$ testit completion bash|fish|powershell|zsh > /path/to/completions/...
$ testit completion > /path/to/completions/...
# or
$ source <(testit completion bash|fish|powershell|zsh)
$ source <(testit completion)
```

<p align="right">made with ❤️ for everyone</p>
Expand All @@ -83,5 +83,5 @@ $ source <(testit completion bash|fish|powershell|zsh)
[template.page]: https://github.com/octomation/go-tool
[template.icon]: https://img.shields.io/badge/template-go--tool-blue

[_]: https://img.shields.io/sourcegraph/rrc/github.com/octolab/testit
[_]: https://img.shields.io/sourcegraph/rrc/go.octolab.org/toolset/testit
[egg]: https://github.com/kamilsk/egg
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ require (
github.com/spf13/cobra v1.1.1
github.com/stretchr/testify v1.7.0
go.octolab.org v0.7.2
go.octolab.org/toolkit/cli v0.4.0
go.octolab.org/toolkit/cli v0.5.0
go.octolab.org/toolkit/config v0.0.4
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ go.octolab.org v0.7.2 h1:Dg3SsGKlny1p8asWaMnx+87mEzImIFj5mSSqjTFlWZU=
go.octolab.org v0.7.2/go.mod h1:nKRNx6ldZmcrMJckYRvIctL2ed8IremxWwK30lrVL5A=
go.octolab.org/toolkit/cli v0.4.0 h1:QDt4zPcaeHBw/9TDT807L3Nrbt5aqT+iPwqcQl9PH5Q=
go.octolab.org/toolkit/cli v0.4.0/go.mod h1:21e8fMBwJ8n+FXsrMPtD8w0jWNTtjqWYuV2kiD/+vzw=
go.octolab.org/toolkit/cli v0.4.1 h1:nOWiz31c07fHeiBq3c89iA7E1wRTrJrb/he5t5A85Gc=
go.octolab.org/toolkit/cli v0.4.1/go.mod h1:21e8fMBwJ8n+FXsrMPtD8w0jWNTtjqWYuV2kiD/+vzw=
go.octolab.org/toolkit/cli v0.5.0 h1:wxLaKbsCrkHBzW31cDY6CEbZYpnh3PmAe8Hy52dWC6g=
go.octolab.org/toolkit/cli v0.5.0/go.mod h1:21e8fMBwJ8n+FXsrMPtD8w0jWNTtjqWYuV2kiD/+vzw=
go.octolab.org/toolkit/config v0.0.4 h1:Q/MnSuRf4ttHCuAaq7PqpD8xQhEtmfK8mO9NX1IxvlA=
go.octolab.org/toolkit/config v0.0.4/go.mod h1:dI3TlD3b4t0ilP7T+q0BYs6mhYBXm8i4TCLqCabykBA=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down
10 changes: 8 additions & 2 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package cmd

import "github.com/spf13/cobra"
import (
"io"

"github.com/spf13/cobra"
)

// New returns the new root command.
func New() *cobra.Command {
func New(stderr, stdout io.Writer) *cobra.Command {
command := cobra.Command{
Use: "testit",
Short: "extended testing toolset",
Expand All @@ -15,6 +19,8 @@ func New() *cobra.Command {
SilenceUsage: true,
}

command.SetErr(stderr)
command.SetOut(stdout)
command.AddCommand(
Golang(),
)
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd_test

import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -10,7 +11,7 @@ import (
)

func TestNew(t *testing.T) {
root := New()
root := New(ioutil.Discard, ioutil.Discard)
require.NotNil(t, root)
assert.NotEmpty(t, root.Use)
assert.NotEmpty(t, root.Short)
Expand Down
15 changes: 10 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go.octolab.org/errors"
"go.octolab.org/safe"
"go.octolab.org/toolkit/cli/cobra"
cli "go.octolab.org/toolkit/cli/errors"
"go.octolab.org/unsafe"

"go.octolab.org/toolset/testit/internal/cmd"
Expand Down Expand Up @@ -38,9 +39,7 @@ func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

root := cmd.New()
root.SetErr(stderr)
root.SetOut(stdout)
root := cmd.New(stderr, stdout)
root.AddCommand(
cobra.NewCompletionCommand(),
cobra.NewVersionCommand(version, date, commit, cnf.Features...),
Expand All @@ -50,10 +49,16 @@ func main() {
}

func shutdown(err error) {
if recovered, is := errors.Unwrap(err).(errors.Recovered); is {
code := 1
if silent, is := errors.Unwrap(err).(cli.Silent); is {
code = silent.Code
if silent.Message != "" {
unsafe.DoSilent(fmt.Fprintln(stderr, silent.Message))
}
} else if recovered, is := errors.Unwrap(err).(errors.Recovered); is {
unsafe.DoSilent(fmt.Fprintf(stderr, "recovered: %+v\n", recovered.Cause()))
unsafe.DoSilent(fmt.Fprintln(stderr, "---"))
unsafe.DoSilent(fmt.Fprintf(stderr, "%+v\n", err))
}
exit(1)
exit(code)
}
11 changes: 8 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,31 @@ import (

"github.com/stretchr/testify/assert"
"go.octolab.org/safe"
cli "go.octolab.org/toolkit/cli/errors"
)

func TestExecution(t *testing.T) {
stderr, stdout = ioutil.Discard, ioutil.Discard

t.Run("success", func(t *testing.T) {
exit = func(code int) { assert.Equal(t, 0, code) }
stderr, stdout = ioutil.Discard, ioutil.Discard
os.Args = []string{"root", "version"}
main()
})

t.Run("failure", func(t *testing.T) {
exit = func(code int) { assert.Equal(t, 1, code) }
stderr, stdout = ioutil.Discard, ioutil.Discard
os.Args = []string{"root", "unknown"}
main()
})

t.Run("shutdown silent", func(t *testing.T) {
exit = func(code int) { assert.Equal(t, 2, code) }
safe.Do(func() error { return cli.Silent{Code: 2, Message: "silence"} }, shutdown)
})

t.Run("shutdown with panic", func(t *testing.T) {
exit = func(code int) { assert.Equal(t, 1, code) }
stderr, stdout = ioutil.Discard, ioutil.Discard
safe.Do(func() error { panic("test") }, shutdown)
})
}

0 comments on commit fb542e9

Please sign in to comment.