-
Notifications
You must be signed in to change notification settings - Fork 33
/
vars_test.go
72 lines (59 loc) · 1.64 KB
/
vars_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package generator
import (
"bufio"
"os"
"testing"
"github.com/alexandrst88/terraform-variables-generator/pkg/utils"
)
var testExtFile = "*.mock"
func TestContainsElement(t *testing.T) {
testSlice := []string{"Terraform", "Puppet", "Ansible"}
if utils.ContainsElement(testSlice, "Chef") {
t.Error("Should return false, but return true")
}
}
func TestGetAllFiles(t *testing.T) {
files, err := utils.GetAllFiles(testExtFile)
utils.CheckError(err)
if len(files) == 0 {
t.Error("Should found at least one file")
}
}
func TestMatchVariable(t *testing.T) {
ter := &terraformVars{}
var messages []string
file, err := utils.GetAllFiles(testExtFile)
utils.CheckError(err)
fileHandle, _ := os.Open(file[0])
defer fileHandle.Close()
fileScanner := bufio.NewScanner(fileHandle)
for fileScanner.Scan() {
messages = append(messages, fileScanner.Text())
}
for _, text := range messages {
ter.matchVarPref(text, varPrefix)
}
if len(ter.Variables) != 5 {
t.Errorf("Should return five variable. but returned %d", len(ter.Variables))
t.Errorf("Variables found: %s", ter.Variables)
}
}
func TestMatchLocal(t *testing.T) {
ter := &terraformVars{}
var messages []string
file, err := utils.GetAllFiles(testExtFile)
utils.CheckError(err)
fileHandle, _ := os.Open(file[0])
defer fileHandle.Close()
fileScanner := bufio.NewScanner(fileHandle)
for fileScanner.Scan() {
messages = append(messages, fileScanner.Text())
}
for _, text := range messages {
ter.matchLocalPref(text, localPrefix)
}
if len(ter.Locals) != 2 {
t.Errorf("Should return two variable. but returned %d", len(ter.Locals))
t.Errorf("Variables found: %s", ter.Locals)
}
}