From c8b6513dbddc57338a6c9f125eb5b893ab5d98f7 Mon Sep 17 00:00:00 2001 From: nathannaveen <42319948+nathannaveen@users.noreply.github.com> Date: Tue, 10 Jan 2023 11:57:30 -0600 Subject: [PATCH] Included tests for internal/signalio/text - Included test for `internal/signalio/text` Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com> --- internal/signalio/text_test.go | 122 +++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 internal/signalio/text_test.go diff --git a/internal/signalio/text_test.go b/internal/signalio/text_test.go new file mode 100644 index 000000000..dc8a24244 --- /dev/null +++ b/internal/signalio/text_test.go @@ -0,0 +1,122 @@ +package signalio + +import ( + "io" + "sync" + "testing" + + "github.com/ossf/criticality_score/internal/collector/signal" +) + +func Test_textWriter_writeRecord(t *testing.T) { + type fields struct { + w io.Writer + fields []string + firstRecordWritten bool + } + tests := []struct { //nolint:govet + name string + fields fields + values map[string]string + wantErr bool + }{ + { + name: "regular", + fields: fields{ + w: io.Writer(&mockWriter{ + written: []byte{}, + }), + fields: []string{"test"}, + firstRecordWritten: false, + }, + values: map[string]string{ + "test": "test", + }, + }, + { + name: "first record written and w is invalid", + fields: fields{ + w: &mockWriter{}, + fields: []string{"test"}, + firstRecordWritten: true, + }, + values: map[string]string{}, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + w := &textWriter{ + w: test.fields.w, + fields: test.fields.fields, + firstRecordWritten: test.fields.firstRecordWritten, + mu: sync.Mutex{}, + } + + if err := w.writeRecord(test.values); (err != nil) != test.wantErr { + t.Errorf("writeRecord() error = %v, wantErr %v", err, test.wantErr) + } + }) + } +} + +type mockWriter struct { + written []byte +} + +func (m *mockWriter) Write(p []byte) (n int, err error) { + return 0, nil +} + +func Test_textWriter_WriteSignals(t *testing.T) { + type fields struct { + w io.Writer + fields []string + firstRecordWritten bool + } + type args struct { + signals []signal.Set + extra []Field + } + tests := []struct { //nolint:govet + name string + fields fields + args args + wantErr bool + }{ + { + name: "regular", + fields: fields{ + w: io.Writer(&mockWriter{ + written: []byte{}, + }), + fields: []string{"test"}, + firstRecordWritten: true, + }, + args: args{ + signals: []signal.Set{}, + extra: []Field{}, + }, + }, + { + name: "error while marshaling with extra", + args: args{ + signals: []signal.Set{}, + extra: []Field{{"test", []string{"invalid"}}}, + }, + wantErr: true, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + w := &textWriter{ + w: test.fields.w, + fields: test.fields.fields, + firstRecordWritten: test.fields.firstRecordWritten, + mu: sync.Mutex{}, + } + if err := w.WriteSignals(test.args.signals, test.args.extra...); (err != nil) != test.wantErr { + t.Errorf("WriteSignals() error = %v, wantErr %v", err, test.wantErr) + } + }) + } +}