Repository: chanzuckerberg/fogg
Pull request #445 (2020-04-01 19:00:37)

[fix] Strip whitespace when reading .fogg-version
### Summary
When parsing the .fogg-version file, strip out all leading and trailing whitespace. Makes things more resilient to, for example, editors inserting newlines at the end of files.

### Test Plan
added unit tests


Patch:
diff --git a/apply/apply.go b/apply/apply.go
index c84ebaf6..4059fa50 100644
--- a/apply/apply.go
+++ b/apply/apply.go
@@ -100,7 +100,7 @@ func checkToolVersions(fs afero.Fs, current string) (bool, string, error) {
 	if e != nil {
 		return false, "", errs.WrapUser(e, "unable to read .fogg-version file")
 	}
-	repoVersion := string(b)
+	repoVersion := strings.TrimSpace(string(b))
 	changed, e := versionIsChanged(repoVersion, current)
 	return changed, repoVersion, e
 }
diff --git a/templates/repo/.fogg-version.tmpl b/templates/repo/.fogg-version.tmpl
index 6593b8a6..5de39ef5 100644
--- a/templates/repo/.fogg-version.tmpl
+++ b/templates/repo/.fogg-version.tmpl
@@ -1,1 +1,1 @@
-{{.Version}}
+{{- .Version -}}
diff --git a/util/version.go b/util/version.go
index 89c0d051..010ee0dd 100644
--- a/util/version.go
+++ b/util/version.go
@@ -25,7 +25,7 @@ func VersionCacheKey() string {
 func ParseVersion(version string) (semver.Version, string, bool) {
 	var dirty bool
 	var sha string
-	v := version
+	v := strings.TrimSpace(version)
 	if strings.HasSuffix(v, ".dirty") {
 		dirty = true
 		v = strings.TrimSuffix(v, ".dirty")


Test patch:
diff --git a/apply/apply_test.go b/apply/apply_test.go
index 8f883550..c07fea9a 100644
--- a/apply/apply_test.go
+++ b/apply/apply_test.go
@@ -366,6 +366,7 @@ var versionTests = []struct {
 	{"0.1.0-abcdef-dirty", "0.1.0", true},
 	{"0.1.0-abc", "0.1.0-def", true},
 	{"0.1.0-abc", "0.1.0-abc", false},
+	{"\t\n 0.2.0 \n\t", "0.2.0", false},
 }
 
 func TestCheckToolVersions(t *testing.T) {
diff --git a/testdata/bless_provider_yaml/.fogg-version b/testdata/bless_provider_yaml/.fogg-version
index 3da98bb7..2d5cfc01 100644
--- a/testdata/bless_provider_yaml/.fogg-version
+++ b/testdata/bless_provider_yaml/.fogg-version
@@ -1,1 +1,1 @@
-undefined+undefined+dirty
+undefined+undefined+dirty
\ No newline at end of file
diff --git a/testdata/circleci/.fogg-version b/testdata/circleci/.fogg-version
index 3da98bb7..2d5cfc01 100644
--- a/testdata/circleci/.fogg-version
+++ b/testdata/circleci/.fogg-version
@@ -1,1 +1,1 @@
-undefined+undefined+dirty
+undefined+undefined+dirty
\ No newline at end of file
diff --git a/testdata/github_actions/.fogg-version b/testdata/github_actions/.fogg-version
index 3da98bb7..2d5cfc01 100644
--- a/testdata/github_actions/.fogg-version
+++ b/testdata/github_actions/.fogg-version
@@ -1,1 +1,1 @@
-undefined+undefined+dirty
+undefined+undefined+dirty
\ No newline at end of file
diff --git a/testdata/github_provider_yaml/.fogg-version b/testdata/github_provider_yaml/.fogg-version
index 3da98bb7..2d5cfc01 100644
--- a/testdata/github_provider_yaml/.fogg-version
+++ b/testdata/github_provider_yaml/.fogg-version
@@ -1,1 +1,1 @@
-undefined+undefined+dirty
+undefined+undefined+dirty
\ No newline at end of file
diff --git a/testdata/okta_provider_yaml/.fogg-version b/testdata/okta_provider_yaml/.fogg-version
index 3da98bb7..2d5cfc01 100644
--- a/testdata/okta_provider_yaml/.fogg-version
+++ b/testdata/okta_provider_yaml/.fogg-version
@@ -1,1 +1,1 @@
-undefined+undefined+dirty
+undefined+undefined+dirty
\ No newline at end of file
diff --git a/testdata/snowflake_provider_yaml/.fogg-version b/testdata/snowflake_provider_yaml/.fogg-version
index 3da98bb7..2d5cfc01 100644
--- a/testdata/snowflake_provider_yaml/.fogg-version
+++ b/testdata/snowflake_provider_yaml/.fogg-version
@@ -1,1 +1,1 @@
-undefined+undefined+dirty
+undefined+undefined+dirty
\ No newline at end of file
diff --git a/testdata/v2_full_yaml/.fogg-version b/testdata/v2_full_yaml/.fogg-version
index 3da98bb7..2d5cfc01 100644
--- a/testdata/v2_full_yaml/.fogg-version
+++ b/testdata/v2_full_yaml/.fogg-version
@@ -1,1 +1,1 @@
-undefined+undefined+dirty
+undefined+undefined+dirty
\ No newline at end of file
diff --git a/testdata/v2_minimal_valid_yaml/.fogg-version b/testdata/v2_minimal_valid_yaml/.fogg-version
index 3da98bb7..2d5cfc01 100644
--- a/testdata/v2_minimal_valid_yaml/.fogg-version
+++ b/testdata/v2_minimal_valid_yaml/.fogg-version
@@ -1,1 +1,1 @@
-undefined+undefined+dirty
+undefined+undefined+dirty
\ No newline at end of file
diff --git a/testdata/v2_no_aws_provider_yaml/.fogg-version b/testdata/v2_no_aws_provider_yaml/.fogg-version
index 3da98bb7..2d5cfc01 100644
--- a/testdata/v2_no_aws_provider_yaml/.fogg-version
+++ b/testdata/v2_no_aws_provider_yaml/.fogg-version
@@ -1,1 +1,1 @@
-undefined+undefined+dirty
+undefined+undefined+dirty
\ No newline at end of file