| 1 | package test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/owenrumney/go-sarif/sarif" |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | type reportTest struct { |
| 13 | t *testing.T |
| 14 | report *sarif.Report |
| 15 | } |
| 16 | |
| 17 | func newReportTest(t *testing.T) (*reportTest, *reportTest, *reportTest) { |
| 18 | rt := &reportTest{ |
| 19 | t: t, |
| 20 | } |
| 21 | return rt, rt, rt |
| 22 | } |
| 23 | |
| 24 | func (r *reportTest) a_new_report() *reportTest { |
| 25 | report, err := sarif.New(sarif.Version210) |
| 26 | require.NoError(r.t, err) |
| 27 | r.report = report |
| 28 | return r |
| 29 | } |
| 30 | |
| 31 | func (r *reportTest) and() *reportTest { |
| 32 | return r |
| 33 | } |
| 34 | |
| 35 | func (r *reportTest) with_a_run_added(tool, informationUri string) *sarif.Run { |
| 36 | run := sarif.NewRun(tool, informationUri) |
| 37 | r.report.AddRun(run) |
| 38 | return run |
| 39 | } |
| 40 | |
| 41 | func (r *reportTest) with_a_run_with_empty_result_added(tool, informationUri string) *sarif.Run { |
| 42 | run := sarif.NewRun(tool, informationUri) |
| 43 | r.report.AddRun(run) |
| 44 | return run |
| 45 | } |
| 46 | |
| 47 | func (r *reportTest) an_artifact_is_added_to_the_run(run *sarif.Run, filename string) *reportTest { |
| 48 | a := run.AddArtifact() |
| 49 | a.Location = &sarif.ArtifactLocation{ |
| 50 | URI: &filename, |
| 51 | } |
| 52 | return r |
| 53 | } |
| 54 | |
| 55 | func (r *reportTest) some_properties_are_added_to_the_run(run *sarif.Run) *reportTest { |
| 56 | pb := sarif.NewPropertyBag() |
| 57 | pb.AddString("string_property", "this is a string") |
| 58 | pb.AddInteger("integer_property", 10) |
| 59 | run.AttachPropertyBag(pb) |
| 60 | return r |
| 61 | } |
| 62 | |
| 63 | func (r *reportTest) report_text_is(expected string) { |
| 64 | buffer := bytes.NewBufferString("") |
| 65 | err := r.report.Write(buffer) |
| 66 | require.NoError(r.t, err) |
| 67 | |
| 68 | assert.Equal(r.t, expected, buffer.String()) |
| 69 | } |
| 70 | |
| 71 | func (r *reportTest) a_report_is_loaded_from_a_string(content string) { |
| 72 | report, err := sarif.FromString(content) |
| 73 | assert.NoError(r.t, err) |
| 74 | assert.NotNil(r.t, report) |
| 75 | r.report = report |
| 76 | } |
| 77 | |
| 78 | func (r *reportTest) the_report_has_expected_driver_name_and_information_uri(driverName string, informationUri string) { |
| 79 | assert.Equal(r.t, driverName, r.report.Runs[0].Tool.Driver.Name) |
| 80 | assert.Equal(r.t, informationUri, *r.report.Runs[0].Tool.Driver.InformationURI) |
| 81 | } |
| 82 | |
| 83 | func (r *reportTest) a_report_is_loaded_from_a_file(filename string) { |
| 84 | report, err := sarif.Open(filename) |
| 85 | if err != nil { |
| 86 | panic(err) |
| 87 | } |
| 88 | r.report = report |
| 89 | |
| 90 | } |