Skip to content

Commit bb5fe92

Browse files
authored
Print aggregate output as commands complete (#5)
Closes #3
1 parent 2f354b7 commit bb5fe92

File tree

6 files changed

+205
-197
lines changed

6 files changed

+205
-197
lines changed

cmd/concurrently.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ var cCommand = cobra.Command{
4949
dbg.Prettyln(commands)
5050
}
5151

52-
if commands != nil && aggregateOutput {
53-
for _, cmd := range commands {
54-
fmt.Print(cmd.ReadOut())
55-
}
56-
}
57-
5852
return err
5953
},
6054
}

integration/helpers.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package test
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"sort"
7+
"strings"
8+
"testing"
9+
)
10+
11+
type runner struct {
12+
cmd string
13+
flags []string
14+
env []string
15+
}
16+
17+
func newRunner(cmd string) runner {
18+
return runner{cmd: cmd}
19+
}
20+
21+
func (r runner) withFlags(flags ...string) runner {
22+
r.flags = append(r.flags, flags...)
23+
return r
24+
}
25+
26+
func (r runner) withEnv(env ...string) runner {
27+
r.env = append(r.env, env...)
28+
return r
29+
}
30+
31+
func (r runner) run(t *testing.T) (string, error) {
32+
t.Helper()
33+
34+
out := new(strings.Builder)
35+
fullCmd := append([]string{r.cmd}, r.flags...)
36+
cmd := exec.Command("bin/konk", fullCmd...)
37+
cmd.Stdout = out
38+
cmd.Stderr = out
39+
cmd.Env = append(cmd.Env, r.env...)
40+
41+
err := cmd.Run()
42+
return out.String(), err
43+
}
44+
45+
func sortOut(t *testing.T, out string) string {
46+
t.Helper()
47+
48+
lines := strings.Split(out, "\n")
49+
mapByPrefix := make(map[string][]string)
50+
51+
for _, line := range lines {
52+
prefix := strings.SplitN(line, "]", 2)[0]
53+
54+
if prefix == "" {
55+
continue
56+
}
57+
58+
mapByPrefix[prefix] = append(mapByPrefix[prefix], line)
59+
}
60+
61+
keys := make([]string, 0, len(mapByPrefix))
62+
for k := range mapByPrefix {
63+
keys = append(keys, k)
64+
}
65+
sort.Strings(keys)
66+
67+
var sortedLines []string
68+
69+
for _, k := range keys {
70+
sortedLines = append(sortedLines, mapByPrefix[k]...)
71+
}
72+
73+
// Our output always ends in a newline.
74+
return fmt.Sprintf("%s\n", strings.Join(sortedLines, "\n"))
75+
}

integration/proc_test.go

Lines changed: 31 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package test
22

