Files
carbon-lang/testing/file_test/rules.bzl
T
Jon Ross-PerkinsandChandler Carruth 941e60ade6 Add framework for replacing lit with cc_test (#2814)
This is really part of #2811, but is extracted out to allow a little review in parallelism because #2811 expects #2813. Getting this in will allow migration of toolchain tests, whereas #2811 is focused on explorer tests. For explorer test timing information, see #2811.

The syntax being used for matching deliberately mirrors the `FileCheck` setup, partly for compatibility if something changes, partly so there's nothing new to learn, partly so that we don't need to build more test updating.

Individual tests look like:

```
[ RUN      ] ParseAndExecuteTestFile.explorer/parse_and_execute/testdata/assert/convert.carbon

To test this file alone, run:
  bazel test //explorer/parse_and_execute:file_test.subset --test_arg=explorer/parse_and_execute/testdata/assert/convert.carbon

[       OK ] ParseAndExecuteTestFile.explorer/parse_and_execute/testdata/assert/convert.carbon (202 ms)
```

The printed command line is intended to assist developers in debugging a single test, particularly when sharding the main test. The use of a single `.subset` target means the total number of targets is constant even as the number of test files increases, which may be important for some `bazel` execution environments. I plan to make similar changes to the `glob_sh_run` implementation so that we have consistent setups, i.e. that we no longer create target-per-file scaling risks.

This uses `native_test` to share the test binary, avoiding re-linking if files are individually run.

Investigation did reveal a mistake where STDOUT/STDERR wasn't prefixed on empty output lines; this PR fixes that mistake, so that output is fully covered.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2023-05-12 10:05:08 -07:00

41 lines
1.2 KiB
Python

# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
"""Rules for building fuzz tests."""
load("@bazel_skylib//rules:native_binary.bzl", "native_test")
def file_test(name, srcs, deps, tests, shard_count = 1):
"""Generates tests using the file_test base.
There will be one main test using `name` that can be sharded, and includes
all files. Additionally, per-file tests will be generated as
`name.file_path`; these per-file tests will be manual.
Args:
name: The base name of the tests.
srcs: cc_test srcs.
deps: cc_test deps.
tests: The list of test files to use as data.
shard_count: The number of shards to use; defaults to 1.
"""
subset_name = "{0}.subset".format(name)
native.cc_test(
name = name,
srcs = srcs,
deps = deps,
data = tests,
args = ["$(location {0})".format(x) for x in tests],
shard_count = shard_count,
)
native_test(
name = subset_name,
src = name,
out = subset_name,
data = tests,
tags = ["manual"],
)