Skip to content

Commit

Permalink
Add config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed May 17, 2018
1 parent cb30c66 commit a5232e9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
12 changes: 6 additions & 6 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func NewConfig(version, project, buildType string) *Config {

// Write writes configuration to Inertia config file at path. Optionally
// takes io.Writers.
func (config *Config) Write(path string, writers ...io.Writer) error {
if len(writers) == 0 && path == "" {
func (config *Config) Write(filePath string, writers ...io.Writer) error {
if len(writers) == 0 && filePath == "" {
return errors.New("nothing to write to")
}

Expand All @@ -47,15 +47,15 @@ func (config *Config) Write(path string, writers ...io.Writer) error {
}

// If path is given, attach file writer
if path != "" {
w, err := os.OpenFile(path, os.O_WRONLY, os.ModePerm)
if filePath != "" {
w, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}

// Overwrite file if file exists
if _, err := os.Stat(path); !os.IsNotExist(err) {
ioutil.WriteFile(path, []byte(""), 0644)
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
ioutil.WriteFile(filePath, []byte(""), 0644)
} else if err != nil {
return err
}
Expand Down
57 changes: 57 additions & 0 deletions client/config_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,68 @@
package client

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"

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

func TestNewConfig(t *testing.T) {
cfg := NewConfig("test", "best-project", "docker-compose")
assert.Equal(t, cfg.Version, "test")
}

func TestWriteFailed(t *testing.T) {
cfg := NewConfig("test", "best-project", "docker-compose")
err := cfg.Write("")
assert.NotNil(t, err)
assert.Contains(t, "nothing to write to", err.Error())
}

func TestWriteToPath(t *testing.T) {
configPath := "/test-config.toml"
cfg := NewConfig("test", "best-project", "docker-compose")

cwd, err := os.Getwd()
assert.Nil(t, err)
absPath := filepath.Join(cwd, configPath)
defer os.RemoveAll(absPath)

err = cfg.Write(absPath)
assert.Nil(t, err)

writtenConfigContents, err := ioutil.ReadFile(absPath)
assert.Nil(t, err)
assert.Contains(t, string(writtenConfigContents), "best-project")
assert.Contains(t, string(writtenConfigContents), "docker-compose")
}

func TestWriteToWritersAndFile(t *testing.T) {
configPath := "/test-config.toml"
cfg := NewConfig("test", "best-project", "docker-compose")

cwd, err := os.Getwd()
assert.Nil(t, err)
absPath := filepath.Join(cwd, configPath)
defer os.RemoveAll(absPath)

buffer1 := bytes.NewBuffer(nil)
buffer2 := bytes.NewBuffer(nil)

err = cfg.Write(absPath, buffer1, buffer2)
assert.Nil(t, err)

writtenConfigContents, err := ioutil.ReadFile(absPath)
assert.Nil(t, err)
assert.Contains(t, string(writtenConfigContents), "best-project")
assert.Contains(t, string(writtenConfigContents), "docker-compose")
assert.Contains(t, buffer1.String(), "best-project")
assert.Contains(t, buffer2.String(), "best-project")
}

func TestConfigGetRemote(t *testing.T) {
config := &Config{Remotes: make([]*RemoteVPS, 0)}
testRemote := &RemoteVPS{
Expand Down

0 comments on commit a5232e9

Please sign in to comment.