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
15package git
16
17import "testing"
18
19func 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
38func 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			name:    "with package",
73			changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar", Package: "baz"}},
74			want:    "\nChanges:\n\nfix(baz): foo\n  bar\n\n",
75		},
76		{
77			name:    "multiple changes",
78			changes: []*ChangeInfo{{Title: "fix: foo", Body: "bar", Package: "foo"}, {Title: "fix: baz", Body: "bar"}},
79			want:    "\nChanges:\n\nfix(foo): foo\n  bar\n\nfix: baz\n  bar\n\n",
80		},
81		{
82			name:       "multiple changes, some filtered",
83			changes:    []*ChangeInfo{{Title: "fix: foo", Body: "bar", Package: "foo"}, {Title: "fix: baz", Body: "bar"}},
84			onlyGapics: true,
85			want:       "\nChanges:\n\nfix(foo): foo\n  bar\n\n",
86		},
87	}
88
89	for _, tc := range tests {
90		t.Run(tc.name, func(t *testing.T) {
91			if got := FormatChanges(tc.changes, tc.onlyGapics); got != tc.want {
92				t.Errorf("FormatChanges() = %q, want %q", got, tc.want)
93			}
94		})
95	}
96}
97