file_test.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include "absl/flags/flag.h"
  5. #include "explorer/parse_and_execute/parse_and_execute.h"
  6. #include "testing/file_test/file_test_base.h"
  7. #include "testing/util/test_raw_ostream.h"
  8. ABSL_FLAG(bool, trace, false,
  9. "Set to true to run tests with tracing enabled, even if they don't "
  10. "otherwise specify it. This does not result in checking trace output "
  11. "contents; it essentially only verifies there's not a crash bug.");
  12. namespace Carbon::Testing {
  13. namespace {
  14. class ParseAndExecuteTestFile : public FileTestBase {
  15. public:
  16. explicit ParseAndExecuteTestFile(const std::filesystem::path& path)
  17. : FileTestBase(path) {}
  18. auto RunWithFiles(const llvm::SmallVector<llvm::StringRef>& test_args,
  19. const llvm::SmallVector<TestFile>& test_files,
  20. llvm::raw_pwrite_stream& stdout,
  21. llvm::raw_pwrite_stream& stderr) -> bool override {
  22. CARBON_CHECK(test_args.empty())
  23. << "ARGS are not currently used in explorer's file_test.";
  24. if (test_files.size() != 1) {
  25. ADD_FAILURE() << "Only 1 file is supported: " << test_files.size()
  26. << " provided";
  27. return false;
  28. }
  29. // Trace output is only checked for a few tests.
  30. bool check_trace_output =
  31. path().string().find("/trace_testdata/") != std::string::npos;
  32. // Capture trace streaming, but only when in debug mode.
  33. TraceStream trace_stream;
  34. TestRawOstream trace_stream_ostream;
  35. if (check_trace_output || absl::GetFlag(FLAGS_trace)) {
  36. trace_stream.set_stream(check_trace_output ? &stdout
  37. : &trace_stream_ostream);
  38. trace_stream.set_allowed_phases({ProgramPhase::All});
  39. trace_stream.set_allowed_file_kinds({FileKind::Main});
  40. }
  41. // Set the location of the prelude.
  42. char* test_srcdir = getenv("TEST_SRCDIR");
  43. CARBON_CHECK(test_srcdir != nullptr);
  44. std::string prelude_path(test_srcdir);
  45. prelude_path += "/carbon/explorer/data/prelude.carbon";
  46. // Run the parse. Parser debug output is always off because it's difficult
  47. // to redirect.
  48. auto result = ParseAndExecute(
  49. prelude_path, test_files[0].filename, test_files[0].content,
  50. /*parser_debug=*/false, &trace_stream, &stdout);
  51. // This mirrors printing currently done by main.cpp.
  52. if (result.ok()) {
  53. stdout << "result: " << *result << "\n";
  54. } else {
  55. stderr << result.error() << "\n";
  56. }
  57. // Skip trace test check as they use stdout stream instead of
  58. // trace_stream_ostream
  59. if (absl::GetFlag(FLAGS_trace)) {
  60. CARBON_CHECK(!check_trace_output)
  61. << "trace tests should only be run in the default mode.";
  62. EXPECT_FALSE(trace_stream_ostream.TakeStr().empty())
  63. << "Tracing should always do something";
  64. }
  65. return result.ok();
  66. }
  67. auto GetDefaultArgs() -> llvm::SmallVector<std::string> override {
  68. return {};
  69. }
  70. };
  71. } // namespace
  72. extern auto RegisterFileTests(
  73. const llvm::SmallVector<std::filesystem::path>& paths) -> void {
  74. ParseAndExecuteTestFile::RegisterTests(
  75. "ParseAndExecuteTestFile", paths, [](const std::filesystem::path& path) {
  76. return new ParseAndExecuteTestFile(path);
  77. });
  78. }
  79. } // namespace Carbon::Testing