Files
carbon-lang/testing/file_test/test_file.h
T
Dana Jansens d58b523a5e Add INCLUDE-FILE: and --custom-core for file tests to specify a minimal prelude library (#5080)
The INCLUDE-FILE option is only used in the toolchain tests for now. If
specified in a file test, the given file path is added to the test's
arguments. For toolchain tests this makes the file's package available
to the test. The `--custom-core` command line flag is added to the
driver, which avoids adding the production `Core` package to the command
line. Together, these allow a test to provide their own minimal `Core`
package.

For example, this would replace `Core` with the package and prelude in
`facet_types.carbon`.
```
// INCLUDE-FILE: toolchain/testing/min_prelude/facet_types.carbon
// EXTRA-ARGS: --custom-core
```

To support this:
* //testing knows how to parse INCLUDE-FILE out of the header of a test
file.
* //testing adds the file to the virtual file system, and includes it in
the test's arguments.
* //toolchain/driver grows the --custom-core command line flag to avoid
loading the production `Core` package.

Tests that were creating their own minimal prelude to define BitAnd on
types are now pointed to
toolchain/testing/min_prelude/facet_types.carbon as the prelude. They no
longer need to `import Core` in each test as a result.

Such tests are no longer `no_prelude`, but instead have their own
prelude. So they are moved to a `min_prelude` subdirectory.

Closes #5076
2025-03-10 19:44:50 +00:00

96 lines
3.2 KiB
C++

// 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
#ifndef CARBON_TESTING_FILE_TEST_TEST_FILE_H_
#define CARBON_TESTING_FILE_TEST_TEST_FILE_H_
#include <gmock/gmock.h>
#include <string>
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "testing/file_test/file_test_base.h"
#include "testing/file_test/line.h"
namespace Carbon::Testing {
// Encapsulates test context generated by processing and running.
//
// Note this should remain internal to `FileTestBase`, not exposed to individual
// tests.
struct TestFile {
// Represents a split within the test file.
struct Split {
friend auto PrintTo(const Split& f, std::ostream* os) -> void {
// Print content escaped.
llvm::raw_os_ostream os_wrap(*os);
os_wrap << "Split(" << f.filename << ", \"" << FormatEscaped(f.content)
<< "\")";
}
std::string filename;
std::string content;
};
// The input test file content. Other parts may reference this.
std::string input_content;
// Lines which don't contain CHECKs, and thus need to be retained by
// autoupdate. Their file and line numbers are attached.
//
// If there are splits, then the splitting line is in the respective file.
// For N splits, the 0th file is the parts of the input file which are not
// in any split, plus one file per split file.
llvm::SmallVector<FileTestLine> non_check_lines;
// Whether there are splits.
bool has_splits = false;
// Arguments for the test, generated from ARGS.
llvm::SmallVector<std::string> test_args;
// Extra arguments for the test, generated from EXTRA-ARGS. Unlike ARGS,
// setting EXTRA-ARGS does not suppress the default arguments.
llvm::SmallVector<std::string> extra_args;
// Files in the test, generated by content and splits.
llvm::SmallVector<Split> file_splits;
// // Paths to files to be included in the test. They are added to `test_args`
// in `Run`, and added to the virtual file system to make them accessible.
llvm::SmallVector<std::string> include_files;
// The location of the autoupdate marker, for autoupdated files.
std::optional<int> autoupdate_line_number;
// Whether there should be an AUTOUPDATE-SPLIT.
bool autoupdate_split = false;
// Whether to capture stderr and stdout that would head to console,
// generated from SET-CAPTURE-CONSOLE-OUTPUT.
bool capture_console_output = false;
// Whether checks are a subset, generated from SET-CHECK-SUBSET.
bool check_subset = false;
// stdout and stderr based on CHECK lines in the file.
llvm::SmallVector<testing::Matcher<std::string>> expected_stdout;
llvm::SmallVector<testing::Matcher<std::string>> expected_stderr;
// stdout and stderr from Run. 16 is arbitrary but a required value.
llvm::SmallString<16> actual_stdout;
llvm::SmallString<16> actual_stderr;
FileTestBase::RunResult run_result = {.success = false};
};
// Processes the test input, producing test files and expected output.
auto ProcessTestFile(llvm::StringRef test_name, bool running_autoupdate)
-> ErrorOr<TestFile>;
} // namespace Carbon::Testing
#endif // CARBON_TESTING_FILE_TEST_TEST_FILE_H_