Repository: nuveo/prest
Pull request #188 (2017-07-21 19:42:51)

Fix issue #186
Create a function that return a default PREST_CONF in case empty settings

codecov[bot]: # [Codecov](https://codecov.io/gh/nuveo/prest/pull/188?src=pr&el=h1) Report
> Merging [#188](https://codecov.io/gh/nuveo/prest/pull/188?src=pr&el=desc) into [master](https://codecov.io/gh/nuveo/prest/commit/029f2de0ba2a0349926241f1e249b6bf1b39c299?src=pr&el=desc) will **increase** coverage by `0.06%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/nuveo/prest/pull/188/graphs/tree.svg?height=150&width=650&token=eVD9urwIEv&src=pr)](https://codecov.io/gh/nuveo/prest/pull/188?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #188      +/-   ##
==========================================
+ Coverage    85.4%   85.47%   +0.06%     
==========================================
  Files          11       11              
  Lines        1110     1115       +5     
==========================================
+ Hits          948      953       +5     
  Misses        125      125              
  Partials       37       37
```


| [Impacted Files](https://codecov.io/gh/nuveo/prest/pull/188?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [config/config.go](https://codecov.io/gh/nuveo/prest/pull/188?src=pr&el=tree#diff-Y29uZmlnL2NvbmZpZy5nbw==) | `68.83% <100%> (+2.16%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/nuveo/prest/pull/188?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/nuveo/prest/pull/188?src=pr&el=footer). Last update [029f2de...8f5cc9c](https://codecov.io/gh/nuveo/prest/pull/188?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/config/config.go b/config/config.go
index bf7d214..69974ca 100644
--- a/config/config.go
+++ b/config/config.go
@@ -7,6 +7,7 @@ import (
 	"os/user"
 	"path/filepath"
 	"strings"
+
 	"github.com/spf13/viper"
 )
 
@@ -47,7 +48,7 @@ type Prest struct {
 var PrestConf *Prest
 
 func viperCfg() {
-	filePath := os.Getenv("PREST_CONF")
+	filePath := getDefaultPrestConf(os.Getenv("PREST_CONF"))
 	dir, file := filepath.Split(filePath)
 	file = strings.TrimSuffix(file, filepath.Ext(file))
 	replacer := strings.NewReplacer(".", "_")
@@ -73,6 +74,13 @@ func viperCfg() {
 	viper.SetDefault("queries.location", filepath.Join(user.HomeDir, "queries"))
 }
 
+func getDefaultPrestConf(prestConf string) string {
+	if prestConf == "" {
+		return "./prest.toml"
+	}
+	return prestConf
+}
+
 // Parse pREST config
 func Parse(cfg *Prest) (err error) {
 	err = viper.ReadInConfig()


Test patch:
diff --git a/config/config_test.go b/config/config_test.go
index afd0406..aa92a77 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -65,3 +65,15 @@ func TestParse(t *testing.T) {
 		t.Errorf("expected port: 4000, got: %d", cfg.HTTPPort)
 	}
 }
+
+func TestGetDefaultPrestConf(t *testing.T) {
+	prestConf := ""
+	if conf := getDefaultPrestConf(prestConf); conf != "./prest.toml" {
+		t.Errorf("expected ./prest.toml, but got: %q", conf)
+	}
+
+	prestConf = "../prest.toml"
+	if conf := getDefaultPrestConf(prestConf); conf != "../prest.toml" {
+		t.Errorf("expected ../prest.toml, but got: %q", conf)
+	}
+}