Skip to content

Commit

Permalink
internal/database, cmd/gendb: refactor Generate for unit testing
Browse files Browse the repository at this point in the history
Modify Generate to take in a *git.Repository instead of a directory
string, so it can be more easily unit tested. Add a unit test.

For golang/go#56417

Change-Id: I3eaa84b41568e9582ac1f16be8c979d7b71d5ad3
Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/457017
Run-TryBot: Tatiana Bradley <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Than McIntosh <[email protected]>
  • Loading branch information
tatianab authored and Tatiana Bradley committed Dec 13, 2022
1 parent f9476be commit ffba2fd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
7 changes: 6 additions & 1 deletion cmd/gendb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"log"

"golang.org/x/vulndb/internal/database"
"golang.org/x/vulndb/internal/gitrepo"
)

var (
Expand All @@ -23,7 +24,11 @@ var (
func main() {
flag.Parse()
ctx := context.Background()
if err := database.Generate(ctx, *repoDir, *jsonDir, *indent); err != nil {
repo, err := gitrepo.CloneOrOpen(ctx, *repoDir)
if err != nil {
log.Fatal(err)
}
if err := database.Generate(ctx, repo, *jsonDir, *indent); err != nil {
log.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const (
// associated index maps). The result is an in-memory vulnerability database
// that can be written to files via Database.Write.
//
// The repo must contain a "data/osv" folder to with files in
// The repo must contain a "data/osv" folder with files in
// OSV JSON format with filenames of the form GO-YYYY-XXXX.json.
//
// New does not modify the repo.
Expand Down
17 changes: 6 additions & 11 deletions internal/database/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,23 @@ package database
import (
"context"

"github.com/go-git/go-git/v5"
"golang.org/x/vulndb/internal/derrors"
"golang.org/x/vulndb/internal/gitrepo"
)

// Generate creates and writes a new Go vulnerability database to outDir
// based on the contents of the "data/osv" folder inside repoDir, a local
// git repo.
// based on the contents of the "data/osv" folder inside repo.
//
// repoDir must contain a "data/osv" folder to with files in
// The repo must contain a "data/osv" folder with files in
// OSV JSON format with filenames of the form GO-YYYY-XXXX.json.
func Generate(ctx context.Context, repoDir, outDir string, indent bool) (err error) {
defer derrors.Wrap(&err, "Generate(%q)", repoDir)
func Generate(ctx context.Context, repo *git.Repository, outDir string, indent bool) (err error) {
defer derrors.Wrap(&err, "Generate()")

repo, err := gitrepo.Open(ctx, repoDir)
if err != nil {
return err
}
new, err := New(ctx, repo)
if err != nil {
return err
}
if err = new.Write(outDir, false); err != nil {
if err = new.Write(outDir, indent); err != nil {
return err
}

Expand Down
23 changes: 17 additions & 6 deletions internal/database/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@ import (
)

func TestGenerate(t *testing.T) {
// TODO(https://github.com/golang/go#56417): Write unit tests for Generate.
ctx := context.Background()
testRepo, err := gitrepo.ReadTxtarRepo(testRepoDir, jan2002)
if err != nil {
t.Fatal(err)
}
tmpDir := t.TempDir()
err = Generate(ctx, testRepo, tmpDir, true)
if err != nil {
t.Fatal(err)
}
if err = cmpDirHashes(tmpDir, validDir); err != nil {
t.Error(err)
}
}

func TestGenerateIntegration(t *testing.T) {
// Generate (in its current state) can only be tested with respect to the
// real contents of vulndb.
if !*integration {
t.Skip("Skipping integration tests, use flag -integration to run")
}
Expand All @@ -28,16 +38,17 @@ func TestGenerateIntegration(t *testing.T) {

ctx := context.Background()

genDir := t.TempDir()
err := Generate(ctx, ".", genDir, false)
repo, err := gitrepo.Open(ctx, ".")
if err != nil {
t.Fatal(err)
}

repo, err := gitrepo.Open(ctx, ".")
genDir := t.TempDir()
err = Generate(ctx, repo, genDir, false)
if err != nil {
t.Fatal(err)
}

new, err := New(ctx, repo)
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit ffba2fd

Please sign in to comment.