mirror of
https://github.com/NixOS/nix.git
synced 2025-11-09 03:56:01 +01:00
Merge pull request #14254 from roberth/upstream-RossComputerGuy/feat/expose-computefsclosure
libstore-c: add nix_store_get_fs_closure #14025 with tests and realise error fix
This commit is contained in:
commit
6ca2efc7d4
3 changed files with 607 additions and 1 deletions
|
|
@ -126,6 +126,36 @@ StorePath * nix_store_parse_path(nix_c_context * context, Store * store, const c
|
|||
NIXC_CATCH_ERRS_NULL
|
||||
}
|
||||
|
||||
nix_err nix_store_get_fs_closure(
|
||||
nix_c_context * context,
|
||||
Store * store,
|
||||
const StorePath * store_path,
|
||||
bool flip_direction,
|
||||
bool include_outputs,
|
||||
bool include_derivers,
|
||||
void * userdata,
|
||||
void (*callback)(nix_c_context * context, void * userdata, const StorePath * store_path))
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
const auto nixStore = store->ptr;
|
||||
|
||||
nix::StorePathSet set;
|
||||
nixStore->computeFSClosure(store_path->path, set, flip_direction, include_outputs, include_derivers);
|
||||
|
||||
if (callback) {
|
||||
for (const auto & path : set) {
|
||||
const StorePath tmp{path};
|
||||
callback(context, userdata, &tmp);
|
||||
if (context && context->last_err_code != NIX_OK)
|
||||
return context->last_err_code;
|
||||
}
|
||||
}
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
}
|
||||
|
||||
nix_err nix_store_realise(
|
||||
nix_c_context * context,
|
||||
Store * store,
|
||||
|
|
@ -143,6 +173,14 @@ nix_err nix_store_realise(
|
|||
const auto nixStore = store->ptr;
|
||||
auto results = nixStore->buildPathsWithResults(paths, nix::bmNormal, nixStore);
|
||||
|
||||
assert(results.size() == 1);
|
||||
|
||||
// Check if any builds failed
|
||||
for (auto & result : results) {
|
||||
if (auto * failureP = result.tryGetFailure())
|
||||
failureP->rethrow();
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
for (const auto & result : results) {
|
||||
if (auto * success = result.tryGetSuccess()) {
|
||||
|
|
|
|||
|
|
@ -186,6 +186,8 @@ nix_err nix_store_real_path(
|
|||
* @param[in] path Path to build
|
||||
* @param[in] userdata data to pass to every callback invocation
|
||||
* @param[in] callback called for every realised output
|
||||
* @return NIX_OK if the build succeeded, or an error code if the build/scheduling/outputs/copying/etc failed.
|
||||
* On error, the callback is never invoked and error information is stored in context.
|
||||
*/
|
||||
nix_err nix_store_realise(
|
||||
nix_c_context * context,
|
||||
|
|
@ -245,6 +247,35 @@ void nix_derivation_free(nix_derivation * drv);
|
|||
*/
|
||||
nix_err nix_store_copy_closure(nix_c_context * context, Store * srcStore, Store * dstStore, StorePath * path);
|
||||
|
||||
/**
|
||||
* @brief Gets the closure of a specific store path
|
||||
*
|
||||
* @note The callback borrows each StorePath only for the duration of the call.
|
||||
*
|
||||
* @param[out] context Optional, stores error information
|
||||
* @param[in] store nix store reference
|
||||
* @param[in] store_path The path to compute from
|
||||
* @param[in] flip_direction If false, compute the forward closure (paths referenced by any store path in the closure).
|
||||
* If true, compute the backward closure (paths that reference any store path in the closure).
|
||||
* @param[in] include_outputs If flip_direction is false: for any derivation in the closure, include its outputs.
|
||||
* If flip_direction is true: for any output in the closure, include derivations that produce
|
||||
* it.
|
||||
* @param[in] include_derivers If flip_direction is false: for any output in the closure, include the derivation that
|
||||
* produced it.
|
||||
* If flip_direction is true: for any derivation in the closure, include its outputs.
|
||||
* @param[in] callback The function to call for every store path, in no particular order
|
||||
* @param[in] userdata The userdata to pass to the callback
|
||||
*/
|
||||
nix_err nix_store_get_fs_closure(
|
||||
nix_c_context * context,
|
||||
Store * store,
|
||||
const StorePath * store_path,
|
||||
bool flip_direction,
|
||||
bool include_outputs,
|
||||
bool include_derivers,
|
||||
void * userdata,
|
||||
void (*callback)(nix_c_context * context, void * userdata, const StorePath * store_path));
|
||||
|
||||
// cffi end
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -218,6 +218,70 @@ struct LambdaAdapter
|
|||
}
|
||||
};
|
||||
|
||||
class NixApiStoreTestWithRealisedPath : public nix_api_store_test_base
|
||||
{
|
||||
public:
|
||||
StorePath * drvPath = nullptr;
|
||||
nix_derivation * drv = nullptr;
|
||||
Store * store = nullptr;
|
||||
StorePath * outPath = nullptr;
|
||||
|
||||
void SetUp() override
|
||||
{
|
||||
nix_api_store_test_base::SetUp();
|
||||
|
||||
nix::experimentalFeatureSettings.set("extra-experimental-features", "ca-derivations");
|
||||
nix::settings.substituters = {};
|
||||
|
||||
store = open_local_store();
|
||||
|
||||
std::filesystem::path unitTestData{getenv("_NIX_TEST_UNIT_DATA")};
|
||||
std::ifstream t{unitTestData / "derivation/ca/self-contained.json"};
|
||||
std::stringstream buffer;
|
||||
buffer << t.rdbuf();
|
||||
|
||||
// Replace the hardcoded system with the current system
|
||||
std::string jsonStr = nix::replaceStrings(buffer.str(), "x86_64-linux", nix::settings.thisSystem.get());
|
||||
|
||||
drv = nix_derivation_from_json(ctx, store, jsonStr.c_str());
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drv, nullptr);
|
||||
|
||||
drvPath = nix_add_derivation(ctx, store, drv);
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drvPath, nullptr);
|
||||
|
||||
auto cb = LambdaAdapter{.fun = [&](const char * outname, const StorePath * outPath_) {
|
||||
ASSERT_NE(outname, nullptr) << "Output name should not be NULL";
|
||||
auto is_valid_path = nix_store_is_valid_path(ctx, store, outPath_);
|
||||
ASSERT_EQ(is_valid_path, true);
|
||||
ASSERT_STREQ(outname, "out") << "Expected single 'out' output";
|
||||
ASSERT_EQ(outPath, nullptr) << "Output path callback should only be called once";
|
||||
outPath = nix_store_path_clone(outPath_);
|
||||
}};
|
||||
|
||||
auto ret = nix_store_realise(
|
||||
ctx, store, drvPath, static_cast<void *>(&cb), decltype(cb)::call_void<const char *, const StorePath *>);
|
||||
assert_ctx_ok();
|
||||
ASSERT_EQ(ret, NIX_OK);
|
||||
ASSERT_NE(outPath, nullptr) << "Derivation should have produced an output";
|
||||
}
|
||||
|
||||
void TearDown() override
|
||||
{
|
||||
if (drvPath)
|
||||
nix_store_path_free(drvPath);
|
||||
if (outPath)
|
||||
nix_store_path_free(outPath);
|
||||
if (drv)
|
||||
nix_derivation_free(drv);
|
||||
if (store)
|
||||
nix_store_free(store);
|
||||
|
||||
nix_api_store_test_base::TearDown();
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(nix_api_store_test_base, build_from_json)
|
||||
{
|
||||
// FIXME get rid of these
|
||||
|
|
@ -232,7 +296,10 @@ TEST_F(nix_api_store_test_base, build_from_json)
|
|||
std::stringstream buffer;
|
||||
buffer << t.rdbuf();
|
||||
|
||||
auto * drv = nix_derivation_from_json(ctx, store, buffer.str().c_str());
|
||||
// Replace the hardcoded system with the current system
|
||||
std::string jsonStr = nix::replaceStrings(buffer.str(), "x86_64-linux", nix::settings.thisSystem.get());
|
||||
|
||||
auto * drv = nix_derivation_from_json(ctx, store, jsonStr.c_str());
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drv, nullptr);
|
||||
|
||||
|
|
@ -240,15 +307,21 @@ TEST_F(nix_api_store_test_base, build_from_json)
|
|||
assert_ctx_ok();
|
||||
ASSERT_NE(drv, nullptr);
|
||||
|
||||
int callbackCount = 0;
|
||||
auto cb = LambdaAdapter{.fun = [&](const char * outname, const StorePath * outPath) {
|
||||
ASSERT_NE(outname, nullptr);
|
||||
ASSERT_STREQ(outname, "out");
|
||||
ASSERT_NE(outPath, nullptr);
|
||||
auto is_valid_path = nix_store_is_valid_path(ctx, store, outPath);
|
||||
ASSERT_EQ(is_valid_path, true);
|
||||
callbackCount++;
|
||||
}};
|
||||
|
||||
auto ret = nix_store_realise(
|
||||
ctx, store, drvPath, static_cast<void *>(&cb), decltype(cb)::call_void<const char *, const StorePath *>);
|
||||
assert_ctx_ok();
|
||||
ASSERT_EQ(ret, NIX_OK);
|
||||
ASSERT_EQ(callbackCount, 1) << "Callback should have been invoked exactly once";
|
||||
|
||||
// Clean up
|
||||
nix_store_path_free(drvPath);
|
||||
|
|
@ -256,4 +329,468 @@ TEST_F(nix_api_store_test_base, build_from_json)
|
|||
nix_store_free(store);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test_base, nix_store_realise_invalid_system)
|
||||
{
|
||||
// Test that nix_store_realise properly reports errors when the system is invalid
|
||||
nix::experimentalFeatureSettings.set("extra-experimental-features", "ca-derivations");
|
||||
nix::settings.substituters = {};
|
||||
|
||||
auto * store = open_local_store();
|
||||
|
||||
std::filesystem::path unitTestData{getenv("_NIX_TEST_UNIT_DATA")};
|
||||
std::ifstream t{unitTestData / "derivation/ca/self-contained.json"};
|
||||
std::stringstream buffer;
|
||||
buffer << t.rdbuf();
|
||||
|
||||
// Use an invalid system that cannot be built
|
||||
std::string jsonStr = nix::replaceStrings(buffer.str(), "x86_64-linux", "bogus65-bogusos");
|
||||
|
||||
auto * drv = nix_derivation_from_json(ctx, store, jsonStr.c_str());
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drv, nullptr);
|
||||
|
||||
auto * drvPath = nix_add_derivation(ctx, store, drv);
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drvPath, nullptr);
|
||||
|
||||
int callbackCount = 0;
|
||||
auto cb = LambdaAdapter{.fun = [&](const char * outname, const StorePath * outPath) { callbackCount++; }};
|
||||
|
||||
auto ret = nix_store_realise(
|
||||
ctx, store, drvPath, static_cast<void *>(&cb), decltype(cb)::call_void<const char *, const StorePath *>);
|
||||
|
||||
// Should fail with an error
|
||||
ASSERT_NE(ret, NIX_OK);
|
||||
ASSERT_EQ(callbackCount, 0) << "Callback should not be invoked when build fails";
|
||||
|
||||
// Check that error message is set
|
||||
std::string errMsg = nix_err_msg(nullptr, ctx, nullptr);
|
||||
ASSERT_FALSE(errMsg.empty()) << "Error message should be set";
|
||||
ASSERT_NE(errMsg.find("system"), std::string::npos) << "Error should mention system";
|
||||
|
||||
// Clean up
|
||||
nix_store_path_free(drvPath);
|
||||
nix_derivation_free(drv);
|
||||
nix_store_free(store);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test_base, nix_store_realise_builder_fails)
|
||||
{
|
||||
// Test that nix_store_realise properly reports errors when the builder fails
|
||||
nix::experimentalFeatureSettings.set("extra-experimental-features", "ca-derivations");
|
||||
nix::settings.substituters = {};
|
||||
|
||||
auto * store = open_local_store();
|
||||
|
||||
std::filesystem::path unitTestData{getenv("_NIX_TEST_UNIT_DATA")};
|
||||
std::ifstream t{unitTestData / "derivation/ca/self-contained.json"};
|
||||
std::stringstream buffer;
|
||||
buffer << t.rdbuf();
|
||||
|
||||
// Replace with current system and make builder command fail
|
||||
std::string jsonStr = nix::replaceStrings(buffer.str(), "x86_64-linux", nix::settings.thisSystem.get());
|
||||
jsonStr = nix::replaceStrings(jsonStr, "echo $name foo > $out", "exit 1");
|
||||
|
||||
auto * drv = nix_derivation_from_json(ctx, store, jsonStr.c_str());
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drv, nullptr);
|
||||
|
||||
auto * drvPath = nix_add_derivation(ctx, store, drv);
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drvPath, nullptr);
|
||||
|
||||
int callbackCount = 0;
|
||||
auto cb = LambdaAdapter{.fun = [&](const char * outname, const StorePath * outPath) { callbackCount++; }};
|
||||
|
||||
auto ret = nix_store_realise(
|
||||
ctx, store, drvPath, static_cast<void *>(&cb), decltype(cb)::call_void<const char *, const StorePath *>);
|
||||
|
||||
// Should fail with an error
|
||||
ASSERT_NE(ret, NIX_OK);
|
||||
ASSERT_EQ(callbackCount, 0) << "Callback should not be invoked when build fails";
|
||||
|
||||
// Check that error message is set
|
||||
std::string errMsg = nix_err_msg(nullptr, ctx, nullptr);
|
||||
ASSERT_FALSE(errMsg.empty()) << "Error message should be set";
|
||||
|
||||
// Clean up
|
||||
nix_store_path_free(drvPath);
|
||||
nix_derivation_free(drv);
|
||||
nix_store_free(store);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test_base, nix_store_realise_builder_no_output)
|
||||
{
|
||||
// Test that nix_store_realise properly reports errors when builder succeeds but produces no output
|
||||
nix::experimentalFeatureSettings.set("extra-experimental-features", "ca-derivations");
|
||||
nix::settings.substituters = {};
|
||||
|
||||
auto * store = open_local_store();
|
||||
|
||||
std::filesystem::path unitTestData{getenv("_NIX_TEST_UNIT_DATA")};
|
||||
std::ifstream t{unitTestData / "derivation/ca/self-contained.json"};
|
||||
std::stringstream buffer;
|
||||
buffer << t.rdbuf();
|
||||
|
||||
// Replace with current system and make builder succeed but not produce output
|
||||
std::string jsonStr = nix::replaceStrings(buffer.str(), "x86_64-linux", nix::settings.thisSystem.get());
|
||||
jsonStr = nix::replaceStrings(jsonStr, "echo $name foo > $out", "true");
|
||||
|
||||
auto * drv = nix_derivation_from_json(ctx, store, jsonStr.c_str());
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drv, nullptr);
|
||||
|
||||
auto * drvPath = nix_add_derivation(ctx, store, drv);
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drvPath, nullptr);
|
||||
|
||||
int callbackCount = 0;
|
||||
auto cb = LambdaAdapter{.fun = [&](const char * outname, const StorePath * outPath) { callbackCount++; }};
|
||||
|
||||
auto ret = nix_store_realise(
|
||||
ctx, store, drvPath, static_cast<void *>(&cb), decltype(cb)::call_void<const char *, const StorePath *>);
|
||||
|
||||
// Should fail with an error
|
||||
ASSERT_NE(ret, NIX_OK);
|
||||
ASSERT_EQ(callbackCount, 0) << "Callback should not be invoked when build produces no output";
|
||||
|
||||
// Check that error message is set
|
||||
std::string errMsg = nix_err_msg(nullptr, ctx, nullptr);
|
||||
ASSERT_FALSE(errMsg.empty()) << "Error message should be set";
|
||||
|
||||
// Clean up
|
||||
nix_store_path_free(drvPath);
|
||||
nix_derivation_free(drv);
|
||||
nix_store_free(store);
|
||||
}
|
||||
|
||||
TEST_F(NixApiStoreTestWithRealisedPath, nix_store_get_fs_closure_with_outputs)
|
||||
{
|
||||
// Test closure computation with include_outputs on a derivation path
|
||||
struct CallbackData
|
||||
{
|
||||
std::set<std::string> * paths;
|
||||
};
|
||||
|
||||
std::set<std::string> closure_paths;
|
||||
CallbackData data{&closure_paths};
|
||||
|
||||
auto ret = nix_store_get_fs_closure(
|
||||
ctx,
|
||||
store,
|
||||
drvPath, // Use derivation path
|
||||
false, // flip_direction
|
||||
true, // include_outputs - include the outputs in the closure
|
||||
false, // include_derivers
|
||||
&data,
|
||||
[](nix_c_context * context, void * userdata, const StorePath * path) {
|
||||
auto * data = static_cast<CallbackData *>(userdata);
|
||||
std::string path_str;
|
||||
nix_store_path_name(path, OBSERVE_STRING(path_str));
|
||||
auto [it, inserted] = data->paths->insert(path_str);
|
||||
ASSERT_TRUE(inserted) << "Duplicate path in closure: " << path_str;
|
||||
});
|
||||
assert_ctx_ok();
|
||||
ASSERT_EQ(ret, NIX_OK);
|
||||
|
||||
// The closure should contain the derivation and its outputs
|
||||
ASSERT_GE(closure_paths.size(), 2);
|
||||
|
||||
// Verify the output path is in the closure
|
||||
std::string outPathName;
|
||||
nix_store_path_name(outPath, OBSERVE_STRING(outPathName));
|
||||
ASSERT_EQ(closure_paths.count(outPathName), 1);
|
||||
}
|
||||
|
||||
TEST_F(NixApiStoreTestWithRealisedPath, nix_store_get_fs_closure_without_outputs)
|
||||
{
|
||||
// Test closure computation WITHOUT include_outputs on a derivation path
|
||||
struct CallbackData
|
||||
{
|
||||
std::set<std::string> * paths;
|
||||
};
|
||||
|
||||
std::set<std::string> closure_paths;
|
||||
CallbackData data{&closure_paths};
|
||||
|
||||
auto ret = nix_store_get_fs_closure(
|
||||
ctx,
|
||||
store,
|
||||
drvPath, // Use derivation path
|
||||
false, // flip_direction
|
||||
false, // include_outputs - do NOT include the outputs
|
||||
false, // include_derivers
|
||||
&data,
|
||||
[](nix_c_context * context, void * userdata, const StorePath * path) {
|
||||
auto * data = static_cast<CallbackData *>(userdata);
|
||||
std::string path_str;
|
||||
nix_store_path_name(path, OBSERVE_STRING(path_str));
|
||||
auto [it, inserted] = data->paths->insert(path_str);
|
||||
ASSERT_TRUE(inserted) << "Duplicate path in closure: " << path_str;
|
||||
});
|
||||
assert_ctx_ok();
|
||||
ASSERT_EQ(ret, NIX_OK);
|
||||
|
||||
// Verify the output path is NOT in the closure
|
||||
std::string outPathName;
|
||||
nix_store_path_name(outPath, OBSERVE_STRING(outPathName));
|
||||
ASSERT_EQ(closure_paths.count(outPathName), 0) << "Output path should not be in closure when includeOutputs=false";
|
||||
}
|
||||
|
||||
TEST_F(NixApiStoreTestWithRealisedPath, nix_store_get_fs_closure_flip_direction)
|
||||
{
|
||||
// Test closure computation with flip_direction on a derivation path
|
||||
// When flip_direction=true, we get the reverse dependencies (what depends on this path)
|
||||
// For a derivation, this should NOT include outputs even with include_outputs=true
|
||||
struct CallbackData
|
||||
{
|
||||
std::set<std::string> * paths;
|
||||
};
|
||||
|
||||
std::set<std::string> closure_paths;
|
||||
CallbackData data{&closure_paths};
|
||||
|
||||
auto ret = nix_store_get_fs_closure(
|
||||
ctx,
|
||||
store,
|
||||
drvPath, // Use derivation path
|
||||
true, // flip_direction - get reverse dependencies
|
||||
true, // include_outputs
|
||||
false, // include_derivers
|
||||
&data,
|
||||
[](nix_c_context * context, void * userdata, const StorePath * path) {
|
||||
auto * data = static_cast<CallbackData *>(userdata);
|
||||
std::string path_str;
|
||||
nix_store_path_name(path, OBSERVE_STRING(path_str));
|
||||
auto [it, inserted] = data->paths->insert(path_str);
|
||||
ASSERT_TRUE(inserted) << "Duplicate path in closure: " << path_str;
|
||||
});
|
||||
assert_ctx_ok();
|
||||
ASSERT_EQ(ret, NIX_OK);
|
||||
|
||||
// Verify the output path is NOT in the closure when direction is flipped
|
||||
std::string outPathName;
|
||||
nix_store_path_name(outPath, OBSERVE_STRING(outPathName));
|
||||
ASSERT_EQ(closure_paths.count(outPathName), 0) << "Output path should not be in closure when flip_direction=true";
|
||||
}
|
||||
|
||||
TEST_F(NixApiStoreTestWithRealisedPath, nix_store_get_fs_closure_include_derivers)
|
||||
{
|
||||
// Test closure computation with include_derivers on an output path
|
||||
// This should include the derivation that produced the output
|
||||
struct CallbackData
|
||||
{
|
||||
std::set<std::string> * paths;
|
||||
};
|
||||
|
||||
std::set<std::string> closure_paths;
|
||||
CallbackData data{&closure_paths};
|
||||
|
||||
auto ret = nix_store_get_fs_closure(
|
||||
ctx,
|
||||
store,
|
||||
outPath, // Use output path (not derivation)
|
||||
false, // flip_direction
|
||||
false, // include_outputs
|
||||
true, // include_derivers - include the derivation
|
||||
&data,
|
||||
[](nix_c_context * context, void * userdata, const StorePath * path) {
|
||||
auto * data = static_cast<CallbackData *>(userdata);
|
||||
std::string path_str;
|
||||
nix_store_path_name(path, OBSERVE_STRING(path_str));
|
||||
auto [it, inserted] = data->paths->insert(path_str);
|
||||
ASSERT_TRUE(inserted) << "Duplicate path in closure: " << path_str;
|
||||
});
|
||||
assert_ctx_ok();
|
||||
ASSERT_EQ(ret, NIX_OK);
|
||||
|
||||
// Verify the derivation path is in the closure
|
||||
// Deriver is nasty stateful, and this assertion is only guaranteed because
|
||||
// we're using an empty store as our starting point. Otherwise, if the
|
||||
// output happens to exist, the deriver could be anything.
|
||||
std::string drvPathName;
|
||||
nix_store_path_name(drvPath, OBSERVE_STRING(drvPathName));
|
||||
ASSERT_EQ(closure_paths.count(drvPathName), 1) << "Derivation should be in closure when include_derivers=true";
|
||||
}
|
||||
|
||||
TEST_F(NixApiStoreTestWithRealisedPath, nix_store_realise_output_ordering)
|
||||
{
|
||||
// Test that nix_store_realise returns outputs in alphabetical order by output name.
|
||||
// This test uses a CA derivation with 10 outputs in randomized input order
|
||||
// to verify that the callback order is deterministic and alphabetical.
|
||||
nix::experimentalFeatureSettings.set("extra-experimental-features", "ca-derivations");
|
||||
nix::settings.substituters = {};
|
||||
|
||||
auto * store = open_local_store();
|
||||
|
||||
// Create a CA derivation with 10 outputs using proper placeholders
|
||||
auto outa_ph = nix::hashPlaceholder("outa");
|
||||
auto outb_ph = nix::hashPlaceholder("outb");
|
||||
auto outc_ph = nix::hashPlaceholder("outc");
|
||||
auto outd_ph = nix::hashPlaceholder("outd");
|
||||
auto oute_ph = nix::hashPlaceholder("oute");
|
||||
auto outf_ph = nix::hashPlaceholder("outf");
|
||||
auto outg_ph = nix::hashPlaceholder("outg");
|
||||
auto outh_ph = nix::hashPlaceholder("outh");
|
||||
auto outi_ph = nix::hashPlaceholder("outi");
|
||||
auto outj_ph = nix::hashPlaceholder("outj");
|
||||
|
||||
std::string drvJson = R"({
|
||||
"version": 3,
|
||||
"name": "multi-output-test",
|
||||
"system": ")" + nix::settings.thisSystem.get()
|
||||
+ R"(",
|
||||
"builder": "/bin/sh",
|
||||
"args": ["-c", "echo a > $outa; echo b > $outb; echo c > $outc; echo d > $outd; echo e > $oute; echo f > $outf; echo g > $outg; echo h > $outh; echo i > $outi; echo j > $outj"],
|
||||
"env": {
|
||||
"builder": "/bin/sh",
|
||||
"name": "multi-output-test",
|
||||
"system": ")" + nix::settings.thisSystem.get()
|
||||
+ R"(",
|
||||
"outf": ")" + outf_ph
|
||||
+ R"(",
|
||||
"outd": ")" + outd_ph
|
||||
+ R"(",
|
||||
"outi": ")" + outi_ph
|
||||
+ R"(",
|
||||
"oute": ")" + oute_ph
|
||||
+ R"(",
|
||||
"outh": ")" + outh_ph
|
||||
+ R"(",
|
||||
"outc": ")" + outc_ph
|
||||
+ R"(",
|
||||
"outb": ")" + outb_ph
|
||||
+ R"(",
|
||||
"outg": ")" + outg_ph
|
||||
+ R"(",
|
||||
"outj": ")" + outj_ph
|
||||
+ R"(",
|
||||
"outa": ")" + outa_ph
|
||||
+ R"("
|
||||
},
|
||||
"inputDrvs": {},
|
||||
"inputSrcs": [],
|
||||
"outputs": {
|
||||
"outd": { "hashAlgo": "sha256", "method": "nar" },
|
||||
"outf": { "hashAlgo": "sha256", "method": "nar" },
|
||||
"outg": { "hashAlgo": "sha256", "method": "nar" },
|
||||
"outb": { "hashAlgo": "sha256", "method": "nar" },
|
||||
"outc": { "hashAlgo": "sha256", "method": "nar" },
|
||||
"outi": { "hashAlgo": "sha256", "method": "nar" },
|
||||
"outj": { "hashAlgo": "sha256", "method": "nar" },
|
||||
"outh": { "hashAlgo": "sha256", "method": "nar" },
|
||||
"outa": { "hashAlgo": "sha256", "method": "nar" },
|
||||
"oute": { "hashAlgo": "sha256", "method": "nar" }
|
||||
}
|
||||
})";
|
||||
|
||||
auto * drv = nix_derivation_from_json(ctx, store, drvJson.c_str());
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drv, nullptr);
|
||||
|
||||
auto * drvPath = nix_add_derivation(ctx, store, drv);
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drvPath, nullptr);
|
||||
|
||||
// Realise the derivation - capture the order outputs are returned
|
||||
std::map<std::string, nix::StorePath> outputs;
|
||||
std::vector<std::string> output_order;
|
||||
auto cb = LambdaAdapter{.fun = [&](const char * outname, const StorePath * outPath) {
|
||||
ASSERT_NE(outname, nullptr);
|
||||
ASSERT_NE(outPath, nullptr);
|
||||
output_order.push_back(outname);
|
||||
outputs.emplace(outname, outPath->path);
|
||||
}};
|
||||
|
||||
auto ret = nix_store_realise(
|
||||
ctx, store, drvPath, static_cast<void *>(&cb), decltype(cb)::call_void<const char *, const StorePath *>);
|
||||
assert_ctx_ok();
|
||||
ASSERT_EQ(ret, NIX_OK);
|
||||
ASSERT_EQ(outputs.size(), 10);
|
||||
|
||||
// Verify outputs are returned in alphabetical order by output name
|
||||
std::vector<std::string> expected_order = {
|
||||
"outa", "outb", "outc", "outd", "oute", "outf", "outg", "outh", "outi", "outj"};
|
||||
ASSERT_EQ(output_order, expected_order) << "Outputs should be returned in alphabetical order by output name";
|
||||
|
||||
// Now compute closure with include_outputs and collect paths in order
|
||||
struct CallbackData
|
||||
{
|
||||
std::vector<std::string> * paths;
|
||||
};
|
||||
|
||||
std::vector<std::string> closure_paths;
|
||||
CallbackData data{&closure_paths};
|
||||
|
||||
ret = nix_store_get_fs_closure(
|
||||
ctx,
|
||||
store,
|
||||
drvPath,
|
||||
false, // flip_direction
|
||||
true, // include_outputs - include the outputs in the closure
|
||||
false, // include_derivers
|
||||
&data,
|
||||
[](nix_c_context * context, void * userdata, const StorePath * path) {
|
||||
auto * data = static_cast<CallbackData *>(userdata);
|
||||
std::string path_str;
|
||||
nix_store_path_name(path, OBSERVE_STRING(path_str));
|
||||
data->paths->push_back(path_str);
|
||||
});
|
||||
assert_ctx_ok();
|
||||
ASSERT_EQ(ret, NIX_OK);
|
||||
|
||||
// Should contain at least the derivation and 10 outputs
|
||||
ASSERT_GE(closure_paths.size(), 11);
|
||||
|
||||
// Verify all outputs are present in the closure
|
||||
for (const auto & [outname, outPath] : outputs) {
|
||||
std::string outPathName = store->ptr->printStorePath(outPath);
|
||||
|
||||
bool found = false;
|
||||
for (const auto & p : closure_paths) {
|
||||
// nix_store_path_name returns just the name part, so match against full path name
|
||||
if (outPathName.find(p) != std::string::npos) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ASSERT_TRUE(found) << "Output " << outname << " (" << outPathName << ") not found in closure";
|
||||
}
|
||||
|
||||
nix_store_path_free(drvPath);
|
||||
nix_derivation_free(drv);
|
||||
nix_store_free(store);
|
||||
}
|
||||
|
||||
TEST_F(NixApiStoreTestWithRealisedPath, nix_store_get_fs_closure_error_propagation)
|
||||
{
|
||||
// Test that errors in the callback abort the closure computation
|
||||
struct CallbackData
|
||||
{
|
||||
int * count;
|
||||
};
|
||||
|
||||
int call_count = 0;
|
||||
CallbackData data{&call_count};
|
||||
|
||||
auto ret = nix_store_get_fs_closure(
|
||||
ctx,
|
||||
store,
|
||||
drvPath, // Use derivation path
|
||||
false, // flip_direction
|
||||
true, // include_outputs
|
||||
false, // include_derivers
|
||||
&data,
|
||||
[](nix_c_context * context, void * userdata, const StorePath * path) {
|
||||
auto * data = static_cast<CallbackData *>(userdata);
|
||||
(*data->count)++;
|
||||
// Set an error immediately
|
||||
nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Test error");
|
||||
});
|
||||
|
||||
// Should have aborted with error
|
||||
ASSERT_EQ(ret, NIX_ERR_UNKNOWN);
|
||||
ASSERT_EQ(call_count, 1); // Should have been called exactly once, then aborted
|
||||
}
|
||||
|
||||
} // namespace nixC
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue