diff options
author | Matthew Lemon <matt@matthewlemon.com> | 2020-07-25 18:24:18 +0100 |
---|---|---|
committer | Matthew Lemon <matt@matthewlemon.com> | 2020-07-25 18:24:18 +0100 |
commit | 278eef90bffce6066a98a996a1462acd96b21d4e (patch) | |
tree | b9c791e9b6a86682d29581ee5635463598b648b3 | |
parent | 93e049282e9a490dcb179209209f0300fbfbebb6 (diff) |
test to detect config path passes
Diffstat (limited to '')
-rw-r--r-- | cmd/datamaps/main.go | 8 | ||||
-rw-r--r-- | pkg/datamaps/config.go | 23 | ||||
-rw-r--r-- | pkg/datamaps/config_test.go | 17 |
3 files changed, 14 insertions, 34 deletions
diff --git a/cmd/datamaps/main.go b/cmd/datamaps/main.go index cc634a6..b421223 100644 --- a/cmd/datamaps/main.go +++ b/cmd/datamaps/main.go @@ -12,10 +12,10 @@ import ( ) func main() { - env := datamaps.DetectConfig() - if !env { - datamaps.SetUp() - } + // env := datamaps.DetectConfig() + // if !env { + // datamaps.SetUp() + // } opts := datamaps.ParseOptions() switch opts.Command { diff --git a/pkg/datamaps/config.go b/pkg/datamaps/config.go index 6c82a9f..8dc8896 100644 --- a/pkg/datamaps/config.go +++ b/pkg/datamaps/config.go @@ -8,7 +8,7 @@ import ( ) const ( - configDirName = "datamaps-go" + configDirName = "datamaps" dbName = "datamaps.db" ) @@ -37,26 +37,7 @@ func (db *DBPathChecker) check() bool { 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 DetectDBFile() bool { - dir, err := os.UserConfigDir() - if err != nil { - log.Fatal(err) - } - // check if config folder exists - configPath := filepath.Join(dir, configDirName) - dbPath := filepath.Join(configPath, dbName) + dbPath := filepath.Join(userConfig, "datamaps.db") if _, err := os.Stat(dbPath); os.IsNotExist(err) { fmt.Fprintf(os.Stderr, "db does not exist\n") return false diff --git a/pkg/datamaps/config_test.go b/pkg/datamaps/config_test.go index 5f7becc..ad19879 100644 --- a/pkg/datamaps/config_test.go +++ b/pkg/datamaps/config_test.go @@ -1,7 +1,6 @@ package datamaps import ( - "log" "os" "path/filepath" "testing" @@ -10,25 +9,25 @@ import ( // mocking funcs in go https://stackoverflow.com/questions/19167970/mock-functions-in-go func mockConfigDir() (string, error) { - return "/tmp/CONFIG", nil + return "/tmp/CONFIG/datamaps/", nil } func TestDBDetect(t *testing.T) { - if err := os.Mkdir(filepath.Join("/tmp", "CONFIG"), 0700); err != nil { - t.Fatal("cannot create temporary directory") + cPath := filepath.Join("/tmp", "CONFIG", "datamaps") + t.Logf("%s\n", cPath) + if err := os.MkdirAll(cPath, 0700); err != nil { + t.Fatalf("cannot create temporary directory - %v", err) } - os.Create(filepath.Join("/tmp", "CONFIG", "datamaps.db")) + os.Create(filepath.Join("/tmp", "CONFIG", "datamaps", "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") + if !h { + t.Error("the db file should be found but isn't") } } |