xref: /aosp_15_r20/external/bazelbuild-rules_go/go/tools/releaser/upgradedep_test.go (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1package main
2
3import (
4	"fmt"
5	"testing"
6
7	bzl "github.com/bazelbuild/buildtools/build"
8)
9
10func TestPatchItemParser_Success(t *testing.T) {
11	tests := []struct {
12		expression []byte
13		result     string
14	}{
15		{
16			expression: []byte(`# releaser:patch-cmd gazelle -repo_root . -go_prefix golang.org/x/tools -go_naming_convention import_alias
17			Label("//third_party:org_golang_x_tools-gazelle.patch")`),
18			result: "//third_party:org_golang_x_tools-gazelle.patch",
19		},
20		{
21			expression: []byte(`# releaser:patch-cmd gazelle -repo_root . -go_prefix golang.org/x/tools -go_naming_convention import_alias
22			"@io_bazel_rules_go//third_party:org_golang_x_tools-gazelle.patch"`),
23			result: "//third_party:org_golang_x_tools-gazelle.patch",
24		},
25		{
26			expression: []byte(`# releaser:patch-cmd gazelle -repo_root . -go_prefix golang.org/x/tools -go_naming_convention import_alias
27			"//third_party:org_golang_x_tools-gazelle.patch"`),
28			result: "//third_party:org_golang_x_tools-gazelle.patch",
29		},
30		{
31			expression: []byte(`# releaser:patch-cmd gazelle -repo_root . -go_prefix golang.org/x/tools -go_naming_convention import_alias
32			Label("@io_bazel_rules_go//third_party:org_golang_x_tools-gazelle.patch")`),
33			result: "@io_bazel_rules_go//third_party:org_golang_x_tools-gazelle.patch",
34		},
35	}
36
37	for _, tt := range tests {
38		t.Run(fmt.Sprintf("%v", tt.expression), func(t *testing.T) {
39			patchExpr, err := bzl.Parse("repos.bzl", tt.expression)
40			if err != nil {
41				t.Fatalf(err.Error())
42			}
43
44			patchLabelStr, _, err := parsePatchesItem(patchExpr.Stmt[0])
45			if err != nil {
46				t.Errorf("unexpected error while parsing expression: %q", err.Error())
47			} else if patchLabelStr != tt.result {
48				t.Errorf("expected result %q, but got result %q instead", tt.result, patchLabelStr)
49			}
50		})
51	}
52}
53
54func TestPatchItemParser_Error(t *testing.T) {
55	tests := []struct {
56		expression []byte
57		error      string
58	}{
59		{
60			expression: []byte(`# releaser:patch-cmd gazelle -repo_root . -go_prefix golang.org/x/tools -go_naming_convention import_alias
61			NotLabel("//third_party:org_golang_x_tools-gazelle.patch")`),
62			error: `invalid patch function: "NotLabel"`,
63		},
64		{
65			expression: []byte(`# releaser:patch-cmd gazelle -repo_root . -go_prefix golang.org/x/tools -go_naming_convention import_alias
66			NotLabel(True)`),
67			error: `invalid patch function: "NotLabel"`,
68		},
69		{
70			expression: []byte(`# releaser:patch-cmd gazelle -repo_root . -go_prefix golang.org/x/tools -go_naming_convention import_alias
71			True`),
72			error: "not all patches are string literals or Label()",
73		},
74		{
75			expression: []byte(`# releaser:patch-cmd gazelle -repo_root . -go_prefix golang.org/x/tools -go_naming_convention import_alias
76			Label("//third_party:org_golang_x_tools-gazelle.patch", True)`),
77			error: "Label expr should have 1 argument, found 2",
78		},
79		{
80			expression: []byte(`# releaser:patch-cmd gazelle -repo_root . -go_prefix golang.org/x/tools -go_naming_convention import_alias
81			Label(True)`),
82			error: "Label expr does not contain a string literal",
83		},
84	}
85
86	for _, tt := range tests {
87		t.Run(fmt.Sprintf("%v", tt.expression), func(t *testing.T) {
88			patchExpr, err := bzl.Parse("repos.bzl", tt.expression)
89			if err != nil {
90				t.Fatalf(err.Error())
91			}
92
93			patchLabelStr, _, err := parsePatchesItem(patchExpr.Stmt[0])
94
95			if err == nil {
96				t.Errorf("expected error %q, but got result %q instead", tt.error, patchLabelStr)
97			} else if err.Error() != tt.error {
98				t.Errorf("expected error %q, but got error %q instead", tt.error, err.Error())
99			}
100		})
101	}
102}
103