33
import (
4-
"os/exec"
5-
"sort"
6-
"strings"
74
"testing"
85

96
"github.com/stretchr/testify/assert"
@@ -12,96 +9,58 @@ import (
129
func TestProc(t *testing.T) {
1310
t.Parallel()
1411

15-
out := new(strings.Builder)
16-
cmd := exec.Command(
17-
"bin/konk", "proc",
18-
"-w", "fixtures/proc")
19-
cmd.Stdout = out
20-
cmd.Stderr = out
21-
if err := cmd.Run(); err != nil {
22-
t.Error(err)
23-
}
12+
out, err := newProcRunner().run(t)
13+
assert.NoError(t, err)
2414

25-
lines := strings.Split(out.String(), "\n")
26-
sort.Strings(lines)
27-
sortedOut := strings.Join(lines, "\n")
28-
29-
assert.Equal(t, `
30-
[echo-a] a
15+
assert.Equal(t, `[echo-a] a
3116
[echo-b] b
32-
[echo-c] `, sortedOut, "output did not match expected output")
17+
[echo-c]
18+
`, sortOut(t, out), "output did not match expected output")
3319
}
3420

3521
func TestProcEnvSpaces(t *testing.T) {
3622
t.Parallel()
3723

38-
out := new(strings.Builder)
39-
cmd := exec.Command(
40-
"bin/konk", "proc",
24+
out, err := newProcRunner().withFlags(
4125
"-e", ".env-spaces",
42-
"-p", "Procfile-spaces",
43-
"-w", "fixtures/proc")
44-
cmd.Stdout = out
45-
cmd.Stderr = out
46-
if err := cmd.Run(); err != nil {
47-
t.Error(err)
48-
}
49-
50-
lines := strings.Split(out.String(), "\n")
51-
sort.Strings(lines)
52-
sortedOut := strings.Join(lines, "\n")
26+
"-p", "Procfile-spaces").
27+
run(t)
28+
assert.NoError(t, err)
5329

54-
assert.Equal(t, `
55-
[echo-abc] a b c
56-
[echo-def] d "e" f`, sortedOut, "output did not match expected output")
30+
assert.Equal(t, `[echo-abc] a b c
31+
[echo-def] d "e" f
32+
`, sortOut(t, out), "output did not match expected output")
5733
}
5834

5935
func TestProcWithExternalEnvNoEnv(t *testing.T) {
6036
t.Parallel()
6137

62-
out := new(strings.Builder)
63-
cmd := exec.Command(
64-
"bin/konk", "proc", "-E",
65-
"-w", "fixtures/proc")
66-
cmd.Env = append(cmd.Env, "A=new-a")
67-
cmd.Env = append(cmd.Env, "B=new-b")
68-
cmd.Env = append(cmd.Env, "C=new-c")
69-
cmd.Stdout = out
70-
cmd.Stderr = out
71-
if err := cmd.Run(); err != nil {
72-
t.Error(err)
73-
}
74-
75-
lines := strings.Split(out.String(), "\n")
76-
sort.Strings(lines)
77-
sortedOut := strings.Join(lines, "\n")
38+
out, err := newProcRunner().
39+
withFlags("-E").
40+
withEnv("A=new-a", "B=new-b", "C=new-c").
41+
run(t)
42+
assert.NoError(t, err)
7843

79-
assert.Equal(t, `
80-
[echo-a] new-a
44+
assert.Equal(t, `[echo-a] new-a
8145
[echo-b] new-b
82-
[echo-c] new-c`, sortedOut, "output did not match expected output")
46+
[echo-c] new-c
47+
`, sortOut(t, out), "output did not match expected output")
8348
}
8449

8550
func TestProcWithExternalEnvAndEnv(t *testing.T) {
8651
t.Parallel()
8752

88-
out := new(strings.Builder)
89-
cmd := exec.Command(
90-
"bin/konk", "proc",
91-
"-w", "fixtures/proc")
92-
cmd.Env = append(cmd.Env, "C=c")
93-
cmd.Stdout = out
94-
cmd.Stderr = out
95-
if err := cmd.Run(); err != nil {
96-
t.Error(err)
97-
}
53+
out, err := newProcRunner().
54+
withEnv("C=c").
55+
run(t)
56+
assert.NoError(t, err)
9857

99-
lines := strings.Split(out.String(), "\n")
100-
sort.Strings(lines)
101-
sortedOut := strings.Join(lines, "\n")
102-
103-
assert.Equal(t, `
104-
[echo-a] a
58+
assert.Equal(t, `[echo-a] a
10559
[echo-b] b
106-
[echo-c] c`, sortedOut, "output did not match expected output")
60+
[echo-c] c
61+
`, sortOut(t, out), "output did not match expected output")
62+
}
63+
64+
func newProcRunner() runner {
65+
return newRunner("proc").withFlags("-w", "fixtures/proc")
10766
}

integration/run_c_test.go

Lines changed: 47 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package test
22

33
import (
44
"os/exec"
5-
"strings"
65
"testing"
76

87
"github.com/stretchr/testify/assert"
@@ -11,88 +10,74 @@ import (
1110
func TestRunConcurrently(t *testing.T) {
1211
t.Parallel()
1312

14-
out := new(strings.Builder)
15-
cmd := exec.Command("bin/konk", "run", "concurrently", "-g",
16-
"echo a", "echo b", "echo c")
17-
cmd.Stdout = out
18-
cmd.Stderr = out
19-
if err := cmd.Run(); err != nil {
20-
t.Error(err)
21-
}
13+
out, err := newGroupedConcurrentRunner().
14+
withFlags("echo a", "echo b", "echo c").
15+
run(t)
16+
assert.NoError(t, err)
17+
2218
assert.Equal(t, `[0] a
2319
[1] b
2420
[2] c
25-
`, out.String(), "output did not match expected output")
21+
`, sortOut(t, out), "output did not match expected output")
2622
}
2723

2824
func TestRunConcurrentlyWithLabels(t *testing.T) {
2925
t.Parallel()
3026

31-
out := new(strings.Builder)
32-
cmd := exec.Command(
33-
"bin/konk", "run", "concurrently", "-g",
34-
"-l", "a", "-l", "b", "-l", "c",
35-
"echo a", "echo b", "echo c")
36-
cmd.Stdout = out
37-
cmd.Stderr = out
38-
if err := cmd.Run(); err != nil {
39-
t.Error(err)
40-
}
27+
out, err := newGroupedConcurrentRunner().
28+
withFlags(
29+
"-l", "a", "-l", "b", "-l", "c",
30+
"echo a", "echo b", "echo c").
31+
run(t)
32+
assert.NoError(t, err)
33+
4134
assert.Equal(t, `[a] a
4235
[b] b
4336
[c] c
44-
`, out.String(), "output did not match expected output")
37+
`, sortOut(t, out), "output did not match expected output")
4538
}
4639

4740
func TestRunConcurrentlyWithLabelsMismatch(t *testing.T) {
4841
t.Parallel()
4942

50-
out := new(strings.Builder)
51-
cmd := exec.Command(
52-
"bin/konk", "run", "concurrently", "-g",
53-
"-l", "a", "-l", "b",
54-
"echo a", "echo b", "echo c")
55-
cmd.Stdout = out
56-
cmd.Stderr = out
43+
out, err := newGroupedConcurrentRunner().
44+
withFlags(
45+
"-l", "a", "-l", "b",
46+
"echo a", "echo b", "echo c").
47+
run(t)
5748

58-
err := cmd.Run()
5949
if assert.Error(t, err) {
6050
assert.IsType(t, &exec.ExitError{}, err)
6151
}
6252

63-
assert.Equal(t, "Error: number of names must match number of commands\n", out.String(), "error output did not match expectation")
53+
assert.Equal(t, "Error: number of names must match number of commands\n", out, "error output did not match expectation")
6454
}
6555

6656
func TestRunConcurrentlyWithCommandLabels(t *testing.T) {
6757
t.Parallel()
6858

69-
out := new(strings.Builder)
70-
cmd := exec.Command(
71-
"bin/konk", "run", "concurrently", "-gL",
72-
"echo a", "echo b", "echo c")
73-
cmd.Stdout = out
74-
if err := cmd.Run(); err != nil {
75-
t.Error(err)
76-
}
59+
out, err := newGroupedConcurrentRunner().
60+
withFlags("-L", "echo a", "echo b", "echo c").
61+
run(t)
62+
assert.NoError(t, err)
63+
7764
assert.Equal(t, `[echo a] a
7865
[echo b] b
7966
[echo c] c
80-
`, out.String(), "output did not match expected output")
67+
`, sortOut(t, out), "output did not match expected output")
8168
}
8269

8370
func TestRunConcurrentlyWithNpm(t *testing.T) {
8471
t.Parallel()
8572

86-
out := new(strings.Builder)
87-
cmd := exec.Command(
88-
"bin/konk", "run", "concurrently", "-g",
89-
"-w", "fixtures/npm",
90-
"--npm", "echo-a",
91-
"--npm", "echo-b")
92-
cmd.Stdout = out
93-
if err := cmd.Run(); err != nil {
94-
t.Error(err)
95-
}
73+
out, err := newGroupedConcurrentRunner().
74+
withFlags(
75+
"-w", "fixtures/npm",
76+
"--npm", "echo-a",
77+
"--npm", "echo-b").
78+
run(t)
79+
assert.NoError(t, err)
80+
9681
assert.Equal(t, `[0]
9782
[0] > echo-a
9883
[0] > echo a
@@ -103,21 +88,19 @@ func TestRunConcurrentlyWithNpm(t *testing.T) {
10388
[1] > echo b
10489
[1]
10590
[1] b
106-
`, out.String(), "output did not match expected output")
91+
`, sortOut(t, out), "output did not match expected output")
10792
}
10893

10994
func TestRunConcurrentlyWithNpmGlob(t *testing.T) {
11095
t.Parallel()
11196

112-
out := new(strings.Builder)
113-
cmd := exec.Command(
114-
"bin/konk", "run", "concurrently", "-g",
115-
"-w", "fixtures/npm",
116-
"--npm", "echo-*")
117-
cmd.Stdout = out
118-
if err := cmd.Run(); err != nil {
119-
t.Error(err)
120-
}
97+
out, err := newGroupedConcurrentRunner().
98+
withFlags(
99+
"-w", "fixtures/npm",
100+
"--npm", "echo-*").
101+
run(t)
102+
assert.NoError(t, err)
103+
121104
assert.Equal(t, `[0]
122105
[0] > echo-a
123106
[0] > echo a
@@ -128,5 +111,9 @@ func TestRunConcurrentlyWithNpmGlob(t *testing.T) {
128111
[1] > echo b
129112
[1]
130113
[1] b
131-
`, out.String(), "output did not match expected output")
114+
`, sortOut(t, out), "output did not match expected output")
115+
}
116+
117+
func newGroupedConcurrentRunner() runner {
118+
return newRunner("run").withFlags("concurrently", "-g")
132119
}

0 commit comments

Comments
 (0)