added test to increase coverage #31

This commit is contained in:
winhung 2019-07-25 22:23:27 +08:00
parent 4de994e030
commit 42e69ac821

View file

@ -1,7 +1,10 @@
package config
import (
"os"
"testing"
"github.com/jesseduffield/yaml"
)
func TestDockerComposeCommandNoFiles(t *testing.T) {
@ -48,3 +51,50 @@ func TestDockerComposeCommandMultipleFiles(t *testing.T) {
t.Fatalf("Expected %s but got %s", expected, actual)
}
}
func TestWritingToConfigFile(t *testing.T) {
//init the AppConfig
emptyComposeFiles := []string{}
conf, err := NewAppConfig("name", "version", "commit", "date", "buildSource", false, emptyComposeFiles, "projectDir")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
testFn := func(ac *AppConfig, newReportingValue string, t *testing.T) {
updateFn := func(uc *UserConfig) error {
uc.Reporting = newReportingValue
return nil
}
err = ac.WriteToUserConfig(updateFn)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
file, err := os.OpenFile(ac.ConfigFilename(), os.O_RDONLY, 0660)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
sampleUC := UserConfig{}
err = yaml.NewDecoder(file).Decode(&sampleUC)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
err = file.Close()
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if sampleUC.Reporting != newReportingValue {
t.Fatalf("Got %s, Expected %s\n", sampleUC.Reporting, newReportingValue)
}
}
// insert value into an empty file
testFn(conf, "on", t)
// modifying an existing file that already has 'Reporting'
testFn(conf, "off", t)
}