settings tests

This commit is contained in:
alessio 2017-08-12 19:48:06 +02:00
parent a26b505473
commit bd9435693c
8 changed files with 190 additions and 21 deletions

View File

@ -196,7 +196,7 @@ func main() {
Questions: []*interact.Question{ Questions: []*interact.Question{
{ {
Before: func(d interact.Context) error { 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.Skip()
} }
d.SetDef(false, style.Green.Regular("(n)")) d.SetDef(false, style.Green.Regular("(n)"))
@ -894,7 +894,7 @@ func main() {
}, },
After: func(d interact.Context) error { After: func(d interact.Context) error {
if val, _ := d.Qns().Get(0).Ans().Bool(); val { if val, _ := d.Qns().Get(0).Ans().Bool(); val {
actErr = r.Settings.Remove() actErr = r.Settings.Remove(settings.Directory)
if actErr != nil { if actErr != nil {
return actErr return actErr
} }
@ -946,7 +946,7 @@ func main() {
Aliases: []string{"c"}, Aliases: []string{"c"},
Description: "Remove realize folder.", Description: "Remove realize folder.",
Action: func(p *cli.Context) error { Action: func(p *cli.Context) error {
if err := r.Settings.Remove(); err != nil { if err := r.Settings.Remove(settings.Directory); err != nil {
return err return err
} }
fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Realize folder successfully removed.")) fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Realize folder successfully removed."))

13
settings/flimit_test.go Normal file
View File

@ -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)
}
}

View File

@ -17,21 +17,21 @@ func (s Settings) Stream(file string) ([]byte, error) {
return content, err 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 { 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) return s.Validate(err)
} }
// Create a new file and return its pointer // Create a new file and return its pointer
func (s Settings) Create(path string, name string) *os.File { func (s Settings) Create(path string, name string) *os.File {
var file string var file string
if _, err := os.Stat(Dir); err == nil { if _, err := os.Stat(Directory); err == nil {
file = filepath.Join(path, Dir, name) file = filepath.Join(path, Directory, name)
} else { } else {
file = filepath.Join(path, name) 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) s.Validate(err)
return out return out
} }

43
settings/io_test.go Executable file
View File

@ -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())
}

View File

@ -6,8 +6,11 @@ import (
"time" "time"
) )
// realize hidden dir // settings const
var Dir = ".realize/" const (
Permission = 0775
Directory = ".realize/"
)
// Settings defines a group of general settings // Settings defines a group of general settings
type Settings struct { type Settings struct {
@ -48,15 +51,16 @@ type Resources struct {
// Read from config file // Read from config file
func (s *Settings) Read(out interface{}) error { func (s *Settings) Read(out interface{}) error {
localConfigPath := s.Resources.Config localConfigPath := s.Resources.Config
if _, err := os.Stat(Dir + s.Resources.Config); err == nil { // backward compatibility
localConfigPath = Dir + s.Resources.Config if _, err := os.Stat(Directory + s.Resources.Config); err == nil {
localConfigPath = Directory + s.Resources.Config
} }
content, err := s.Stream(localConfigPath) content, err := s.Stream(localConfigPath)
if err == nil { if err == nil {
err = yaml.Unmarshal(content, out) err = yaml.Unmarshal(content, out)
return err return err
} }
return nil return err
} }
// Record create and unmarshal the yaml config file // Record create and unmarshal the yaml config file
@ -66,20 +70,21 @@ func (s *Settings) Record(out interface{}) error {
if err != nil { if err != nil {
return err return err
} }
if _, err := os.Stat(Dir); os.IsNotExist(err) { if _, err := os.Stat(Directory); os.IsNotExist(err) {
if err = os.Mkdir(Dir, 0770); err != nil { if err = os.Mkdir(Directory, Permission); err != nil {
return s.Write(s.Resources.Config, y) 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 return nil
} }
// Remove realize folder // Remove realize folder
func (s *Settings) Remove() error { func (s *Settings) Remove(d string) error {
if _, err := os.Stat(Dir); !os.IsNotExist(err) { _, err := os.Stat(d)
return os.RemoveAll(Dir) if !os.IsNotExist(err) {
return os.RemoveAll(d)
} }
return nil return err
} }

51
settings/settings_test.go Normal file
View File

@ -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))
}

View File

@ -9,7 +9,7 @@ import (
"github.com/tockins/realize/style" "github.com/tockins/realize/style"
) )
// Wdir return the current working directory // Wdir return the current working Directory
func (s Settings) Wdir() string { func (s Settings) Wdir() string {
dir, err := os.Getwd() dir, err := os.Getwd()
s.Validate(err) s.Validate(err)

57
settings/utils_test.go Normal file
View File

@ -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)
}
}