Skip to content

Commit

Permalink
Improved test coverage and functions in common.go (thrasher-corp#266)
Browse files Browse the repository at this point in the history
Improved directory creation, including permissions and expanded test coverage
  • Loading branch information
MadCozBadd authored and thrasher- committed Apr 17, 2019
1 parent ca344ba commit 400c1cc
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 17 deletions.
34 changes: 21 additions & 13 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,25 +607,33 @@ func GetDefaultDataDir(env string) string {
if env == "windows" {
return os.Getenv("APPDATA") + GetOSPathSlash() + "GoCryptoTrader"
}
return path.Join(os.ExpandEnv("$HOME"), ".gocryptotrader")
dir, ok := os.LookupEnv("HOME")
if !ok {
return ""
}
return path.Join(dir, ".gocryptotrader")
}

// CheckDir checks to see if a particular directory exists
// and attempts to create it if desired, if it doesn't exist
func CheckDir(dir string, create bool) error {
// CreateDir creates a directory based on the supplied parameter
func CreateDir(dir string) error {
_, err := os.Stat(dir)
if !os.IsNotExist(err) {
return nil
}

if !create {
return fmt.Errorf("directory %s does not exist. Err: %s", dir, err)
}

log.Warnf("Directory %s does not exist.. creating.", dir)
err = os.MkdirAll(dir, 0777)
if err != nil {
return fmt.Errorf("failed to create dir. Err: %s", err)
}
return nil
return os.MkdirAll(dir, 0770)
}

// ChangePerm lists all the directories and files in an array
func ChangePerm(directory string) error {
return filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Mode().Perm() != 0770 {
return os.Chmod(path, 0770)
}
return nil
})
}
177 changes: 177 additions & 0 deletions common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package common
import (
"bytes"
"net/url"
"os"
"path"
"reflect"
"runtime"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -950,3 +953,177 @@ func TestTimeFromUnixTimestampFloat(t *testing.T) {
t.Error("Test failed. Common TimeFromUnixTimestampFloat. Converted invalid syntax.")
}
}

func TestGetDefaultDataDir(t *testing.T) {
switch runtime.GOOS {
case "windows":
dir, ok := os.LookupEnv("APPDATA")
if !ok {
t.Fatal("APPDATA is not set")
}
dir += GetOSPathSlash() + "GoCryptoTrader"
actualOutput := GetDefaultDataDir("windows")
if actualOutput != dir {
t.Fatalf("Unexpected result. Got: %v Expected: %v", actualOutput, dir)
}
case "linux", "darwin":
dir, ok := os.LookupEnv("HOME")
if !ok {
t.Fatal("HOME is not set")
}
dir += GetOSPathSlash() + ".gocryptotrader"
actualOutput := GetDefaultDataDir(runtime.GOOS)
if actualOutput != dir {
t.Fatalf("Unexpected result. Got: %v Expected: %v", actualOutput, dir)
}
}
}

func TestCreateDir(t *testing.T) {
switch runtime.GOOS {
case "windows":
// test for a directory that exists
dir, ok := os.LookupEnv("TEMP")
if !ok {
t.Fatal("LookupEnv failed. TEMP is not set")
}
err := CreateDir(dir)
if err != nil {
t.Fatalf("CreateDir failed. Err: %v", err)
}

// test for creating a directory
dir, ok = os.LookupEnv("APPDATA")
if !ok {
t.Fatal("LookupEnv failed. APPDATA is not set")
}
dir = dir + GetOSPathSlash() + "GoCryptoTrader\\TestFileASDFG"
err = CreateDir(dir)
if err != nil {
t.Fatalf("CreateDir failed. Err: %v", err)
}
err = os.Remove(dir)
if err != nil {
t.Fatalf("Failed to remove file. Err: %v", err)
}

// test for looking up an invalid directory
err = CreateDir("")
if err == nil {
t.Fatal("expected err due to invalid path, but got nil")
}
case "linux":
// same tests for linux
dir := "/home"
err := CreateDir(dir)
if err != nil {
t.Fatalf("CreateDir failed. Err: %v", err)
}
var ok bool
dir, ok = os.LookupEnv("HOME")
if !ok {
t.Fatal("LookupEnv of HOME failed")
}
dir = path.Join(dir, ".gocryptotrader", "TestFileASFG")
err = CreateDir(dir)
if err != nil {
t.Errorf("CreateDir failed. Err: %s", err)
}
err = os.Remove(dir)
if err != nil {
t.Fatalf("Failed to remove file. Err: %v", err)
}

// test for creating an invalid directory
err = CreateDir("")
if err == nil {
t.Fatal("expected err due to invalid path, but got nil")
}

case "darwin":
// same test except for the invalid directory
dir := "/home"
err := CreateDir(dir)
if err != nil {
t.Fatalf("CreateDir failed. Err: %v", err)
}
var ok bool
dir, ok = os.LookupEnv("HOME")
if !ok {
t.Fatal("LookupEnv of HOME failed")
}
dir = path.Join(dir, ".gocryptotrader", "TestFileASFG")
err = CreateDir(dir)
if err != nil {
t.Fatalf("CreateDir failed. Err: %s", err)
}
err = os.Remove(dir)
if err != nil {
t.Fatalf("Failed to remove file. Err: %v", err)
}

err = CreateDir(":")
if err == nil {
t.Fatal("expected err due to invalid path, but got nil")
}
}
}

