github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/internal/gazellebinarytest/gazellebinary_test.go (about)

     1  /* Copyright 2018 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  */
    15  
    16  package gazellebinarytest
    17  
    18  import (
    19  	"os"
    20  	"os/exec"
    21  	"testing"
    22  
    23  	"github.com/bazelbuild/bazel-gazelle/testtools"
    24  	"github.com/bazelbuild/rules_go/go/tools/bazel"
    25  )
    26  
    27  func TestGazelleBinary(t *testing.T) {
    28  	gazellePath, ok := bazel.FindBinary("internal/gazellebinarytest", "gazelle_go_x")
    29  	if !ok {
    30  		t.Fatal("could not find gazelle binary")
    31  	}
    32  
    33  	files := []testtools.FileSpec{
    34  		{Path: "WORKSPACE"},
    35  		{Path: "BUILD.bazel", Content: "# gazelle:prefix example.com/test"},
    36  		{Path: "foo.go", Content: "package foo"},
    37  		{Path: "foo.proto", Content: `syntax = "proto3";`},
    38  	}
    39  	dir, cleanup := testtools.CreateFiles(t, files)
    40  	defer cleanup()
    41  
    42  	cmd := exec.Command(gazellePath)
    43  	cmd.Stdout = os.Stdout
    44  	cmd.Stderr = os.Stderr
    45  	cmd.Dir = dir
    46  	if err := cmd.Run(); err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	testtools.CheckFiles(t, dir, []testtools.FileSpec{{
    51  		Path: "BUILD.bazel",
    52  		Content: `
    53  load("@io_bazel_rules_go//go:def.bzl", "go_library")
    54  
    55  # gazelle:prefix example.com/test
    56  
    57  go_library(
    58      name = "test",
    59      srcs = ["foo.go"],
    60      importpath = "example.com/test",
    61      visibility = ["//visibility:public"],
    62  )
    63  
    64  x_library(name = "x_default_library")
    65  `,
    66  	}})
    67  }