github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/command/expansion_test.go (about)

     1  package command
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/evergreen-ci/evergreen/testutil"
     8  	. "github.com/smartystreets/goconvey/convey"
     9  )
    10  
    11  func TestExpansions(t *testing.T) {
    12  
    13  	Convey("With a set of expansions", t, func() {
    14  
    15  		Convey("the expansions object should contain the values it is"+
    16  			" initialized with", func() {
    17  
    18  			expansions := NewExpansions(map[string]string{
    19  				"key1": "val1",
    20  				"key2": "val2",
    21  			})
    22  			So((*expansions)["key1"], ShouldEqual, "val1")
    23  			So((*expansions)["key2"], ShouldEqual, "val2")
    24  
    25  		})
    26  
    27  		Convey("updating the expansions with a map should update all of the"+
    28  			" specified values", func() {
    29  
    30  			expansions := NewExpansions(map[string]string{
    31  				"key1": "val1",
    32  				"key2": "val2",
    33  			})
    34  			expansions.Update(map[string]string{
    35  				"key2": "newval1",
    36  				"key3": "newval2",
    37  			})
    38  
    39  			So((*expansions)["key1"], ShouldEqual, "val1")
    40  			So((*expansions)["key2"], ShouldEqual, "newval1")
    41  			So((*expansions)["key3"], ShouldEqual, "newval2")
    42  
    43  		})
    44  
    45  		Convey("fetching an expansion should return the appropriate value,"+
    46  			" or the empty string if the value is not present", func() {
    47  
    48  			expansions := NewExpansions(map[string]string{
    49  				"key1": "val1",
    50  				"key2": "val2",
    51  			})
    52  			So(expansions.Get("key1"), ShouldEqual, "val1")
    53  			So(expansions.Get("key2"), ShouldEqual, "val2")
    54  			So(expansions.Get("key3"), ShouldEqual, "")
    55  
    56  		})
    57  
    58  		Convey("checking for a key's existence should return whether it is"+
    59  			" specified as an expansion", func() {
    60  
    61  			expansions := NewExpansions(map[string]string{
    62  				"key1": "val1",
    63  				"key2": "val2",
    64  			})
    65  			So(expansions.Exists("key1"), ShouldBeTrue)
    66  			So(expansions.Exists("key2"), ShouldBeTrue)
    67  			So(expansions.Exists("key3"), ShouldBeFalse)
    68  
    69  		})
    70  
    71  		Convey("updating from a yaml file should get keys copied over", func() {
    72  			expansions := NewExpansions(map[string]string{
    73  				"key1": "val1",
    74  				"key2": "val2",
    75  			})
    76  
    77  			err := expansions.UpdateFromYaml(filepath.Join(
    78  				testutil.GetDirectoryOfFile(), "testdata", "expansions.yml"))
    79  			So(err, ShouldBeNil)
    80  			So(expansions.Get("marge"), ShouldEqual, "1")
    81  			So(expansions.Get("lisa"), ShouldEqual, "2")
    82  			So(expansions.Get("bart"), ShouldEqual, "3")
    83  			So(expansions.Get("key1"), ShouldEqual, "blah")
    84  		})
    85  
    86  	})
    87  }
    88  
    89  func TestExpandString(t *testing.T) {
    90  
    91  	Convey("When applying expansions to a command string", t, func() {
    92  
    93  		expansions := NewExpansions(map[string]string{
    94  			"key1": "val1",
    95  			"key2": "val2",
    96  		})
    97  
    98  		Convey("if the string contains no variables to be expanded, it should"+
    99  			" be returned unchanged", func() {
   100  
   101  			toExpand := "hello hello hellohello"
   102  			expanded := "hello hello hellohello"
   103  
   104  			exp, err := expansions.ExpandString("")
   105  			So(err, ShouldBeNil)
   106  			So(exp, ShouldEqual, "")
   107  
   108  			exp, err = expansions.ExpandString(toExpand)
   109  			So(err, ShouldBeNil)
   110  			So(exp, ShouldEqual, expanded)
   111  
   112  		})
   113  
   114  		Convey("if the string contains variables to be expanded", func() {
   115  
   116  			Convey("if the variables occur as keys in the expansions map",
   117  				func() {
   118  
   119  					Convey("they should be replaced with the appropriate"+
   120  						" values from the map", func() {
   121  
   122  						toExpand := "hello hello ${key1} hello${key2}hello" +
   123  							"${key1}"
   124  						expanded := "hello hello val1 helloval2helloval1"
   125  
   126  						exp, err := expansions.ExpandString(toExpand)
   127  						So(err, ShouldBeNil)
   128  						So(exp, ShouldEqual, expanded)
   129  
   130  					})
   131  
   132  				})
   133  
   134  			Convey("if the variables do not occur as keys in the expansions"+
   135  				" map", func() {
   136  
   137  				Convey("any variables with default values provided should be"+
   138  					" replaced with their default value", func() {
   139  
   140  					toExpand := "hello ${key1|blah}${key3|blech}hello " +
   141  						"${key4|}hello"
   142  					expanded := "hello val1blechhello hello"
   143  
   144  					exp, err := expansions.ExpandString(toExpand)
   145  					So(err, ShouldBeNil)
   146  					So(exp, ShouldEqual, expanded)
   147  
   148  				})
   149  
   150  				Convey("any variables without default values provided should"+
   151  					" be replaced with the empty string", func() {
   152  
   153  					toExpand := "hello ${key1|blah}${key3}hello ${key4} " +
   154  						"${key5|blech}hello"
   155  					expanded := "hello val1hello  blechhello"
   156  
   157  					exp, err := expansions.ExpandString(toExpand)
   158  					So(err, ShouldBeNil)
   159  					So(exp, ShouldEqual, expanded)
   160  				})
   161  
   162  			})
   163  
   164  		})
   165  
   166  		Convey("badly formed command strings should cause an error", func() {
   167  
   168  			badStr1 := "hello ${key1|blah}${key3}hello${ ${key4} " +
   169  				"${key5|blech}hello"
   170  			badStr2 := "hello ${hellohello"
   171  
   172  			_, err := expansions.ExpandString(badStr1)
   173  			So(err, ShouldNotBeNil)
   174  
   175  			_, err = expansions.ExpandString(badStr2)
   176  			So(err, ShouldNotBeNil)
   177  		})
   178  
   179  	})
   180  }