2013-07-30 19:05:20 -04:00
|
|
|
package main
|
2013-08-12 02:12:20 -04:00
|
|
|
|
|
|
|
import (
|
2015-10-11 15:35:13 -04:00
|
|
|
"math/rand"
|
2013-08-12 02:12:20 -04:00
|
|
|
"reflect"
|
2015-08-20 19:43:30 -04:00
|
|
|
"strings"
|
2013-08-12 02:12:20 -04:00
|
|
|
"testing"
|
2015-08-20 19:43:30 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/mitchellh/packer/command"
|
2013-08-12 02:12:20 -04:00
|
|
|
)
|
|
|
|
|
2015-08-20 19:43:30 -04:00
|
|
|
func TestExcludeHelpFunc(t *testing.T) {
|
|
|
|
commands := map[string]cli.CommandFactory{
|
|
|
|
"build": func() (cli.Command, error) {
|
|
|
|
return &command.BuildCommand{
|
|
|
|
Meta: command.Meta{},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
|
|
|
|
"fix": func() (cli.Command, error) {
|
|
|
|
return &command.FixCommand{
|
|
|
|
Meta: command.Meta{},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
helpFunc := excludeHelpFunc(commands, []string{"fix"})
|
|
|
|
helpText := helpFunc(commands)
|
|
|
|
|
|
|
|
if strings.Contains(helpText, "fix") {
|
2015-10-12 22:14:01 -04:00
|
|
|
t.Fatalf("Found fix in help text even though we excluded it: \n\n%s\n\n", helpText)
|
2015-08-20 19:43:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 02:12:20 -04:00
|
|
|
func TestExtractMachineReadable(t *testing.T) {
|
|
|
|
var args, expected, result []string
|
|
|
|
var mr bool
|
|
|
|
|
|
|
|
// Not
|
|
|
|
args = []string{"foo", "bar", "baz"}
|
|
|
|
result, mr = extractMachineReadable(args)
|
|
|
|
expected = []string{"foo", "bar", "baz"}
|
|
|
|
if !reflect.DeepEqual(result, expected) {
|
|
|
|
t.Fatalf("bad: %#v", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
if mr {
|
|
|
|
t.Fatal("should not be mr")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Yes
|
2013-08-12 12:19:24 -04:00
|
|
|
args = []string{"foo", "-machine-readable", "baz"}
|
2013-08-12 02:12:20 -04:00
|
|
|
result, mr = extractMachineReadable(args)
|
|
|
|
expected = []string{"foo", "baz"}
|
|
|
|
if !reflect.DeepEqual(result, expected) {
|
|
|
|
t.Fatalf("bad: %#v", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mr {
|
|
|
|
t.Fatal("should be mr")
|
|
|
|
}
|
|
|
|
}
|
2015-10-11 15:35:13 -04:00
|
|
|
|
|
|
|
func TestRandom(t *testing.T) {
|
|
|
|
if rand.Intn(9999999) == 8498210 {
|
|
|
|
t.Fatal("math.rand is not seeded properly")
|
|
|
|
}
|
|
|
|
}
|