aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorMatthew Lemon <lemon@matthewlemon.com>2020-07-24 17:01:59 +0100
committerMatthew Lemon <lemon@matthewlemon.com>2020-07-24 17:01:59 +0100
commit93e049282e9a490dcb179209209f0300fbfbebb6 (patch)
treefcb0046ccd9a1836d4a171dd4b1628736f24f524 /pkg
parent5a30da3fbdab17ecbcacb09b38d4cd9faf9e3044 (diff)
failing test but learning a hell of a lot from this - mocking!
Diffstat (limited to 'pkg')
-rw-r--r--pkg/datamaps/config.go48
-rw-r--r--pkg/datamaps/config_test.go33
2 files changed, 79 insertions, 2 deletions
diff --git a/pkg/datamaps/config.go b/pkg/datamaps/config.go
index 687166c..6c82a9f 100644
--- a/pkg/datamaps/config.go
+++ b/pkg/datamaps/config.go
@@ -1,6 +1,7 @@
package datamaps
import (
+ "fmt"
"log"
"os"
"path/filepath"
@@ -11,13 +12,56 @@ const (
dbName = "datamaps.db"
)
+// mocking funcs in go https://stackoverflow.com/questions/19167970/mock-functions-in-go
+// we only need the func signature to create the type. This is pretty weird, we're want to mock
+// os.UserHomeDir so that we can set it to something like /tmp in our tests. Here we are creating
+// two types: GetUserConfigDir to represent the os.UserConfigDir function, DBPathChecker as a wrapper
+// which which we can assign methods to that holds the value of the func os.UserConfigDir and the
+// method, check(), which does the work, using the passed in func to determine the user $HOME/.config
+// directory.
+// Which is a lot of work for what it is, but it does make this testable and serves as an example
+// of how things could be done in Go.
+
+type GetUserConfigDir func() (string, error)
+
+type DBPathChecker struct {
+ getUserConfigDir GetUserConfigDir
+}
+
+func NewDBPathChecker(h GetUserConfigDir) *DBPathChecker {
+ return &DBPathChecker{getUserConfigDir: h}
+}
+
+func (db *DBPathChecker) check() bool {
+ userConfig, err := db.getUserConfigDir()
+ if err != nil {
+ log.Fatal(err)
+ }
+ configPath := filepath.Join(userConfig, "datamaps")
+ dbPath := filepath.Join(configPath, "datamaps.db")
+ fmt.Fprintf(os.Stderr, "dbPath is definitely %s\n", dbPath)
+ if _, err := os.Stat(dbPath); os.IsNotExist(err) {
+ fmt.Fprintf(os.Stderr, "db does not exist\n")
+ return false
+ }
+ return true
+}
+
// DetectConfig looks for the configuration directory and
// files, and the database file needed to run the application.
-func DetectConfig() bool {
+func DetectDBFile() bool {
dir, err := os.UserConfigDir()
if err != nil {
- return "", err
+ log.Fatal(err)
+ }
+ // check if config folder exists
+ configPath := filepath.Join(dir, configDirName)
+ dbPath := filepath.Join(configPath, dbName)
+ if _, err := os.Stat(dbPath); os.IsNotExist(err) {
+ fmt.Fprintf(os.Stderr, "db does not exist\n")
+ return false
}
+ return true
}
// SetUp creates the config directory and requisite files
diff --git a/pkg/datamaps/config_test.go b/pkg/datamaps/config_test.go
index ffeaf5e..5f7becc 100644
--- a/pkg/datamaps/config_test.go
+++ b/pkg/datamaps/config_test.go
@@ -1 +1,34 @@
package datamaps
+
+import (
+ "log"
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+// mocking funcs in go https://stackoverflow.com/questions/19167970/mock-functions-in-go
+
+func mockConfigDir() (string, error) {
+ return "/tmp/CONFIG", nil
+}
+
+func TestDBDetect(t *testing.T) {
+
+ if err := os.Mkdir(filepath.Join("/tmp", "CONFIG"), 0700); err != nil {
+ t.Fatal("cannot create temporary directory")
+ }
+
+ os.Create(filepath.Join("/tmp", "CONFIG", "datamaps.db"))
+ defer func() {
+ os.RemoveAll(filepath.Join("/tmp", "CONFIG"))
+ }()
+
+ dbpc := NewDBPathChecker(mockConfigDir)
+ h := dbpc.check()
+ log.SetOutput(os.Stderr)
+ t.Logf("h is %v\n", h)
+ if h != true {
+ t.Error("Not there")
+ }
+}