From b4982ee9333f53bd532f0d4b6014dd4e77bacab4 Mon Sep 17 00:00:00 2001 From: Brandon Banks Date: Thu, 26 Jan 2017 11:38:41 -0500 Subject: [PATCH] add ability to compare envs --- main.go | 15 ++++++++++++--- main_test.go | 24 ++++++++++++++++++++++-- testfiles/.env1 | 2 ++ testfiles/.env2 | 2 ++ testfiles/.env3 | 1 + 5 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 testfiles/.env1 create mode 100644 testfiles/.env2 create mode 100644 testfiles/.env3 diff --git a/main.go b/main.go index dec03cc..5e39793 100644 --- a/main.go +++ b/main.go @@ -15,8 +15,13 @@ import ( func extractKeys(fileData []byte, ymlPathArgs []string) []string { keys := make([]string, 0, 50) - y, _ := simpleyaml.NewYaml(fileData) - data, _ := y.GetPath(ymlPathArgs...).String() + data := "" + if len(ymlPathArgs) > 0 { + y, _ := simpleyaml.NewYaml(fileData) + data, _ = y.GetPath(ymlPathArgs...).String() + } else { + data = string(fileData[:]) + } lines := strings.Split(data, "\n") for _, line := range lines { keyVals := strings.Split(line, "=") @@ -81,7 +86,11 @@ func main() { app.Action = func(c *cli.Context) error { fileNames := strings.Split(c.Args().Get(0), ",") - ymlArgs := strings.Split(c.Args().Get(1), ",") + ymlArgs := []string{} + cmdArgs := c.Args().Get(1) + if cmdArgs != "" { + ymlArgs = strings.Split(c.Args().Get(1), ",") + } envKeys := ParseFileData(fileNames, ymlArgs) result := CompareEnvArrays(envKeys, fileNames) if result { diff --git a/main_test.go b/main_test.go index b2fdc4b..ea2964a 100644 --- a/main_test.go +++ b/main_test.go @@ -1,8 +1,10 @@ package main -import "testing" +import ( + "testing" -import "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/assert" +) func TestParseData(t *testing.T) { fileNames := []string{"testfiles/data1.yml", "testfiles/data2.yml"} @@ -64,3 +66,21 @@ func TestComparingArraysThatArentEqualByLength(t *testing.T) { testArraysAreEqual := CompareEnvArrays(expectedValues, fileNames) assert.Equal(t, false, testArraysAreEqual) } + +func TestComparingEnvFilesThatAreEqual(t *testing.T) { + fileNames := []string{"testfiles/.env1", "testfiles/.env2"} + + ymlArgs := []string{} + envKeys := ParseFileData(fileNames, ymlArgs) + result := CompareEnvArrays(envKeys, fileNames) + assert.Equal(t, true, result) +} + +func TestComparingEnvFilesThatAreNotEqual(t *testing.T) { + fileNames := []string{"testfiles/.env1", "testfiles/.env3"} + + ymlArgs := []string{} + envKeys := ParseFileData(fileNames, ymlArgs) + result := CompareEnvArrays(envKeys, fileNames) + assert.Equal(t, false, result) +} diff --git a/testfiles/.env1 b/testfiles/.env1 new file mode 100644 index 0000000..44040f1 --- /dev/null +++ b/testfiles/.env1 @@ -0,0 +1,2 @@ +a=b +b=a diff --git a/testfiles/.env2 b/testfiles/.env2 new file mode 100644 index 0000000..44040f1 --- /dev/null +++ b/testfiles/.env2 @@ -0,0 +1,2 @@ +a=b +b=a diff --git a/testfiles/.env3 b/testfiles/.env3 new file mode 100644 index 0000000..efc73ad --- /dev/null +++ b/testfiles/.env3 @@ -0,0 +1 @@ +a=b