C API: add nix_api_expr tests

This commit is contained in:
José Luis Lafuente 2024-01-03 19:10:43 +01:00 committed by José Luis Lafuente
parent 55601963b3
commit ac3a9c6605
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
5 changed files with 139 additions and 24 deletions

View file

@ -32,6 +32,6 @@ libexpr-tests_CXXFLAGS += $(libexpr-tests_EXTRA_INCLUDES)
libexpr-tests_LIBS = \
libexpr-test-support libstore-test-support libutils-test-support \
libexpr libfetchers libstore libutil
libexpr libexprc libfetchers libstore libstorec libutil libutilc
libexpr-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) -lgmock

View file

@ -0,0 +1,82 @@
#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"
#include "tests/nix_api_store.hh"
#include <gtest/gtest.h>
namespace nixC {
class nix_api_expr_test : public nix_api_store_test
{
public:
nix_api_expr_test()
{
state = nix_state_create(nullptr, nullptr, store);
value = nix_alloc_value(nullptr, state);
}
~nix_api_expr_test()
{
nix_gc_decref(nullptr, value);
nix_state_free(state);
}
State * state;
Value * value;
};
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);
nix_value_force(nullptr, state, value);
ASSERT_EQ(NIX_TYPE_ATTRS, nix_get_type(nullptr, value));
auto stateFn = nix_state_create(nullptr, nullptr, store);
auto valueFn = nix_alloc_value(nullptr, state);
nix_expr_eval_from_string(nullptr, stateFn, "builtins.toString", ".", valueFn);
ASSERT_EQ(NIX_TYPE_FUNCTION, nix_get_type(nullptr, valueFn));
auto stateResult = nix_state_create(nullptr, nullptr, store);
auto valueResult = nix_alloc_value(nullptr, stateResult);
nix_value_call(ctx, stateResult, valueFn, value, valueResult);
nix_value_force(nullptr, stateResult, valueResult);
auto p = nix_get_string(nullptr, valueResult);
ASSERT_STREQ("/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname", p);
// Clean up
nix_gc_decref(nullptr, valueFn);
nix_state_free(stateFn);
nix_gc_decref(nullptr, valueResult);
nix_state_free(stateResult);
}
}

View file

@ -0,0 +1,40 @@
#pragma once
///@file
#include "tests/nix_api_util.hh"
#include "nix_api_store.h"
#include "nix_api_store_internal.h"
#include <filesystem>
#include <gtest/gtest.h>
namespace fs = std::filesystem;
class nix_api_store_test : public nix_api_util_context
{
public:
nix_api_store_test()
{
nix_libstore_init(ctx);
auto tmpl = nix::getEnv("TMPDIR").value_or("/tmp") + "/tests_nix-store.XXXXXX";
nixStoreDir = mkdtemp((char *) tmpl.c_str());
// Options documented in `nix help-stores`
const char * p1[] = {"root", nixStoreDir.c_str()};
const char ** params[] = {p1, nullptr};
store = nix_store_open(ctx, "local", params);
};
~nix_api_store_test() override
{
nix_store_unref(store);
for (auto & path : fs::recursive_directory_iterator(nixStoreDir)) {
fs::permissions(path, fs::perms::owner_all);
}
fs::remove_all(nixStoreDir);
}
Store * store;
std::string nixStoreDir;
};

View file

@ -2,7 +2,8 @@
#include "nix_api_util_internal.h"
#include "nix_api_store.h"
#include "nix_api_store_internal.h"
#include "tests/nix_api_util.hh"
#include "tests/nix_api_store.hh"
#define STORE_DIR "/nix/store/"
#define HASH_PART "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q"
@ -10,24 +11,6 @@ const char * validPath = STORE_DIR HASH_PART "-x";
namespace nixC {
class nix_api_store_test : public nix_api_util_context
{
public:
void SetUp() override
{
nix_api_util_context::SetUp();
nix_libstore_init(ctx);
store = nix_store_open(ctx, "dummy://", NULL);
};
void TearDown() override
{
nix_store_unref(store);
nix_api_util_context::TearDown();
}
Store * store;
};
TEST_F(nix_api_util_context, nix_libstore_init)
{
auto ret = nix_libstore_init(ctx);
@ -39,7 +22,7 @@ TEST_F(nix_api_store_test, nix_store_get_uri)
char value[256];
auto ret = nix_store_get_uri(ctx, store, value, 256);
ASSERT_EQ(NIX_OK, ret);
ASSERT_STREQ("dummy", value);
ASSERT_STREQ("local", value);
}
TEST_F(nix_api_store_test, InvalidPathFails)
@ -67,4 +50,12 @@ TEST_F(nix_api_store_test, DoesNotCrashWhenContextIsNull)
ASSERT_NO_THROW(nix_store_parse_path(nullptr, store, validPath));
}
TEST_F(nix_api_store_test, get_version)
{
char value[256];
auto ret = nix_store_get_version(ctx, store, value, 256);
ASSERT_EQ(NIX_OK, ret);
ASSERT_STREQ(PACKAGE_VERSION, value);
}
}

View file

@ -4,7 +4,6 @@
#include <gtest/gtest.h>
class nix_api_util_context : public ::testing::Test
{
protected:
@ -12,14 +11,17 @@ protected:
{
nix_libutil_init(NULL);
}
void SetUp() override
nix_api_util_context()
{
ctx = nix_c_context_create();
};
void TearDown() override
~nix_api_util_context() override
{
nix_c_context_free(ctx);
ctx = nullptr;
}
nix_c_context * ctx;
};