Skip to content

Commit

Permalink
Tests for RC
Browse files Browse the repository at this point in the history
  • Loading branch information
hypnoglow committed Nov 30, 2016
1 parent f220edc commit 9e58ca0
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 6 deletions.
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ func getConfigPath(configArg interface{}) string {
exit(1)
}

if err = rc.Save(configPath); err != nil {
rc.SetPath(configPath)

if err = rc.Save(); err != nil {
outError("Cannot save rc file: %s", err)
exit(1)
}
Expand Down
13 changes: 8 additions & 5 deletions rc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// RCFilepath is path to dotbro's runcom file.
const RCFilepath = "${HOME}/.dotbro/profile.json"
var RCFilepath = "${HOME}/.dotbro/profile.json"

// RC represents data for dotbro's runcom file.
type RC struct {
Expand All @@ -24,6 +24,11 @@ func NewRC() *RC {
return &RC{}
}

// SetPath sets config path.
func (rc *RC) SetPath(configPath string) {
rc.Config.Path = configPath
}

// Load reads RC data from rcFilepath.
func (rc *RC) Load() (err error) {
rcFile := os.ExpandEnv(RCFilepath)
Expand All @@ -39,16 +44,14 @@ func (rc *RC) Load() (err error) {
return err
}

// Save saves RC data to rcFilepath.
func (rc *RC) Save(configPath string) (err error) {
// Save saves RC data to the file located at `RCFilepath`.
func (rc *RC) Save() (err error) {
rcFile := os.ExpandEnv(RCFilepath)

if err = createPath(rcFile); err != nil {
return err
}

rc.Config.Path = configPath

bytes, err := json.MarshalIndent(rc, "", " ")
if err != nil {
return err
Expand Down
118 changes: 118 additions & 0 deletions rc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"encoding/json"
"os"
"testing"
)

func TestNewRC(t *testing.T) {
NewRC()
}

func TestRC_SetPath(t *testing.T) {
// set up

configPath := "/tmp/dotbro.toml"

// test

rc := NewRC()

rc.SetPath(configPath)

if rc.Config.Path != configPath {
t.Fatal("Fail to set configPath correctly")
}

// tear down
}

func TestRC_LoadNotExists(t *testing.T) {
// set up

RCFilepath = "/tmp/dotbro_rc.json"

// test

rc := NewRC()

if err := rc.Load(); err != nil {
t.Fatal(err)
}
}

func TestRC_LoadExists(t *testing.T) {
// set up

RCFilepath = "/tmp/dotbro_rc.json"
configPath := "/tmp/dotbro.toml"

setupRC := NewRC()
setupRC.SetPath(configPath)

f, err := os.Create(RCFilepath)
if err != nil {
t.Fatal(err)
}

if err = json.NewEncoder(f).Encode(setupRC); err != nil {
t.Fatal(err)
}

// test

rc := NewRC()

if err := rc.Load(); err != nil {
t.Fatal(err)
}

// validate

if rc.Config.Path != configPath {
t.Fatal("Failed to load RC correctly")
}

// tear down

os.Remove(RCFilepath)
}

func TestRC_Save(t *testing.T) {
// set up

RCFilepath = "/tmp/dotbro_rc.json"
configPath := "/tmp/dotbro.toml"

// test

rc := NewRC()

rc.SetPath(configPath)

if err := rc.Save(); err != nil {
t.Fatal(err)
}

// validate

loadedRC := NewRC()

f, err := os.Open(RCFilepath)
if err != nil {
t.Fatal(err)
}

if err = json.NewDecoder(f).Decode(loadedRC); err != nil {
t.Fatal(err)
}

if loadedRC.Config.Path != configPath {
t.Fatal("Failed to save RC correctly")
}

// tear down

os.Remove(RCFilepath)
}

0 comments on commit 9e58ca0

Please sign in to comment.