Skip to content

Commit

Permalink
(nats-io#1267) Don't print empty message string when --translate is p…
Browse files Browse the repository at this point in the history
…assed
  • Loading branch information
ploubser committed Feb 13, 2025
1 parent 3071ae5 commit 0bc4581
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
5 changes: 3 additions & 2 deletions cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/jedib0t/go-pretty/v6/progress"
"io"
"math"
"os"
Expand All @@ -31,6 +30,8 @@ import (
"text/template"
"time"

"github.com/jedib0t/go-pretty/v6/progress"

"github.com/AlecAivazis/survey/v2"
"github.com/choria-io/fisk"
"github.com/google/shlex"
Expand Down Expand Up @@ -740,7 +741,7 @@ func (pr *progressRW) Write(p []byte) (n int, err error) {
}

func outPutMSGBodyCompact(data []byte, filter string, subject string, stream string) (string, error) {
if len(data) == 0 {
if len(data) == 0 && filter == "" {
fmt.Println("nil body")
return "", nil
}
Expand Down
29 changes: 25 additions & 4 deletions nats/sub_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ func TestSubscribe(t *testing.T) {
testMsgData := "this is a test string"
defer srv.Shutdown()

mgr.NewStream("ORDERS", jsm.Subjects("ORDERS.*"))
_, err := mgr.NewStream("ORDERS", jsm.Subjects("ORDERS.*"))
if err != nil {
t.Fatalf("unable to create stream: %s", err)
}
msg := nats.Msg{
Subject: "ORDERS.1",
Data: []byte(testMsgData),
}

nc.PublishMsg(&msg)

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

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

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

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

t.Run("--translate empty message", func(t *testing.T) {
empty := nats.Msg{
Subject: "ORDERS.1",
Data: []byte(""),
}
nc.PublishMsg(&empty)
output := runNatsCli(t, fmt.Sprintf("--server='%s' sub --stream ORDERS --last --count=1 --translate=\"wc -c\"", srv.ClientURL()))
// We trimspace here because some shells can pre- and append whitespaces to the output
resp := strings.TrimSpace(strings.Split(string(output), "\n")[2])
if resp != "0" {
t.Errorf("unexpected response. Got \"%s\" expected \"%s\"", resp, "21")
}
})

t.Run("--dump and --translate", func(t *testing.T) {
nc.PublishMsg(&msg)
runNatsCli(t, fmt.Sprintf("--server='%s' sub --stream ORDERS --last --count=1 --dump=/tmp/test2 --translate=\"wc -c\"", srv.ClientURL()))
defer os.RemoveAll("/tmp/test2")

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

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

t.Run("--pretty", func(t *testing.T) {
nc.PublishMsg(&msg)
output := runNatsCli(t, fmt.Sprintf("--server='%s' sub --stream ORDERS --last --count=1", srv.ClientURL()))
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`
matcher := regexp.MustCompile(pattern)
Expand Down

0 comments on commit 0bc4581

Please # to comment.