Skip to content

Commit

Permalink
✨ Add JSON unit tests (#915)
Browse files Browse the repository at this point in the history
* fix

* typo

* draft

* fixes

* typo

* add validator

* comments

* typo
  • Loading branch information
laurentsimon committed Aug 26, 2021
1 parent e083f04 commit 788fd33
Show file tree
Hide file tree
Showing 13 changed files with 646 additions and 12 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/shurcooL/githubv4 v0.0.0-20201206200315-234843c633fa
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
github.com/spf13/cobra v1.2.1
github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f
go.opencensus.io v0.23.0
go.uber.org/zap v1.19.0
gocloud.dev v0.23.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1502,8 +1502,11 @@ github.com/xanzy/go-gitlab v0.31.0/go.mod h1:sPLojNBn68fMUWSxIJtdVVIP8uSBYqesTfD
github.com/xanzy/go-gitlab v0.32.0/go.mod h1:sPLojNBn68fMUWSxIJtdVVIP8uSBYqesTfDUseX11Ug=
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f h1:mvXjJIHRZyhNuGassLTcXTwjiWq7NmjdavZsUnmFybQ=
github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
Expand Down
4 changes: 2 additions & 2 deletions pkg/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func textToMarkdown(s string) string {
}

func detailToString(d *checker.CheckDetail, logLevel zapcore.Level) string {
// UPGRADEv3: remove swtch statement.
switch d.Msg.Version {
//nolint
case 3:
Expand All @@ -56,8 +57,7 @@ func detailToString(d *checker.CheckDetail, logLevel zapcore.Level) string {
}

func detailsToString(details []checker.CheckDetail, logLevel zapcore.Level) (string, bool) {
// UPGRADEv2: change to make([]string, len(details))
// followed by sa[i] = instead of append.
// UPGRADEv2: change to make([]string, len(details)).
var sa []string
for i := range details {
v := details[i]
Expand Down
1 change: 1 addition & 0 deletions pkg/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@ func (r *ScorecardResult) AsJSON2(showDetails bool, logLevel zapcore.Level, writ
//nolint:wrapcheck
return sce.Create(sce.ErrScorecardInternal, fmt.Sprintf("encoder.Encode: %v", err))
}

return nil
}
60 changes: 60 additions & 0 deletions pkg/json.v2.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"$schema": "http://json-schema.org/schema#",
"$id": "https://github.com/ossf/scorecard/pkg/schema.v2.json",
"title": "Scorecard",
"description": "A tool to assess the security posture of open-source projects",
"type": "object",
"properties": {
"Checks": {
"type": "array",
"items": {
"type": "object",
"properties": {
"Details": {
"type": "array",
"items": {
"type": "string"
}
},
"Name": {
"type": "string"
},
"Reason": {
"type": "string"
},
"Score": {
"type": "integer"
}
},
"required": [
"Details",
"Score",
"Reason",
"Name"
]
}
},
"Commit": {
"type": "string"
},
"Date": {
"type": "string"
},
"Metadata": {
"type": "array",
"items": {
"type": "string"
}
},
"Repo": {
"type": "string"
}
},
"required": [
"Repo",
"Date",
"Commit",
"Checks",
"Metadata"
]
}
Loading

0 comments on commit 788fd33

Please sign in to comment.