Skip to content

Commit b8f480e

Browse files
committed
Add tests for TF_ADD_MODULE_PATH.
1 parent 6319446 commit b8f480e

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

parser_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,56 @@ package main
33
import (
44
"bytes"
55
"encoding/json"
6+
"os"
67
"strings"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
1011
)
1112

13+
const exampleStateFileEnvModulePath = `
14+
{
15+
"version": 1,
16+
"serial": 1,
17+
"modules": [
18+
{
19+
"path": [
20+
"root",
21+
"web"
22+
],
23+
"resources": {
24+
"libvirt_domain.fourteen": {
25+
"type": "libvirt_domain",
26+
"primary": {
27+
"id": "824c29be-2164-44c8-83e0-787705571d95",
28+
"attributes": {
29+
"name": "fourteen",
30+
"network_interface.#": "1",
31+
"network_interface.0.addresses.#": "1",
32+
"network_interface.0.addresses.0": "192.168.102.14",
33+
"network_interface.0.mac": "96:EE:4D:BD:B2:45"
34+
}
35+
}
36+
}
37+
}
38+
}
39+
]
40+
}`
41+
42+
const expectedListOutputEnvModulePath = `
43+
{
44+
"all": {
45+
"hosts": [
46+
"192.168.102.14"
47+
],
48+
"vars": {
49+
}
50+
},
51+
"root.web.fourteen": ["192.168.102.14"],
52+
"root.web.fourteen.0": ["192.168.102.14"],
53+
"type_libvirt_domain": ["192.168.102.14"]
54+
}`
55+
1256
const exampleStateFile = `
1357
{
1458
"version": 1,
@@ -665,6 +709,33 @@ func TestListCommand(t *testing.T) {
665709
assert.Equal(t, exp, act)
666710
}
667711

712+
func TestListCommandEnvModulePath(t *testing.T) {
713+
var s state
714+
r := strings.NewReader(exampleStateFileEnvModulePath)
715+
err := s.read(r)
716+
assert.NoError(t, err)
717+
718+
// Decode expectation as JSON
719+
var exp interface{}
720+
err = json.Unmarshal([]byte(expectedListOutputEnvModulePath), &exp)
721+
assert.NoError(t, err)
722+
723+
// Run the command, capture the output
724+
var stdout, stderr bytes.Buffer
725+
os.Setenv("TF_ADD_MODULE_PATH", "true")
726+
exitCode := cmdList(&stdout, &stderr, &s)
727+
os.Unsetenv("TF_ADD_MODULE_PATH")
728+
assert.Equal(t, 0, exitCode)
729+
assert.Equal(t, "", stderr.String())
730+
731+
// Decode the output to compare
732+
var act interface{}
733+
err = json.Unmarshal([]byte(stdout.String()), &act)
734+
assert.NoError(t, err)
735+
736+
assert.Equal(t, exp, act)
737+
}
738+
668739
func TestHostCommand(t *testing.T) {
669740
var s state
670741
r := strings.NewReader(exampleStateFile)

0 commit comments

Comments
 (0)