1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-19 08:49:35 +01:00

Add new C API for working with derivations

Also test the APIs we just added.
This commit is contained in:
John Ericson 2025-09-02 11:20:51 -04:00
parent 1a69fc6ab5
commit 9bc218ca3f
13 changed files with 270 additions and 47 deletions

View file

@ -9,4 +9,5 @@ headers = files(
'outputs-spec.hh',
'path.hh',
'protocol.hh',
'test-main.hh',
)

View file

@ -12,33 +12,32 @@
#include <gtest/gtest.h>
namespace nixC {
class nix_api_store_test : public nix_api_util_context
class nix_api_store_test_base : public nix_api_util_context
{
public:
nix_api_store_test()
nix_api_store_test_base()
{
nix_libstore_init(ctx);
init_local_store();
};
~nix_api_store_test() override
~nix_api_store_test_base() override
{
nix_store_free(store);
for (auto & path : std::filesystem::recursive_directory_iterator(nixDir)) {
std::filesystem::permissions(path, std::filesystem::perms::owner_all);
if (exists(std::filesystem::path{nixDir})) {
for (auto & path : std::filesystem::recursive_directory_iterator(nixDir)) {
std::filesystem::permissions(path, std::filesystem::perms::owner_all);
}
std::filesystem::remove_all(nixDir);
}
std::filesystem::remove_all(nixDir);
}
Store * store;
std::string nixDir;
std::string nixStoreDir;
std::string nixStateDir;
std::string nixLogDir;
protected:
void init_local_store()
Store * open_local_store()
{
#ifdef _WIN32
// no `mkdtemp` with MinGW
@ -66,11 +65,37 @@ protected:
const char ** params[] = {p1, p2, p3, nullptr};
store = nix_store_open(ctx, "local", params);
auto * store = nix_store_open(ctx, "local", params);
if (!store) {
std::string errMsg = nix_err_msg(nullptr, ctx, nullptr);
ASSERT_NE(store, nullptr) << "Could not open store: " << errMsg;
EXPECT_NE(store, nullptr) << "Could not open store: " << errMsg;
assert(store);
};
return store;
}
};
class nix_api_store_test : public nix_api_store_test_base
{
public:
nix_api_store_test()
: nix_api_store_test_base{}
{
init_local_store();
};
~nix_api_store_test() override
{
nix_store_free(store);
}
Store * store;
protected:
void init_local_store()
{
store = open_local_store();
}
};
} // namespace nixC

View file

@ -0,0 +1,13 @@
#pragma once
///@file
namespace nix {
/**
* Call this for a GTest test suite that will including performing Nix
* builds, before running tests.
*/
int testMainForBuidingPre(int argc, char ** argv);
} // namespace nix

View file

@ -34,6 +34,7 @@ sources = files(
'derived-path.cc',
'outputs-spec.cc',
'path.cc',
'test-main.cc',
)
subdir('include/nix/store/tests')

View file

@ -0,0 +1,47 @@
#include <cstdlib>
#include "nix/store/globals.hh"
#include "nix/util/logging.hh"
#include "nix/store/tests/test-main.hh"
namespace nix {
int testMainForBuidingPre(int argc, char ** argv)
{
if (argc > 1 && std::string_view(argv[1]) == "__build-remote") {
printError("test-build-remote: not supported in libexpr unit tests");
return EXIT_FAILURE;
}
// Disable build hook. We won't be testing remote builds in these unit tests. If we do, fix the above build hook.
settings.buildHook = {};
// No substituters, unless a test specifically requests.
settings.substituters = {};
#ifdef __linux__ // should match the conditional around sandboxBuildDir declaration.
// When building and testing nix within the host's Nix sandbox, our store dir will be located in the host's
// sandboxBuildDir, e.g.: Host
// storeDir = /nix/store
// sandboxBuildDir = /build
// This process
// storeDir = /build/foo/bar/store
// sandboxBuildDir = /build
// However, we have a rule that the store dir must not be inside the storeDir, so we need to pick a different
// sandboxBuildDir.
settings.sandboxBuildDir = "/test-build-dir-instead-of-usual-build-dir";
#endif
#ifdef __APPLE__
// Avoid this error, when already running in a sandbox:
// sandbox-exec: sandbox_apply: Operation not permitted
settings.sandboxMode = smDisabled;
setEnv("_NIX_TEST_NO_SANDBOX", "1");
#endif
return EXIT_SUCCESS;
}
} // namespace nix