Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
nix_api_store.hh
Go to the documentation of this file.
1#pragma once
4
6#include <filesystem>
7
8#include "nix_api_store.h"
9#include "nix_api_store_internal.h"
10
11#include <filesystem>
12#include <gtest/gtest.h>
13
14namespace nixC {
15
16class nix_api_store_test_base : public nix_api_util_context
17{
18public:
19 nix_api_store_test_base()
20 {
21 nix_libstore_init(ctx);
22 };
23
24 ~nix_api_store_test_base() override
25 try {
26 nix::deletePath(nixDir);
27 } catch (...) {
29 }
30
31 std::string nixDir;
32 std::string nixStoreDir;
33 std::string nixStateDir;
34 std::string nixLogDir;
35
36protected:
37 Store * open_local_store()
38 {
39#ifdef _WIN32
40 // no `mkdtemp` with MinGW
41 auto tmpl = nix::defaultTempDir() / "tests_nix-store.";
42 for (size_t i = 0; true; ++i) {
43 nixDir = tmpl.string() + std::to_string(i);
44 if (std::filesystem::create_directory(nixDir))
45 break;
46 }
47#else
48 // resolve any symlinks in i.e. on macOS /tmp -> /private/tmp
49 // because this is not allowed for a nix store.
50 auto tmpl = nix::absPath(nix::defaultTempDir() / "tests_nix-store.XXXXXX", nullptr, true);
51 nixDir = mkdtemp((char *) tmpl.c_str());
52#endif
53
54 nixStoreDir = nixDir + "/my_nix_store";
55 nixStateDir = nixDir + "/my_state";
56 nixLogDir = nixDir + "/my_log";
57
58 // Options documented in `nix help-stores`
59 const char * p1[] = {"store", nixStoreDir.c_str()};
60 const char * p2[] = {"state", nixStateDir.c_str()};
61 const char * p3[] = {"log", nixLogDir.c_str()};
62
63 const char ** params[] = {p1, p2, p3, nullptr};
64
65 auto * store = nix_store_open(ctx, "local", params);
66 if (!store) {
67 std::string errMsg = nix_err_msg(nullptr, ctx, nullptr);
68 EXPECT_NE(store, nullptr) << "Could not open store: " << errMsg;
69 assert(store);
70 };
71 return store;
72 }
73};
74
75class nix_api_store_test : public nix_api_store_test_base
76{
77public:
78 nix_api_store_test()
79 : nix_api_store_test_base{} {};
80
81 void SetUp() override
82 {
83#ifdef _WIN32
84 GTEST_SKIP() << "Wine does not support symlinks needed for local store gcroots";
85#endif
86 store = open_local_store();
87 }
88
89 ~nix_api_store_test() override
90 {
91 if (store)
92 nix_store_free(store);
93 }
94
95 Store * store = nullptr;
96};
97
98} // namespace nixC
Utilities for working with the file system and file paths.
std::filesystem::path defaultTempDir()
Definition file-system.cc:96
void deletePath(const std::filesystem::path &path)
Definition file-system.cc:313
void ignoreExceptionInDestructor(Verbosity lvl=lvlError)
Definition util.cc:265