xref: /aosp_15_r20/external/bazelbuild-rules_go/tests/integration/gazelle/gazelle_test.go (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1// Copyright 2020 The Bazel Authors. All rights reserved.
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 gazelle_test
16
17import (
18	"io/ioutil"
19	"strings"
20	"testing"
21
22	"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
23)
24
25func TestMain(m *testing.M) {
26	bazel_testing.TestMain(m, bazel_testing.Args{
27		Main: `
28-- BUILD.bazel --
29load("@bazel_gazelle//:def.bzl", "gazelle")
30
31# gazelle:prefix example.com/hello
32gazelle(
33    name = "gazelle",
34)
35-- hello.go --
36package hello
37`,
38		WorkspaceSuffix: `
39load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
40
41gazelle_dependencies()
42`,
43	})
44}
45
46func TestUpdate(t *testing.T) {
47	if err := bazel_testing.RunBazel("run", "//:gazelle"); err != nil {
48		t.Fatal(err)
49	}
50	data, err := ioutil.ReadFile("BUILD.bazel")
51	if err != nil {
52		t.Fatal(err)
53	}
54	got := strings.TrimSpace(string(data))
55	want := strings.TrimSpace(`
56load("@io_bazel_rules_go//go:def.bzl", "go_library")
57load("@bazel_gazelle//:def.bzl", "gazelle")
58
59# gazelle:prefix example.com/hello
60gazelle(
61    name = "gazelle",
62)
63
64go_library(
65    name = "hello",
66    srcs = ["hello.go"],
67    importpath = "example.com/hello",
68    visibility = ["//visibility:public"],
69)
70`)
71	if got != want {
72		t.Errorf("got:\n%s\n\nwant:\n%s", got, want)
73	}
74}
75