github.com/goatapp/phraseapp-go@v3.0.0+incompatible/phraseapp/config_test.go (about)

     1  package phraseapp
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  func TestValidateIsType(t *testing.T) {
    10  	var t1 string = "foobar"
    11  	var t2 int = 1
    12  	var t3 bool = true
    13  	expErrT1 := fmt.Sprintf(cfgValueErrStr, "a", t1)
    14  	expErrT2 := fmt.Sprintf(cfgValueErrStr, "a", t2)
    15  	expErrT3 := fmt.Sprintf(cfgValueErrStr, "a", t3)
    16  
    17  	switch res, err := ValidateIsString("a", t1); {
    18  	case err != nil:
    19  		t.Errorf("didn't expect an error, got %q", err)
    20  	case res != t1:
    21  		t.Errorf("expected value to be %q, got %q", t1, res)
    22  	}
    23  
    24  	switch _, err := ValidateIsString("a", t2); {
    25  	case err == nil:
    26  		t.Errorf("expect an error, got none")
    27  	case err.Error() != expErrT2:
    28  		t.Errorf("expected error to be %q, got %q", expErrT2, err)
    29  	}
    30  
    31  	switch _, err := ValidateIsString("a", t3); {
    32  	case err == nil:
    33  		t.Errorf("expect an error, got none")
    34  	case err.Error() != expErrT3:
    35  		t.Errorf("expected error to be %q, got %q", expErrT3, err)
    36  	}
    37  
    38  	switch _, err := ValidateIsInt("a", t1); {
    39  	case err == nil:
    40  		t.Errorf("expect an error, got none")
    41  	case err.Error() != expErrT1:
    42  		t.Errorf("expected error to be %q, got %q", expErrT1, err)
    43  	}
    44  
    45  	switch res, err := ValidateIsInt("a", t2); {
    46  	case err != nil:
    47  		t.Errorf("didn't expect an error, got %q", err)
    48  	case res != t2:
    49  		t.Errorf("expected value to be %q, got %q", t2, res)
    50  	}
    51  
    52  	switch _, err := ValidateIsInt("a", t3); {
    53  	case err == nil:
    54  		t.Errorf("expect an error, got none")
    55  	case err.Error() != expErrT3:
    56  		t.Errorf("expected error to be %q, got %q", expErrT3, err)
    57  	}
    58  
    59  	switch _, err := ValidateIsBool("a", t1); {
    60  	case err == nil:
    61  		t.Errorf("expect an error, got none")
    62  	case err.Error() != expErrT1:
    63  		t.Errorf("expected error to be %q, got %q", expErrT1, err)
    64  	}
    65  
    66  	switch _, err := ValidateIsBool("a", t2); {
    67  	case err == nil:
    68  		t.Errorf("expect an error, got none")
    69  	case err.Error() != expErrT2:
    70  		t.Errorf("expected error to be %q, got %q", expErrT2, err)
    71  	}
    72  
    73  	switch res, err := ValidateIsBool("a", t3); {
    74  	case err != nil:
    75  		t.Errorf("didn't expect an error, got %q", err)
    76  	case res != t3:
    77  		t.Errorf("expected value to be %t, got %t", t3, res)
    78  	}
    79  }
    80  
    81  func TestValidateIsRawMapHappyPath(t *testing.T) {
    82  	m := map[interface{}]interface{}{
    83  		"foo": "bar",
    84  		"fuu": 1,
    85  		"few": true,
    86  	}
    87  
    88  	res, err := ValidateIsRawMap("a", m)
    89  	if err != nil {
    90  		t.Errorf("didn't expect an error, got %q", err)
    91  	}
    92  
    93  	if len(m) != len(res) {
    94  		t.Errorf("expected %d elements, got %d", len(m), len(res))
    95  	}
    96  
    97  	for k, v := range res {
    98  		if value, found := m[k]; !found {
    99  			t.Errorf("expected key %q to be in source set, it wasn't", k)
   100  		} else if value != v {
   101  			t.Errorf("expected value of %q to be %q, got %q", k, value, v)
   102  		}
   103  	}
   104  }
   105  
   106  func TestValidateIsRawMapWithErrors(t *testing.T) {
   107  	m := map[interface{}]interface{}{
   108  		4: "should be error",
   109  	}
   110  
   111  	expErr := fmt.Sprintf(cfgKeyErrStr, "a.4", 4)
   112  	_, err := ValidateIsRawMap("a", m)
   113  	if err == nil {
   114  		t.Errorf("expect an error, got none")
   115  	} else if err.Error() != expErr {
   116  		t.Errorf("expected error %q, got %q", expErr, err)
   117  	}
   118  }
   119  
   120  func TestParseYAMLToMap(t *testing.T) {
   121  	var a string
   122  	var b int
   123  	var c bool
   124  	var d []byte
   125  	e := map[string]interface{}{}
   126  
   127  	err := ParseYAMLToMap(func(raw interface{}) error {
   128  		m, ok := raw.(map[string]interface{})
   129  		if !ok {
   130  			return fmt.Errorf("invalid type received")
   131  		}
   132  		m["a"] = "foo"
   133  		m["b"] = 1
   134  		m["c"] = true
   135  		m["d"] = &struct {
   136  			A string
   137  			B int
   138  		}{A: "bar", B: 2}
   139  		m["e"] = map[interface{}]interface{}{"c": "baz", "d": 4}
   140  		return nil
   141  	}, map[string]interface{}{
   142  		"a": &a,
   143  		"b": &b,
   144  		"c": &c,
   145  		"d": &d,
   146  		"e": &e,
   147  	})
   148  	if err != nil {
   149  		t.Fatalf("didn't expect an error, got %q", err)
   150  	}
   151  
   152  	if a != "foo" {
   153  		t.Errorf("expected %q, got %q", "foo", a)
   154  	}
   155  
   156  	if b != 1 {
   157  		t.Errorf("expected %d, got %d", 1, b)
   158  	}
   159  
   160  	if c != true {
   161  		t.Errorf("expected %t, got %t", true, c)
   162  	}
   163  
   164  	if string(d) != "a: bar\nb: 2\n" {
   165  		t.Errorf("expected %s, got %s", "a: bar\nb: 2\n", string(d))
   166  	}
   167  
   168  	if val, found := e["c"]; !found {
   169  		t.Errorf("expected e to contain key %q, it didn't", "c")
   170  	} else if val != "baz" {
   171  		t.Errorf("expected e['c'] to have value %q, got %q", "baz", val)
   172  	}
   173  
   174  	if val, found := e["d"]; !found {
   175  		t.Errorf("expected e to contain key %q, it didn't", "d")
   176  	} else if val != 4 {
   177  		t.Errorf("expected e['d'] to have value %d, got %d", 4, val)
   178  	}
   179  }
   180  
   181  func TestConfigPath_ConfigFromEnv(t *testing.T) {
   182  	// The phraseapp.yml file without the leading '.' so not hidden. Any file can be used from the environment!
   183  	p := os.ExpandEnv("$GOPATH/src/github.com/phrase/phraseapp-go/testdata/phraseapp.yml")
   184  
   185  	os.Setenv("PHRASEAPP_CONFIG", p)
   186  	defer os.Unsetenv("PHRASEAPP_CONFIG")
   187  
   188  	path, err := configPath()
   189  	if err != nil {
   190  		t.Fatalf("didn't expect an error, got: %s", err)
   191  	} else if path != p {
   192  		t.Errorf("expected path to be %q, got %q", p, path)
   193  	}
   194  }
   195  
   196  func TestConfigPath_ConfigFromEnvButNotExisting(t *testing.T) {
   197  	os.Setenv("PHRASEAPP_CONFIG", "phraseapp_does_not_exist.yml")
   198  	defer os.Unsetenv("PHRASEAPP_CONFIG")
   199  
   200  	_, err := configPath()
   201  	if err == nil {
   202  		t.Fatalf("expect an error, got none")
   203  	}
   204  
   205  	expErr := `file "phraseapp_does_not_exist.yml" (from PHRASEAPP_CONFIG environment variable) doesn't exist`
   206  	if err.Error() != expErr {
   207  		t.Errorf("expected error to be %q, got %q", expErr, err)
   208  	}
   209  }
   210  
   211  func TestConfigPath_ConfigInCWD(t *testing.T) {
   212  	cwd := os.ExpandEnv("$GOPATH/src/github.com/phrase/phraseapp-go/testdata")
   213  
   214  	oldDir, _ := os.Getwd()
   215  	err := os.Chdir(cwd)
   216  	if err != nil {
   217  		t.Fatalf("didn't expect an error changing the working directory, got: %s", err)
   218  	}
   219  	defer os.Chdir(oldDir)
   220  
   221  	path, err := configPath()
   222  	if err != nil {
   223  		t.Fatalf("didn't expect an error, got: %s", err)
   224  	}
   225  	expPath := cwd + "/.phraseapp.yml"
   226  	if path != expPath {
   227  		t.Errorf("expected path to be %q, got %q", expPath, path)
   228  	}
   229  }
   230  
   231  func TestConfigPath_ConfigInHomeDir(t *testing.T) {
   232  	cwd := os.ExpandEnv("$GOPATH/src/github.com/phrase/phraseapp-go/testdata/empty")
   233  	oldDir, _ := os.Getwd()
   234  	err := os.Chdir(cwd)
   235  	if err != nil {
   236  		t.Fatalf("didn't expect an error changing the working directory, got: %s", err)
   237  	}
   238  	defer os.Chdir(oldDir)
   239  
   240  	newHome := os.ExpandEnv("$GOPATH/src/github.com/phrase/phraseapp-go/testdata")
   241  	oldHome := os.Getenv("HOME")
   242  	os.Setenv("HOME", newHome)
   243  	defer os.Setenv("HOME", oldHome)
   244  
   245  	path, err := configPath()
   246  	if err != nil {
   247  		t.Fatalf("didn't expect an error, got: %s", err)
   248  	}
   249  	expPath := newHome + "/.phraseapp.yml"
   250  	if path != expPath {
   251  		t.Errorf("expected path to be %q, got %q", expPath, path)
   252  	}
   253  }
   254  
   255  func TestConfigPath_NoConfigAvailable(t *testing.T) {
   256  	// For this to work the configuration of the user running the test
   257  	// must be obfuscated (changing the CWD and HOME env variable), so
   258  	// user's files do not inflict the test environment.
   259  
   260  	cwd := os.ExpandEnv("$GOPATH/src/github.com/phrase/phraseapp-go/testdata/empty")
   261  	oldDir, _ := os.Getwd()
   262  	err := os.Chdir(cwd)
   263  	if err != nil {
   264  		t.Fatalf("didn't expect an error changing the working directory, got: %s", err)
   265  	}
   266  	defer os.Chdir(oldDir)
   267  
   268  	oldHome := os.Getenv("HOME")
   269  	os.Setenv("HOME", os.ExpandEnv("$GOPATH/src/github.com/phrase/phraseapp-go/testdata/empty2"))
   270  	defer os.Setenv("HOME", oldHome)
   271  
   272  	path, err := configPath()
   273  	if err != nil {
   274  		t.Fatalf("didn't expect an error, got: %s", err)
   275  	}
   276  	expPath := ""
   277  	if path != expPath {
   278  		t.Errorf("expected path to be %q, got %q", expPath, path)
   279  	}
   280  }