From 2e1dbbe307199442d7975958664fd57fc37eee70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Sun, 25 Feb 2024 18:14:32 +0100 Subject: [PATCH] C API: refactor test support --- .../libexpr-support/tests/nix_api_expr.hh | 29 +++++++++++++++++++ tests/unit/libexpr/nix_api_expr.cc | 20 +------------ tests/unit/libexpr/nix_api_external.cc | 23 ++------------- tests/unit/libexpr/nix_api_value.cc | 26 +++-------------- .../libstore-support/tests/nix_api_store.hh | 24 ++++++++++----- .../libutil-support/tests/nix_api_util.hh | 7 ++--- 6 files changed, 55 insertions(+), 74 deletions(-) create mode 100644 tests/unit/libexpr-support/tests/nix_api_expr.hh diff --git a/tests/unit/libexpr-support/tests/nix_api_expr.hh b/tests/unit/libexpr-support/tests/nix_api_expr.hh new file mode 100644 index 000000000..f63c03319 --- /dev/null +++ b/tests/unit/libexpr-support/tests/nix_api_expr.hh @@ -0,0 +1,29 @@ +#pragma once +///@file +#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 +{ +protected: + + nix_api_expr_test() + { + nix_libexpr_init(ctx); + 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); + } + + EvalState * state; + Value * value; +}; +} diff --git a/tests/unit/libexpr/nix_api_expr.cc b/tests/unit/libexpr/nix_api_expr.cc index 5caccea9a..d14e90097 100644 --- a/tests/unit/libexpr/nix_api_expr.cc +++ b/tests/unit/libexpr/nix_api_expr.cc @@ -5,30 +5,12 @@ #include "nix_api_expr.h" #include "nix_api_value.h" -#include "tests/nix_api_store.hh" +#include "tests/nix_api_expr.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); - } - - EvalState * state; - Value * value; -}; - TEST_F(nix_api_expr_test, nix_expr_eval_from_string) { nix_expr_eval_from_string(nullptr, state, "builtins.nixVersion", ".", value); diff --git a/tests/unit/libexpr/nix_api_external.cc b/tests/unit/libexpr/nix_api_external.cc index 5f5353b04..1f190d9c8 100644 --- a/tests/unit/libexpr/nix_api_external.cc +++ b/tests/unit/libexpr/nix_api_external.cc @@ -6,9 +6,8 @@ #include "nix_api_expr_internal.h" #include "nix_api_value.h" #include "nix_api_external.h" -#include "tests/nix_api_store.hh" +#include "tests/nix_api_expr.hh" -#include #include namespace nixC { @@ -42,25 +41,7 @@ private: } }; -class nix_api_external_test : public nix_api_store_test -{ -public: - nix_api_external_test() - { - state = nix_state_create(nullptr, nullptr, store); - value = nix_alloc_value(nullptr, state); - } - ~nix_api_external_test() - { - nix_gc_decref(nullptr, value); - nix_state_free(state); - } - - EvalState * state; - Value * value; -}; - -TEST_F(nix_api_external_test, nix_expr_eval_from_string) +TEST_F(nix_api_expr_test, nix_expr_eval_external) { MyExternalValueDesc * external = new MyExternalValueDesc(42); ExternalValue * val = nix_create_external_value(ctx, external, external); diff --git a/tests/unit/libexpr/nix_api_value.cc b/tests/unit/libexpr/nix_api_value.cc index d1247e027..abed456f7 100644 --- a/tests/unit/libexpr/nix_api_value.cc +++ b/tests/unit/libexpr/nix_api_value.cc @@ -5,32 +5,14 @@ #include "nix_api_expr.h" #include "nix_api_value.h" -#include "tests/nix_api_store.hh" +#include "tests/nix_api_expr.hh" #include #include namespace nixC { -class nix_api_value_test : public nix_api_store_test -{ -public: - nix_api_value_test() - { - state = nix_state_create(nullptr, nullptr, store); - value = nix_alloc_value(nullptr, state); - } - ~nix_api_value_test() - { - nix_gc_decref(nullptr, value); - nix_state_free(state); - } - - EvalState * state; - Value * value; -}; - -TEST_F(nix_api_value_test, nix_value_set_get_int) +TEST_F(nix_api_expr_test, nix_value_set_get_int) { int myInt = 1; nix_init_int(nullptr, value, myInt); @@ -38,7 +20,7 @@ TEST_F(nix_api_value_test, nix_value_set_get_int) ASSERT_EQ(myInt, nix_get_int(nullptr, value)); } -TEST_F(nix_api_value_test, nix_make_and_set_list) +TEST_F(nix_api_expr_test, nix_build_and_init_list) { int size = 10; ListBuilder * builder = nix_make_list_builder(nullptr, state, size); @@ -56,7 +38,7 @@ TEST_F(nix_api_value_test, nix_make_and_set_list) nix_gc_decref(nullptr, intValue); } -TEST_F(nix_api_value_test, nix_make_attrs_t) +TEST_F(nix_api_expr_test, nix_build_and_init_attr) { int size = 10; const char ** out_name = (const char **) malloc(sizeof(char *)); diff --git a/tests/unit/libstore-support/tests/nix_api_store.hh b/tests/unit/libstore-support/tests/nix_api_store.hh index 34d467d49..4608dd90d 100644 --- a/tests/unit/libstore-support/tests/nix_api_store.hh +++ b/tests/unit/libstore-support/tests/nix_api_store.hh @@ -10,21 +10,16 @@ namespace fs = std::filesystem; +namespace nixC { 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); + init_local_store(); }; + ~nix_api_store_test() override { nix_store_free(store); @@ -37,4 +32,17 @@ public: Store * store; std::string nixStoreDir; + +protected: + void init_local_store() + { + 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); + } }; +} diff --git a/tests/unit/libutil-support/tests/nix_api_util.hh b/tests/unit/libutil-support/tests/nix_api_util.hh index b007ac4b1..314ec70de 100644 --- a/tests/unit/libutil-support/tests/nix_api_util.hh +++ b/tests/unit/libutil-support/tests/nix_api_util.hh @@ -4,17 +4,15 @@ #include +namespace nixC { class nix_api_util_context : public ::testing::Test { protected: - static void SetUpTestSuite() - { - nix_libutil_init(NULL); - } nix_api_util_context() { ctx = nix_c_context_create(); + nix_libutil_init(ctx); }; ~nix_api_util_context() override @@ -25,3 +23,4 @@ protected: nix_c_context * ctx; }; +}