github.com/rothwerx/packer@v0.9.0/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"math/rand"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/mitchellh/cli"
    10  	"github.com/mitchellh/packer/command"
    11  )
    12  
    13  func TestExcludeHelpFunc(t *testing.T) {
    14  	commands := map[string]cli.CommandFactory{
    15  		"build": func() (cli.Command, error) {
    16  			return &command.BuildCommand{
    17  				Meta: command.Meta{},
    18  			}, nil
    19  		},
    20  
    21  		"fix": func() (cli.Command, error) {
    22  			return &command.FixCommand{
    23  				Meta: command.Meta{},
    24  			}, nil
    25  		},
    26  	}
    27  
    28  	helpFunc := excludeHelpFunc(commands, []string{"fix"})
    29  	helpText := helpFunc(commands)
    30  
    31  	if strings.Contains(helpText, "fix") {
    32  		t.Fatalf("Found fix in help text even though we excluded it: \n\n%s\n\n", helpText)
    33  	}
    34  }
    35  
    36  func TestExtractMachineReadable(t *testing.T) {
    37  	var args, expected, result []string
    38  	var mr bool
    39  
    40  	// Not
    41  	args = []string{"foo", "bar", "baz"}
    42  	result, mr = extractMachineReadable(args)
    43  	expected = []string{"foo", "bar", "baz"}
    44  	if !reflect.DeepEqual(result, expected) {
    45  		t.Fatalf("bad: %#v", result)
    46  	}
    47  
    48  	if mr {
    49  		t.Fatal("should not be mr")
    50  	}
    51  
    52  	// Yes
    53  	args = []string{"foo", "-machine-readable", "baz"}
    54  	result, mr = extractMachineReadable(args)
    55  	expected = []string{"foo", "baz"}
    56  	if !reflect.DeepEqual(result, expected) {
    57  		t.Fatalf("bad: %#v", result)
    58  	}
    59  
    60  	if !mr {
    61  		t.Fatal("should be mr")
    62  	}
    63  }
    64  
    65  func TestRandom(t *testing.T) {
    66  	if rand.Intn(9999999) == 8498210 {
    67  		t.Fatal("math.rand is not seeded properly")
    68  	}
    69  }