nix-super/tests/unit/libexpr/nix_api_expr.cc

101 lines
3.5 KiB
C++
Raw Normal View History

2024-01-03 20:10:43 +02:00
#include "nix_api_store.h"
#include "nix_api_store_internal.h"
#include "nix_api_util.h"
#include "nix_api_util_internal.h"
#include "nix_api_expr.h"
#include "nix_api_value.h"
2024-02-25 19:14:32 +02:00
#include "tests/nix_api_expr.hh"
2024-01-03 20:10:43 +02:00
#include <gtest/gtest.h>
namespace nixC {
TEST_F(nix_api_expr_test, nix_expr_eval_from_string)
{
nix_expr_eval_from_string(nullptr, state, "builtins.nixVersion", ".", value);
nix_value_force(nullptr, state, value);
auto result = nix_get_string(nullptr, value);
ASSERT_STREQ(PACKAGE_VERSION, result);
}
TEST_F(nix_api_expr_test, nix_expr_eval_add_numbers)
{
nix_expr_eval_from_string(nullptr, state, "1 + 1", ".", value);
nix_value_force(nullptr, state, value);
auto result = nix_get_int(nullptr, value);
ASSERT_EQ(2, result);
}
TEST_F(nix_api_expr_test, nix_expr_eval_drv)
{
auto expr = R"(derivation { name = "myname"; builder = "mybuilder"; system = "mysystem"; })";
nix_expr_eval_from_string(nullptr, state, expr, ".", value);
ASSERT_EQ(NIX_TYPE_ATTRS, nix_get_type(nullptr, value));
2024-01-16 00:44:57 +02:00
EvalState * stateFn = nix_state_create(nullptr, nullptr, store);
Value * valueFn = nix_alloc_value(nullptr, state);
2024-01-03 20:10:43 +02:00
nix_expr_eval_from_string(nullptr, stateFn, "builtins.toString", ".", valueFn);
ASSERT_EQ(NIX_TYPE_FUNCTION, nix_get_type(nullptr, valueFn));
2024-01-16 00:44:57 +02:00
EvalState * stateResult = nix_state_create(nullptr, nullptr, store);
Value * valueResult = nix_alloc_value(nullptr, stateResult);
2024-01-03 20:10:43 +02:00
nix_value_call(ctx, stateResult, valueFn, value, valueResult);
2024-01-16 00:44:57 +02:00
ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(nullptr, valueResult));
2024-01-03 20:10:43 +02:00
2024-02-29 19:33:07 +02:00
std::string p = nix_get_string(nullptr, valueResult);
std::string pEnd = "-myname";
ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size()));
2024-01-03 20:10:43 +02:00
// Clean up
nix_gc_decref(nullptr, valueFn);
nix_state_free(stateFn);
nix_gc_decref(nullptr, valueResult);
nix_state_free(stateResult);
}
2024-01-16 00:44:57 +02:00
TEST_F(nix_api_expr_test, nix_build_drv)
{
auto expr = R"(derivation { name = "myname";
system = builtins.currentSystem;
builder = "/bin/sh";
2024-02-24 17:07:16 +02:00
args = [ "-c" "echo foo > $out" ];
2024-01-16 00:44:57 +02:00
})";
nix_expr_eval_from_string(nullptr, state, expr, ".", value);
Value * drvPathValue = nix_get_attr_byname(nullptr, value, state, "drvPath");
const char * drvPath = nix_get_string(nullptr, drvPathValue);
2024-02-22 13:57:51 +02:00
std::string p = drvPath;
std::string pEnd = "-myname.drv";
ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size()));
2024-01-16 00:44:57 +02:00
StorePath * drvStorePath = nix_store_parse_path(ctx, store, drvPath);
2024-02-29 19:33:07 +02:00
ASSERT_EQ(true, nix_store_is_valid_path(ctx, store, drvStorePath));
2024-01-16 00:44:57 +02:00
2024-02-29 19:33:07 +02:00
Value * outPathValue = nix_get_attr_byname(ctx, value, state, "outPath");
const char * outPath = nix_get_string(ctx, outPathValue);
2024-02-22 13:57:51 +02:00
p = outPath;
pEnd = "-myname";
ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size()));
2024-02-29 19:33:07 +02:00
ASSERT_EQ(true, drvStorePath->path.isDerivation());
2024-01-16 00:44:57 +02:00
StorePath * outStorePath = nix_store_parse_path(ctx, store, outPath);
2024-02-29 19:33:07 +02:00
ASSERT_EQ(false, nix_store_is_valid_path(ctx, store, outStorePath));
2024-02-24 17:07:16 +02:00
// TODO figure out why fails.
2024-02-29 19:33:07 +02:00
// `make libexpr-tests_RUN` works, but `nix build .` enters an infinite loop
/* nix_store_realise(ctx, store, drvStorePath, nullptr, nullptr); */
2024-02-24 17:07:16 +02:00
/* auto is_valid_path = nix_store_is_valid_path(ctx, store, outStorePath); */
/* ASSERT_EQ(true, is_valid_path); */
2024-01-16 00:44:57 +02:00
// Clean up
nix_store_path_free(drvStorePath);
nix_store_path_free(outStorePath);
}
2024-01-03 20:10:43 +02:00
}