diff --git a/cmd/gendb/main.go b/cmd/gendb/main.go index 9680540f..2787b7d0 100644 --- a/cmd/gendb/main.go +++ b/cmd/gendb/main.go @@ -12,6 +12,7 @@ import ( "log" "golang.org/x/vulndb/internal/database" + "golang.org/x/vulndb/internal/gitrepo" ) var ( @@ -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) } } diff --git a/internal/database/database.go b/internal/database/database.go index 8565a5ae..0b90a2b9 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -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. diff --git a/internal/database/generate.go b/internal/database/generate.go index 70786d60..eb788bb7 100644 --- a/internal/database/generate.go +++ b/internal/database/generate.go @@ -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 } diff --git a/internal/database/generate_test.go b/internal/database/generate_test.go index ed62cab3..3d92bad1 100644 --- a/internal/database/generate_test.go +++ b/internal/database/generate_test.go @@ -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") } @@ -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)