file_helpers.h 1.5 KB

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