driver_file_test_base.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_TOOLCHAIN_DRIVER_DRIVER_FILE_TEST_BASE_H_
  5. #define CARBON_TOOLCHAIN_DRIVER_DRIVER_FILE_TEST_BASE_H_
  6. #include <cstdio>
  7. #include <fstream>
  8. #include "llvm/ADT/ScopeExit.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "llvm/ADT/StringRef.h"
  11. #include "llvm/Support/MemoryBuffer.h"
  12. #include "llvm/Support/VirtualFileSystem.h"
  13. #include "testing/file_test/file_test_base.h"
  14. #include "toolchain/driver/driver.h"
  15. namespace Carbon::Testing {
  16. // Provides common test support for the driver. This is used by file tests in
  17. // phase subdirectories.
  18. class DriverFileTestBase : public FileTestBase {
  19. public:
  20. using FileTestBase::FileTestBase;
  21. auto RunWithFiles(const llvm::SmallVector<TestFile>& test_files,
  22. llvm::raw_pwrite_stream& stdout,
  23. llvm::raw_pwrite_stream& stderr) -> bool override {
  24. // Prepare a list of filenames for MakeArgs. Also create the files
  25. // in-memory.
  26. llvm::SmallVector<llvm::StringRef> test_file_names;
  27. llvm::vfs::InMemoryFileSystem fs;
  28. for (const auto& test_file : test_files) {
  29. test_file_names.push_back(test_file.filename);
  30. if (!fs.addFile(test_file.filename, /*ModificationTime=*/0,
  31. llvm::MemoryBuffer::getMemBuffer(test_file.content))) {
  32. ADD_FAILURE() << "File is repeated: " << test_file.filename;
  33. return false;
  34. }
  35. }
  36. Driver driver(fs, stdout, stderr);
  37. return driver.RunFullCommand(MakeArgs(test_file_names));
  38. }
  39. virtual auto MakeArgs(const llvm::SmallVector<llvm::StringRef>& test_files)
  40. -> llvm::SmallVector<llvm::StringRef> = 0;
  41. };
  42. } // namespace Carbon::Testing
  43. #endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_FILE_TEST_BASE_H_