Skip to content

Commit

Permalink
Merge pull request #156 from askgitdev/go-mod-to-json-tests
Browse files Browse the repository at this point in the history
add some test cases for `go_mod_to_json` and some better edge case handling
  • Loading branch information
patrickdevivo authored Aug 12, 2021
2 parents b6313be + a941151 commit 070ac07
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 3 deletions.
20 changes: 17 additions & 3 deletions extensions/internal/golang/go_mod_to_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,34 @@ func goModVersionToVersion(v module.Version) Version {
func (f *GoModToJSON) Args() int { return 1 }
func (f *GoModToJSON) Deterministic() bool { return true }
func (f *GoModToJSON) Apply(context *sqlite.Context, value ...sqlite.Value) {
parsed, err := modfile.Parse("go.mod", value[0].Blob(), nil)
input := value[0].Blob()

if len(input) == 0 {
context.ResultNull()
return
}

parsed, err := modfile.Parse("go.mod", input, nil)
if err != nil {
context.ResultError(err)
return
}

file := &GoModFile{
Version: goModVersionToVersion(parsed.Module.Mod),
Go: parsed.Go.Version,
Require: make([]*Require, len(parsed.Require)),
Exclude: make([]*Exclude, len(parsed.Exclude)),
Replace: make([]*Replace, len(parsed.Replace)),
Retract: make([]*Retract, len(parsed.Retract)),
}

if parsed.Module != nil {
file.Version = goModVersionToVersion(parsed.Module.Mod)
}

if parsed.Go != nil {
file.Go = parsed.Go.Version
}

for i, r := range parsed.Require {
file.Require[i] = &Require{
Mod: goModVersionToVersion(r.Mod),
Expand Down
86 changes: 86 additions & 0 deletions extensions/internal/golang/go_mod_to_json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package golang

import (
"encoding/json"
"io/ioutil"
"testing"

"github.com/askgitdev/askgit/extensions/internal/tools"
)

func TestGoModToJSONOK(t *testing.T) {
goMod, err := ioutil.ReadFile("testdata/GoModOK")
if err != nil {
t.Fatal(err)
}

rows, err := FixtureDatabase.Query("SELECT go_mod_to_json(?)", goMod)
if err != nil {
t.Fatal(err)
}

rowNum, contents, err := tools.RowContent(rows)
if err != nil {
t.Fatalf("err %d at row Number %d", err, rowNum)
}

var parsed map[string]interface{}
err = json.Unmarshal([]byte(contents[0][0]), &parsed)
if err != nil {
t.Fatal(err)
}

goVersion := parsed["go"].(string)
if goVersion != "1.13" {
t.Fatalf("expected go version 1.13, got %s", goVersion)
}

req := parsed["require"].([]interface{})
if len(req) != 33 {
t.Fatalf("expected 33 required modules, got %d", len(req))
}
}

func TestGoModToJSONEmpty(t *testing.T) {
rows, err := FixtureDatabase.Query("SELECT go_mod_to_json('')")
if err != nil {
t.Fatal(err)
}

rowNum, contents, err := tools.RowContent(rows)
if err != nil {
t.Fatalf("err %d at row Number %d", err, rowNum)
}

if contents[0][0] != "NULL" {
t.Fatalf("expected NULL, got %s", contents[0][0])
}
}

func TestGoModToJSONMissingVals(t *testing.T) {
goMod, err := ioutil.ReadFile("testdata/GoModMissingVals")
if err != nil {
t.Fatal(err)
}

rows, err := FixtureDatabase.Query("SELECT go_mod_to_json(?)", goMod)
if err != nil {
t.Fatal(err)
}

rowNum, contents, err := tools.RowContent(rows)
if err != nil {
t.Fatalf("err %d at row Number %d", err, rowNum)
}

var parsed map[string]interface{}
err = json.Unmarshal([]byte(contents[0][0]), &parsed)
if err != nil {
t.Fatal(err)
}

goVersion := parsed["go"].(string)
if goVersion != "" {
t.Fatalf("expected no go version, got %s", goVersion)
}
}
30 changes: 30 additions & 0 deletions extensions/internal/golang/go_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package golang

import (
"database/sql"
"log"
"os"
"testing"

_ "github.com/askgitdev/askgit/pkg/sqlite"
"go.riyazali.net/sqlite"
)

// FixtureDatabase represents the database connection to run the test against
var FixtureDatabase *sql.DB

func init() {
// register sqlite extension when this package is loaded
sqlite.Register(func(ext *sqlite.ExtensionApi) (_ sqlite.ErrorCode, err error) {
return Register(ext, nil)
})
}

func TestMain(m *testing.M) {
var err error
if FixtureDatabase, err = sql.Open("sqlite3", "file:testing.db?mode=memory"); err != nil {
log.Fatalf("failed to open database connection: %v", err)
}

os.Exit(m.Run())
}
10 changes: 10 additions & 0 deletions extensions/internal/golang/testdata/GoModMissingVals
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/askgitdev/askgit

require (
github.com/BurntSushi/toml v0.3.1
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210707164159-52430bf6b52c // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/augmentable-dev/vtab v0.0.0-20210717200339-0c8dcfe2033b
)
39 changes: 39 additions & 0 deletions extensions/internal/golang/testdata/GoModOK
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module github.com/askgitdev/askgit

go 1.13

require (
github.com/BurntSushi/toml v0.3.1
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210707164159-52430bf6b52c // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/augmentable-dev/vtab v0.0.0-20210717200339-0c8dcfe2033b
github.com/clbanning/mxj/v2 v2.5.5
github.com/dnaeon/go-vcr/v2 v2.0.1
github.com/ghodss/yaml v1.0.0
github.com/go-enry/go-enry/v2 v2.7.1
github.com/go-git/go-billy/v5 v5.3.1
github.com/go-git/go-git/v5 v5.4.2
github.com/go-openapi/errors v0.20.0 // indirect
github.com/go-openapi/strfmt v0.20.1 // indirect
github.com/jedib0t/go-pretty v4.3.0+incompatible
github.com/kevinburke/ssh_config v1.1.0 // indirect
github.com/libgit2/git2go/v31 v31.4.14
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mattn/go-sqlite3 v1.14.8
github.com/pkg/errors v0.9.1
github.com/sergi/go-diff v1.2.0 // indirect
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
go.mongodb.org/mongo-driver v1.6.0 // indirect
go.riyazali.net/sqlite v0.0.0-20210707161919-414349b4032a
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/mod v0.4.2
golang.org/x/net v0.0.0-20210716203947-853a461950ff // indirect
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac
)

0 comments on commit 070ac07

Please sign in to comment.