driver_file_test_base.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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/SmallVector.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/Support/MemoryBuffer.h"
  11. #include "llvm/Support/VirtualFileSystem.h"
  12. #include "testing/file_test/file_test_base.h"
  13. #include "toolchain/driver/driver.h"
  14. namespace Carbon::Testing {
  15. // Provides common test support for the driver. This is used by file tests in
  16. // phase subdirectories.
  17. class DriverFileTestBase : public FileTestBase {
  18. public:
  19. using FileTestBase::FileTestBase;
  20. auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
  21. const llvm::SmallVector<TestFile>& test_files,
  22. llvm::raw_pwrite_stream& stdout, llvm::raw_pwrite_stream& stderr)
  23. -> ErrorOr<bool> override {
  24. // Create the files in-memory.
  25. llvm::vfs::InMemoryFileSystem fs;
  26. for (const auto& test_file : test_files) {
  27. if (!fs.addFile(test_file.filename, /*ModificationTime=*/0,
  28. llvm::MemoryBuffer::getMemBuffer(test_file.content))) {
  29. return ErrorBuilder() << "File is repeated: " << test_file.filename;
  30. }
  31. }
  32. Driver driver(fs, stdout, stderr);
  33. return driver.RunCommand(test_args);
  34. }
  35. };
  36. } // namespace Carbon::Testing
  37. #endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_FILE_TEST_BASE_H_