github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/internal/generationtest/generationtest.bzl (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 Test for generating rules from gazelle. 17 """ 18 19 load("@io_bazel_rules_go//go:def.bzl", "go_test") 20 21 def gazelle_generation_test(name, gazelle_binary, test_data, build_in_suffix = ".in", build_out_suffix = ".out", gazelle_timeout_seconds = 2, size = None, **kwargs): 22 """ 23 gazelle_generation_test is a macro for testing gazelle against workspaces. 24 25 The generation test expects a file structure like the following: 26 27 ``` 28 |-- <testDataPath> 29 |-- some_test 30 |-- WORKSPACE 31 |-- README.md --> README describing what the test does. 32 |-- arguments.txt --> newline delimited list of arguments to pass in (ignored if empty). 33 |-- expectedStdout.txt --> Expected stdout for this test. 34 |-- expectedStderr.txt --> Expected stderr for this test. 35 |-- expectedExitCode.txt --> Expected exit code for this test. 36 |-- app 37 |-- sourceFile.foo 38 |-- BUILD.in --> BUILD file prior to running gazelle. 39 |-- BUILD.out --> BUILD file expected after running gazelle. 40 ``` 41 42 To update the expected files, run `UPDATE_SNAPSHOTS=true bazel run //path/to:the_test_target`. 43 44 Args: 45 name: The name of the test. 46 gazelle_binary: The name of the gazelle binary target. For example, //path/to:my_gazelle. 47 test_data: A list of target of the test data files you will pass to the test. 48 This can be a https://bazel.build/reference/be/general#filegroup. 49 build_in_suffix: The suffix for the input BUILD.bazel files. Defaults to .in. 50 By default, will use files named BUILD.in as the BUILD files before running gazelle. 51 build_out_suffix: The suffix for the expected BUILD.bazel files after running gazelle. Defaults to .out. 52 By default, will use files named check the results of the gazelle run against files named BUILD.out. 53 timeout_seconds: Number of seconds to allow the gazelle process to run before killing. 54 size: Specifies a test target's "heaviness": how much time/resources it needs to run. 55 **kwargs: Attributes that are passed directly to the test declaration. 56 """ 57 go_test( 58 name = name, 59 srcs = [Label("//internal/generationtest:generation_test.go")], 60 deps = [ 61 Label("//testtools"), 62 Label("@io_bazel_rules_go//go/tools/bazel:go_default_library"), 63 ], 64 args = [ 65 "-gazelle_binary_path=$(rootpath %s)" % gazelle_binary, 66 "-build_in_suffix=%s" % build_in_suffix, 67 "-build_out_suffix=%s" % build_out_suffix, 68 "-timeout=%ds" % gazelle_timeout_seconds, 69 ], 70 size = size, 71 data = test_data + [ 72 gazelle_binary, 73 ], 74 **kwargs 75 )