Skip to content

Commit

Permalink
Merge pull request #3 from generationtux/bugfix-files-not-existing
Browse files Browse the repository at this point in the history
throw an error when files aren't found and success when keys arent in…
  • Loading branch information
weklund authored Dec 21, 2016
2 parents 6873c7d + 3d776ee commit 0ee8bdd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added main
Binary file not shown.
19 changes: 15 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,32 @@ func extractKeys(fileData []byte, ymlPathArgs []string) []string {
func ParseFileData(filenames []string, ymlPathArgs []string) [][]string {
envKeys := make([][]string, 0, 50)
for _, file := range filenames {
if _, err := os.Stat(file); os.IsNotExist(err) {
fmt.Println(file + " Does not exist")
os.Exit(1)
}
fileData, _ := ioutil.ReadFile(file)
envKeys = append(envKeys, extractKeys(fileData, ymlPathArgs))
}
return envKeys
}

func CompareEnvArrays(envKeys [][]string) bool {
func CompareEnvArrays(envKeys [][]string, filenames []string) bool {
for i := 0; i < len(envKeys); i++ {
for j := i + 1; j < len(envKeys); j++ {
if len(envKeys[i]) != len(envKeys[j]) {
fmt.Println("Unequal number of keys")
return false
}
for y := 0; y < len(envKeys[j]); y++ {
if envKeys[j][y] != envKeys[i][y] {
fmt.Println("A key exists in one file, that another one doesn't have")
keyExists := false
for z := 0; z < len(envKeys[j]); z++ {
if envKeys[j][y] == envKeys[i][z] {
keyExists = true
}
}
if !keyExists {
fmt.Println("Key " + envKeys[j][y] + " doesn't exist in file parameter " + filenames[j])
return false
}
}
Expand All @@ -73,8 +83,9 @@ func main() {
fileNames := strings.Split(c.Args().Get(0), ",")
ymlArgs := strings.Split(c.Args().Get(1), ",")
envKeys := ParseFileData(fileNames, ymlArgs)
result := CompareEnvArrays(envKeys)
result := CompareEnvArrays(envKeys, fileNames)
if result {
fmt.Println("All configs are matching")
os.Exit(0)
} else {
fmt.Println("Keys arent equal")
Expand Down
12 changes: 9 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ func TestParseData(t *testing.T) {

func TestComparingArraysThatAreEqual(t *testing.T) {
expectedValues := [][]string{}
fileNames := []string{"testfiles/data1.yml", "testfiles/data2.yml"}

// These are the first two rows.
row1 := []string{"a", "b"}
row2 := []string{"a", "b"}

expectedValues = append(expectedValues, row1)
expectedValues = append(expectedValues, row2)

testArraysAreEqual := CompareEnvArrays(expectedValues)
testArraysAreEqual := CompareEnvArrays(expectedValues, fileNames)
assert.Equal(t, true, testArraysAreEqual)
}

Expand All @@ -39,10 +41,12 @@ func TestComparingArraysThatArentEqualByKeys(t *testing.T) {
// These are the first two rows.
row1 := []string{"a", "c"}
row2 := []string{"a", "b"}
fileNames := []string{"testfiles/data1.yml", "testfiles/data2.yml"}

expectedValues = append(expectedValues, row1)
expectedValues = append(expectedValues, row2)

testArraysAreEqual := CompareEnvArrays(expectedValues)
testArraysAreEqual := CompareEnvArrays(expectedValues, fileNames)
assert.Equal(t, false, testArraysAreEqual)
}

Expand All @@ -52,9 +56,11 @@ func TestComparingArraysThatArentEqualByLength(t *testing.T) {
// These are the first two rows.
row1 := []string{"a", "b", "c"}
row2 := []string{"a", "b"}
fileNames := []string{"testfiles/data1.yml", "testfiles/data2.yml"}

expectedValues = append(expectedValues, row1)
expectedValues = append(expectedValues, row2)

testArraysAreEqual := CompareEnvArrays(expectedValues)
testArraysAreEqual := CompareEnvArrays(expectedValues, fileNames)
assert.Equal(t, false, testArraysAreEqual)
}
3 changes: 2 additions & 1 deletion testfiles/data3.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
data:
.env: |-
a=5
b=15
b=15
c=15

0 comments on commit 0ee8bdd

Please sign in to comment.