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-04-08 14:14:03 +03:00
|
|
|
#include "tests/string_callback.hh"
|
2024-01-03 20:10:43 +02:00
|
|
|
|
2024-04-05 17:08:18 +03:00
|
|
|
#include "gmock/gmock.h"
|
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);
|
2024-04-15 22:05:52 +03:00
|
|
|
std::string result;
|
|
|
|
nix_get_string(nullptr, value, OBSERVE_STRING(result));
|
2024-01-03 20:10:43 +02:00
|
|
|
|
2024-04-15 22:05:52 +03:00
|
|
|
ASSERT_STREQ(PACKAGE_VERSION, result.c_str());
|
2024-01-03 20:10:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-04-15 22:05:52 +03:00
|
|
|
std::string p;
|
|
|
|
nix_get_string(nullptr, valueResult, OBSERVE_STRING(p));
|
2024-02-29 19:33:07 +02:00
|
|
|
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");
|
2024-04-15 22:05:52 +03:00
|
|
|
std::string drvPath;
|
|
|
|
nix_get_string(nullptr, drvPathValue, OBSERVE_STRING(drvPath));
|
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
|
|
|
|
2024-04-07 22:19:44 +03:00
|
|
|
// NOTE: .drvPath should be usually be ignored. Output paths are more versatile.
|
|
|
|
// See https://github.com/NixOS/nix/issues/6507
|
|
|
|
// Use e.g. nix_string_realise to realise the output.
|
2024-04-15 22:05:52 +03:00
|
|
|
StorePath * drvStorePath = nix_store_parse_path(ctx, store, drvPath.c_str());
|
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");
|
2024-04-15 22:05:52 +03:00
|
|
|
std::string outPath;
|
|
|
|
nix_get_string(ctx, outPathValue, OBSERVE_STRING(outPath));
|
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
|
|
|
|
2024-04-15 22:05:52 +03:00
|
|
|
StorePath * outStorePath = nix_store_parse_path(ctx, store, outPath.c_str());
|
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
|
|
|
|
2024-04-07 22:19:44 +03:00
|
|
|
nix_store_realise(ctx, store, drvStorePath, nullptr, nullptr);
|
|
|
|
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-04-05 17:08:18 +03:00
|
|
|
|
|
|
|
TEST_F(nix_api_expr_test, nix_expr_realise_context_bad_value)
|
|
|
|
{
|
|
|
|
auto expr = "true";
|
|
|
|
nix_expr_eval_from_string(ctx, state, expr, ".", value);
|
|
|
|
assert_ctx_ok();
|
|
|
|
auto r = nix_string_realise(ctx, state, value, false);
|
|
|
|
ASSERT_EQ(nullptr, r);
|
|
|
|
ASSERT_EQ(ctx->last_err_code, NIX_ERR_NIX_ERROR);
|
|
|
|
ASSERT_THAT(ctx->last_err, testing::Optional(testing::HasSubstr("cannot coerce")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(nix_api_expr_test, nix_expr_realise_context_bad_build)
|
|
|
|
{
|
|
|
|
auto expr = R"(
|
|
|
|
derivation { name = "letsbuild";
|
|
|
|
system = builtins.currentSystem;
|
|
|
|
builder = "/bin/sh";
|
|
|
|
args = [ "-c" "echo failing a build for testing purposes; exit 1;" ];
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
nix_expr_eval_from_string(ctx, state, expr, ".", value);
|
|
|
|
assert_ctx_ok();
|
|
|
|
auto r = nix_string_realise(ctx, state, value, false);
|
|
|
|
ASSERT_EQ(nullptr, r);
|
|
|
|
ASSERT_EQ(ctx->last_err_code, NIX_ERR_NIX_ERROR);
|
|
|
|
ASSERT_THAT(ctx->last_err, testing::Optional(testing::HasSubstr("failed with exit code 1")));
|
2024-01-03 20:10:43 +02:00
|
|
|
}
|
2024-04-05 17:08:18 +03:00
|
|
|
|
|
|
|
TEST_F(nix_api_expr_test, nix_expr_realise_context)
|
|
|
|
{
|
|
|
|
// TODO (ca-derivations): add a content-addressed derivation output, which produces a placeholder
|
|
|
|
auto expr = R"(
|
|
|
|
''
|
|
|
|
a derivation output: ${
|
|
|
|
derivation { name = "letsbuild";
|
|
|
|
system = builtins.currentSystem;
|
|
|
|
builder = "/bin/sh";
|
|
|
|
args = [ "-c" "echo foo > $out" ];
|
|
|
|
}}
|
|
|
|
a path: ${builtins.toFile "just-a-file" "ooh file good"}
|
|
|
|
a derivation path by itself: ${
|
2024-04-15 22:05:52 +03:00
|
|
|
builtins.unsafeDiscardOutputDependency
|
2024-04-05 17:08:18 +03:00
|
|
|
(derivation {
|
|
|
|
name = "not-actually-built-yet";
|
|
|
|
system = builtins.currentSystem;
|
|
|
|
builder = "/bin/sh";
|
|
|
|
args = [ "-c" "echo foo > $out" ];
|
|
|
|
}).drvPath}
|
|
|
|
''
|
|
|
|
)";
|
|
|
|
nix_expr_eval_from_string(ctx, state, expr, ".", value);
|
|
|
|
assert_ctx_ok();
|
|
|
|
auto r = nix_string_realise(ctx, state, value, false);
|
|
|
|
assert_ctx_ok();
|
|
|
|
ASSERT_NE(nullptr, r);
|
|
|
|
|
|
|
|
auto s = std::string(nix_realised_string_get_buffer_start(r), nix_realised_string_get_buffer_size(r));
|
|
|
|
|
|
|
|
EXPECT_THAT(s, testing::StartsWith("a derivation output:"));
|
|
|
|
EXPECT_THAT(s, testing::HasSubstr("-letsbuild\n"));
|
|
|
|
EXPECT_THAT(s, testing::Not(testing::HasSubstr("-letsbuild.drv")));
|
|
|
|
EXPECT_THAT(s, testing::HasSubstr("a path:"));
|
|
|
|
EXPECT_THAT(s, testing::HasSubstr("-just-a-file"));
|
|
|
|
EXPECT_THAT(s, testing::Not(testing::HasSubstr("-just-a-file.drv")));
|
|
|
|
EXPECT_THAT(s, testing::Not(testing::HasSubstr("ooh file good")));
|
|
|
|
EXPECT_THAT(s, testing::HasSubstr("a derivation path by itself:"));
|
|
|
|
EXPECT_THAT(s, testing::EndsWith("-not-actually-built-yet.drv\n"));
|
|
|
|
|
2024-04-08 14:14:03 +03:00
|
|
|
std::vector<std::string> names;
|
2024-04-05 17:08:18 +03:00
|
|
|
size_t n = nix_realised_string_get_store_path_count(r);
|
|
|
|
for (size_t i = 0; i < n; ++i) {
|
|
|
|
const StorePath * p = nix_realised_string_get_store_path(r, i);
|
2024-04-08 14:14:03 +03:00
|
|
|
ASSERT_NE(nullptr, p);
|
|
|
|
std::string name;
|
|
|
|
nix_store_path_name(p, OBSERVE_STRING(name));
|
|
|
|
names.push_back(name);
|
2024-04-05 17:08:18 +03:00
|
|
|
}
|
|
|
|
std::sort(names.begin(), names.end());
|
|
|
|
ASSERT_EQ(3, names.size());
|
|
|
|
EXPECT_THAT(names[0], testing::StrEq("just-a-file"));
|
|
|
|
EXPECT_THAT(names[1], testing::StrEq("letsbuild"));
|
|
|
|
EXPECT_THAT(names[2], testing::StrEq("not-actually-built-yet.drv"));
|
|
|
|
|
|
|
|
nix_realised_string_free(r);
|
|
|
|
}
|
|
|
|
|
2024-05-23 19:07:33 +03:00
|
|
|
const char * SAMPLE_USER_DATA = "whatever";
|
|
|
|
|
|
|
|
static void primop_square(void * user_data, nix_c_context * context, EvalState * state, Value ** args, Value * ret)
|
|
|
|
{
|
|
|
|
assert(context);
|
|
|
|
assert(state);
|
|
|
|
assert(user_data == SAMPLE_USER_DATA);
|
|
|
|
auto i = nix_get_int(context, args[0]);
|
|
|
|
nix_init_int(context, ret, i * i);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(nix_api_expr_test, nix_expr_primop)
|
|
|
|
{
|
|
|
|
PrimOp * primop =
|
|
|
|
nix_alloc_primop(ctx, primop_square, 1, "square", nullptr, "square an integer", (void *) SAMPLE_USER_DATA);
|
|
|
|
assert_ctx_ok();
|
|
|
|
Value * primopValue = nix_alloc_value(ctx, state);
|
|
|
|
assert_ctx_ok();
|
|
|
|
nix_init_primop(ctx, primopValue, primop);
|
|
|
|
assert_ctx_ok();
|
|
|
|
|
|
|
|
Value * three = nix_alloc_value(ctx, state);
|
|
|
|
assert_ctx_ok();
|
|
|
|
nix_init_int(ctx, three, 3);
|
|
|
|
assert_ctx_ok();
|
|
|
|
|
|
|
|
Value * result = nix_alloc_value(ctx, state);
|
|
|
|
assert_ctx_ok();
|
|
|
|
nix_value_call(ctx, state, primopValue, three, result);
|
|
|
|
assert_ctx_ok();
|
|
|
|
|
|
|
|
auto r = nix_get_int(ctx, result);
|
|
|
|
ASSERT_EQ(9, r);
|
|
|
|
}
|
|
|
|
|
2024-04-05 17:08:18 +03:00
|
|
|
} // namespace nixC
|