-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
76 lines (63 loc) · 2.15 KB
/
main_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
73
74
75
76
package main
import (
"os"
"strings"
"testing"
)
func TestParseEnv(t *testing.T) {
input := "VAR1=value1 # First variable\nVAR2=value2 # Second variable\nVAR3=value3"
r := strings.NewReader(input)
vars, descriptions, err := parseEnv(r)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if vars["VAR1"] != "value1" || vars["VAR2"] != "value2" || vars["VAR3"] != "value3" {
t.Errorf("Parsed values do not match expected output: %v", vars)
}
if descriptions["VAR1"] != "First variable" || descriptions["VAR2"] != "Second variable" {
t.Errorf("Parsed descriptions do not match expected output: %v", descriptions)
}
}
func TestParseEnvFromFile(t *testing.T) {
fileContent := "TEST_VAR=hello # A test variable\nSECOND_VAR=world"
filePath := "test.env"
err := os.WriteFile(filePath, []byte(fileContent), 0o644)
if err != nil {
t.Fatalf("Error writing test file: %v", err)
}
defer os.Remove(filePath)
vars, descriptions, err := parseEnvFromFile(filePath)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if vars["TEST_VAR"] != "hello" || vars["SECOND_VAR"] != "world" {
t.Errorf("Parsed values do not match expected output: %v", vars)
}
if descriptions["TEST_VAR"] != "A test variable" {
t.Errorf("Parsed descriptions do not match expected output: %v", descriptions)
}
}
func TestGenerateVariablesTfFile(t *testing.T) {
variables := map[string]string{
"MY_VAR": "",
"ANOTHER_VAR": "",
}
descriptions := map[string]string{
"MY_VAR": "A sample variable",
"ANOTHER_VAR": "Another sample variable",
}
filePath := "test.variables.tf"
err := generateVariablesTfFile(filePath, variables, descriptions)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer os.Remove(filePath)
content, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("Error reading file: %v", err)
}
expected := "variable \"MY_VAR\" {\n description = \"A sample variable\"\n type = string\n}\n\nvariable \"ANOTHER_VAR\" {\n description = \"Another sample variable\"\n type = string\n}\n\n"
if string(content) != expected {
t.Errorf("File content mismatch. Expected:\n%s\nGot:\n%s", expected, string(content))
}
}