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