github.com/hashicorp/packer@v1.14.3/command/console_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package command 5 6 import ( 7 "fmt" 8 "os" 9 "path/filepath" 10 "strings" 11 "testing" 12 13 "github.com/hashicorp/packer/hcl2template" 14 "github.com/hashicorp/packer/packer" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func Test_console(t *testing.T) { 19 cwd, err := os.Getwd() 20 if err != nil { 21 t.Fatalf("Getwd: %v", err) 22 } 23 24 tc := []struct { 25 piped string 26 command []string 27 env []string 28 expected string 29 }{ 30 {"help", []string{"console"}, nil, packer.ConsoleHelp + "\n"}, 31 {"help", []string{"console", "--config-type=hcl2"}, nil, hcl2template.PackerConsoleHelp + "\n"}, 32 {"help", []string{"console", "--config-type=hcl2", "--use-sequential-evaluation"}, nil, hcl2template.PackerConsoleHelp + "\n"}, 33 {"var.fruit", []string{"console", filepath.Join(testFixture("var-arg"), "fruit_builder.pkr.hcl")}, []string{"PKR_VAR_fruit=potato"}, "potato\n"}, 34 {"upper(var.fruit)", []string{"console", filepath.Join(testFixture("var-arg"), "fruit_builder.pkr.hcl")}, []string{"PKR_VAR_fruit=potato"}, "POTATO\n"}, 35 {"1 + 5", []string{"console", "--config-type=hcl2"}, nil, "6\n"}, 36 {"var.images", []string{"console", filepath.Join(testFixture("var-arg"), "map.pkr.hcl")}, nil, "{\n" + ` "key" = "value"` + "\n}\n"}, 37 {"path.cwd", []string{"console", filepath.Join(testFixture("var-arg"), "map.pkr.hcl")}, nil, strings.ReplaceAll(cwd, `\`, `/`) + "\n"}, 38 {"path.root", []string{"console", filepath.Join(testFixture("var-arg"), "map.pkr.hcl")}, nil, strings.ReplaceAll(testFixture("var-arg"), `\`, `/`) + "\n"}, 39 {"var.list_of_string[0]", []string{"console", `-var=list_of_string=["first"]`, filepath.Join(testFixture("hcl", "variables", "list_of_string"))}, nil, "first\n"}, 40 {"var.untyped[2]", []string{"console", filepath.Join(testFixture("hcl", "variables", "untyped_var"))}, nil, "strings\n"}, 41 {"var.untyped", []string{"console", `-var=untyped=just_a_string`, filepath.Join(testFixture("hcl", "variables", "untyped_var"))}, nil, "just_a_string\n"}, 42 {"var.untyped", []string{"console", filepath.Join(testFixture("hcl", "variables", "untyped_var", "var.pkr.hcl"))}, nil, "<unknown>\n"}, 43 {"var.untyped", []string{"console", filepath.Join(testFixture("hcl", "variables", "untyped_var", "var.pkr.hcl"))}, []string{"PKR_VAR_untyped=just_a_string"}, "just_a_string\n"}, 44 } 45 46 for _, tc := range tc { 47 t.Run(fmt.Sprintf("echo %q | packer %s", tc.piped, tc.command), func(t *testing.T) { 48 p := helperCommand(t, tc.command...) 49 p.Stdin = strings.NewReader(tc.piped) 50 p.Env = append(p.Env, tc.env...) 51 bs, err := p.Output() 52 if err != nil { 53 t.Fatalf("%v: %s", err, bs) 54 } 55 assert.Equal(t, tc.expected, string(bs)) 56 }) 57 } 58 }