github.com/hashicorp/packer@v1.14.3/main_test.go (about)

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