Skip to content

Commit

Permalink
Added test.
Browse files Browse the repository at this point in the history
Refactors.
  • Loading branch information
ryanfox1985 committed Jun 21, 2016
1 parent 4f71f9f commit 3c65ae9
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 26 deletions.
9 changes: 8 additions & 1 deletion .goxc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
"go-vet",
"go-fmt",
"go-install",
"go-clean"
"go-clean",
"publish-github"
],
"TaskSettings": {
"publish-github": {
"repository": "devcows/share"
}
},

"ConfigVersion": "0.9",
"Env": [
"SHARE_MODE=release"
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ script:

after_success:
- goxc
- ls -la ./dist
# - gox -cgo -output "dist/{{.OS}}_{{.Arch}}_{{.Dir}}"
# - ghr -t $GITHUB_TOKEN -u $CIRCLE_PROJECT_USERNAME -r $CIRCLE_PROJECT_REPONAME --replace `git describe --tags` dist/
# - ghr --username tcnksm-sample --token $GITHUB_TOKEN --replace --prerelease --debug pre-release dist/
10 changes: 9 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,21 @@ func mainServer() {
r.Run(lib.ConfigServerEndPoint(settings))
}

func overwriteSettings() {
//overwrite settings
if portAPI > 0 {
settings.Daemon.Port = portAPI
}
}

var ServerCmd = &cobra.Command{
Use: "server",
Short: "Server APIREST",
Long: `Server APIREST`,
Run: func(cmd *cobra.Command, args []string) {
var err error

if err = lib.InitSettings(&settings, portAPI); err != nil {
if err = lib.InitSettings(lib.ConfigFile(), &settings); err != nil {
fmt.Println(err)
os.Exit(-1)
}
Expand All @@ -122,6 +129,7 @@ var ServerCmd = &cobra.Command{
os.Exit(-1)
}

overwriteSettings()
mainServer()
},
}
11 changes: 1 addition & 10 deletions lib/database_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
package lib

import (
"crypto/rand"
"encoding/hex"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

var test_settings SettingsShare

func TempFilename() string {
randBytes := make([]byte, 16)
rand.Read(randBytes)
return filepath.Join(os.TempDir(), "foo"+hex.EncodeToString(randBytes)+".sqlite3")
}

func setup() {
test_settings = NewSettings()
test_settings.Daemon.DatabaseFilePath = TempFilename()
test_settings.Daemon.DatabaseFilePath = TempFilename("db_", ".sqlite3")

err := InitDB(test_settings)
if err != nil {
Expand Down
29 changes: 15 additions & 14 deletions lib/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lib
import (
"bytes"
"os"
"path/filepath"
"runtime"
"strconv"

Expand All @@ -21,42 +22,42 @@ type Daemon struct {
DatabaseFilePath string
}

func CreateConfigFile(settings SettingsShare, outputFolder string, outputFile string) {
func CreateConfigFile(outputFile string, settings SettingsShare) error {
outputFolder := filepath.Dir(outputFile)
os.MkdirAll(outputFolder, 0700)

f, err := os.Create(outputFile)
if err != nil {
panic(err)
return err
}

var buf bytes.Buffer
e := toml.NewEncoder(&buf)
err = e.Encode(settings)
if err != nil {
panic(err)
f.Close()
return err
}

f.WriteString(buf.String())
f.Close()

return nil
}

func InitSettings(settings *SettingsShare, portApi int) error {
if _, err := os.Stat(ConfigFile()); os.IsNotExist(err) {
log.Info("New config file: %s\n", ConfigFile())
func InitSettings(configFile string, settings *SettingsShare) error {
if _, err := os.Stat(configFile); os.IsNotExist(err) {
log.Info("New config file: %s\n", configFile)
*settings = NewSettings()

CreateConfigFile(*settings, ConfigFolder(), ConfigFile())
CreateConfigFile(configFile, *settings)
} else {
log.Info("Loading config file: %s\n", ConfigFile())
if _, err := toml.DecodeFile(ConfigFile(), &settings); err != nil {
panic(err)
log.Info("Loading config file: %s\n", configFile)
if _, err := toml.DecodeFile(configFile, &settings); err != nil {
return err
}
}

if portApi > 0 {
settings.Daemon.Port = portApi
}

log.Info("Current config: %v\n", settings)
return nil
}
Expand Down
85 changes: 85 additions & 0 deletions lib/settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package lib

import (
"crypto/rand"
"encoding/hex"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TempFilename(prefix string, extension string) string {
randBytes := make([]byte, 16)
rand.Read(randBytes)
return filepath.Join(os.TempDir(), prefix+hex.EncodeToString(randBytes)+extension)
}

func TestCreateConfigFile(t *testing.T) {
test_settings = NewSettings()

configFile := TempFilename("config_", ".toml")
err := CreateConfigFile(configFile, test_settings)
assert.Nil(t, err)

_, err2 := os.Stat(test_settings.Daemon.DatabaseFilePath)
assert.False(t, os.IsNotExist(err2), "The config file: %s doesn't exists!", configFile)
}

func TestInitSettings(t *testing.T) {
var test_settings SettingsShare

configFile := TempFilename("config_", ".toml")
// Create
err := InitSettings(configFile, &test_settings)
assert.Nil(t, err)
assert.NotNil(t, test_settings)

// LOAD
err2 := InitSettings(configFile, &test_settings)
assert.Nil(t, err2)
assert.NotNil(t, test_settings)
}

func TestNewSettings(t *testing.T) {
test_settings := NewSettings()

assert.NotNil(t, test_settings)
}

func TestConfigFolder(t *testing.T) {
pathConfigFolder := ConfigFolder()

assert.NotNil(t, pathConfigFolder)
assert.True(t, len(pathConfigFolder) > 0)
}

func TestConfigFile(t *testing.T) {
pathConfigFile := ConfigFile()

assert.NotNil(t, pathConfigFile)
assert.True(t, len(pathConfigFile) > 0)
}

func TestConfigFileSQLITE(t *testing.T) {
pathConfigDb := ConfigFileSQLITE()

assert.NotNil(t, pathConfigDb)
assert.True(t, len(pathConfigDb) > 0)
}

func TestConfigServerEndPoint(t *testing.T) {
test_settings := NewSettings()
endPoint := ConfigServerEndPoint(test_settings)

assert.NotNil(t, endPoint)
assert.True(t, len(endPoint) > 0)
}

func TestUserHomeDir(t *testing.T) {
pathConfigUserDir := UserHomeDir()

assert.NotNil(t, pathConfigUserDir)
assert.True(t, len(pathConfigUserDir) > 0)
}

0 comments on commit 3c65ae9

Please sign in to comment.