add a basic test for the inspect command

This commit is contained in:
Adrien Delorme 2020-06-23 15:13:53 +02:00
parent 51d02f8c2d
commit f98576b19e
2 changed files with 58 additions and 0 deletions

View File

@ -85,6 +85,8 @@ func TestHelperProcess(*testing.T) {
switch cmd {
case "console":
os.Exit((&ConsoleCommand{Meta: commandMeta()}).Run(args))
case "inspect":
os.Exit((&InspectCommand{Meta: commandMeta()}).Run(args))
default:
fmt.Fprintf(os.Stderr, "Unknown command %q\n", cmd)
os.Exit(2)

View File

@ -1 +1,57 @@
package command
import (
"fmt"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
)
func Test_commands(t *testing.T) {
tc := []struct {
command []string
env []string
expected string
}{
{[]string{"inspect", "-var=fruit=banana", filepath.Join(testFixture("var-arg"), "fruit_builder.pkr.hcl")}, nil, `Packer Inspect: HCL2 mode
> input-variables:
var.fruit: "banana" [debug: {Type:cty.String,CmdValue:banana,VarfileValue:null,EnvValue:null,DefaultValue:null}]
> local-variables:
local.fruit: "banana"
> builds:
> <unnamed build 0>:
provisioners:
shell-local
post-processors:
<no post-processor>
`},
}
for _, tc := range tc {
t.Run(fmt.Sprintf("packer %s", tc.command), func(t *testing.T) {
p := helperCommand(t, tc.command...)
p.Env = append(p.Env, tc.env...)
bs, err := p.Output()
if err != nil {
t.Fatalf("%v: %s", err, bs)
}
actual := string(bs)
if diff := cmp.Diff(tc.expected, actual); diff != "" {
t.Fatalf("unexpected ouput %s", diff)
}
})
}
}