github.com/nektos/act@v0.2.63/pkg/workflowpattern/workflow_pattern_test.go (about)

     1  package workflowpattern
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestMatchPattern(t *testing.T) {
    11  	kases := []struct {
    12  		inputs       []string
    13  		patterns     []string
    14  		skipResult   bool
    15  		filterResult bool
    16  	}{
    17  		{
    18  			patterns:     []string{"*"},
    19  			inputs:       []string{"path/with/slash"},
    20  			skipResult:   true,
    21  			filterResult: false,
    22  		},
    23  		{
    24  			patterns:     []string{"path/a", "path/b", "path/c"},
    25  			inputs:       []string{"meta", "path/b", "otherfile"},
    26  			skipResult:   false,
    27  			filterResult: false,
    28  		},
    29  		{
    30  			patterns:     []string{"path/a", "path/b", "path/c"},
    31  			inputs:       []string{"path/b"},
    32  			skipResult:   false,
    33  			filterResult: true,
    34  		},
    35  		{
    36  			patterns:     []string{"path/a", "path/b", "path/c"},
    37  			inputs:       []string{"path/c", "path/b"},
    38  			skipResult:   false,
    39  			filterResult: true,
    40  		},
    41  		{
    42  			patterns:     []string{"path/a", "path/b", "path/c"},
    43  			inputs:       []string{"path/c", "path/b", "path/a"},
    44  			skipResult:   false,
    45  			filterResult: true,
    46  		},
    47  		{
    48  			patterns:     []string{"path/a", "path/b", "path/c"},
    49  			inputs:       []string{"path/c", "path/b", "path/d", "path/a"},
    50  			skipResult:   false,
    51  			filterResult: false,
    52  		},
    53  		{
    54  			patterns:     []string{},
    55  			inputs:       []string{},
    56  			skipResult:   false,
    57  			filterResult: false,
    58  		},
    59  		{
    60  			patterns:     []string{"\\!file"},
    61  			inputs:       []string{"!file"},
    62  			skipResult:   false,
    63  			filterResult: true,
    64  		},
    65  		{
    66  			patterns:     []string{"escape\\\\backslash"},
    67  			inputs:       []string{"escape\\backslash"},
    68  			skipResult:   false,
    69  			filterResult: true,
    70  		},
    71  		{
    72  			patterns:     []string{".yml"},
    73  			inputs:       []string{"fyml"},
    74  			skipResult:   true,
    75  			filterResult: false,
    76  		},
    77  		// https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#patterns-to-match-branches-and-tags
    78  		{
    79  			patterns:     []string{"feature/*"},
    80  			inputs:       []string{"feature/my-branch"},
    81  			skipResult:   false,
    82  			filterResult: true,
    83  		},
    84  		{
    85  			patterns:     []string{"feature/*"},
    86  			inputs:       []string{"feature/your-branch"},
    87  			skipResult:   false,
    88  			filterResult: true,
    89  		},
    90  		{
    91  			patterns:     []string{"feature/**"},
    92  			inputs:       []string{"feature/beta-a/my-branch"},
    93  			skipResult:   false,
    94  			filterResult: true,
    95  		},
    96  		{
    97  			patterns:     []string{"feature/**"},
    98  			inputs:       []string{"feature/beta-a/my-branch"},
    99  			skipResult:   false,
   100  			filterResult: true,
   101  		},
   102  		{
   103  			patterns:     []string{"feature/**"},
   104  			inputs:       []string{"feature/mona/the/octocat"},
   105  			skipResult:   false,
   106  			filterResult: true,
   107  		},
   108  		{
   109  			patterns:     []string{"main", "releases/mona-the-octocat"},
   110  			inputs:       []string{"main"},
   111  			skipResult:   false,
   112  			filterResult: true,
   113  		},
   114  		{
   115  			patterns:     []string{"main", "releases/mona-the-octocat"},
   116  			inputs:       []string{"releases/mona-the-octocat"},
   117  			skipResult:   false,
   118  			filterResult: true,
   119  		},
   120  		{
   121  			patterns:     []string{"*"},
   122  			inputs:       []string{"main"},
   123  			skipResult:   false,
   124  			filterResult: true,
   125  		},
   126  		{
   127  			patterns:     []string{"*"},
   128  			inputs:       []string{"releases"},
   129  			skipResult:   false,
   130  			filterResult: true,
   131  		},
   132  		{
   133  			patterns:     []string{"**"},
   134  			inputs:       []string{"all/the/branches"},
   135  			skipResult:   false,
   136  			filterResult: true,
   137  		},
   138  		{
   139  			patterns:     []string{"**"},
   140  			inputs:       []string{"every/tag"},
   141  			skipResult:   false,
   142  			filterResult: true,
   143  		},
   144  		{
   145  			patterns:     []string{"*feature"},
   146  			inputs:       []string{"mona-feature"},
   147  			skipResult:   false,
   148  			filterResult: true,
   149  		},
   150  		{
   151  			patterns:     []string{"*feature"},
   152  			inputs:       []string{"feature"},
   153  			skipResult:   false,
   154  			filterResult: true,
   155  		},
   156  		{
   157  			patterns:     []string{"*feature"},
   158  			inputs:       []string{"ver-10-feature"},
   159  			skipResult:   false,
   160  			filterResult: true,
   161  		},
   162  		{
   163  			patterns:     []string{"v2*"},
   164  			inputs:       []string{"v2"},
   165  			skipResult:   false,
   166  			filterResult: true,
   167  		},
   168  		{
   169  			patterns:     []string{"v2*"},
   170  			inputs:       []string{"v2.0"},
   171  			skipResult:   false,
   172  			filterResult: true,
   173  		},
   174  		{
   175  			patterns:     []string{"v2*"},
   176  			inputs:       []string{"v2.9"},
   177  			skipResult:   false,
   178  			filterResult: true,
   179  		},
   180  		{
   181  			patterns:     []string{"v[12].[0-9]+.[0-9]+"},
   182  			inputs:       []string{"v1.10.1"},
   183  			skipResult:   false,
   184  			filterResult: true,
   185  		},
   186  		{
   187  			patterns:     []string{"v[12].[0-9]+.[0-9]+"},
   188  			inputs:       []string{"v2.0.0"},
   189  			skipResult:   false,
   190  			filterResult: true,
   191  		},
   192  		// https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#patterns-to-match-file-paths
   193  		{
   194  			patterns:     []string{"*"},
   195  			inputs:       []string{"README.md"},
   196  			skipResult:   false,
   197  			filterResult: true,
   198  		},
   199  		{
   200  			patterns:     []string{"*"},
   201  			inputs:       []string{"server.rb"},
   202  			skipResult:   false,
   203  			filterResult: true,
   204  		},
   205  		{
   206  			patterns:     []string{"*.jsx?"},
   207  			inputs:       []string{"page.js"},
   208  			skipResult:   false,
   209  			filterResult: true,
   210  		},
   211  		{
   212  			patterns:     []string{"*.jsx?"},
   213  			inputs:       []string{"page.jsx"},
   214  			skipResult:   false,
   215  			filterResult: true,
   216  		},
   217  		{
   218  			patterns:     []string{"**"},
   219  			inputs:       []string{"all/the/files.md"},
   220  			skipResult:   false,
   221  			filterResult: true,
   222  		},
   223  		{
   224  			patterns:     []string{"*.js"},
   225  			inputs:       []string{"app.js"},
   226  			skipResult:   false,
   227  			filterResult: true,
   228  		},
   229  		{
   230  			patterns:     []string{"*.js"},
   231  			inputs:       []string{"index.js"},
   232  			skipResult:   false,
   233  			filterResult: true,
   234  		},
   235  		{
   236  			patterns:     []string{"**.js"},
   237  			inputs:       []string{"index.js"},
   238  			skipResult:   false,
   239  			filterResult: true,
   240  		},
   241  		{
   242  			patterns:     []string{"**.js"},
   243  			inputs:       []string{"js/index.js"},
   244  			skipResult:   false,
   245  			filterResult: true,
   246  		},
   247  		{
   248  			patterns:     []string{"**.js"},
   249  			inputs:       []string{"src/js/app.js"},
   250  			skipResult:   false,
   251  			filterResult: true,
   252  		},
   253  		{
   254  			patterns:     []string{"docs/*"},
   255  			inputs:       []string{"docs/README.md"},
   256  			skipResult:   false,
   257  			filterResult: true,
   258  		},
   259  		{
   260  			patterns:     []string{"docs/*"},
   261  			inputs:       []string{"docs/file.txt"},
   262  			skipResult:   false,
   263  			filterResult: true,
   264  		},
   265  		{
   266  			patterns:     []string{"docs/**"},
   267  			inputs:       []string{"docs/README.md"},
   268  			skipResult:   false,
   269  			filterResult: true,
   270  		},
   271  		{
   272  			patterns:     []string{"docs/**"},
   273  			inputs:       []string{"docs/mona/octocat.txt"},
   274  			skipResult:   false,
   275  			filterResult: true,
   276  		},
   277  		{
   278  			patterns:     []string{"docs/**/*.md"},
   279  			inputs:       []string{"docs/README.md"},
   280  			skipResult:   false,
   281  			filterResult: true,
   282  		},
   283  		{
   284  			patterns:     []string{"docs/**/*.md"},
   285  			inputs:       []string{"docs/mona/hello-world.md"},
   286  			skipResult:   false,
   287  			filterResult: true,
   288  		},
   289  		{
   290  			patterns:     []string{"docs/**/*.md"},
   291  			inputs:       []string{"docs/a/markdown/file.md"},
   292  			skipResult:   false,
   293  			filterResult: true,
   294  		},
   295  		{
   296  			patterns:     []string{"**/docs/**"},
   297  			inputs:       []string{"docs/hello.md"},
   298  			skipResult:   false,
   299  			filterResult: true,
   300  		},
   301  		{
   302  			patterns:     []string{"**/docs/**"},
   303  			inputs:       []string{"dir/docs/my-file.txt"},
   304  			skipResult:   false,
   305  			filterResult: true,
   306  		},
   307  		{
   308  			patterns:     []string{"**/docs/**"},
   309  			inputs:       []string{"space/docs/plan/space.doc"},
   310  			skipResult:   false,
   311  			filterResult: true,
   312  		},
   313  		{
   314  			patterns:     []string{"**/README.md"},
   315  			inputs:       []string{"README.md"},
   316  			skipResult:   false,
   317  			filterResult: true,
   318  		},
   319  		{
   320  			patterns:     []string{"**/README.md"},
   321  			inputs:       []string{"js/README.md"},
   322  			skipResult:   false,
   323  			filterResult: true,
   324  		},
   325  		{
   326  			patterns:     []string{"**/*src/**"},
   327  			inputs:       []string{"a/src/app.js"},
   328  			skipResult:   false,
   329  			filterResult: true,
   330  		},
   331  		{
   332  			patterns:     []string{"**/*src/**"},
   333  			inputs:       []string{"my-src/code/js/app.js"},
   334  			skipResult:   false,
   335  			filterResult: true,
   336  		},
   337  		{
   338  			patterns:     []string{"**/*-post.md"},
   339  			inputs:       []string{"my-post.md"},
   340  			skipResult:   false,
   341  			filterResult: true,
   342  		},
   343  		{
   344  			patterns:     []string{"**/*-post.md"},
   345  			inputs:       []string{"path/their-post.md"},
   346  			skipResult:   false,
   347  			filterResult: true,
   348  		},
   349  		{
   350  			patterns:     []string{"**/migrate-*.sql"},
   351  			inputs:       []string{"migrate-10909.sql"},
   352  			skipResult:   false,
   353  			filterResult: true,
   354  		},
   355  		{
   356  			patterns:     []string{"**/migrate-*.sql"},
   357  			inputs:       []string{"db/migrate-v1.0.sql"},
   358  			skipResult:   false,
   359  			filterResult: true,
   360  		},
   361  		{
   362  			patterns:     []string{"**/migrate-*.sql"},
   363  			inputs:       []string{"db/sept/migrate-v1.sql"},
   364  			skipResult:   false,
   365  			filterResult: true,
   366  		},
   367  		{
   368  			patterns:     []string{"*.md", "!README.md"},
   369  			inputs:       []string{"hello.md"},
   370  			skipResult:   false,
   371  			filterResult: true,
   372  		},
   373  		{
   374  			patterns:     []string{"*.md", "!README.md"},
   375  			inputs:       []string{"README.md"},
   376  			skipResult:   true,
   377  			filterResult: true,
   378  		},
   379  		{
   380  			patterns:     []string{"*.md", "!README.md"},
   381  			inputs:       []string{"docs/hello.md"},
   382  			skipResult:   true,
   383  			filterResult: true,
   384  		},
   385  		{
   386  			patterns:     []string{"*.md", "!README.md", "README*"},
   387  			inputs:       []string{"hello.md"},
   388  			skipResult:   false,
   389  			filterResult: true,
   390  		},
   391  		{
   392  			patterns:     []string{"*.md", "!README.md", "README*"},
   393  			inputs:       []string{"README.md"},
   394  			skipResult:   false,
   395  			filterResult: true,
   396  		},
   397  		{
   398  			patterns:     []string{"*.md", "!README.md", "README*"},
   399  			inputs:       []string{"README.doc"},
   400  			skipResult:   false,
   401  			filterResult: true,
   402  		},
   403  	}
   404  
   405  	for _, kase := range kases {
   406  		t.Run(strings.Join(kase.patterns, ","), func(t *testing.T) {
   407  			patterns, err := CompilePatterns(kase.patterns...)
   408  			assert.NoError(t, err)
   409  
   410  			assert.EqualValues(t, kase.skipResult, Skip(patterns, kase.inputs, &StdOutTraceWriter{}), "skipResult")
   411  			assert.EqualValues(t, kase.filterResult, Filter(patterns, kase.inputs, &StdOutTraceWriter{}), "filterResult")
   412  		})
   413  	}
   414  }