scoped_path_override.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TEST_SCOPED_PATH_OVERRIDE_H_
  5. #define BASE_TEST_SCOPED_PATH_OVERRIDE_H_
  6. #include "base/files/scoped_temp_dir.h"
  7. #include "third_party/abseil-cpp/absl/types/optional.h"
  8. namespace base {
  9. class FilePath;
  10. // Sets a path override on construction, and removes it when the object goes out
  11. // of scope. This class is intended to be used by tests that need to override
  12. // paths to ensure their overrides are properly handled and reverted when the
  13. // scope of the test is left.
  14. class ScopedPathOverride {
  15. public:
  16. // Contructor that initializes the override to a scoped temp directory.
  17. explicit ScopedPathOverride(int key);
  18. // Constructor that would use a path provided by the user.
  19. ScopedPathOverride(int key, const FilePath& dir);
  20. // See PathService::OverrideAndCreateIfNeeded.
  21. ScopedPathOverride(int key,
  22. const FilePath& path,
  23. bool is_absolute,
  24. bool create);
  25. ScopedPathOverride(const ScopedPathOverride&) = delete;
  26. ScopedPathOverride& operator=(const ScopedPathOverride&) = delete;
  27. ~ScopedPathOverride();
  28. private:
  29. // Used for saving original_override_ when an override already exists.
  30. void SaveOriginal();
  31. int key_;
  32. ScopedTempDir temp_dir_;
  33. absl::optional<FilePath> original_override_;
  34. };
  35. } // namespace base
  36. #endif // BASE_TEST_SCOPED_PATH_OVERRIDE_H_