-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Included tests for internal/signalio/text
- Included test for `internal/signalio/text` Signed-off-by: nathannaveen <[email protected]>
- Loading branch information
1 parent
27ad7d2
commit c8b6513
Showing
1 changed file
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) | ||
} | ||
} |