Debian Codegraph @

test/report_test.go

raw symbols SCIP
1 package test
2
3 import (
4 "os"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 func Test_new_simple_report_with_single_run(t *testing.T) {
11 given, _, then := newReportTest(t)
12
13 given.a_new_report().
14 with_a_run_added("tfsec", "https://tfsec.dev")
15 then.report_text_is(`{"version":"2.1.0","$schema":"https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json","runs":[{"tool":{"driver":{"name":"tfsec","informationUri":"https://tfsec.dev"}},"results":[]}]}`)
16 }
17
18 func Test_new_report_with_empty_run(t *testing.T) {
19 given, _, then := newReportTest(t)
20
21 given.a_new_report().
22 with_a_run_with_empty_result_added("tfsec", "https://tfsec.dev")
23 then.report_text_is(`{"version":"2.1.0","$schema":"https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json","runs":[{"tool":{"driver":{"name":"tfsec","informationUri":"https://tfsec.dev"}},"results":[]}]}`)
24 }
25
26 func Test_new_simple_report_with_artifact(t *testing.T) {
27 given, when, then := newReportTest(t)
28
29 run := given.a_new_report().
30 with_a_run_added("tfsec", "https://tfsec.dev")
31 when.an_artifact_is_added_to_the_run(run, "file://broken.go")
32 then.report_text_is(`{"version":"2.1.0","$schema":"https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json","runs":[{"tool":{"driver":{"name":"tfsec","informationUri":"https://tfsec.dev"}},"artifacts":[{"location":{"uri":"file://broken.go"},"length":-1}],"results":[]}]}`)
33 }
34
35 func Test_new_simple_report_with_propertybag(t *testing.T) {
36 given, when, then := newReportTest(t)
37
38 run := given.a_new_report().
39 with_a_run_added("tfsec", "https://tfsec.dev")
40 when.some_properties_are_added_to_the_run(run)
41 then.report_text_is(`{"version":"2.1.0","$schema":"https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json","runs":[{"tool":{"driver":{"name":"tfsec","informationUri":"https://tfsec.dev"}},"results":[],"properties":{"integer_property":10,"string_property":"this is a string"}}]}`)
42 }
43
44 func Test_new_simple_report_with_duplicate_artifact(t *testing.T) {
45 given, when, then := newReportTest(t)
46
47 run := given.a_new_report().
48 with_a_run_added("tfsec", "https://tfsec.dev")
49 when.an_artifact_is_added_to_the_run(run, "file://broken.go").
50 and().
51 an_artifact_is_added_to_the_run(run, "file://broken.go")
52 then.report_text_is(`{"version":"2.1.0","$schema":"https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json","runs":[{"tool":{"driver":{"name":"tfsec","informationUri":"https://tfsec.dev"}},"artifacts":[{"location":{"uri":"file://broken.go"},"length":-1},{"location":{"uri":"file://broken.go"},"length":-1}],"results":[]}]}`)
53 }
54
55 func Test_load_sarif_from_string(t *testing.T) {
56 given, _, then := newReportTest(t)
57
58 content := `{
59 "version": "2.1.0",
60 "$schema": "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json",
61 "runs": [
62 {
63 "tool": {
64 "driver": {
65 "name": "ESLint",
66 "informationUri": "https://eslint.org"
67 }
68 }
69 }
70 ]
71 }`
72
73 given.a_report_is_loaded_from_a_string(content)
74 then.the_report_has_expected_driver_name_and_information_uri("ESLint", "https://eslint.org")
75 }
76
77 func Test_load_sarif_report_from_file(t *testing.T) {
78 given, _, then := newReportTest(t)
79
80 content := `{
81 "version": "2.1.0",
82 "$schema": "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json",
83 "runs": [
84 {
85 "tool": {
86 "driver": {
87 "name": "ESLint",
88 "informationUri": "https://eslint.org"
89 }
90 }
91 }
92 ]
93 }`
94
95 file, err := os.CreateTemp(t.TempDir(), "sarifReport")
96 assert.NoError(t, err)
97 defer file.Close()
98
99 os.WriteFile(file.Name(), []byte(content), 755)
100
101 given.a_report_is_loaded_from_a_file(file.Name())
102 then.the_report_has_expected_driver_name_and_information_uri("ESLint", "https://eslint.org")
103 }
104
105 func Test_err_on_load_sarif_report_from_file_when_not_exists(t *testing.T) {
106 given, _, _ := newReportTest(t)
107 defer func() {
108 if err := recover().(error); err != nil {
109 assert.Equal(t, "the provided file path doesn't have a file", err.Error())
110 }
111 }()
112 given.a_report_is_loaded_from_a_file("")
113 }
114
115 func Test_err_on_load_sarif_report_from_file_when_file_not_legit(t *testing.T) {
116 given, _, _ := newReportTest(t)
117 defer func() {
118 if err := recover().(error); err != nil {
119 assert.Equal(t, "the provided filepath could not be opened. read /tmp: is a directory", err.Error())
120 }
121 }()
122 given.a_report_is_loaded_from_a_file("/tmp")
123 }