github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/v7/v7_suite_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strings"
     7  
     8  	uuid "github.com/nu7hatch/gouuid"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	log "github.com/sirupsen/logrus"
    12  
    13  	"testing"
    14  )
    15  
    16  func TestV3(t *testing.T) {
    17  	RegisterFailHandler(Fail)
    18  	RunSpecs(t, "V7 Command Suite")
    19  }
    20  
    21  var _ = BeforeEach(func() {
    22  	log.SetLevel(log.PanicLevel)
    23  })
    24  
    25  // RandomString provides a random string
    26  func RandomString(prefix string) string {
    27  	guid, err := uuid.NewV4()
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  
    32  	return prefix + "-" + guid.String()
    33  }
    34  
    35  func setFlag(cmd interface{}, flag string, values ...interface{}) {
    36  	var value reflect.Value
    37  	switch len(values) {
    38  	case 0:
    39  		value = reflect.ValueOf(true)
    40  	case 1:
    41  		value = reflect.ValueOf(values[0])
    42  	default:
    43  		Fail(fmt.Sprintf("cannot take more than one value for flag '%s'", flag))
    44  	}
    45  
    46  	var key, trimmedFlag string
    47  	switch {
    48  	case strings.HasPrefix(flag, "--"):
    49  		trimmedFlag = strings.TrimPrefix(flag, "--")
    50  		key = "long"
    51  	case strings.HasPrefix(flag, "-"):
    52  		trimmedFlag = strings.TrimPrefix(flag, "-")
    53  		key = "short"
    54  	default:
    55  		Fail("flag must start with prefix '--' or '-'")
    56  	}
    57  
    58  	ptr := reflect.ValueOf(cmd)
    59  	if ptr.Kind() != reflect.Ptr {
    60  		Fail("need to pass a pointer to the command struct")
    61  	}
    62  
    63  	val := ptr.Elem()
    64  	if val.Kind() != reflect.Struct {
    65  		Fail("need to pass a command struct")
    66  	}
    67  
    68  	typ := val.Type()
    69  	for i := 0; i < typ.NumField(); i++ {
    70  		field := typ.Field(i)
    71  
    72  		if tagValue, ok := field.Tag.Lookup(key); ok {
    73  			if tagValue == trimmedFlag {
    74  				if value.Type().ConvertibleTo(field.Type) {
    75  					val.Field(i).Set(value.Convert(field.Type))
    76  					return
    77  				}
    78  				Fail(fmt.Sprintf(
    79  					"Could not set field '%s' type '%s' to '%v' type '%s' for flag '%s'",
    80  					field.Name,
    81  					field.Type,
    82  					value.Interface(),
    83  					value.Type(),
    84  					flag,
    85  				))
    86  			}
    87  		}
    88  	}
    89  
    90  	Fail(fmt.Sprintf("could not find flag '%s' in command struct", flag))
    91  }
    92  
    93  func setPositionalFlags(cmd interface{}, values ...interface{}) {
    94  	ptr := reflect.ValueOf(cmd)
    95  	if ptr.Kind() != reflect.Ptr {
    96  		Fail("need to pass a pointer to the command struct")
    97  	}
    98  
    99  	val := ptr.Elem()
   100  	if val.Kind() != reflect.Struct {
   101  		Fail("need to pass a command struct")
   102  	}
   103  
   104  	typ := val.Type()
   105  	for i := 0; i < typ.NumField(); i++ {
   106  		field := typ.Field(i)
   107  
   108  		if tagValue, ok := field.Tag.Lookup("positional-args"); ok && tagValue == "yes" && field.Type.Kind() == reflect.Struct {
   109  			if len(values) != field.Type.NumField() {
   110  				Fail(fmt.Sprintf("%d values provided but positional args struct %s has %d fields", len(values), field.Name, field.Type.NumField()))
   111  			}
   112  
   113  			for j := 0; j < field.Type.NumField(); j++ {
   114  				posField := field.Type.Field(j)
   115  				value := reflect.ValueOf(values[j])
   116  				if value.Type().ConvertibleTo(posField.Type) {
   117  					val.Field(i).Field(j).Set(value.Convert(posField.Type))
   118  				} else {
   119  					Fail(fmt.Sprintf(
   120  						"Could not set field '%s' type '%s' to '%v' type '%s'",
   121  						posField.Name,
   122  						posField.Type,
   123  						value.Interface(),
   124  						value.Type(),
   125  					))
   126  				}
   127  			}
   128  
   129  			return
   130  		}
   131  	}
   132  
   133  	Fail(`Did not find a field with 'positional-args:"yes"' in the struct`)
   134  }