github.com/nektos/act@v0.2.63/pkg/exprparser/functions_test.go (about)

     1  package exprparser
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/nektos/act/pkg/model"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestFunctionContains(t *testing.T) {
    12  	table := []struct {
    13  		input    string
    14  		expected interface{}
    15  		name     string
    16  	}{
    17  		{"contains('search', 'item') }}", false, "contains-str-str"},
    18  		{`cOnTaInS('Hello', 'll') }}`, true, "contains-str-casing"},
    19  		{`contains('HELLO', 'll') }}`, true, "contains-str-casing"},
    20  		{`contains('3.141592', 3.14) }}`, true, "contains-str-number"},
    21  		{`contains(3.141592, '3.14') }}`, true, "contains-number-str"},
    22  		{`contains(3.141592, 3.14) }}`, true, "contains-number-number"},
    23  		{`contains(true, 'u') }}`, true, "contains-bool-str"},
    24  		{`contains(null, '') }}`, true, "contains-null-str"},
    25  		{`contains(fromJSON('["first","second"]'), 'first') }}`, true, "contains-item"},
    26  		{`contains(fromJSON('[null,"second"]'), '') }}`, true, "contains-item-null-empty-str"},
    27  		{`contains(fromJSON('["","second"]'), null) }}`, true, "contains-item-empty-str-null"},
    28  		{`contains(fromJSON('[true,"second"]'), 'true') }}`, false, "contains-item-bool-arr"},
    29  		{`contains(fromJSON('["true","second"]'), true) }}`, false, "contains-item-str-bool"},
    30  		{`contains(fromJSON('[3.14,"second"]'), '3.14') }}`, true, "contains-item-number-str"},
    31  		{`contains(fromJSON('[3.14,"second"]'), 3.14) }}`, true, "contains-item-number-number"},
    32  		{`contains(fromJSON('["","second"]'), fromJSON('[]')) }}`, false, "contains-item-str-arr"},
    33  		{`contains(fromJSON('["","second"]'), fromJSON('{}')) }}`, false, "contains-item-str-obj"},
    34  	}
    35  
    36  	env := &EvaluationEnvironment{}
    37  
    38  	for _, tt := range table {
    39  		t.Run(tt.name, func(t *testing.T) {
    40  			output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
    41  			assert.Nil(t, err)
    42  
    43  			assert.Equal(t, tt.expected, output)
    44  		})
    45  	}
    46  }
    47  
    48  func TestFunctionStartsWith(t *testing.T) {
    49  	table := []struct {
    50  		input    string
    51  		expected interface{}
    52  		name     string
    53  	}{
    54  		{"startsWith('search', 'se') }}", true, "startswith-string"},
    55  		{"startsWith('search', 'sa') }}", false, "startswith-string"},
    56  		{"startsWith('123search', '123s') }}", true, "startswith-string"},
    57  		{"startsWith(123, 's') }}", false, "startswith-string"},
    58  		{"startsWith(123, '12') }}", true, "startswith-string"},
    59  		{"startsWith('123', 12) }}", true, "startswith-string"},
    60  		{"startsWith(null, '42') }}", false, "startswith-string"},
    61  		{"startsWith('null', null) }}", true, "startswith-string"},
    62  		{"startsWith('null', '') }}", true, "startswith-string"},
    63  	}
    64  
    65  	env := &EvaluationEnvironment{}
    66  
    67  	for _, tt := range table {
    68  		t.Run(tt.name, func(t *testing.T) {
    69  			output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
    70  			assert.Nil(t, err)
    71  
    72  			assert.Equal(t, tt.expected, output)
    73  		})
    74  	}
    75  }
    76  
    77  func TestFunctionEndsWith(t *testing.T) {
    78  	table := []struct {
    79  		input    string
    80  		expected interface{}
    81  		name     string
    82  	}{
    83  		{"endsWith('search', 'ch') }}", true, "endsWith-string"},
    84  		{"endsWith('search', 'sa') }}", false, "endsWith-string"},
    85  		{"endsWith('search123s', '123s') }}", true, "endsWith-string"},
    86  		{"endsWith(123, 's') }}", false, "endsWith-string"},
    87  		{"endsWith(123, '23') }}", true, "endsWith-string"},
    88  		{"endsWith('123', 23) }}", true, "endsWith-string"},
    89  		{"endsWith(null, '42') }}", false, "endsWith-string"},
    90  		{"endsWith('null', null) }}", true, "endsWith-string"},
    91  		{"endsWith('null', '') }}", true, "endsWith-string"},
    92  	}
    93  
    94  	env := &EvaluationEnvironment{}
    95  
    96  	for _, tt := range table {
    97  		t.Run(tt.name, func(t *testing.T) {
    98  			output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
    99  			assert.Nil(t, err)
   100  
   101  			assert.Equal(t, tt.expected, output)
   102  		})
   103  	}
   104  }
   105  
   106  func TestFunctionJoin(t *testing.T) {
   107  	table := []struct {
   108  		input    string
   109  		expected interface{}
   110  		name     string
   111  	}{
   112  		{"join(fromJSON('[\"a\", \"b\"]'), ',')", "a,b", "join-arr"},
   113  		{"join('string', ',')", "string", "join-str"},
   114  		{"join(1, ',')", "1", "join-number"},
   115  		{"join(null, ',')", "", "join-number"},
   116  		{"join(fromJSON('[\"a\", \"b\", null]'), null)", "ab", "join-number"},
   117  		{"join(fromJSON('[\"a\", \"b\"]'))", "a,b", "join-number"},
   118  		{"join(fromJSON('[\"a\", \"b\", null]'), 1)", "a1b1", "join-number"},
   119  	}
   120  
   121  	env := &EvaluationEnvironment{}
   122  
   123  	for _, tt := range table {
   124  		t.Run(tt.name, func(t *testing.T) {
   125  			output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
   126  			assert.Nil(t, err)
   127  
   128  			assert.Equal(t, tt.expected, output)
   129  		})
   130  	}
   131  }
   132  
   133  func TestFunctionToJSON(t *testing.T) {
   134  	table := []struct {
   135  		input    string
   136  		expected interface{}
   137  		name     string
   138  	}{
   139  		{"toJSON(env) }}", "{\n  \"key\": \"value\"\n}", "toJSON"},
   140  		{"toJSON(null)", "null", "toJSON-null"},
   141  	}
   142  
   143  	env := &EvaluationEnvironment{
   144  		Env: map[string]string{
   145  			"key": "value",
   146  		},
   147  	}
   148  
   149  	for _, tt := range table {
   150  		t.Run(tt.name, func(t *testing.T) {
   151  			output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
   152  			assert.Nil(t, err)
   153  
   154  			assert.Equal(t, tt.expected, output)
   155  		})
   156  	}
   157  }
   158  
   159  func TestFunctionFromJSON(t *testing.T) {
   160  	table := []struct {
   161  		input    string
   162  		expected interface{}
   163  		name     string
   164  	}{
   165  		{"fromJSON('{\"foo\":\"bar\"}') }}", map[string]interface{}{
   166  			"foo": "bar",
   167  		}, "fromJSON"},
   168  	}
   169  
   170  	env := &EvaluationEnvironment{}
   171  
   172  	for _, tt := range table {
   173  		t.Run(tt.name, func(t *testing.T) {
   174  			output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
   175  			assert.Nil(t, err)
   176  
   177  			assert.Equal(t, tt.expected, output)
   178  		})
   179  	}
   180  }
   181  
   182  func TestFunctionHashFiles(t *testing.T) {
   183  	table := []struct {
   184  		input    string
   185  		expected interface{}
   186  		name     string
   187  	}{
   188  		{"hashFiles('**/non-extant-files') }}", "", "hash-non-existing-file"},
   189  		{"hashFiles('**/non-extant-files', '**/more-non-extant-files') }}", "", "hash-multiple-non-existing-files"},
   190  		{"hashFiles('./for-hashing-1.txt') }}", "66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18", "hash-single-file"},
   191  		{"hashFiles('./for-hashing-*.txt') }}", "8e5935e7e13368cd9688fe8f48a0955293676a021562582c7e848dafe13fb046", "hash-multiple-files"},
   192  		{"hashFiles('./for-hashing-*.txt', '!./for-hashing-2.txt') }}", "66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18", "hash-negative-pattern"},
   193  		{"hashFiles('./for-hashing-**') }}", "c418ba693753c84115ced0da77f876cddc662b9054f4b129b90f822597ee2f94", "hash-multiple-files-and-directories"},
   194  		{"hashFiles('./for-hashing-3/**') }}", "6f5696b546a7a9d6d42a449dc9a56bef244aaa826601ef27466168846139d2c2", "hash-nested-directories"},
   195  		{"hashFiles('./for-hashing-3/**/nested-data.txt') }}", "8ecadfb49f7f978d0a9f3a957e9c8da6cc9ab871f5203b5d9f9d1dc87d8af18c", "hash-nested-directories-2"},
   196  	}
   197  
   198  	env := &EvaluationEnvironment{}
   199  
   200  	for _, tt := range table {
   201  		t.Run(tt.name, func(t *testing.T) {
   202  			workdir, err := filepath.Abs("testdata")
   203  			assert.Nil(t, err)
   204  			output, err := NewInterpeter(env, Config{WorkingDir: workdir}).Evaluate(tt.input, DefaultStatusCheckNone)
   205  			assert.Nil(t, err)
   206  
   207  			assert.Equal(t, tt.expected, output)
   208  		})
   209  	}
   210  }
   211  
   212  func TestFunctionFormat(t *testing.T) {
   213  	table := []struct {
   214  		input    string
   215  		expected interface{}
   216  		error    interface{}
   217  		name     string
   218  	}{
   219  		{"format('text')", "text", nil, "format-plain-string"},
   220  		{"format('Hello {0} {1} {2}!', 'Mona', 'the', 'Octocat')", "Hello Mona the Octocat!", nil, "format-with-placeholders"},
   221  		{"format('{{Hello {0} {1} {2}!}}', 'Mona', 'the', 'Octocat')", "{Hello Mona the Octocat!}", nil, "format-with-escaped-braces"},
   222  		{"format('{{0}}', 'test')", "{0}", nil, "format-with-escaped-braces"},
   223  		{"format('{{{0}}}', 'test')", "{test}", nil, "format-with-escaped-braces-and-value"},
   224  		{"format('}}')", "}", nil, "format-output-closing-brace"},
   225  		{`format('Hello "{0}" {1} {2} {3} {4}', null, true, -3.14, NaN, Infinity)`, `Hello "" true -3.14 NaN Infinity`, nil, "format-with-primitives"},
   226  		{`format('Hello "{0}" {1} {2}', fromJSON('[0, true, "abc"]'), fromJSON('[{"a":1}]'), fromJSON('{"a":{"b":1}}'))`, `Hello "Array" Array Object`, nil, "format-with-complex-types"},
   227  		{"format(true)", "true", nil, "format-with-primitive-args"},
   228  		{"format('echo Hello {0} ${{Test}}', github.undefined_property)", "echo Hello  ${Test}", nil, "format-with-undefined-value"},
   229  		{"format('{0}}', '{1}', 'World')", nil, "Closing bracket without opening one. The following format string is invalid: '{0}}'", "format-invalid-format-string"},
   230  		{"format('{0', '{1}', 'World')", nil, "Unclosed brackets. The following format string is invalid: '{0'", "format-invalid-format-string"},
   231  		{"format('{2}', '{1}', 'World')", "", "The following format string references more arguments than were supplied: '{2}'", "format-invalid-replacement-reference"},
   232  		{"format('{2147483648}')", "", "The following format string is invalid: '{2147483648}'", "format-invalid-replacement-reference"},
   233  		{"format('{0} {1} {2} {3}', 1.0, 1.1, 1234567890.0, 12345678901234567890.0)", "1 1.1 1234567890 1.23456789012346E+19", nil, "format-floats"},
   234  	}
   235  
   236  	env := &EvaluationEnvironment{
   237  		Github: &model.GithubContext{},
   238  	}
   239  
   240  	for _, tt := range table {
   241  		t.Run(tt.name, func(t *testing.T) {
   242  			output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
   243  			if tt.error != nil {
   244  				assert.Equal(t, tt.error, err.Error())
   245  			} else {
   246  				assert.Nil(t, err)
   247  				assert.Equal(t, tt.expected, output)
   248  			}
   249  		})
   250  	}
   251  }