Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit 8d89424

Browse files
authored
fix: correctly handle empty params (#36)
1 parent 485a71a commit 8d89424

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

cmd/cmd_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cmd
2+
3+
import (
4+
"github.com/magiconair/properties/assert"
5+
"testing"
6+
)
7+
8+
func TestCmdBuilder(t *testing.T) {
9+
cb := newCmdBuilder("go test").arg("zero", "").
10+
argNoBlank("one", "", "two").arg("", "three").argNoBlank("", "four")
11+
assert.Equal(t, cb.cmd, "go", "unexpected cmd")
12+
assert.Equal(t, len(cb.args), 8, "unexpected number of args")
13+
assert.Equal(t, cb.args[0], "test", "unexpected value on arg")
14+
assert.Equal(t, cb.args[1], "zero", "unexpected value on arg")
15+
assert.Equal(t, cb.args[2], "", "unexpected value on arg")
16+
assert.Equal(t, cb.args[3], "one", "unexpected value on arg")
17+
assert.Equal(t, cb.args[4], "two", "unexpected value on arg")
18+
assert.Equal(t, cb.args[5], "", "unexpected value on arg")
19+
assert.Equal(t, cb.args[6], "three", "unexpected value on arg")
20+
assert.Equal(t, cb.args[7], "four", "unexpected value on arg")
21+
}

cmd/root.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ GO_TEST_BINARY="gotest"
6565
if err != nil {
6666
return err
6767
} else if len(tags) != 0 {
68-
tagsArg = "-tags="+strings.Join(tags, ",")
68+
tagsArg = "-tags=" + strings.Join(tags, ",")
6969
}
7070

7171
payload := "mode: " + mode + "\n"
@@ -85,7 +85,7 @@ GO_TEST_BINARY="gotest"
8585

8686
if len(a) > 4 && a[len(a)-4:] == "/..." {
8787
var buf bytes.Buffer
88-
c := exec.Command("go", "list", tagsArg, a)
88+
c := newCmdBuilder("go list").argNoBlank(tagsArg).arg(a).exec()
8989
c.Stdout = &buf
9090
c.Stderr = &buf
9191
if err := c.Run(); err != nil {
@@ -129,24 +129,11 @@ GO_TEST_BINARY="gotest"
129129
gotest = "go test"
130130
}
131131

132-
gt := strings.Split(gotest, " ")
133-
if len(gt) != 2 {
134-
gt = append(gt, "")
135-
}
136-
137-
var c *exec.Cmd
138-
ca := append(append(
139-
[]string{
140-
gt[1],
141-
"-covermode=" + mode,
142-
"-coverprofile=" + files[k],
143-
"-coverpkg=" + strings.Join(packages, ","),
144-
tagsArg,
145-
},
146-
passthrough...),
147-
pkg)
148-
c = exec.Command(gt[0], ca...)
149-
132+
c := newCmdBuilder(gotest).arg(
133+
"-covermode=" + mode,
134+
"-coverprofile=" + files[k],
135+
"-coverpkg=" + strings.Join(packages, ","),
136+
).argNoBlank(tagsArg).arg(passthrough...).arg(pkg).exec()
150137
stderr, err := c.StderrPipe()
151138
check(err)
152139

@@ -239,3 +226,35 @@ func (f *filter) Write(p []byte) (n int, err error) {
239226
}
240227
return len(p), nil
241228
}
229+
230+
type cmdBuilder struct {
231+
cmd string
232+
args []string
233+
}
234+
235+
func newCmdBuilder(cmd string) *cmdBuilder {
236+
c := strings.Split(cmd, " ")
237+
b := &cmdBuilder{cmd: c[0]}
238+
for i := 1; i < len(c); i++ {
239+
b = b.argNoBlank(c[i])
240+
}
241+
return b
242+
}
243+
244+
func (b *cmdBuilder) argNoBlank(args ...string) *cmdBuilder {
245+
for _, a := range args {
246+
if a != "" {
247+
b.args = append(b.args, a)
248+
}
249+
}
250+
return b
251+
}
252+
253+
func (b *cmdBuilder) arg(args ...string) *cmdBuilder {
254+
b.args = append(b.args, args...)
255+
return b
256+
}
257+
258+
func (b *cmdBuilder) exec() *exec.Cmd {
259+
return exec.Command(b.cmd, b.args...)
260+
}

0 commit comments

Comments
 (0)