Skip to content

Commit

Permalink
Add tests for helper/files.go
Browse files Browse the repository at this point in the history
Closes #72
  • Loading branch information
jrasell committed Dec 27, 2017
1 parent 054878c commit f7fe6ec
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions helper/files_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package helper

import (
"io/ioutil"
"os"
"reflect"
"testing"
)

func TestHelper_GetDefaultTmplFile(t *testing.T) {
d1 := []byte("Levant Test Job File\n")

cases := []struct {
TmplFiles []string
Output string
}{
{
[]string{"example.nomad", "example1.nomad"},
"",
},
{
[]string{"example.nomad"},
"example.nomad",
},
}

for _, tc := range cases {
for _, f := range tc.TmplFiles {

// Use write file as tmpfile adds a prefix which doesn't work with the
// GetDefaultTmplFile function.
err := ioutil.WriteFile(f, d1, 0644)
if err != nil {
t.Fatal(err)
}
}

actual := GetDefaultTmplFile()

// Call explicit Remove as the function is dependant on the number of files
// in the target directory.
for _, f := range tc.TmplFiles {
os.Remove(f)
}

if !reflect.DeepEqual(actual, tc.Output) {
t.Fatalf("got: %#v, expected %#v", actual, tc.Output)
}
}

}

func TestHelper_GetDefaultVarFile(t *testing.T) {
d1 := []byte("Levant Test Variable File\n")

cases := []struct {
VarFile string
}{
{"levant.yaml"},
{"levant.yml"},
{"levant.tf"},
{""},
}

for _, tc := range cases {
if tc.VarFile != "" {

// Use write file as tmpfile adds a prefix which doesn't work with the
// GetDefaultTmplFile function.
err := ioutil.WriteFile(tc.VarFile, d1, 0644)
if err != nil {
t.Fatal(err)
}
}

actual := GetDefaultVarFile()
if !reflect.DeepEqual(actual, tc.VarFile) {
t.Fatalf("got: %#v, expected %#v", actual, tc.VarFile)
}

os.Remove(tc.VarFile)
}
}

0 comments on commit f7fe6ec

Please sign in to comment.