file_test_base.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TESTING_FILE_TEST_FILE_TEST_BASE_H_
  5. #define CARBON_TESTING_FILE_TEST_FILE_TEST_BASE_H_
  6. #include <gmock/gmock.h>
  7. #include <gtest/gtest.h>
  8. #include <functional>
  9. #include <mutex>
  10. #include "common/error.h"
  11. #include "common/ostream.h"
  12. #include "llvm/ADT/SmallString.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/ADT/StringMap.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/Support/VirtualFileSystem.h"
  17. #include "testing/file_test/autoupdate.h"
  18. namespace Carbon::Testing {
  19. // A framework for testing files. See README.md for documentation.
  20. class FileTestBase : public testing::Test {
  21. public:
  22. // Provided for child class convenience.
  23. using LineNumberReplacement = FileTestAutoupdater::LineNumberReplacement;
  24. // The result of Run(), used to detect errors. Failing test files should be
  25. // named with a `fail_` prefix to indicate an expectation of failure.
  26. //
  27. // If per_file_success is empty:
  28. // - The main file has a `fail_` prefix if !success.
  29. // - The prefix of split files is unused.
  30. //
  31. // If per_file_success is non-empty:
  32. // - Each file has a `fail_` prefix if !per_file_success[i].second.
  33. // - Files may be in per_file_success that aren't part of the main test
  34. // file. This allows tracking success in handling files that are
  35. // well-known, such as standard libraries. It is still the responsibility
  36. // of callers to use a `fail_` prefix if !per_file_success[i].second.
  37. // - If any file has a `fail_` prefix, success must be false, and the prefix
  38. // of the main file is unused.
  39. // - If no file has a `fail_` prefix, the main file has a `fail_` prefix if
  40. // !success.
  41. struct RunResult {
  42. bool success;
  43. // Per-file success results. May be empty.
  44. llvm::SmallVector<std::pair<std::string, bool>> per_file_success;
  45. };
  46. explicit FileTestBase(std::mutex* output_mutex, llvm::StringRef test_name)
  47. : output_mutex_(output_mutex), test_name_(test_name) {}
  48. // Implemented by children to run the test. The framework will validate the
  49. // content written to `output_stream` and `error_stream`. Children should use
  50. // `fs` for file content, and may add more files.
  51. //
  52. // If there is a split test file named "STDIN", then its contents will be
  53. // provided at `input_stream` instead of `fs`. Otherwise, `input_stream` will
  54. // be null.
  55. //
  56. // This cannot be used for test expectations, such as EXPECT_TRUE.
  57. //
  58. // The return value should be an error if there was an abnormal error, and
  59. // RunResult otherwise.
  60. virtual auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
  61. llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>& fs,
  62. FILE* input_stream, llvm::raw_pwrite_stream& output_stream,
  63. llvm::raw_pwrite_stream& error_stream)
  64. -> ErrorOr<RunResult> = 0;
  65. // Returns default arguments. Only called when a file doesn't set ARGS.
  66. virtual auto GetDefaultArgs() -> llvm::SmallVector<std::string> = 0;
  67. // Returns a map of string replacements to implement `%{key}` -> `value` in
  68. // arguments.
  69. virtual auto GetArgReplacements() -> llvm::StringMap<std::string> {
  70. return {};
  71. }
  72. // Returns a regex to match the default file when a line may not be present.
  73. // May return nullptr if unused. If GetLineNumberReplacements returns an entry
  74. // with has_file=false, this is required.
  75. virtual auto GetDefaultFileRE(llvm::ArrayRef<llvm::StringRef> /*filenames*/)
  76. -> std::optional<RE2> {
  77. return std::nullopt;
  78. }
  79. // Returns replacement information for line numbers. See LineReplacement for
  80. // construction.
  81. virtual auto GetLineNumberReplacements(
  82. llvm::ArrayRef<llvm::StringRef> filenames)
  83. -> llvm::SmallVector<LineNumberReplacement>;
  84. // Optionally allows children to provide extra replacements for autoupdate.
  85. virtual auto DoExtraCheckReplacements(std::string& /*check_line*/) -> void {}
  86. // Whether to allow running the test in parallel, particularly for autoupdate.
  87. // This can be overridden to force some tests to be run serially. At any given
  88. // time, all parallel tests and a single non-parallel test will be allowed to
  89. // run.
  90. virtual auto AllowParallelRun() const -> bool { return true; }
  91. // Runs a test and compares output. This keeps output split by line so that
  92. // issues are a little easier to identify by the different line.
  93. auto TestBody() -> void final;
  94. // Runs the test and autoupdates checks. Returns true if updated.
  95. auto Autoupdate() -> ErrorOr<bool>;
  96. // Runs the test and dumps output.
  97. auto DumpOutput() -> ErrorOr<Success>;
  98. // Returns the name of the test (relative to the repo root).
  99. auto test_name() const -> llvm::StringRef { return test_name_; }
  100. private:
  101. // An optional mutex for output. If provided, it will be locked during `Run`
  102. // when stderr/stdout are being captured (SET-CAPTURE-CONSOLE-OUTPUT), in
  103. // order to avoid output conflicts.
  104. std::mutex* output_mutex_;
  105. llvm::StringRef test_name_;
  106. };
  107. // Aggregate a name and factory function for tests using this framework.
  108. struct FileTestFactory {
  109. // The test fixture name.
  110. const char* name;
  111. // A factory function for tests. The output_mutex is optional; see
  112. // `FileTestBase::output_mutex_`.
  113. std::function<auto(llvm::StringRef exe_path, std::mutex* output_mutex,
  114. llvm::StringRef test_name)
  115. ->FileTestBase*>
  116. factory_fn;
  117. };
  118. // Must be implemented by the individual file_test to initialize tests.
  119. //
  120. // We can't use INSTANTIATE_TEST_CASE_P because of ordering issues between
  121. // container initialization and test instantiation by InitGoogleTest, but this
  122. // also allows us more flexibility in execution.
  123. //
  124. // The `CARBON_FILE_TEST_FACTOR` macro below provides a standard, convenient way
  125. // to implement this function.
  126. extern auto GetFileTestFactory() -> FileTestFactory;
  127. // Provides a standard GetFileTestFactory implementation.
  128. #define CARBON_FILE_TEST_FACTORY(Name) \
  129. auto GetFileTestFactory() -> FileTestFactory { \
  130. return {#Name, [](llvm::StringRef exe_path, std::mutex* output_mutex, \
  131. llvm::StringRef test_name) { \
  132. return new Name(exe_path, output_mutex, test_name); \
  133. }}; \
  134. }
  135. } // namespace Carbon::Testing
  136. #endif // CARBON_TESTING_FILE_TEST_FILE_TEST_BASE_H_