Skip to content

Commit

Permalink
do not assert on text output; check for crashes in all files; fix one…
Browse files Browse the repository at this point in the history
… more NPE
  • Loading branch information
b-camacho committed Oct 27, 2023
1 parent 0a2cfef commit 8c598c0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 82 deletions.
4 changes: 2 additions & 2 deletions go/cli/mcap/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ func printInfo(w io.Writer, info *mcap.Info) error {
for _, chanID := range chanIDs {
channel := info.Channels[chanID]
schema := info.Schemas[channel.SchemaID]
channelMessageCount := info.Statistics.ChannelMessageCounts[chanID]
frequency := 1e9 * float64(channelMessageCount) / float64(end-start)
width := digits(uint64(chanID)) + 2
padding := strings.Repeat(" ", maxChanIDWidth-width)
row := []string{
fmt.Sprintf("\t(%d)%s%s", channel.ID, padding, channel.Topic),
}
if info.Statistics != nil {
channelMessageCount := info.Statistics.ChannelMessageCounts[chanID]
frequency := 1e9 * float64(channelMessageCount) / float64(end-start)
row = append(row, fmt.Sprintf("%*d msgs (%.2f Hz)", maxCountWidth, channelMessageCount, frequency))
}
switch {
Expand Down
107 changes: 27 additions & 80 deletions go/cli/mcap/cmd/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"

Expand All @@ -11,84 +12,30 @@ import (
)

func TestInfo(t *testing.T) {
cases := []struct {
assertion string
inputfile string
expected string
}{
{
"OneMessage",
"../../../../tests/conformance/data/OneMessage/OneMessage-ch-chx-mx-pad-rch-rsh-st-sum.mcap",
`library:
profile:
messages: 1
duration: 0s
start: 0.000000002
end: 0.000000002
compression:
: [1/1 chunks] [115.00 B/115.00 B (0.00%)]
channels:
(1) example 1 msgs (+Inf Hz) : Example [c]
attachments: 0
metadata: 0`,
},
{
"OneSchemalessMessage",
"../../../../tests/conformance/data/OneSchemalessMessage/OneSchemalessMessage-ch-chx-mx-pad-rch-st.mcap",
`library:
profile:
messages: 1
duration: 0s
start: 0.000000002
end: 0.000000002
compression:
: [1/1 chunks] [70.00 B/70.00 B (0.00%)]
channels:
(1) example 1 msgs (+Inf Hz) : <no schema>
attachments: 0
metadata: 0`,
},
{
"OneSchemalessMessage_NoChannels",
"../../../../tests/conformance/data/OneSchemalessMessage/OneSchemalessMessage.mcap",
`library:
profile:
channels:
attachments: unknown
metadata: unknown`,
},
}
for _, c := range cases {
input, err := os.ReadFile(c.inputfile)
assert.Nil(t, err)
r := bytes.NewReader(input)
w := new(bytes.Buffer)

t.Run(c.assertion, func(t *testing.T) {
reader, err := mcap.NewReader(r)
assert.Nil(t, err)
defer reader.Close()
info, err := reader.Info()
assert.Nil(t, err)
err = printInfo(w, info)
assert.Nil(t, err)

// for each line, strip leading/trailing whitespace
// this prevents test failures from formatting changes
actualLines := strings.Split(strings.TrimSpace(w.String()), "\n")
expectedLines := strings.Split(strings.TrimSpace(c.expected), "\n")

for i, line := range actualLines {
actualLines[i] = strings.TrimSpace(line)
}
for i, line := range expectedLines {
expectedLines[i] = strings.TrimSpace(line)
}

actualStripped := strings.Join(actualLines, "\n")
expectedStripped := strings.Join(expectedLines, "\n")

assert.Equal(t, expectedStripped, actualStripped)
})
}
err := filepath.Walk("../../../../tests/conformance/data/", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, ".mcap") {
t.Run(path, func(t *testing.T) {
input, err := os.ReadFile(path)
assert.Nil(t, err)
r := bytes.NewReader(input)
w := new(bytes.Buffer)

reader, err := mcap.NewReader(r)
assert.Nil(t, err)
defer reader.Close()
info, err := reader.Info()
assert.Nil(t, err)
err = printInfo(w, info)
assert.Nil(t, err)
})
}
return nil
})
assert.Nil(t, err)
}

0 comments on commit 8c598c0

Please sign in to comment.