file_helpers.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  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_BASE_FILE_HELPERS_H_
  5. #define CARBON_TESTING_BASE_FILE_HELPERS_H_
  6. #include <filesystem>
  7. #include <string>
  8. #include "common/error.h"
  9. namespace Carbon::Testing {
  10. // Reads a file to string.
  11. auto ReadFile(std::filesystem::path path) -> ErrorOr<std::string>;
  12. // Writes a test file to disk and returns an error or the full path to the file.
  13. //
  14. // Note that this expects to be run from within a GoogleTest test, and relies on
  15. // global state of GoogleTest.
  16. //
  17. // This locates a suitable temporary directory for the test, creates a file with
  18. // the requested name in that directory, and writes the provided content to that
  19. // file. The full path to the written file is returned.
  20. //
  21. // Where possible, this will use a Bazel-provided test temporary directory.
  22. // However, if unavailable, it falls back to a system temporary directory. This
  23. // helps tests be runnable outside of Bazel, for example under a debugger. It
  24. // also works to create test file names that are unlikely to conflict with other
  25. // tests when run.
  26. auto WriteTestFile(llvm::StringRef name, llvm::StringRef contents)
  27. -> ErrorOr<std::filesystem::path>;
  28. } // namespace Carbon::Testing
  29. #endif // CARBON_TESTING_BASE_FILE_HELPERS_H_