Skip to content

Commit

Permalink
Add options to load content
Browse files Browse the repository at this point in the history
- load from string or file
- add tests to support the new functionality
  • Loading branch information
owenrumney committed Feb 20, 2021
1 parent 70869ca commit f47f342
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 1 deletion.
27 changes: 27 additions & 0 deletions sarif/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/owenrumney/go-sarif/models"
)
Expand Down Expand Up @@ -38,6 +40,31 @@ func New(version Version) (*Report, error) {
}, nil
}

func Open(filename string) (*Report, error) {
if _, err := os.Stat(filename); err != nil && os.IsNotExist(err) {
return nil, fmt.Errorf("the provided file path doesn't have a file")
}

content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("the provided filepath could not be opened. %w", err)
}
return readBytes(content)
}


func FromString(content string) (*Report, error) {
return readBytes([]byte(content))
}

func readBytes(content []byte) (*Report, error) {
var report Report
if err := json.Unmarshal(content, &report); err != nil{
return nil, err
}
return &report, nil
}

// AddRun allows adding run information to the current report
func (sarif *Report) AddRun(toolName, informationURI string) *models.Run {
run := models.NewRun(toolName, informationURI)
Expand Down
33 changes: 32 additions & 1 deletion test/sarif_stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,18 @@ func (st *sarifTest) theReportIsWrittenToString() {
st.content = buf.String()
}


func (st *sarifTest) theReportIsWrittenToStringInAPrettyFormat() {
buf := new(bytes.Buffer)
err := st.sarifReport.PrettyWrite(buf)
if err != nil {
st.t.Error(err)
}
st.content = buf.String()
}

func (st *sarifTest) contentShouldBe(expected string) {
assert.Equal(st.t, st.content, expected)
assert.Equal(st.t, expected, st.content)
}

func (st *sarifTest) and() *sarifTest {
Expand All @@ -50,3 +60,24 @@ func (st *sarifTest) aDriverIsAdded() *sarifTest {
st.sarifReport.AddRun("ESLint", "https://eslint.org")
return st
}

func (st *sarifTest) aReportIsLoadedFromString(content string) {
report, err := sarif.FromString(content)
assert.NoError(st.t, err)
assert.NotNil(st.t, report)
st.sarifReport = report
}

func (st *sarifTest) theReportHasDriverNameAndInformationUri(driverName string, informationUri string) {
assert.Equal(st.t, driverName, st.sarifReport.Runs[0].Tool.Driver.Name)
assert.Equal(st.t, informationUri, st.sarifReport.Runs[0].Tool.Driver.InformationURI)
}

func (st *sarifTest) aReportIsLoadedFromFile(filename string) {
report, err := sarif.Open(filename)
if err != nil {
panic(err)
}
st.sarifReport = report

}
97 changes: 97 additions & 0 deletions test/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package test

import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"testing"
)

Expand All @@ -26,6 +28,30 @@ func Test_create_new_a_new_sarif_file_with_a_driver(t *testing.T) {
then.contentShouldBe(expected)
}

func Test_create_new_a_new_sarif_file_with_a_driver_in_a_pretty_format(t *testing.T) {
given, when, then := createNewSarifTest(t)

expected := `{
"version": "2.1.0",
"$schema": "http://json.schemastore.org/sarif-2.1.0-rtm.4",
"runs": [
{
"tool": {
"driver": {
"name": "ESLint",
"informationUri": "https://eslint.org"
}
}
}
]
}`

given.aNewSarifReport("2.1.0")
when.aDriverIsAdded().
and().theReportIsWrittenToStringInAPrettyFormat()
then.contentShouldBe(expected)
}

func Test_error_when_unsupported_version_requested(t *testing.T) {
given, _, _ := createNewSarifTest(t)

Expand All @@ -36,3 +62,74 @@ func Test_error_when_unsupported_version_requested(t *testing.T) {
}()
given.aNewSarifReport("bad_version")
}

func Test_load_sarif_report_from_string(t *testing.T) {
given, _, then := createNewSarifTest(t)

content := `{
"version": "2.1.0",
"$schema": "http://json.schemastore.org/sarif-2.1.0-rtm.4",
"runs": [
{
"tool": {
"driver": {
"name": "ESLint",
"informationUri": "https://eslint.org"
}
}
}
]
}`

given.aReportIsLoadedFromString(content)
then.theReportHasDriverNameAndInformationUri("ESLint","https://eslint.org")
}

func Test_load_sarif_report_from_file(t *testing.T) {
given, _, then := createNewSarifTest(t)

content := `{
"version": "2.1.0",
"$schema": "http://json.schemastore.org/sarif-2.1.0-rtm.4",
"runs": [
{
"tool": {
"driver": {
"name": "ESLint",
"informationUri": "https://eslint.org"
}
}
}
]
}`

file, err := ioutil.TempFile(os.TempDir(), "sarifReport")
assert.NoError(t, err)
defer file.Close()

ioutil.WriteFile(file.Name(), []byte(content), 755)

given.aReportIsLoadedFromFile(file.Name())
then.theReportHasDriverNameAndInformationUri("ESLint","https://eslint.org")
}


func Test_err_on_load_sarif_report_from_file_when_not_exists(t *testing.T) {
given, _, _ := createNewSarifTest(t)
defer func() {
if err := recover().(error); err != nil {
assert.Equal(t, "the provided file path doesn't have a file", err.Error())
}
}()
given.aReportIsLoadedFromFile("")
}

func Test_err_on_load_sarif_report_from_file_when_file_not_legit(t *testing.T) {
given, _, _ := createNewSarifTest(t)
defer func() {
if err := recover().(error); err != nil {
assert.Equal(t, "the provided filepath could not be opened. read /tmp: is a directory", err.Error())
}
}()
given.aReportIsLoadedFromFile("/tmp")
}

0 comments on commit f47f342

Please sign in to comment.