From 8c598c06fd23dd4cf7ad6f3a477cad45baae0587 Mon Sep 17 00:00:00 2001 From: Brian Camacho Date: Thu, 26 Oct 2023 22:47:36 -0700 Subject: [PATCH] do not assert on text output; check for crashes in all files; fix one more NPE --- go/cli/mcap/cmd/info.go | 4 +- go/cli/mcap/cmd/info_test.go | 107 +++++++++-------------------------- 2 files changed, 29 insertions(+), 82 deletions(-) diff --git a/go/cli/mcap/cmd/info.go b/go/cli/mcap/cmd/info.go index ca3f88ea56..234f30a4da 100644 --- a/go/cli/mcap/cmd/info.go +++ b/go/cli/mcap/cmd/info.go @@ -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 { diff --git a/go/cli/mcap/cmd/info_test.go b/go/cli/mcap/cmd/info_test.go index 9b8a065e6e..f42cf2a507 100644 --- a/go/cli/mcap/cmd/info_test.go +++ b/go/cli/mcap/cmd/info_test.go @@ -3,6 +3,7 @@ package cmd import ( "bytes" "os" + "path/filepath" "strings" "testing" @@ -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) : -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) }