From f47f342ecb29764cfe4bc6f761c9bf63da6f9d26 Mon Sep 17 00:00:00 2001 From: Owen Rumney Date: Sat, 20 Feb 2021 16:35:43 +0000 Subject: [PATCH 1/3] Add options to load content - load from string or file - add tests to support the new functionality --- sarif/sarif.go | 27 +++++++++++ test/sarif_stage_test.go | 33 +++++++++++++- test/sarif_test.go | 97 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+), 1 deletion(-) diff --git a/sarif/sarif.go b/sarif/sarif.go index 20ddc9c..9087a64 100644 --- a/sarif/sarif.go +++ b/sarif/sarif.go @@ -4,6 +4,8 @@ import ( "encoding/json" "fmt" "io" + "io/ioutil" + "os" "github.com/owenrumney/go-sarif/models" ) @@ -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) diff --git a/test/sarif_stage_test.go b/test/sarif_stage_test.go index ea874b1..700c0f0 100644 --- a/test/sarif_stage_test.go +++ b/test/sarif_stage_test.go @@ -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 { @@ -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 + +} \ No newline at end of file diff --git a/test/sarif_test.go b/test/sarif_test.go index f25ae22..30c4282 100644 --- a/test/sarif_test.go +++ b/test/sarif_test.go @@ -2,6 +2,8 @@ package test import ( "github.com/stretchr/testify/assert" + "io/ioutil" + "os" "testing" ) @@ -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) @@ -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") +} \ No newline at end of file From a507ea5a1a31c28f6a3ec0c2c25ce7c473475c04 Mon Sep 17 00:00:00 2001 From: Owen Rumney Date: Sat, 20 Feb 2021 16:44:29 +0000 Subject: [PATCH 2/3] update versions --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 5e73c8d..c9d2b17 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/owenrumney/go-sarif go 1.15 -require github.com/stretchr/testify v1.6.1 +require github.com/stretchr/testify v1.7.0 diff --git a/go.sum b/go.sum index 1f1e7af..26500d5 100644 --- a/go.sum +++ b/go.sum @@ -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= From 0b38d001a8644ad66ad664c4dfb15f7194815f2f Mon Sep 17 00:00:00 2001 From: Owen Rumney Date: Sat, 20 Feb 2021 16:52:07 +0000 Subject: [PATCH 3/3] Add Makefile --- .travis.yml | 6 ++---- Makefile | 12 ++++++++++++ go.mod | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 Makefile diff --git a/.travis.yml b/.travis.yml index 0a83f10..0ac7c7d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..51c6f12 --- /dev/null +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/go.mod b/go.mod index c9d2b17..08bacfa 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/owenrumney/go-sarif -go 1.15 +go 1.16 require github.com/stretchr/testify v1.7.0