| 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://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json","runs":[{"tool":{"driver":{"informationUri":"https://tfsec.dev","name":"tfsec","rules":[]}},"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://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json","runs":[{"tool":{"driver":{"informationUri":"https://tfsec.dev","name":"tfsec","rules":[]}},"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://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json","runs":[{"tool":{"driver":{"informationUri":"https://tfsec.dev","name":"tfsec","rules":[]}},"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://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json","runs":[{"tool":{"driver":{"informationUri":"https://tfsec.dev","name":"tfsec","rules":[]}},"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://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json","runs":[{"tool":{"driver":{"informationUri":"https://tfsec.dev","name":"tfsec","rules":[]}},"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://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/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_from_string_with_extensions(t *testing.T) { |
| 78 |
given, _, then := newReportTest(t) |
| 79 |
|
| 80 |
content := `{ |
| 81 |
"version": "2.1.0", |
| 82 |
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", |
| 83 |
"runs": [ |
| 84 |
{ |
| 85 |
"tool": { |
| 86 |
"driver": { |
| 87 |
"name": "ESLint", |
| 88 |
"informationUri": "https://eslint.org" |
| 89 |
}, |
| 90 |
"extensions": [ |
| 91 |
{ |
| 92 |
"name": "Extension1", |
| 93 |
"semanticVersion": "1.0.0-rc" |
| 94 |
} |
| 95 |
] |
| 96 |
} |
| 97 |
} |
| 98 |
] |
| 99 |
}` |
| 100 |
|
| 101 |
given.a_report_is_loaded_from_a_string(content) |
| 102 |
then.the_report_has_expected_extension_name_and_semantic_version("Extension1", "1.0.0-rc") |
| 103 |
} |
| 104 |
|
| 105 |
func Test_load_sarif_report_from_file(t *testing.T) { |
| 106 |
given, _, then := newReportTest(t) |
| 107 |
|
| 108 |
content := `{ |
| 109 |
"version": "2.1.0", |
| 110 |
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", |
| 111 |
"runs": [ |
| 112 |
{ |
| 113 |
"tool": { |
| 114 |
"driver": { |
| 115 |
"name": "ESLint", |
| 116 |
"informationUri": "https://eslint.org" |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
] |
| 121 |
}` |
| 122 |
|
| 123 |
file, err := os.CreateTemp(t.TempDir(), "sarifReport") |
| 124 |
assert.NoError(t, err) |
| 125 |
defer file.Close() |
| 126 |
|
| 127 |
os.WriteFile(file.Name(), []byte(content), 755) |
| 128 |
|
| 129 |
given.a_report_is_loaded_from_a_file(file.Name()) |
| 130 |
then.the_report_has_expected_driver_name_and_information_uri("ESLint", "https://eslint.org") |
| 131 |
} |
| 132 |
|
| 133 |
func Test_err_on_load_sarif_report_from_file_when_not_exists(t *testing.T) { |
| 134 |
given, _, _ := newReportTest(t) |
| 135 |
defer func() { |
| 136 |
if err := recover().(error); err != nil { |
| 137 |
assert.Equal(t, "the provided file path doesn't have a file", err.Error()) |
| 138 |
} |
| 139 |
}() |
| 140 |
given.a_report_is_loaded_from_a_file("") |
| 141 |
} |
| 142 |
|
| 143 |
func Test_err_on_load_sarif_report_from_file_when_file_not_legit(t *testing.T) { |
| 144 |
given, _, _ := newReportTest(t) |
| 145 |
defer func() { |
| 146 |
if err := recover().(error); err != nil { |
| 147 |
assert.Equal(t, "the provided filepath could not be opened. read /tmp: is a directory", err.Error()) |
| 148 |
} |
| 149 |
}() |
| 150 |
given.a_report_is_loaded_from_a_file("/tmp") |
| 151 |
} |