Repository: chanzuckerberg/fogg
Pull request #324 (2019-07-19 20:58:12)

[fix] v1 to v2 upgrade will not convert contents of config file to yaml
### Summary
Migrating from v1 to v2 rewrote the entire fogg.json file to yaml, without changing the file type. This caused issues when migrating json to yaml because the program could not read the json file. The fix will keep the configuration file in json format after the v1 to v2 migration.

### Test Plan
I updated the tests to ensure that v1 to v2 does not change the contents of the fogg.json to yaml.

### References
(Optional) Additional links to provide more context.


codecov[bot]: # [Codecov](https://codecov.io/gh/chanzuckerberg/fogg/pull/324?src=pr&el=h1) Report
> Merging [#324](https://codecov.io/gh/chanzuckerberg/fogg/pull/324?src=pr&el=desc) into [master](https://codecov.io/gh/chanzuckerberg/fogg/commit/b3dfef5b55241e78e522dd16fc0456c16afc1bbd?src=pr&el=desc) will **increase** coverage by `0.53%`.
> The diff coverage is `50%`.

[![Impacted file tree graph](https://codecov.io/gh/chanzuckerberg/fogg/pull/324/graphs/tree.svg?width=650&token=57DhtZ7g2A&height=150&src=pr)](https://codecov.io/gh/chanzuckerberg/fogg/pull/324?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #324      +/-   ##
==========================================
+ Coverage   80.07%   80.61%   +0.53%     
==========================================
  Files          20       20              
  Lines        1787     1847      +60     
==========================================
+ Hits         1431     1489      +58     
+ Misses        223      221       -2     
- Partials      133      137       +4
```


| [Impacted Files](https://codecov.io/gh/chanzuckerberg/fogg/pull/324?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [migrations/upgrade.go](https://codecov.io/gh/chanzuckerberg/fogg/pull/324/diff?src=pr&el=tree#diff-bWlncmF0aW9ucy91cGdyYWRlLmdv) | `63.15% <50%> (ø)` | :arrow_up: |
| [util/module\_storage.go](https://codecov.io/gh/chanzuckerberg/fogg/pull/324/diff?src=pr&el=tree#diff-dXRpbC9tb2R1bGVfc3RvcmFnZS5nbw==) | `61.36% <0%> (+3.22%)` | :arrow_up: |
| [plugins/custom\_plugin.go](https://codecov.io/gh/chanzuckerberg/fogg/pull/324/diff?src=pr&el=tree#diff-cGx1Z2lucy9jdXN0b21fcGx1Z2luLmdv) | `69.29% <0%> (+8.94%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/chanzuckerberg/fogg/pull/324?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/chanzuckerberg/fogg/pull/324?src=pr&el=footer). Last update [b3dfef5...b7ce866](https://codecov.io/gh/chanzuckerberg/fogg/pull/324?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/migrations/upgrade.go b/migrations/upgrade.go
index 35ba908e..4d70e021 100644
--- a/migrations/upgrade.go
+++ b/migrations/upgrade.go
@@ -1,13 +1,14 @@
 package migrations
 
 import (
+	"encoding/json"
+
 	"github.com/chanzuckerberg/fogg/config"
 	v1 "github.com/chanzuckerberg/fogg/config/v1"
 	"github.com/chanzuckerberg/fogg/errs"
 	prompt "github.com/segmentio/go-prompt"
 	"github.com/sirupsen/logrus"
 	"github.com/spf13/afero"
-	"gopkg.in/yaml.v3"
 )
 
 //VersionUpgradeMigration Defines a fogg version upgrade
@@ -55,9 +56,9 @@ func (m *VersionUpgradeMigration) Migrate(fs afero.Fs, configFile string) (strin
 			return "", err
 		}
 
-		marshalled, err := yaml.Marshal(c2)
+		marshalled, err := json.Marshal(c2)
 		if err != nil {
-			return "", errs.WrapInternal(err, "Could not serialize config to yaml.")
+			return "", errs.WrapInternal(err, "Could not serialize config to json.")
 		}
 		err = afero.WriteFile(fs, configFile, marshalled, 0644)
 		return configFile, errs.WrapInternal(err, "Could not write config to disk")


Test patch:
diff --git a/migrations/upgrade_test.go b/migrations/upgrade_test.go
index 467b2cf5..abfe22c3 100644
--- a/migrations/upgrade_test.go
+++ b/migrations/upgrade_test.go
@@ -11,7 +11,7 @@ import (
 
 func TestUpgradeV2(t *testing.T) {
 	r := require.New(t)
-	confPath := "fogg.yml"
+	confPath := "fogg.json"
 	conf := []byte(`{"version": 1}`)
 	upgradeMigration := VersionUpgradeMigration{}
 
@@ -27,7 +27,7 @@ func TestUpgradeV2(t *testing.T) {
 
 	configFile, err := upgradeMigration.Migrate(fs, confPath)
 	r.Nil(err)
-	r.Equal("fogg.yml", configFile)
+	r.Equal("fogg.json", configFile)
 
 	_, version, err := config.FindConfig(fs, configFile)
 	r.Nil(err)
@@ -36,7 +36,7 @@ func TestUpgradeV2(t *testing.T) {
 
 func TestV2DoNothing(t *testing.T) {
 	r := require.New(t)
-	confPath := "fogg.yml"
+	confPath := "fogg.json"
 	conf := []byte(`{"version": 2}`)
 	upgradeMigration := VersionUpgradeMigration{}
 
@@ -52,7 +52,7 @@ func TestV2DoNothing(t *testing.T) {
 
 	configFile, err := upgradeMigration.Migrate(fs, confPath)
 	r.Nil(err)
-	r.Equal("fogg.yml", configFile)
+	r.Equal("fogg.json", configFile)
 
 	_, version, err := config.FindConfig(fs, configFile)
 	r.Nil(err)
@@ -61,7 +61,7 @@ func TestV2DoNothing(t *testing.T) {
 
 func TestUpgradeUnknownVersion(t *testing.T) {
 	r := require.New(t)
-	confPath := "fogg.yml"
+	confPath := "fogg.json"
 	conf := []byte(`{"version": 100}`)
 	upgradeMigration := VersionUpgradeMigration{}
 
@@ -77,12 +77,12 @@ func TestUpgradeUnknownVersion(t *testing.T) {
 
 	configFile, err := upgradeMigration.Migrate(fs, confPath)
 	r.Error(err, "config version 100 unrecognized")
-	r.Equal(configFile, "fogg.yml")
+	r.Equal(configFile, "fogg.json")
 }
 
 func TestUpgradeV1(t *testing.T) {
 	r := require.New(t)
-	confPath := "fogg.yml"
+	confPath := "fogg.json"
 	upgradeMigration := VersionUpgradeMigration{}
 
 	fs, _, err := util.TestFs()
@@ -105,7 +105,7 @@ func TestUpgradeV1(t *testing.T) {
 	configFile, err := upgradeMigration.Migrate(fs, confPath)
 	r.NoError(err)
 
-	//configFile is passed to validate the Migrate returned string
+	//configFile is passed to validate that Migrate returned string
 	_, v, err = config.FindConfig(fs, configFile)
 	r.NoError(err)
 	r.Equal(2, v) // now upgraded