From e70d7c5611bd1be1f873cc234876aa77525a9178 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Wed, 15 Sep 2021 00:25:05 +0100 Subject: [PATCH] feat: add database initialisation --- db.go | 11 +++++++---- db_test.go | 20 +++++++++++++++----- magefile.go | 42 +++++++++++++++++++++++++++++------------- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/db.go b/db.go index 9aca982..1cb8698 100644 --- a/db.go +++ b/db.go @@ -22,7 +22,13 @@ func mkDataDir(dir string) error { return os.MkdirAll(dir, 0700) } -// TODO: Needs unit testing and documentation +// dbPath returns the path to the database file. If a path is given then that is returned. Otherwise the default path is returned. +// For linux, the default location of the database file is $XDG_DATA_HOME/pelican/pelican.db. If the XDG_DATA_HOME environment +// variable is not set then it will default to $HOME/.local/share/pelican/pelican.db. For all other operating systems the default +// location is $HOME/.pelican/pelican.db. +// +// Before returning the path, dbPath attempts to create the directory of the database file. An error is returned if there +// are any issues creating this directory. func dbPath(path string) (string, error) { if len(path) > 0 { filepath.Dir(path) @@ -58,9 +64,6 @@ func dbPath(path string) (string, error) { } // openDatabase opens the database, at a given path, for reading and writing. If the file does not exist it will be created. -// For linux, the default location of the database file is $XDG_DATA_HOME/pelican/pelican.db. If the XDG_DATA_HOME environment -// variable is not set then it will default to $HOME/.local/share/pelican/pelican.db. -// For all other operating systems the default location is $HOME/.pelican/pelican.db. func openDatabase(path string) (*bolt.DB, error) { opts := bolt.Options{ Timeout: 1 * time.Second, diff --git a/db_test.go b/db_test.go index db12cf1..c00486b 100644 --- a/db_test.go +++ b/db_test.go @@ -34,10 +34,7 @@ func TestDbCustomPath(t *testing.T) { } } -// TODO: Finish testing func TestDbDefaultPath(t *testing.T) { - var wantPath string - cwd, err := os.Getwd() if err != nil { t.Fatalf("An error occurred whilst getting the current directory, %s", err) @@ -50,12 +47,25 @@ func TestDbDefaultPath(t *testing.T) { t.Fatalf("An error occurred whilst setting the XDG_DATA_HOME environment variable, %s", err) } + var want string + goos := runtime.GOOS switch goos { case "linux": - wantPath = filepath.Join(os.Getenv("XDG_DATA_HOME"), "pelican", "pelican.db") + want = filepath.Join(os.Getenv("XDG_DATA_HOME"), "pelican", "pelican.db") default: - wantPath = filepath.Join(os.Getenv("HOME"), ".pelican", "pelican.db") + want = filepath.Join(os.Getenv("HOME"), ".pelican", "pelican.db") + } + + got, err := dbPath("") + if err != nil { + t.Fatalf("An error occurred whilst executing `dbPath`, %s", err) + } + + if got != want { + t.Errorf("Got unexpected database path: want %s, got %s", want, got) + } else { + t.Logf("Got expected database path: got %s", got) } } diff --git a/magefile.go b/magefile.go index 9570cf9..e5debc7 100644 --- a/magefile.go +++ b/magefile.go @@ -9,13 +9,39 @@ import ( "github.com/magefile/mage/sh" ) +const ( + binary = "pelican" +) + var Default = Build -var binary = "pelican" +// Test run the go tests +// To enable verbose mode set PELICAN_TEST_VERBOSE=1. +// To enable coverage mode set PELICAN_TEST_COVER=1. +func Test() error { + goTest := sh.RunCmd("go", "test") + + args := []string{"."} + + if os.Getenv("PELICAN_TEST_VERBOSE") == "1" { + args = append(args, "-v") + } + + if os.Getenv("PELICAN_TEST_COVER") == "1" { + args = append(args, "-cover") + } + + return goTest(args...) +} + +// Build build the executable +func Build() error { + return sh.Run("go", "build", "-o", binary, "main.go") +} // Clean clean the workspace func Clean() error { - if err := os.Remove(binary); err != nil { + if err := sh.Rm(binary); err != nil { return err } @@ -33,19 +59,9 @@ func Clean() error { for _, f := range files { filename := f.Name() if strings.HasSuffix(filename, ".db") { - os.Remove(testDBDir + "/" + filename) + sh.Rm(testDBDir + "/" + filename) } } return nil } - -// Test run the go tests -func Test() error { - return sh.Run("go", "test", ".") -} - -// Build build the executable -func Build() error { - return sh.Run("go", "build", "-o", binary, "main.go") -}