File: report_stage_test.go

package info (click to toggle)
golang-github-owenrumney-go-sarif 2.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: makefile: 13
file content (90 lines) | stat: -rw-r--r-- 2,177 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package test

import (
	"bytes"
	"testing"

	"github.com/owenrumney/go-sarif/sarif"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

type reportTest struct {
	t      *testing.T
	report *sarif.Report
}

func newReportTest(t *testing.T) (*reportTest, *reportTest, *reportTest) {
	rt := &reportTest{
		t: t,
	}
	return rt, rt, rt
}

func (r *reportTest) a_new_report() *reportTest {
	report, err := sarif.New(sarif.Version210)
	require.NoError(r.t, err)
	r.report = report
	return r
}

func (r *reportTest) and() *reportTest {
	return r
}

func (r *reportTest) with_a_run_added(tool, informationUri string) *sarif.Run {
	run := sarif.NewRun(tool, informationUri)
	r.report.AddRun(run)
	return run
}

func (r *reportTest) with_a_run_with_empty_result_added(tool, informationUri string) *sarif.Run {
	run := sarif.NewRun(tool, informationUri)
	r.report.AddRun(run)
	return run
}

func (r *reportTest) an_artifact_is_added_to_the_run(run *sarif.Run, filename string) *reportTest {
	a := run.AddArtifact()
	a.Location = &sarif.ArtifactLocation{
		URI: &filename,
	}
	return r
}

func (r *reportTest) some_properties_are_added_to_the_run(run *sarif.Run) *reportTest {
	pb := sarif.NewPropertyBag()
	pb.AddString("string_property", "this is a string")
	pb.AddInteger("integer_property", 10)
	run.AttachPropertyBag(pb)
	return r
}

func (r *reportTest) report_text_is(expected string) {
	buffer := bytes.NewBufferString("")
	err := r.report.Write(buffer)
	require.NoError(r.t, err)

	assert.Equal(r.t, expected, buffer.String())
}

func (r *reportTest) a_report_is_loaded_from_a_string(content string) {
	report, err := sarif.FromString(content)
	assert.NoError(r.t, err)
	assert.NotNil(r.t, report)
	r.report = report
}

func (r *reportTest) the_report_has_expected_driver_name_and_information_uri(driverName string, informationUri string) {
	assert.Equal(r.t, driverName, r.report.Runs[0].Tool.Driver.Name)
	assert.Equal(r.t, informationUri, *r.report.Runs[0].Tool.Driver.InformationURI)
}

func (r *reportTest) a_report_is_loaded_from_a_file(filename string) {
	report, err := sarif.Open(filename)
	if err != nil {
		panic(err)
	}
	r.report = report

}