github.com/twelho/conform@v0.0.0-20231016230407-c25e9238598a/internal/policy/commit/check_jira_test.go (about)

     1  // This Source Code Form is subject to the terms of the Mozilla Public
     2  // License, v. 2.0. If a copy of the MPL was not distributed with this
     3  // file, You can obtain one at http://mozilla.org/MPL/2.0/.
     4  
     5  //nolint:testpackage
     6  package commit
     7  
     8  import (
     9  	"testing"
    10  )
    11  
    12  func TestCommit_ValidateJiraCheck(t *testing.T) {
    13  	//nolint:govet
    14  	type fields struct {
    15  		SpellCheck         *SpellCheck
    16  		Conventional       *Conventional
    17  		Header             *HeaderChecks
    18  		Body               *BodyChecks
    19  		DCO                bool
    20  		GPG                bool
    21  		MaximumOfOneCommit bool
    22  		msg                string
    23  	}
    24  
    25  	type want struct {
    26  		errorCount int
    27  	}
    28  
    29  	tests := []struct {
    30  		name   string
    31  		fields fields
    32  		want   want
    33  	}{
    34  		{
    35  			name: "Missing jira issue no type",
    36  			fields: fields{
    37  				Header: &HeaderChecks{
    38  					Jira: &JiraChecks{
    39  						Keys: []string{"JIRA", "PROJ"},
    40  					},
    41  				},
    42  				msg: "invalid commit",
    43  			},
    44  			want: want{errorCount: 1},
    45  		},
    46  		{
    47  			name: "Missing jira issue with type",
    48  			fields: fields{
    49  				Header: &HeaderChecks{
    50  					Jira: &JiraChecks{
    51  						Keys: []string{"JIRA", "PROJ"},
    52  					},
    53  				},
    54  				msg: "fix: invalid commit",
    55  			},
    56  			want: want{errorCount: 1},
    57  		},
    58  		{
    59  			name: "Valid commit",
    60  			fields: fields{
    61  				Header: &HeaderChecks{
    62  					Jira: &JiraChecks{
    63  						Keys: []string{"JIRA", "PROJ"},
    64  					},
    65  				},
    66  				msg: "fix: [JIRA-1234] valid commit",
    67  			},
    68  			want: want{errorCount: 0},
    69  		},
    70  		{
    71  			name: "Valid commit 2",
    72  			fields: fields{
    73  				Header: &HeaderChecks{
    74  					Jira: &JiraChecks{
    75  						Keys: []string{"JIRA", "PROJ"},
    76  					},
    77  				},
    78  				msg: "fix: [PROJ-1234] valid commit",
    79  			},
    80  			want: want{errorCount: 0},
    81  		},
    82  		{
    83  			name: "Invalid jira project",
    84  			fields: fields{
    85  				Header: &HeaderChecks{
    86  					Jira: &JiraChecks{
    87  						Keys: []string{"JIRA", "PROJ"},
    88  					},
    89  				},
    90  				msg: "fix: [FALSE-1234] valid commit",
    91  			},
    92  			want: want{errorCount: 1},
    93  		},
    94  		{
    95  			name: "Invalid jira issue number",
    96  			fields: fields{
    97  				Header: &HeaderChecks{
    98  					Jira: &JiraChecks{
    99  						Keys: []string{"JIRA", "PROJ"},
   100  					},
   101  				},
   102  				msg: "fix: JIRA-0 valid commit",
   103  			},
   104  			want: want{errorCount: 1},
   105  		},
   106  		{
   107  			name: "Valid commit with scope",
   108  			fields: fields{
   109  				Header: &HeaderChecks{
   110  					Jira: &JiraChecks{
   111  						Keys: []string{"JIRA", "PROJ"},
   112  					},
   113  				},
   114  				msg: "fix(test): [PROJ-1234] valid commit",
   115  			},
   116  			want: want{errorCount: 0},
   117  		},
   118  		{
   119  			name: "Valid commit without square brackets",
   120  			fields: fields{
   121  				Header: &HeaderChecks{
   122  					Jira: &JiraChecks{
   123  						Keys: []string{"JIRA", "PROJ"},
   124  					},
   125  				},
   126  				msg: "fix: PROJ-1234 valid commit",
   127  			},
   128  			want: want{errorCount: 0},
   129  		},
   130  	}
   131  	for _, tt := range tests {
   132  		tt := tt
   133  		t.Run(tt.name, func(t *testing.T) {
   134  			c := Commit{
   135  				SpellCheck:   tt.fields.SpellCheck,
   136  				Conventional: tt.fields.Conventional,
   137  				Header:       tt.fields.Header,
   138  				Body:         tt.fields.Body,
   139  				DCO:          tt.fields.DCO,
   140  				GPG: &GPG{
   141  					Required: tt.fields.GPG,
   142  				},
   143  				MaximumOfOneCommit: tt.fields.MaximumOfOneCommit,
   144  				msg:                tt.fields.msg,
   145  			}
   146  			got := c.ValidateJiraCheck()
   147  			if len(got.Errors()) != tt.want.errorCount {
   148  				t.Errorf("Wanted %d errors but got %d errors: %v", tt.want.errorCount, len(got.Errors()), got.Errors())
   149  			}
   150  		})
   151  	}
   152  }