Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly handle empty params #36

Merged
merged 4 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"github.com/magiconair/properties/assert"
"testing"
)

func TestCmdBuilder(t *testing.T) {
cb := newCmdBuilder("go test").arg("zero", "").
argNoBlank("one", "", "two").arg("", "three").argNoBlank("", "four")
assert.Equal(t, cb.cmd, "go", "unexpected cmd")
assert.Equal(t, len(cb.args), 8, "unexpected number of args")
assert.Equal(t, cb.args[0], "test", "unexpected value on arg")
assert.Equal(t, cb.args[1], "zero", "unexpected value on arg")
assert.Equal(t, cb.args[2], "", "unexpected value on arg")
assert.Equal(t, cb.args[3], "one", "unexpected value on arg")
assert.Equal(t, cb.args[4], "two", "unexpected value on arg")
assert.Equal(t, cb.args[5], "", "unexpected value on arg")
assert.Equal(t, cb.args[6], "three", "unexpected value on arg")
assert.Equal(t, cb.args[7], "four", "unexpected value on arg")
}
59 changes: 39 additions & 20 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ GO_TEST_BINARY="gotest"
if err != nil {
return err
} else if len(tags) != 0 {
tagsArg = "-tags="+strings.Join(tags, ",")
tagsArg = "-tags=" + strings.Join(tags, ",")
}

payload := "mode: " + mode + "\n"
Expand All @@ -85,7 +85,7 @@ GO_TEST_BINARY="gotest"

if len(a) > 4 && a[len(a)-4:] == "/..." {
var buf bytes.Buffer
c := exec.Command("go", "list", tagsArg, a)
c := newCmdBuilder("go list").argNoBlank(tagsArg).arg(a).exec()
c.Stdout = &buf
c.Stderr = &buf
if err := c.Run(); err != nil {
Expand Down Expand Up @@ -129,24 +129,11 @@ GO_TEST_BINARY="gotest"
gotest = "go test"
}

gt := strings.Split(gotest, " ")
if len(gt) != 2 {
gt = append(gt, "")
}

var c *exec.Cmd
ca := append(append(
[]string{
gt[1],
"-covermode=" + mode,
"-coverprofile=" + files[k],
"-coverpkg=" + strings.Join(packages, ","),
tagsArg,
},
passthrough...),
pkg)
c = exec.Command(gt[0], ca...)

c := newCmdBuilder(gotest).arg(
"-covermode=" + mode,
"-coverprofile=" + files[k],
"-coverpkg=" + strings.Join(packages, ","),
).argNoBlank(tagsArg).arg(passthrough...).arg(pkg).exec()
stderr, err := c.StderrPipe()
check(err)

Expand Down Expand Up @@ -239,3 +226,35 @@ func (f *filter) Write(p []byte) (n int, err error) {
}
return len(p), nil
}

type cmdBuilder struct {
cmd string
args []string
}

func newCmdBuilder(cmd string) *cmdBuilder {
c := strings.Split(cmd, " ")
b := &cmdBuilder{cmd: c[0]}
for i := 1; i < len(c); i++ {
b = b.argNoBlank(c[i])
}
return b
}

func (b *cmdBuilder) argNoBlank(args ...string) *cmdBuilder {
for _, a := range args {
if a != "" {
b.args = append(b.args, a)
}
}
return b
}

func (b *cmdBuilder) arg(args ...string) *cmdBuilder {
b.args = append(b.args, args...)
return b
}

func (b *cmdBuilder) exec() *exec.Cmd {
return exec.Command(b.cmd, b.args...)
}