Skip to content

Commit

Permalink
Merge pull request #46 from johejo/wrap_error
Browse files Browse the repository at this point in the history
Use fmt.Errorf to be able to unwrap error
  • Loading branch information
mattn authored Jan 17, 2021
2 parents ce7c607 + 3136567 commit 501b5b2
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: ci
on:
pull_request:
branches:
- master
push:
branches:
- master
jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
go: ["1.14", "1.15"]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- run: |
go test -cover -coverprofile coverage.txt -race -v ./...
- uses: codecov/codecov-action@v1
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

[![codecov](https://codecov.io/gh/mattn/go-shellwords/branch/master/graph/badge.svg)](https://codecov.io/gh/mattn/go-shellwords)
[![Build Status](https://travis-ci.org/mattn/go-shellwords.svg?branch=master)](https://travis-ci.org/mattn/go-shellwords)
[![GoDoc](https://godoc.org/github.com/mattn/go-shellwords?status.svg)](http://godoc.org/github.com/mattn/go-shellwords)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/mattn/go-shellwords)](https://pkg.go.dev/github.com/mattn/go-shellwords)
[![ci](https://github.com/mattn/go-shellwords/ci/badge.svg)](https://github.com/mattn/go-shellwords/actions)

Parse line as shell words.

Expand Down
11 changes: 2 additions & 9 deletions shellwords.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"os"
"regexp"
"strings"
"unicode"
)
Expand All @@ -14,8 +13,6 @@ var (
ParseBacktick bool = false
)

var envRe = regexp.MustCompile(`\$({[a-zA-Z0-9_]+}|[a-zA-Z0-9_]+)`)

func isSpace(r rune) bool {
switch r {
case ' ', '\t', '\r', '\n':
Expand Down Expand Up @@ -166,9 +163,7 @@ loop:
if err != nil {
return nil, err
}
for _, str := range strs {
args = append(args, str)
}
args = append(args, strs...)
} else {
args = append(args, replaceEnv(p.Getenv, buf))
}
Expand Down Expand Up @@ -270,9 +265,7 @@ loop:
if err != nil {
return nil, err
}
for _, str := range strs {
args = append(args, str)
}
args = append(args, strs...)
} else {
args = append(args, replaceEnv(p.Getenv, buf))
}
Expand Down
11 changes: 8 additions & 3 deletions shellwords_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package shellwords

import (
"errors"
"go/build"
"os"
"os/exec"
"path"
"reflect"
"testing"
Expand Down Expand Up @@ -149,6 +151,9 @@ func TestBacktick(t *testing.T) {

parser.ParseBacktick = false
args, err = parser.Parse(`echo $(echo "foo")`)
if err != nil {
t.Fatal(err)
}
expected = []string{"echo", `$(echo "foo")`}
if !reflect.DeepEqual(args, expected) {
t.Fatalf("Expected %#v, but %#v:", expected, args)
Expand Down Expand Up @@ -183,9 +188,9 @@ func TestBacktickError(t *testing.T) {
if err == nil {
t.Fatal("Should be an error")
}
expected := "exit status 2:go Version: unknown command\nRun 'go help' for usage.\n"
if expected != err.Error() {
t.Fatalf("Expected %q, but %q", expected, err.Error())
var eerr *exec.ExitError
if !errors.As(err, &eerr) {
t.Fatal("Should be able to unwrap to *exec.ExitError")
}
_, err = parser.Parse(`echo $(echo1)`)
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions util_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package shellwords

import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
Expand All @@ -23,7 +23,7 @@ func shellRun(line, dir string) (string, error) {
if eerr, ok := err.(*exec.ExitError); ok {
b = eerr.Stderr
}
return "", errors.New(err.Error() + ":" + string(b))
return "", fmt.Errorf("%s: %w", string(b), err)
}
return strings.TrimSpace(string(b)), nil
}
4 changes: 2 additions & 2 deletions util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package shellwords

import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
Expand All @@ -23,7 +23,7 @@ func shellRun(line, dir string) (string, error) {
if eerr, ok := err.(*exec.ExitError); ok {
b = eerr.Stderr
}
return "", errors.New(err.Error() + ":" + string(b))
return "", fmt.Errorf("%s: %w", string(b), err)
}
return strings.TrimSpace(string(b)), nil
}

0 comments on commit 501b5b2

Please sign in to comment.