| 1 |
// Copyright 2020 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 |
// 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 git |
| 16 |
|
| 17 |
import "testing" |
| 18 |
|
| 19 |
func TestParseConventionalCommitPkg(t *testing.T) { |
| 20 |
tests := []struct { |
| 21 |
name string |
| 22 |
importPath string |
| 23 |
want string |
| 24 |
}{ |
| 25 |
{name: "one path element", importPath: "cloud.google.com/go/foo/apiv1", want: "foo"}, |
| 26 |
{name: "two path elements", importPath: "cloud.google.com/go/foo/bar/apiv1", want: "foo/bar"}, |
| 27 |
} |
| 28 |
|
| 29 |
for _, tc := range tests { |
| 30 |
t.Run(tc.name, func(t *testing.T) { |
| 31 |
if got := parseConventionalCommitPkg(tc.importPath); got != tc.want { |
| 32 |
t.Errorf("parseConventionalCommitPkg(%q) = %q, want %q", tc.importPath, got, tc.want) |
| 33 |
} |
| 34 |
}) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
func TestFormatChanges(t *testing.T) { |
| 39 |
tests := []struct { |
| 40 |
name string |
| 41 |
changes []*ChangeInfo |
| 42 |
onlyGapics bool |
| 43 |
want string |
| 44 |
}{ |
| 45 |
{ |
| 46 |
name: "basic", |
| 47 |
changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar"}}, |
| 48 |
want: "\nChanges:\n\nfix: foo\n bar\n\n", |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "breaking change", |
| 52 |
changes: []*ChangeInfo{{Title: "feat!: breaking change", Body: "BREAKING CHANGE: The world is breaking."}}, |
| 53 |
want: "\nChanges:\n\nfeat!: breaking change\n BREAKING CHANGE: The world is breaking.\n\n", |
| 54 |
}, |
| 55 |
{ |
| 56 |
name: "multi-lined body indented", |
| 57 |
changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar\nbaz"}}, |
| 58 |
want: "\nChanges:\n\nfix: foo\n bar\n baz\n\n", |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "multi-lined body indented, multiple changes", |
| 62 |
changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar\nbaz"}, {Title: "fix: baz", Body: "foo\nbar"}}, |
| 63 |
want: "\nChanges:\n\nfix: foo\n bar\n baz\n\nfix: baz\n foo\n bar\n\n", |
| 64 |
}, |
| 65 |
{ |
| 66 |
name: "no package, filtered", |
| 67 |
changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar"}}, |
| 68 |
onlyGapics: true, |
| 69 |
want: "", |
| 70 |
}, |
| 71 |
} |
| 72 |
|
| 73 |
for _, tc := range tests { |
| 74 |
t.Run(tc.name, func(t *testing.T) { |
| 75 |
if got := FormatChanges(tc.changes, tc.onlyGapics); got != tc.want { |
| 76 |
t.Errorf("%s: FormatChanges() = %q, want %q", tc.name, got, tc.want) |
| 77 |
} |
| 78 |
}) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func TestFormatChanges_MaxChangesLen(t *testing.T) { |
| 83 |
tests := []struct { |
| 84 |
name string |
| 85 |
changes []*ChangeInfo |
| 86 |
maxChangesLen int |
| 87 |
want string |
| 88 |
}{ |
| 89 |
{ |
| 90 |
name: "truncate long title", |
| 91 |
changes: []*ChangeInfo{{Title: "fix: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod", Body: "tempor incididunt ut\nPiperOrigin-RevId: bar"}}, |
| 92 |
maxChangesLen: 100, |
| 93 |
want: "\nChanges:\n\nfix: Lorem ipsum dolor...\n PiperOrigin-RevId: bar\n\n", |
| 94 |
}, |
| 95 |
{ |
| 96 |
name: "no truncate short title", |
| 97 |
changes: []*ChangeInfo{{Title: "fix: Lorem ipsum dolor", Body: "tempor incididunt ut\n PiperOrigin-RevId: bar"}}, |
| 98 |
maxChangesLen: 50, |
| 99 |
want: "\nChanges:\n\nfix: Lorem ipsum dolor\n PiperOrigin-RevId: bar\n\n", |
| 100 |
}, |
| 101 |
} |
| 102 |
|
| 103 |
for _, tc := range tests { |
| 104 |
maxChangesLen = tc.maxChangesLen |
| 105 |
t.Run(tc.name, func(t *testing.T) { |
| 106 |
if got := FormatChanges(tc.changes, false); got != tc.want { |
| 107 |
t.Errorf("%s: FormatChanges() = %q, want %q", tc.name, got, tc.want) |
| 108 |
} |
| 109 |
}) |
| 110 |
} |
| 111 |
} |