about summary refs log tree commit diff
path: root/third_party/nix/src/libstore/mock-binary-cache-store.hh
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/nix/src/libstore/mock-binary-cache-store.hh')
-rw-r--r--third_party/nix/src/libstore/mock-binary-cache-store.hh59
1 files changed, 59 insertions, 0 deletions
diff --git a/third_party/nix/src/libstore/mock-binary-cache-store.hh b/third_party/nix/src/libstore/mock-binary-cache-store.hh
new file mode 100644
index 000000000000..419077b6bbb0
--- /dev/null
+++ b/third_party/nix/src/libstore/mock-binary-cache-store.hh
@@ -0,0 +1,59 @@
+#pragma once
+
+#include <absl/container/btree_map.h>
+#include <absl/container/flat_hash_map.h>
+
+#include "libstore/binary-cache-store.hh"
+
+namespace nix {
+
+// MockBinaryCacheStore implements a memory-based BinaryCacheStore, for use in
+// tests.
+class MockBinaryCacheStore : public BinaryCacheStore {
+ public:
+  MockBinaryCacheStore(const Params& params);
+
+  // Store API
+
+  std::string getUri() override;
+
+  bool fileExists(const std::string& path) override;
+
+  void upsertFile(const std::string& path, const std::string& data,
+                  const std::string& mimeType) override;
+
+  void getFile(
+      const std::string& path,
+      Callback<std::shared_ptr<std::string>> callback) noexcept override;
+
+  PathSet queryAllValidPaths() override;
+
+  // Test API
+
+  // Remove a file from the store.
+  void DeleteFile(const std::string& path);
+
+  // Same as upsert, but bypasses injected errors.
+  void SetFileContentsForTest(const std::string& path, const std::string& data,
+                              const std::string& mimeType);
+
+  void PrepareErrorInjection(const std::string& path,
+                             std::function<void()> throw_func);
+
+  void CancelErrorInjection(const std::string& path);
+
+  // Internals
+
+ private:
+  void ThrowInjectedErrors(const std::string& path);
+
+  struct MemoryFile {
+    std::string data;
+    std::string mimeType;
+  };
+
+  absl::btree_map<std::string, MemoryFile> contents_;
+  absl::flat_hash_map<std::string, std::function<void()>> errorInjections_;
+};
+
+}  // namespace nix