From a5232e942c4181bc3c3a05edb09476d6e89396a8 Mon Sep 17 00:00:00 2001 From: Robert Lin Date: Wed, 16 May 2018 23:05:10 -0700 Subject: [PATCH] Add config tests --- client/config.go | 12 ++++----- client/config_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/client/config.go b/client/config.go index 82ce8cce..a0f5e6e7 100644 --- a/client/config.go +++ b/client/config.go @@ -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") } @@ -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 } diff --git a/client/config_test.go b/client/config_test.go index d9a49cff..e4759e92 100644 --- a/client/config_test.go +++ b/client/config_test.go @@ -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{