Files
carbon-lang/testing/file_test/test_file.h
T
Jon Ross-Perkins 868efb7c86 Make INCLUDE-FILE less sensitive to path changes (#5159)
It's helpful for stability to not have the path in the repo reflected in
test files, something I'm separately running into. So to reduce this,
align INCLUDE-FILE with other split behavior:

- Use the filename (with a "include_files/" subdir to disambiguate),
rather than the full path.
- Note if we eventually want to support splits in these, the same
approach could be extended.
- Only provide as an arg if the user requests files as args.

Factoring AddFile back because it's hard to share; I'm also advocating
to remove the prelude manifest, which would mean the remaining call
could be removed.
2025-03-21 16:03:25 +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;
// Files pulled in by `INCLUDE-FILE`. They're combined with `file_splits` for
// execution.
llvm::SmallVector<Split> include_file_splits;
// 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_