...

Source file src/edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/predicate/title_test.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/predicate

     1  // Copyright 2021 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package predicate
    16  
    17  import (
    18  	"context"
    19  	"regexp"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull"
    25  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull/pulltest"
    26  
    27  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common"
    28  )
    29  
    30  func TestWithNotMatchRule(t *testing.T) {
    31  	p := &Title{
    32  		NotMatches: []common.Regexp{
    33  			common.NewCompiledRegexp(regexp.MustCompile("^(fix|feat|chore): (\\w| )+$")),
    34  		},
    35  		Matches: []common.Regexp{},
    36  	}
    37  
    38  	runTitleTestCase(t, p, []TitleTestCase{
    39  		{
    40  			"empty title",
    41  			&pulltest.Context{
    42  				TitleValue: "",
    43  			},
    44  			&common.PredicateResult{
    45  				Satisfied: true,
    46  				Values:    []string{""},
    47  				ConditionsMap: map[string][]string{
    48  					"not match": {"^(fix|feat|chore): (\\w| )+$"},
    49  				},
    50  			},
    51  		},
    52  		{
    53  			"matches pattern",
    54  			&pulltest.Context{
    55  				TitleValue: "chore: added tests",
    56  			},
    57  			&common.PredicateResult{
    58  				Satisfied: false,
    59  				Values:    []string{"chore: added tests"},
    60  				ConditionsMap: map[string][]string{
    61  					"not match": {"^(fix|feat|chore): (\\w| )+$"},
    62  					"match":     nil,
    63  				},
    64  			},
    65  		},
    66  		{
    67  			"does not match pattern",
    68  			&pulltest.Context{
    69  				TitleValue: "changes: added tests",
    70  			},
    71  			&common.PredicateResult{
    72  				Satisfied: true,
    73  				Values:    []string{"changes: added tests"},
    74  				ConditionsMap: map[string][]string{
    75  					"not match": {"^(fix|feat|chore): (\\w| )+$"},
    76  				},
    77  			},
    78  		},
    79  	})
    80  }
    81  
    82  func TestWithMatchRule(t *testing.T) {
    83  	p := &Title{
    84  		NotMatches: []common.Regexp{},
    85  		Matches: []common.Regexp{
    86  			common.NewCompiledRegexp(regexp.MustCompile("^BLOCKED")),
    87  		},
    88  	}
    89  
    90  	runTitleTestCase(t, p, []TitleTestCase{
    91  		{
    92  			"empty title",
    93  			&pulltest.Context{
    94  				TitleValue: "",
    95  			},
    96  			&common.PredicateResult{
    97  				Satisfied: false,
    98  				Values:    []string{""},
    99  				ConditionsMap: map[string][]string{
   100  					"not match": nil,
   101  					"match":     {"^BLOCKED"},
   102  				},
   103  			},
   104  		},
   105  		{
   106  			"matches pattern",
   107  			&pulltest.Context{
   108  				TitleValue: "BLOCKED: new feature",
   109  			},
   110  			&common.PredicateResult{
   111  				Satisfied: true,
   112  				Values:    []string{"BLOCKED: new feature"},
   113  				ConditionsMap: map[string][]string{
   114  					"match": {"^BLOCKED"},
   115  				},
   116  			},
   117  		},
   118  		{
   119  			"does not match pattern",
   120  			&pulltest.Context{
   121  				TitleValue: "feat: new feature",
   122  			},
   123  			&common.PredicateResult{
   124  				Satisfied: false,
   125  				Values:    []string{"feat: new feature"},
   126  				ConditionsMap: map[string][]string{
   127  					"not match": nil,
   128  					"match":     {"^BLOCKED"},
   129  				},
   130  			},
   131  		},
   132  	})
   133  }
   134  
   135  func TestWithMixedRules(t *testing.T) {
   136  	p := &Title{
   137  		NotMatches: []common.Regexp{
   138  			common.NewCompiledRegexp(regexp.MustCompile("^(fix|feat|chore): (\\w| )+$")),
   139  			common.NewCompiledRegexp(regexp.MustCompile("^BREAKING CHANGE: (\\w| )+$")),
   140  		},
   141  		Matches: []common.Regexp{
   142  			common.NewCompiledRegexp(regexp.MustCompile("BLOCKED")),
   143  		},
   144  	}
   145  
   146  	runTitleTestCase(t, p, []TitleTestCase{
   147  		{
   148  			"empty title",
   149  			&pulltest.Context{
   150  				TitleValue: "",
   151  			},
   152  			&common.PredicateResult{
   153  				Satisfied: true,
   154  				Values:    []string{""},
   155  				ConditionsMap: map[string][]string{
   156  					"not match": {"^(fix|feat|chore): (\\w| )+$", "^BREAKING CHANGE: (\\w| )+$"},
   157  				},
   158  			},
   159  		},
   160  		{
   161  			"matches first pattern in matches list",
   162  			&pulltest.Context{
   163  				TitleValue: "fix: fixes failing tests",
   164  			},
   165  			&common.PredicateResult{
   166  				Satisfied: false,
   167  				Values:    []string{"fix: fixes failing tests"},
   168  				ConditionsMap: map[string][]string{
   169  					"not match": {"^(fix|feat|chore): (\\w| )+$", "^BREAKING CHANGE: (\\w| )+$"},
   170  					"match":     {"BLOCKED"},
   171  				},
   172  			},
   173  		},
   174  		{
   175  			"matches second pattern in matches list",
   176  			&pulltest.Context{
   177  				TitleValue: "BREAKING CHANGE: new api version",
   178  			},
   179  			&common.PredicateResult{
   180  				Satisfied: false,
   181  				Values:    []string{"BREAKING CHANGE: new api version"},
   182  				ConditionsMap: map[string][]string{
   183  					"not match": {"^(fix|feat|chore): (\\w| )+$", "^BREAKING CHANGE: (\\w| )+$"},
   184  					"match":     {"BLOCKED"},
   185  				},
   186  			},
   187  		},
   188  		{
   189  			"matches pattern in not_matches list",
   190  			&pulltest.Context{
   191  				TitleValue: "BLOCKED: not working",
   192  			},
   193  			&common.PredicateResult{
   194  				Satisfied: true,
   195  				Values:    []string{"BLOCKED: not working"},
   196  				ConditionsMap: map[string][]string{
   197  					"match": {"BLOCKED"},
   198  				},
   199  			},
   200  		},
   201  		{
   202  			"matches pattern in both lists",
   203  			&pulltest.Context{
   204  				TitleValue: "BREAKING CHANGE: BLOCKED",
   205  			},
   206  			&common.PredicateResult{
   207  				Satisfied: true,
   208  				Values:    []string{"BREAKING CHANGE: BLOCKED"},
   209  				ConditionsMap: map[string][]string{
   210  					"match": {"BLOCKED"},
   211  				},
   212  			},
   213  		},
   214  		{
   215  			"does not match any pattern",
   216  			&pulltest.Context{
   217  				TitleValue: "test: adds tests",
   218  			},
   219  			&common.PredicateResult{
   220  				Satisfied: true,
   221  				Values:    []string{"test: adds tests"},
   222  				ConditionsMap: map[string][]string{
   223  					"not match": {"^(fix|feat|chore): (\\w| )+$", "^BREAKING CHANGE: (\\w| )+$"},
   224  				},
   225  			},
   226  		},
   227  	})
   228  }
   229  
   230  type TitleTestCase struct {
   231  	name                    string
   232  	context                 pull.Context
   233  	ExpectedPredicateResult *common.PredicateResult
   234  }
   235  
   236  func runTitleTestCase(t *testing.T, p Predicate, cases []TitleTestCase) {
   237  	ctx := context.Background()
   238  
   239  	for _, tc := range cases {
   240  		t.Run(tc.name, func(t *testing.T) {
   241  			predicateResult, err := p.Evaluate(ctx, tc.context)
   242  			if assert.NoError(t, err, "evaluation failed") {
   243  				assertPredicateResult(t, tc.ExpectedPredicateResult, predicateResult)
   244  			}
   245  		})
   246  	}
   247  }
   248  

View as plain text