From bd9435693c14d03d2c3ae914e96d86104475b244 Mon Sep 17 00:00:00 2001 From: alessio Date: Sat, 12 Aug 2017 19:48:06 +0200 Subject: [PATCH] settings tests --- realize.go | 6 ++--- settings/flimit_test.go | 13 +++++++++ settings/io.go | 10 +++---- settings/io_test.go | 43 +++++++++++++++++++++++++++++ settings/settings.go | 29 +++++++++++--------- settings/settings_test.go | 51 +++++++++++++++++++++++++++++++++++ settings/utils.go | 2 +- settings/utils_test.go | 57 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 190 insertions(+), 21 deletions(-) create mode 100644 settings/flimit_test.go create mode 100755 settings/io_test.go create mode 100644 settings/settings_test.go create mode 100644 settings/utils_test.go diff --git a/realize.go b/realize.go index 5f9cb92..6c84e42 100644 --- a/realize.go +++ b/realize.go @@ -196,7 +196,7 @@ func main() { Questions: []*interact.Question{ { Before: func(d interact.Context) error { - if _, err := os.Stat(settings.Dir + config); err != nil { + if _, err := os.Stat(settings.Directory + config); err != nil { d.Skip() } d.SetDef(false, style.Green.Regular("(n)")) @@ -894,7 +894,7 @@ func main() { }, After: func(d interact.Context) error { if val, _ := d.Qns().Get(0).Ans().Bool(); val { - actErr = r.Settings.Remove() + actErr = r.Settings.Remove(settings.Directory) if actErr != nil { return actErr } @@ -946,7 +946,7 @@ func main() { Aliases: []string{"c"}, Description: "Remove realize folder.", Action: func(p *cli.Context) error { - if err := r.Settings.Remove(); err != nil { + if err := r.Settings.Remove(settings.Directory); err != nil { return err } fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Realize folder successfully removed.")) diff --git a/settings/flimit_test.go b/settings/flimit_test.go new file mode 100644 index 0000000..da97e34 --- /dev/null +++ b/settings/flimit_test.go @@ -0,0 +1,13 @@ +package settings + +import ( + "testing" +) + +func TestSettings_Flimit(t *testing.T) { + s := Settings{} + s.Config.Flimit = 1 + if err := s.Flimit(); err != nil { + t.Error("Unable to increase limit", err) + } +} diff --git a/settings/io.go b/settings/io.go index d622ee8..2124117 100644 --- a/settings/io.go +++ b/settings/io.go @@ -17,21 +17,21 @@ func (s Settings) Stream(file string) ([]byte, error) { return content, err } -// Write a file given a name and a byte stream +// Write a file func (s Settings) Write(name string, data []byte) error { - err := ioutil.WriteFile(name, data, 0655) + err := ioutil.WriteFile(name, data, Permission) return s.Validate(err) } // Create a new file and return its pointer func (s Settings) Create(path string, name string) *os.File { var file string - if _, err := os.Stat(Dir); err == nil { - file = filepath.Join(path, Dir, name) + if _, err := os.Stat(Directory); err == nil { + file = filepath.Join(path, Directory, name) } else { file = filepath.Join(path, name) } - out, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0655) + out, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_SYNC, Permission) s.Validate(err) return out } diff --git a/settings/io_test.go b/settings/io_test.go new file mode 100755 index 0000000..a45d6e0 --- /dev/null +++ b/settings/io_test.go @@ -0,0 +1,43 @@ +package settings + +import ( + "github.com/labstack/gommon/random" + "io/ioutil" + "os" + "testing" +) + +func TestSettings_Stream(t *testing.T) { + s := Settings{} + filename := random.String(4) + if _, err := s.Stream(filename); err == nil { + t.Fatal("Error expected, none found", filename, err) + } + + filename = "io.go" + if _, err := s.Stream(filename); err != nil { + t.Fatal("Error unexpected", filename, err) + } +} + +func TestSettings_Write(t *testing.T) { + s := Settings{} + data := "abcdefgh" + d, err := ioutil.TempFile("", "io_test") + if err != nil { + t.Fatal(err) + } + if err := s.Write(d.Name(), []byte(data)); err != nil { + t.Fatal(err) + } +} + +func TestSettings_Create(t *testing.T) { + s := Settings{} + p, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + f := s.Create(p, "io_test") + os.Remove(f.Name()) +} diff --git a/settings/settings.go b/settings/settings.go index 5f43106..2812352 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -6,8 +6,11 @@ import ( "time" ) -// realize hidden dir -var Dir = ".realize/" +// settings const +const ( + Permission = 0775 + Directory = ".realize/" +) // Settings defines a group of general settings type Settings struct { @@ -48,15 +51,16 @@ type Resources struct { // Read from config file func (s *Settings) Read(out interface{}) error { localConfigPath := s.Resources.Config - if _, err := os.Stat(Dir + s.Resources.Config); err == nil { - localConfigPath = Dir + s.Resources.Config + // backward compatibility + if _, err := os.Stat(Directory + s.Resources.Config); err == nil { + localConfigPath = Directory + s.Resources.Config } content, err := s.Stream(localConfigPath) if err == nil { err = yaml.Unmarshal(content, out) return err } - return nil + return err } // Record create and unmarshal the yaml config file @@ -66,20 +70,21 @@ func (s *Settings) Record(out interface{}) error { if err != nil { return err } - if _, err := os.Stat(Dir); os.IsNotExist(err) { - if err = os.Mkdir(Dir, 0770); err != nil { + if _, err := os.Stat(Directory); os.IsNotExist(err) { + if err = os.Mkdir(Directory, Permission); err != nil { return s.Write(s.Resources.Config, y) } } - return s.Write(Dir+s.Resources.Config, y) + return s.Write(Directory+s.Resources.Config, y) } return nil } // Remove realize folder -func (s *Settings) Remove() error { - if _, err := os.Stat(Dir); !os.IsNotExist(err) { - return os.RemoveAll(Dir) +func (s *Settings) Remove(d string) error { + _, err := os.Stat(d) + if !os.IsNotExist(err) { + return os.RemoveAll(d) } - return nil + return err } diff --git a/settings/settings_test.go b/settings/settings_test.go new file mode 100644 index 0000000..f55ce9d --- /dev/null +++ b/settings/settings_test.go @@ -0,0 +1,51 @@ +package settings + +import ( + "io/ioutil" + "path/filepath" + "testing" +) + +func TestSettings_Read(t *testing.T) { + s := Settings{} + var a interface{} + s.Resources.Config = "settings_b" + if err := s.Read(a); err == nil { + t.Error("Error unexpected", err) + } + + s.Resources.Config = "settings_test.yaml" + d, err := ioutil.TempFile("", "settings_test.yaml") + if err != nil { + t.Fatal(err) + } + s.Resources.Config = d.Name() + if err := s.Read(a); err != nil { + t.Error("Error unexpected", err) + } +} + +func TestSettings_Remove(t *testing.T) { + s := Settings{} + if err := s.Remove("abcd"); err == nil { + t.Error("Error unexpected, dir dosn't exist", err) + } + + d, err := ioutil.TempDir("", "settings_test") + if err != nil { + t.Error(err) + } + if err := s.Remove(d); err != nil { + t.Error("Error unexpected, dir exist", err) + } +} + +func TestSettings_Record(t *testing.T) { + s := Settings{} + s.Resources.Config = "settings_test.yaml" + var a interface{} + if err := s.Record(a); err != nil { + t.Error(err) + } + s.Remove(filepath.Join(Directory, s.Resources.Config)) +} diff --git a/settings/utils.go b/settings/utils.go index 88ee87a..bf2a41a 100644 --- a/settings/utils.go +++ b/settings/utils.go @@ -9,7 +9,7 @@ import ( "github.com/tockins/realize/style" ) -// Wdir return the current working directory +// Wdir return the current working Directory func (s Settings) Wdir() string { dir, err := os.Getwd() s.Validate(err) diff --git a/settings/utils_test.go b/settings/utils_test.go new file mode 100644 index 0000000..44ccce8 --- /dev/null +++ b/settings/utils_test.go @@ -0,0 +1,57 @@ +package settings + +import ( + "errors" + "github.com/labstack/gommon/random" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestSettings_Wdir(t *testing.T) { + s := Settings{} + expected, err := os.Getwd() + if err != nil { + t.Error(err) + } + result := s.Wdir() + if result != filepath.Base(expected) { + t.Error("Expected", filepath.Base(expected), "instead", result) + } +} + +func TestSettings_Validate(t *testing.T) { + s := Settings{} + input := errors.New("") + input = nil + if err := s.Validate(input); err != nil { + t.Error("Expected", input, "instead", err) + } +} + +func TestSettings_Name(t *testing.T) { + s := Settings{} + name := random.String(8) + path := random.String(5) + dir, err := os.Getwd() + if err != nil { + t.Error(err) + } + result := s.Name(name, path) + if result != dir && result != filepath.Base(path) { + t.Error("Expected", dir, "or", filepath.Base(path), "instead", result) + } + +} + +func TestSettings_Path(t *testing.T) { + s := Settings{} + path := random.String(5) + expected := strings.Replace(filepath.Clean(path), "\\", "/", -1) + result := s.Path(path) + if result != expected { + t.Error("Expected", expected, "instead", result) + } + +}