func TestChangePerm(t *testing.T) {
switch runtime.GOOS {
case "linux", "darwin":
if runtime.GOOS == "linux" {
err := ChangePerm("")
if err == nil {
t.Fatal("expected an error on non-existent path")
}
} else {
err := ChangePerm(":")
if err == nil {
t.Fatal("expected an error on non-existent path")
}
}
err := os.Mkdir(GetDefaultDataDir(runtime.GOOS)+GetOSPathSlash()+"TestFileASDFGHJ", 0777)
if err != nil {
t.Fatalf("Mkdir failed. Err: %v", err)
}
err = ChangePerm(GetDefaultDataDir(runtime.GOOS))
if err != nil {
t.Fatal("ChangePerm was unsuccessful")
}
var a os.FileInfo
a, err = os.Stat(GetDefaultDataDir(runtime.GOOS) + GetOSPathSlash() + "TestFileASDFGHJ")
if err != nil {
t.Fatalf("os.Stat failed. Err: %v", err)
}
if a.Mode().Perm() != 0770 {
t.Fatalf("expected file permissions differ. expecting 0770 got %#o", a.Mode().Perm())
}
err = RemoveFile(GetDefaultDataDir(runtime.GOOS) + GetOSPathSlash() + "TestFileASDFGHJ")
if err != nil {
t.Fatalf("RemoveFile failed. Err: %v", err)
}

case "windows":
err := ChangePerm("*")
if err == nil {
t.Fatal("expected an error on non-existent path")
}
err = os.Mkdir(GetDefaultDataDir(runtime.GOOS)+GetOSPathSlash()+"TestFileASDFGHJ", 0777)
if err != nil {
t.Fatalf("Mkdir failed. Err: %v", err)
}
err = ChangePerm(GetDefaultDataDir(runtime.GOOS))
if err != nil {
t.Fatalf("ChangePerm was unsuccessful. Err: %v", err)
}
_, err = os.Stat(GetDefaultDataDir(runtime.GOOS) + GetOSPathSlash() + "TestFileASDFGHJ")
if err != nil {
t.Fatalf("os.Stat failed. Err: %v", err)
}
err = RemoveFile(GetDefaultDataDir(runtime.GOOS) + GetOSPathSlash() + "TestFileASDFGHJ")
if err != nil {
t.Fatalf("RemoveFile failed. Err: %v", err)
}
}
}
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ func (c *Config) CheckLoggerConfig() error {

if len(c.Logging.File) > 0 {
logPath := path.Join(common.GetDefaultDataDir(runtime.GOOS), "logs")
err := common.CheckDir(logPath, true)
err := common.CreateDir(logPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -1106,7 +1106,7 @@ func GetFilePath(file string) (string, error) {
oldDirs := []string{oldDir + ConfigFile, oldDir + EncryptedConfigFile}

newDir := common.GetDefaultDataDir(runtime.GOOS) + common.GetOSPathSlash()
err = common.CheckDir(newDir, true)
err = common.CreateDir(newDir)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func main() {
log.Fatalf("Failed to load config. Err: %s", err)
}

err = common.CheckDir(bot.dataDir, true)
err = common.CreateDir(bot.dataDir)
if err != nil {
log.Fatalf("Failed to open/create data directory: %s. Err: %s", bot.dataDir, err)
}
Expand Down
1 change: 0 additions & 1 deletion testdata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@ When submitting a PR, please abide by our coding guidelines:
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:

***1F5zVDgNjorJ51oGebSvNCrSAHpwGkUdDB***

0 comments on commit 400c1cc

Please sign in to comment.