Skip to content

Commit 18b8521

Browse files
authored
Merge pull request #1275 from ploubser/issue_1267
(#1267) Don't print empty message string when --translate is passed
2 parents 36d9717 + 76c3ec2 commit 18b8521

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

cli/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ func (pr *progressRW) Write(p []byte) (n int, err error) {
752752
}
753753

754754
func outPutMSGBodyCompact(data []byte, filter string, subject string, stream string) (string, error) {
755-
if len(data) == 0 {
755+
if len(data) == 0 && filter == "" {
756756
fmt.Println("nil body")
757757
return "", nil
758758
}

nats/sub_command_test.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ func TestSubscribe(t *testing.T) {
3030
testMsgData := "this is a test string"
3131
defer srv.Shutdown()
3232

33-
mgr.NewStream("ORDERS", jsm.Subjects("ORDERS.*"))
33+
_, err := mgr.NewStream("ORDERS", jsm.Subjects("ORDERS.*"))
34+
if err != nil {
35+
t.Fatalf("unable to create stream: %s", err)
36+
}
3437
msg := nats.Msg{
3538
Subject: "ORDERS.1",
3639
Data: []byte(testMsgData),
3740
}
3841

39-
nc.PublishMsg(&msg)
40-
4142
t.Run("--dump=file", func(t *testing.T) {
43+
nc.PublishMsg(&msg)
4244
runNatsCli(t, fmt.Sprintf("--server='%s' sub --stream ORDERS --last --count=1 --dump=/tmp/test1", srv.ClientURL()))
4345
defer os.RemoveAll("/tmp/test1/")
4446

@@ -58,6 +60,7 @@ func TestSubscribe(t *testing.T) {
5860
})
5961

6062
t.Run("--dump=-", func(t *testing.T) {
63+
nc.PublishMsg(&msg)
6164
output := runNatsCli(t, fmt.Sprintf("--server='%s' sub --stream ORDERS --last --count=1 --dump=-", srv.ClientURL()))
6265
// We trimspace here because some shells can pre- and append whitespaces to the output
6366
resp := strings.TrimSpace(strings.Split(string(output), "\n")[1])
@@ -75,6 +78,7 @@ func TestSubscribe(t *testing.T) {
7578
})
7679

7780
t.Run("--translate", func(t *testing.T) {
81+
nc.PublishMsg(&msg)
7882
output := runNatsCli(t, fmt.Sprintf("--server='%s' sub --stream ORDERS --last --count=1 --raw --translate=\"wc -c\"", srv.ClientURL()))
7983
// We trimspace here because some shells can pre- and append whitespaces to the output
8084
resp := strings.TrimSpace(strings.Split(string(output), "\n")[1])
@@ -83,11 +87,26 @@ func TestSubscribe(t *testing.T) {
8387
}
8488
})
8589

90+
t.Run("--translate empty message", func(t *testing.T) {
91+
empty := nats.Msg{
92+
Subject: "ORDERS.1",
93+
Data: []byte(""),
94+
}
95+
nc.PublishMsg(&empty)
96+
output := runNatsCli(t, fmt.Sprintf("--server='%s' sub --stream ORDERS --last --count=1 --translate=\"wc -c\"", srv.ClientURL()))
97+
// We trimspace here because some shells can pre- and append whitespaces to the output
98+
resp := strings.TrimSpace(strings.Split(string(output), "\n")[2])
99+
if resp != "0" {
100+
t.Errorf("unexpected response. Got \"%s\" expected \"%s\"", resp, "21")
101+
}
102+
})
103+
86104
t.Run("--dump and --translate", func(t *testing.T) {
105+
nc.PublishMsg(&msg)
87106
runNatsCli(t, fmt.Sprintf("--server='%s' sub --stream ORDERS --last --count=1 --dump=/tmp/test2 --translate=\"wc -c\"", srv.ClientURL()))
88107
defer os.RemoveAll("/tmp/test2")
89108

90-
resp, err := os.ReadFile("/tmp/test2/1.json")
109+
resp, err := os.ReadFile("/tmp/test2/5.json")
91110
if err != nil {
92111
t.Fatal(err)
93112
}
@@ -104,6 +123,7 @@ func TestSubscribe(t *testing.T) {
104123
})
105124

106125
t.Run("--raw", func(t *testing.T) {
126+
nc.PublishMsg(&msg)
107127
output := runNatsCli(t, fmt.Sprintf("--server='%s' sub --stream ORDERS --last --count=1 --raw", srv.ClientURL()))
108128
resp := strings.TrimSpace(strings.Split(string(output), "\n")[1])
109129
if resp != testMsgData {
@@ -112,6 +132,7 @@ func TestSubscribe(t *testing.T) {
112132
})
113133

114134
t.Run("--pretty", func(t *testing.T) {
135+
nc.PublishMsg(&msg)
115136
output := runNatsCli(t, fmt.Sprintf("--server='%s' sub --stream ORDERS --last --count=1", srv.ClientURL()))
116137
pattern := `\[#\d\] Received JetStream message: stream: ORDERS seq (\d+) / subject: ORDERS.1 / time: \d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ`
117138
matcher := regexp.MustCompile(pattern)

0 commit comments

Comments
 (0)