diff --git a/tests/unit/libexpr/local.mk b/tests/unit/libexpr/local.mk index eda65508d..0a7b28436 100644 --- a/tests/unit/libexpr/local.mk +++ b/tests/unit/libexpr/local.mk @@ -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 diff --git a/tests/unit/libexpr/nix_api_expr.cc b/tests/unit/libexpr/nix_api_expr.cc new file mode 100644 index 000000000..03de4547a --- /dev/null +++ b/tests/unit/libexpr/nix_api_expr.cc @@ -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 + +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); +} + +} diff --git a/tests/unit/libstore-support/tests/nix_api_store.hh b/tests/unit/libstore-support/tests/nix_api_store.hh new file mode 100644 index 000000000..e762a3ca8 --- /dev/null +++ b/tests/unit/libstore-support/tests/nix_api_store.hh @@ -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 +#include + +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; +}; diff --git a/tests/unit/libstore/nix_api_store.cc b/tests/unit/libstore/nix_api_store.cc index 3fe55ae93..764cd0d88 100644 --- a/tests/unit/libstore/nix_api_store.cc +++ b/tests/unit/libstore/nix_api_store.cc @@ -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); +} + } diff --git a/tests/unit/libutil-support/tests/nix_api_util.hh b/tests/unit/libutil-support/tests/nix_api_util.hh index f2ee58da2..b007ac4b1 100644 --- a/tests/unit/libutil-support/tests/nix_api_util.hh +++ b/tests/unit/libutil-support/tests/nix_api_util.hh @@ -4,7 +4,6 @@ #include - 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; };