C API: add tests for libutil and libstore

This commit is contained in:
José Luis Lafuente 2023-12-14 20:14:58 +01:00 committed by José Luis Lafuente
parent 46f5d0ee7b
commit 41f1669dea
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
6 changed files with 133 additions and 25 deletions

View file

@ -8,11 +8,6 @@
#include "globals.hh" #include "globals.hh"
struct StorePath
{
nix::StorePath path;
};
nix_err nix_libstore_init(nix_c_context * context) nix_err nix_libstore_init(nix_c_context * context)
{ {
if (context) if (context)

View file

@ -6,4 +6,10 @@ struct Store
{ {
nix::ref<nix::Store> ptr; nix::ref<nix::Store> ptr;
}; };
struct StorePath
{
nix::StorePath path;
};
#endif #endif

View file

@ -26,6 +26,6 @@ libstore-tests_CXXFLAGS += $(libstore-tests_EXTRA_INCLUDES)
libstore-tests_LIBS = \ libstore-tests_LIBS = \
libstore-test-support libutil-test-support \ libstore-test-support libutil-test-support \
libstore libutil libstore libstorec libutil libutilc
libstore-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) libstore-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS)

View file

@ -0,0 +1,70 @@
#include "nix_api_util.h"
#include "nix_api_util_internal.h"
#include "nix_api_store.h"
#include "nix_api_store_internal.h"
#include "tests/nix_api_util.hh"
#define STORE_DIR "/nix/store/"
#define HASH_PART "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q"
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);
ASSERT_EQ(NIX_OK, ret);
}
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);
}
TEST_F(nix_api_store_test, InvalidPathFails)
{
nix_store_parse_path(ctx, store, "invalid-path");
ASSERT_EQ(ctx->last_err_code, NIX_ERR_NIX_ERROR);
}
TEST_F(nix_api_store_test, ReturnsValidStorePath)
{
StorePath * result = nix_store_parse_path(ctx, store, validPath);
ASSERT_NE(result, nullptr);
ASSERT_STREQ("x", result->path.name().data());
ASSERT_STREQ(HASH_PART "-x", result->path.to_string().data());
}
TEST_F(nix_api_store_test, SetsLastErrCodeToNixOk)
{
nix_store_parse_path(ctx, store, validPath);
ASSERT_EQ(ctx->last_err_code, NIX_OK);
}
TEST_F(nix_api_store_test, DoesNotCrashWhenContextIsNull)
{
ASSERT_NO_THROW(nix_store_parse_path(nullptr, store, validPath));
}
}

View file

@ -0,0 +1,25 @@
#pragma once
///@file
#include "nix_api_util.h"
#include <gtest/gtest.h>
class nix_api_util_context : public ::testing::Test
{
protected:
static void SetUpTestSuite()
{
nix_libutil_init(NULL);
}
void SetUp() override
{
ctx = nix_c_context_create();
};
void TearDown() override
{
nix_c_context_free(ctx);
ctx = nullptr;
}
nix_c_context * ctx;
};

View file

@ -3,26 +3,12 @@
#include "args.hh" #include "args.hh"
#include "nix_api_util.h" #include "nix_api_util.h"
#include "nix_api_util_internal.h" #include "nix_api_util_internal.h"
#include "tests/nix_api_util.hh"
#include <gtest/gtest.h> #include <gtest/gtest.h>
namespace nixC { namespace nixC {
class nix_api_util_context : public ::testing::Test {
protected:
static void SetUpTestSuite() {
nix_libutil_init(NULL);
}
void SetUp() override {
ctx = nix_c_context_create();
};
void TearDown() override {
nix_c_context_free(ctx);
ctx = nullptr;
}
nix_c_context* ctx;
};
TEST_F(nix_api_util_context, nix_context_error) { TEST_F(nix_api_util_context, nix_context_error) {
std::string err_msg_ref; std::string err_msg_ref;
try { try {
@ -57,12 +43,38 @@ TEST(nix_api_util, nix_version_get) {
ASSERT_EQ(std::string(nix_version_get()), PACKAGE_VERSION); ASSERT_EQ(std::string(nix_version_get()), PACKAGE_VERSION);
} }
TEST_F(nix_api_util_context, nix_setting_get) { struct MySettings : nix::Config
// todo {
nix::Setting<std::string> settingSet{this, "empty", "setting-name", "Description"};
};
MySettings mySettings;
static nix::GlobalConfig::Register rs(&mySettings);
TEST_F(nix_api_util_context, nix_setting_get)
{
ASSERT_EQ(ctx->last_err_code, NIX_OK);
char value[256];
nix_err result = nix_setting_get(ctx, "invalid-key", value, 256);
ASSERT_EQ(result, NIX_ERR_KEY);
result = nix_setting_get(ctx, "setting-name", value, 256);
ASSERT_EQ(result, NIX_OK);
ASSERT_STREQ("empty", value);
} }
TEST_F(nix_api_util_context, nix_setting_set) { TEST_F(nix_api_util_context, nix_setting_set)
// todo {
nix_err result = nix_setting_set(ctx, "invalid-key", "new-value");
ASSERT_EQ(result, NIX_ERR_KEY);
result = nix_setting_set(ctx, "setting-name", "new-value");
ASSERT_EQ(result, NIX_OK);
char value[256];
result = nix_setting_get(ctx, "setting-name", value, 256);
ASSERT_EQ(result, NIX_OK);
ASSERT_STREQ("new-value", value);
} }
TEST_F(nix_api_util_context, nix_err_msg) { TEST_F(nix_api_util_context, nix_err_msg) {