github.com/mkasner/goat@v0.0.0-20190419083224-77b17249a8e3/context/config_test.go (about)

     1  package context
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  )
     7  
     8  func TestNewConfig(t *testing.T) {
     9  	// When ioutil.ReadFile returns an error.
    10  	expectedErrMsg := "open goat.yml: no such file or directory"
    11  	_, err := NewConfig()
    12  	if err == nil || err.Error() != expectedErrMsg {
    13  		t.Errorf("Error (%s) should be returned.", expectedErrMsg)
    14  	}
    15  
    16  	// When json.Unmarshal returns an error.
    17  	expectedErrMsg = "unexpected end of JSON input"
    18  	os.Chdir(os.Getenv("GOPATH") + "/src/github.com/yosssi/goat/test/context/TestNewConfig001")
    19  	_, err = NewConfig()
    20  	if err == nil || err.Error() != expectedErrMsg {
    21  		t.Errorf("Error (%s) should be returned.", expectedErrMsg)
    22  	}
    23  
    24  	// When config from json is returned.
    25  	os.Chdir(os.Getenv("GOPATH") + "/src/github.com/yosssi/goat/test/context/TestNewConfig002")
    26  	config, err := NewConfig()
    27  	if err != nil {
    28  		t.Errorf("Error (%s) occurred.", err.Error())
    29  	}
    30  	if len(config.Watchers) != 1 || config.Watchers[0].Extension != "go" || len(config.Watchers[0].Tasks) != 1 || config.Watchers[0].Tasks[0].Command != "make rerun" || config.Watchers[0].Tasks[0].Nowait != true {
    31  		t.Errorf("Config is invalid.")
    32  	}
    33  
    34  	// When yaml.Unmarshal returns an error.
    35  	expectedErrMsg = "yaml: line 1: did not find expected node content"
    36  	os.Chdir(os.Getenv("GOPATH") + "/src/github.com/yosssi/goat/test/context/TestNewConfig003")
    37  	_, err = NewConfig()
    38  	if err == nil || err.Error() != expectedErrMsg {
    39  		t.Errorf("Error (%s) should be returned.", expectedErrMsg)
    40  	}
    41  
    42  	// When config from yaml is returned.
    43  	os.Chdir(os.Getenv("GOPATH") + "/src/github.com/yosssi/goat/test/context/TestNewConfig004")
    44  	config, err = NewConfig()
    45  	if err != nil {
    46  		t.Errorf("Error (%s) occurred.", err.Error())
    47  	}
    48  	if len(config.Watchers) != 1 || config.Watchers[0].Extension != "go" || len(config.Watchers[0].Tasks) != 1 || config.Watchers[0].Tasks[0].Command != "make rerun" || config.Watchers[0].Tasks[0].Nowait != true {
    49  		t.Errorf("Config is invalid.")
    50  	}
    51  }