Skip to content

Commit

Permalink
Merge pull request #6 from owenrumney/add-loading-options
Browse files Browse the repository at this point in the history
Add options to load content
  • Loading branch information
owenrumney authored Feb 20, 2021
2 parents 8fe4109 + 0b38d00 commit df6057b
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 9 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
language: go
go:
- master
- 1.16
env:
- GO111MODULE=on
script:
- go vet ./...
- go test -v -covermode=atomic -coverpkg ./... -coverprofile coverage.txt ./...

- make test
after_success:
- bash <(curl -s https://codecov.io/bash)
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

.PHONY: test
test: vet
go test -v -covermode=atomic -coverpkg ./... -coverprofile coverage.txt ./...

.PHONY: vet
vet:
go vet ./...

.PHONY: goimports
goimports:
goimports
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/owenrumney/go-sarif

go 1.15
go 1.16

require github.com/stretchr/testify v1.6.1
require github.com/stretchr/testify v1.7.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
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 df6057b

Please sign in to comment.