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

     1  /* Copyright 2023 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 generationtest
    17  
    18  import (
    19  	"flag"
    20  	"path/filepath"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/bazelbuild/bazel-gazelle/testtools"
    25  	"github.com/bazelbuild/rules_go/go/tools/bazel"
    26  )
    27  
    28  var (
    29  	gazelleBinaryPath = flag.String("gazelle_binary_path", "", "Path to the gazelle binary to test.")
    30  	buildInSuffix     = flag.String("build_in_suffix", ".in", "The suffix on the test input BUILD.bazel files. Defaults to .in. "+
    31  		" By default, will use files named BUILD.in as the BUILD files before running gazelle.")
    32  	buildOutSuffix = flag.String("build_out_suffix", ".out", "The suffix on the expected BUILD.bazel files after running gazelle. Defaults to .out. "+
    33  		" By default, will use files named BUILD.out as the expected results of the gazelle run.")
    34  	timeout = flag.Duration("timeout", 2*time.Second, "Time to allow the gazelle process to run before killing.")
    35  )
    36  
    37  // TestFullGeneration runs the gazelle binary on a few example
    38  // workspaces and confirms that the generated BUILD files match expectation.
    39  func TestFullGeneration(t *testing.T) {
    40  	tests := []*testtools.TestGazelleGenerationArgs{}
    41  	runfiles, err := bazel.ListRunfiles()
    42  	if err != nil {
    43  		t.Fatalf("bazel.ListRunfiles() error: %v", err)
    44  	}
    45  	// Convert workspace relative path for gazelle binary into an absolute path.
    46  	// E.g. path/to/gazelle_binary -> /absolute/path/to/workspace/path/to/gazelle/binary.
    47  	absoluteGazelleBinary, err := bazel.Runfile(*gazelleBinaryPath)
    48  	if err != nil {
    49  		t.Fatalf("Could not convert gazelle binary path %s to absolute path. Error: %v", *gazelleBinaryPath, err)
    50  	}
    51  	for _, f := range runfiles {
    52  		// Look through runfiles for WORKSPACE files. Each WORKSPACE is a test case.
    53  		if filepath.Base(f.Path) == "WORKSPACE" {
    54  			// absolutePathToTestDirectory is the absolute
    55  			// path to the test case directory. For example, /home/<user>/wksp/path/to/test_data/my_test_case
    56  			absolutePathToTestDirectory := filepath.Dir(f.Path)
    57  			// relativePathToTestDirectory is the workspace relative path
    58  			// to this test case directory. For example, path/to/test_data/my_test_case
    59  			relativePathToTestDirectory := filepath.Dir(f.ShortPath)
    60  			// name is the name of the test directory. For example, my_test_case.
    61  			// The name of the directory doubles as the name of the test.
    62  			name := filepath.Base(absolutePathToTestDirectory)
    63  
    64  			tests = append(tests, &testtools.TestGazelleGenerationArgs{
    65  				Name:                 name,
    66  				TestDataPathAbsolute: absolutePathToTestDirectory,
    67  				TestDataPathRelative: relativePathToTestDirectory,
    68  				GazelleBinaryPath:    absoluteGazelleBinary,
    69  				BuildInSuffix:        *buildInSuffix,
    70  				BuildOutSuffix:       *buildOutSuffix,
    71  				Timeout:              *timeout,
    72  			})
    73  		}
    74  	}
    75  	if len(tests) == 0 {
    76  		t.Fatal("no tests found")
    77  	}
    78  
    79  	for _, args := range tests {
    80  		testtools.TestGazelleGenerationOnPath(t, args)
    81  	}
    82  }