...

Source file src/github.com/drone/envsubst/v2/eval_test.go

Documentation: github.com/drone/envsubst/v2

     1  package envsubst
     2  
     3  import "testing"
     4  
     5  // test cases sourced from tldp.org
     6  // http://www.tldp.org/LDP/abs/html/parameter-substitution.html
     7  
     8  func TestExpand(t *testing.T) {
     9  	var expressions = []struct {
    10  		params map[string]string
    11  		input  string
    12  		output string
    13  	}{
    14  		// text-only
    15  		{
    16  			params: map[string]string{},
    17  			input:  "abcdEFGH28ij",
    18  			output: "abcdEFGH28ij",
    19  		},
    20  		// length
    21  		{
    22  			params: map[string]string{"var01": "abcdEFGH28ij"},
    23  			input:  "${#var01}",
    24  			output: "12",
    25  		},
    26  		// uppercase first
    27  		{
    28  			params: map[string]string{"var01": "abcdEFGH28ij"},
    29  			input:  "${var01^}",
    30  			output: "AbcdEFGH28ij",
    31  		},
    32  		// uppercase
    33  		{
    34  			params: map[string]string{"var01": "abcdEFGH28ij"},
    35  			input:  "${var01^^}",
    36  			output: "ABCDEFGH28IJ",
    37  		},
    38  		// lowercase first
    39  		{
    40  			params: map[string]string{"var01": "ABCDEFGH28IJ"},
    41  			input:  "${var01,}",
    42  			output: "aBCDEFGH28IJ",
    43  		},
    44  		// lowercase
    45  		{
    46  			params: map[string]string{"var01": "ABCDEFGH28IJ"},
    47  			input:  "${var01,,}",
    48  			output: "abcdefgh28ij",
    49  		},
    50  		// substring with position
    51  		{
    52  			params: map[string]string{"path_name": "/home/bozo/ideas/thoughts.for.today"},
    53  			input:  "${path_name:11}",
    54  			output: "ideas/thoughts.for.today",
    55  		},
    56  		// substring with position and length
    57  		{
    58  			params: map[string]string{"path_name": "/home/bozo/ideas/thoughts.for.today"},
    59  			input:  "${path_name:11:5}",
    60  			output: "ideas",
    61  		},
    62  		// default not used
    63  		{
    64  			params: map[string]string{"var": "abc"},
    65  			input:  "${var=xyz}",
    66  			output: "abc",
    67  		},
    68  		// default used
    69  		{
    70  			params: map[string]string{},
    71  			input:  "${var=xyz}",
    72  			output: "xyz",
    73  		},
    74  		{
    75  			params: map[string]string{"default_var": "foo"},
    76  			input:  "something ${var=${default_var}}",
    77  			output: "something foo",
    78  		},
    79  		{
    80  			params: map[string]string{"default_var": "foo1"},
    81  			input:  `foo: ${var=${default_var}-suffix}`,
    82  			output: "foo: foo1-suffix",
    83  		},
    84  		{
    85  			params: map[string]string{"default_var": "foo1"},
    86  			input:  `foo: ${var=prefix${default_var}-suffix}`,
    87  			output: "foo: prefixfoo1-suffix",
    88  		},
    89  		{
    90  			params: map[string]string{},
    91  			input:  "${var:=xyz}",
    92  			output: "xyz",
    93  		},
    94  		// replace suffix
    95  		{
    96  			params: map[string]string{"stringZ": "abcABC123ABCabc"},
    97  			input:  "${stringZ/%abc/XYZ}",
    98  			output: "abcABC123ABCXYZ",
    99  		},
   100  		// replace prefix
   101  		{
   102  			params: map[string]string{"stringZ": "abcABC123ABCabc"},
   103  			input:  "${stringZ/#abc/XYZ}",
   104  			output: "XYZABC123ABCabc",
   105  		},
   106  		// replace all
   107  		{
   108  			params: map[string]string{"stringZ": "abcABC123ABCabc"},
   109  			input:  "${stringZ//abc/xyz}",
   110  			output: "xyzABC123ABCxyz",
   111  		},
   112  		// replace first
   113  		{
   114  			params: map[string]string{"stringZ": "abcABC123ABCabc"},
   115  			input:  "${stringZ/abc/xyz}",
   116  			output: "xyzABC123ABCabc",
   117  		},
   118  		// delete shortest match prefix
   119  		{
   120  			params: map[string]string{"filename": "bash.string.txt"},
   121  			input:  "${filename#*.}",
   122  			output: "string.txt",
   123  		},
   124  		{
   125  			params: map[string]string{"filename": "path/to/file"},
   126  			input:  "${filename#*/}",
   127  			output: "to/file",
   128  		},
   129  		{
   130  			params: map[string]string{"filename": "/path/to/file"},
   131  			input:  "${filename#*/}",
   132  			output: "path/to/file",
   133  		},
   134  		// delete longest match prefix
   135  		{
   136  			params: map[string]string{"filename": "bash.string.txt"},
   137  			input:  "${filename##*.}",
   138  			output: "txt",
   139  		},
   140  		{
   141  			params: map[string]string{"filename": "path/to/file"},
   142  			input:  "${filename##*/}",
   143  			output: "file",
   144  		},
   145  		{
   146  			params: map[string]string{"filename": "/path/to/file"},
   147  			input:  "${filename##*/}",
   148  			output: "file",
   149  		},
   150  		// delete shortest match suffix
   151  		{
   152  			params: map[string]string{"filename": "bash.string.txt"},
   153  			input:  "${filename%.*}",
   154  			output: "bash.string",
   155  		},
   156  		// delete longest match suffix
   157  		{
   158  			params: map[string]string{"filename": "bash.string.txt"},
   159  			input:  "${filename%%.*}",
   160  			output: "bash",
   161  		},
   162  
   163  		// nested parameters
   164  		{
   165  			params: map[string]string{"var01": "abcdEFGH28ij"},
   166  			input:  "${var=${var01^^}}",
   167  			output: "ABCDEFGH28IJ",
   168  		},
   169  		// escaped
   170  		{
   171  			params: map[string]string{"var01": "abcdEFGH28ij"},
   172  			input:  "$${var01}",
   173  			output: "${var01}",
   174  		},
   175  		{
   176  			params: map[string]string{"var01": "abcdEFGH28ij"},
   177  			input:  "some text ${var01}$${var$${var01}$var01${var01}",
   178  			output: "some text abcdEFGH28ij${var${var01}$var01abcdEFGH28ij",
   179  		},
   180  		{
   181  			params: map[string]string{"default_var": "foo"},
   182  			input:  "something $${var=${default_var}}",
   183  			output: "something ${var=foo}",
   184  		},
   185  		// some common escaping use cases
   186  		{
   187  			params: map[string]string{"stringZ": "foo/bar"},
   188  			input:  `${stringZ/\//-}`,
   189  			output: "foo-bar",
   190  		},
   191  		{
   192  			params: map[string]string{"stringZ": "foo/bar/baz"},
   193  			input:  `${stringZ//\//-}`,
   194  			output: "foo-bar-baz",
   195  		},
   196  		// escape outside of expansion shouldn't be processed
   197  		{
   198  			params: map[string]string{"default_var": "foo"},
   199  			input:  "\\\\something ${var=${default_var}}",
   200  			output: "\\\\something foo",
   201  		},
   202  		// substitute with a blank string
   203  		{
   204  			params: map[string]string{"stringZ": "foo.bar"},
   205  			input:  `${stringZ/./}`,
   206  			output: "foobar",
   207  		},
   208  	}
   209  
   210  	for _, expr := range expressions {
   211  		t.Run(expr.input, func(t *testing.T) {
   212  			t.Logf(expr.input)
   213  			output, err := Eval(expr.input, func(s string) string {
   214  				return expr.params[s]
   215  			})
   216  			if err != nil {
   217  				t.Errorf("Want %q expanded but got error %q", expr.input, err)
   218  			}
   219  
   220  			if output != expr.output {
   221  				t.Errorf("Want %q expanded to %q, got %q",
   222  					expr.input,
   223  					expr.output,
   224  					output)
   225  			}
   226  		})
   227  	}
   228  }
   229  

View as plain text