|
| 1 | +// Copyright 2025 Google LLC |
| 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 | +// https://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 gitrepo |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/google/go-cmp/cmp" |
| 22 | +) |
| 23 | + |
| 24 | +func TestParseCommit(t *testing.T) { |
| 25 | + for _, test := range []struct { |
| 26 | + name string |
| 27 | + message string |
| 28 | + want *ConventionalCommit |
| 29 | + wantErr bool |
| 30 | + wantErrPhrase string |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "simple feat", |
| 34 | + message: "feat: add new feature", |
| 35 | + want: &ConventionalCommit{ |
| 36 | + Type: "feat", |
| 37 | + Description: "add new feature", |
| 38 | + Footers: make(map[string]string), |
| 39 | + SHA: "fake-sha", |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "feat with scope", |
| 44 | + message: "feat(scope): add new feature", |
| 45 | + want: &ConventionalCommit{ |
| 46 | + Type: "feat", |
| 47 | + Scope: "scope", |
| 48 | + Description: "add new feature", |
| 49 | + Footers: make(map[string]string), |
| 50 | + SHA: "fake-sha", |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "feat with breaking change", |
| 55 | + message: "feat!: add new feature", |
| 56 | + want: &ConventionalCommit{ |
| 57 | + Type: "feat", |
| 58 | + Description: "add new feature", |
| 59 | + IsBreaking: true, |
| 60 | + Footers: make(map[string]string), |
| 61 | + SHA: "fake-sha", |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "feat with single footer", |
| 66 | + message: "feat: add new feature\n\nCo-authored-by: John Doe <[email protected]>", |
| 67 | + want: &ConventionalCommit{ |
| 68 | + Type: "feat", |
| 69 | + Description: "add new feature", |
| 70 | + Footers: map[ string] string{ "Co-authored-by": "John Doe <[email protected]>"}, |
| 71 | + SHA: "fake-sha", |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "feat with multiple footers", |
| 76 | + message: "feat: add new feature\n\nCo-authored-by: John Doe <[email protected]>\nReviewed-by: Jane Smith <[email protected]>", |
| 77 | + want: &ConventionalCommit{ |
| 78 | + Type: "feat", |
| 79 | + Description: "add new feature", |
| 80 | + Footers: map[string]string{ |
| 81 | + "Co-authored-by": "John Doe <[email protected]>", |
| 82 | + "Reviewed-by": "Jane Smith <[email protected]>", |
| 83 | + }, |
| 84 | + SHA: "fake-sha", |
| 85 | + }, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "feat with multiple footers for generated changes", |
| 89 | + message: "feat: [library-name] add new feature\nThis is the body.\n...\n\nPiperOrigin-RevId: piper_cl_number\n\nSource-Link: [googleapis/googleapis@{source_commit_hash}](https://github.com/googleapis/googleapis/commit/{source_commit_hash})", |
| 90 | + want: &ConventionalCommit{ |
| 91 | + Type: "feat", |
| 92 | + Description: "[library-name] add new feature", |
| 93 | + Body: "This is the body.\n...", |
| 94 | + IsBreaking: false, |
| 95 | + Footers: map[string]string{ |
| 96 | + "PiperOrigin-RevId": "piper_cl_number", |
| 97 | + "Source-Link": "[googleapis/googleapis@{source_commit_hash}](https://github.com/googleapis/googleapis/commit/{source_commit_hash})", |
| 98 | + }, |
| 99 | + SHA: "fake-sha", |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "feat with breaking change footer", |
| 104 | + message: "feat: add new feature\n\nBREAKING CHANGE: this is a breaking change", |
| 105 | + want: &ConventionalCommit{ |
| 106 | + Type: "feat", |
| 107 | + Description: "add new feature", |
| 108 | + Body: "", |
| 109 | + IsBreaking: true, |
| 110 | + Footers: map[string]string{"BREAKING CHANGE": "this is a breaking change"}, |
| 111 | + SHA: "fake-sha", |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "feat with wrong breaking change footer", |
| 116 | + message: "feat: add new feature\n\nBreaking change: this is a breaking change", |
| 117 | + want: &ConventionalCommit{ |
| 118 | + Type: "feat", |
| 119 | + Description: "add new feature", |
| 120 | + Body: "Breaking change: this is a breaking change", |
| 121 | + IsBreaking: false, |
| 122 | + Footers: map[string]string{}, |
| 123 | + SHA: "fake-sha", |
| 124 | + }, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "feat with body and footers", |
| 128 | + message: "feat: add new feature\n\nThis is the body of the commit message.\nIt can span multiple lines.\n\nCo-authored-by: John Doe <[email protected]>", |
| 129 | + want: &ConventionalCommit{ |
| 130 | + Type: "feat", |
| 131 | + Description: "add new feature", |
| 132 | + Body: "This is the body of the commit message.\nIt can span multiple lines.", |
| 133 | + Footers: map[ string] string{ "Co-authored-by": "John Doe <[email protected]>"}, |
| 134 | + SHA: "fake-sha", |
| 135 | + }, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "feat with multi-line footer", |
| 139 | + message: "feat: add new feature\n\nThis is the body.\n\nBREAKING CHANGE: this is a breaking change\nthat spans multiple lines.", |
| 140 | + want: &ConventionalCommit{ |
| 141 | + Type: "feat", |
| 142 | + Description: "add new feature", |
| 143 | + Body: "This is the body.", |
| 144 | + IsBreaking: true, |
| 145 | + Footers: map[string]string{"BREAKING CHANGE": "this is a breaking change\nthat spans multiple lines."}, |
| 146 | + SHA: "fake-sha", |
| 147 | + }, |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "invalid conventional commit", |
| 151 | + message: "this is not a conventional commit", |
| 152 | + wantErr: false, |
| 153 | + want: nil, |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "empty commit message", |
| 157 | + message: "", |
| 158 | + wantErr: true, |
| 159 | + wantErrPhrase: "empty commit", |
| 160 | + }, |
| 161 | + } { |
| 162 | + t.Run(test.name, func(t *testing.T) { |
| 163 | + got, err := ParseCommit(test.message, "fake-sha") |
| 164 | + if test.wantErr { |
| 165 | + if err == nil { |
| 166 | + t.Errorf("%s should return error", test.name) |
| 167 | + } |
| 168 | + if !strings.Contains(err.Error(), test.wantErrPhrase) { |
| 169 | + t.Errorf("ParseCommit(%q) returned error %q, want to contain %q", test.message, err.Error(), test.wantErrPhrase) |
| 170 | + } |
| 171 | + return |
| 172 | + } |
| 173 | + if err != nil { |
| 174 | + t.Fatal(err) |
| 175 | + } |
| 176 | + if diff := cmp.Diff(test.want, got); diff != "" { |
| 177 | + t.Errorf("ParseCommit(%q) returned diff (-want +got):\n%s", test.message, diff) |
| 178 | + } |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
0 commit comments