1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-06 21:41:48 +02:00

Merge remote-tracking branch 'origin/master' into finish-value

This commit is contained in:
Eelco Dolstra 2024-04-17 16:02:44 +02:00
commit 6a3ecdaa39
271 changed files with 7672 additions and 1195 deletions

View file

@ -23,15 +23,18 @@ libexpr-tests_EXTRA_INCLUDES = \
-I tests/unit/libexpr-support \
-I tests/unit/libstore-support \
-I tests/unit/libutil-support \
-I src/libexpr \
-I src/libfetchers \
-I src/libstore \
-I src/libutil
$(INCLUDE_libexpr) \
$(INCLUDE_libexprc) \
$(INCLUDE_libfetchers) \
$(INCLUDE_libstore) \
$(INCLUDE_libstorec) \
$(INCLUDE_libutil) \
$(INCLUDE_libutilc)
libexpr-tests_CXXFLAGS += $(libexpr-tests_EXTRA_INCLUDES)
libexpr-tests_LIBS = \
libexpr-test-support libstore-test-support libutils-test-support \
libexpr libfetchers libstore libutil
libexpr-test-support libstore-test-support libutil-test-support \
libexpr libexprc libfetchers libstore libstorec libutil libutilc
libexpr-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) -lgmock

View file

@ -0,0 +1,39 @@
#include <gtest/gtest.h>
#include <cstdlib>
#include "globals.hh"
#include "logging.hh"
using namespace nix;
int main (int argc, char **argv) {
if (argc > 1 && std::string_view(argv[1]) == "__build-remote") {
printError("test-build-remote: not supported in libexpr unit tests");
return 1;
}
// Disable build hook. We won't be testing remote builds in these unit tests. If we do, fix the above build hook.
settings.buildHook = {};
#if __linux__ // should match the conditional around sandboxBuildDir declaration.
// When building and testing nix within the host's Nix sandbox, our store dir will be located in the host's sandboxBuildDir, e.g.:
// Host
// storeDir = /nix/store
// sandboxBuildDir = /build
// This process
// storeDir = /build/foo/bar/store
// sandboxBuildDir = /build
// However, we have a rule that the store dir must not be inside the storeDir, so we need to pick a different sandboxBuildDir.
settings.sandboxBuildDir = "/test-build-dir-instead-of-usual-build-dir";
#endif
#if __APPLE__
// Avoid this error, when already running in a sandbox:
// sandbox-exec: sandbox_apply: Operation not permitted
settings.sandboxMode = smDisabled;
setEnv("_NIX_TEST_NO_SANDBOX", "1");
#endif
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View file

@ -0,0 +1,194 @@
#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_expr.hh"
#include "tests/string_callback.hh"
#include "gmock/gmock.h"
#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);
std::string result;
nix_get_string(nullptr, value, OBSERVE_STRING(result));
ASSERT_STREQ(PACKAGE_VERSION, result.c_str());
}
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));
EvalState * stateFn = nix_state_create(nullptr, nullptr, store);
Value * 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));
EvalState * stateResult = nix_state_create(nullptr, nullptr, store);
Value * valueResult = nix_alloc_value(nullptr, stateResult);
nix_value_call(ctx, stateResult, valueFn, value, valueResult);
ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(nullptr, valueResult));
std::string p;
nix_get_string(nullptr, valueResult, OBSERVE_STRING(p));
std::string pEnd = "-myname";
ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size()));
// Clean up
nix_gc_decref(nullptr, valueFn);
nix_state_free(stateFn);
nix_gc_decref(nullptr, valueResult);
nix_state_free(stateResult);
}
TEST_F(nix_api_expr_test, nix_build_drv)
{
auto expr = R"(derivation { name = "myname";
system = builtins.currentSystem;
builder = "/bin/sh";
args = [ "-c" "echo foo > $out" ];
})";
nix_expr_eval_from_string(nullptr, state, expr, ".", value);
Value * drvPathValue = nix_get_attr_byname(nullptr, value, state, "drvPath");
std::string drvPath;
nix_get_string(nullptr, drvPathValue, OBSERVE_STRING(drvPath));
std::string p = drvPath;
std::string pEnd = "-myname.drv";
ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size()));
// 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.
StorePath * drvStorePath = nix_store_parse_path(ctx, store, drvPath.c_str());
ASSERT_EQ(true, nix_store_is_valid_path(ctx, store, drvStorePath));
Value * outPathValue = nix_get_attr_byname(ctx, value, state, "outPath");
std::string outPath;
nix_get_string(ctx, outPathValue, OBSERVE_STRING(outPath));
p = outPath;
pEnd = "-myname";
ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size()));
ASSERT_EQ(true, drvStorePath->path.isDerivation());
StorePath * outStorePath = nix_store_parse_path(ctx, store, outPath.c_str());
ASSERT_EQ(false, nix_store_is_valid_path(ctx, store, outStorePath));
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);
// Clean up
nix_store_path_free(drvStorePath);
nix_store_path_free(outStorePath);
}
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")));
}
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: ${
builtins.unsafeDiscardOutputDependency
(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"));
std::vector<std::string> names;
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);
ASSERT_NE(nullptr, p);
std::string name;
nix_store_path_name(p, OBSERVE_STRING(name));
names.push_back(name);
}
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);
}
} // namespace nixC

View file

@ -0,0 +1,68 @@
#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_expr_internal.h"
#include "nix_api_value.h"
#include "nix_api_external.h"
#include "tests/nix_api_expr.hh"
#include "tests/string_callback.hh"
#include <gtest/gtest.h>
namespace nixC {
class MyExternalValueDesc : public NixCExternalValueDesc
{
public:
MyExternalValueDesc(int x)
: _x(x)
{
print = print_function;
showType = show_type_function;
typeOf = type_of_function;
}
private:
int _x;
static void print_function(void * self, nix_printer * printer) {}
static void show_type_function(void * self, nix_string_return * res) {}
static void type_of_function(void * self, nix_string_return * res)
{
MyExternalValueDesc * obj = static_cast<MyExternalValueDesc *>(self);
std::string type_string = "nix-external<MyExternalValueDesc( ";
type_string += std::to_string(obj->_x);
type_string += " )>";
res->str = &*type_string.begin();
}
};
TEST_F(nix_api_expr_test, nix_expr_eval_external)
{
MyExternalValueDesc * external = new MyExternalValueDesc(42);
ExternalValue * val = nix_create_external_value(ctx, external, external);
nix_init_external(ctx, value, val);
EvalState * stateResult = nix_state_create(nullptr, nullptr, store);
Value * valueResult = nix_alloc_value(nullptr, stateResult);
EvalState * stateFn = nix_state_create(nullptr, nullptr, store);
Value * valueFn = nix_alloc_value(nullptr, stateFn);
nix_expr_eval_from_string(nullptr, state, "builtins.typeOf", ".", valueFn);
ASSERT_EQ(NIX_TYPE_EXTERNAL, nix_get_type(nullptr, value));
nix_value_call(ctx, state, valueFn, value, valueResult);
std::string string_value;
nix_get_string(nullptr, valueResult, OBSERVE_STRING(string_value));
ASSERT_STREQ("nix-external<MyExternalValueDesc( 42 )>", string_value.c_str());
}
}

View file

@ -0,0 +1,190 @@
#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_expr.hh"
#include "tests/string_callback.hh"
#include <cstdlib>
#include <gtest/gtest.h>
namespace nixC {
TEST_F(nix_api_expr_test, nix_value_set_get_int)
{
ASSERT_EQ(0, nix_get_int(ctx, nullptr));
ASSERT_DEATH(nix_get_int(ctx, value), "");
int myInt = 1;
nix_init_int(ctx, value, myInt);
ASSERT_EQ(myInt, nix_get_int(ctx, value));
ASSERT_STREQ("an integer", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_INT, nix_get_type(ctx, value));
}
TEST_F(nix_api_expr_test, nix_value_set_get_float)
{
ASSERT_FLOAT_EQ(0.0, nix_get_float(ctx, nullptr));
ASSERT_DEATH(nix_get_float(ctx, value), "");
float myDouble = 1.0;
nix_init_float(ctx, value, myDouble);
ASSERT_FLOAT_EQ(myDouble, nix_get_float(ctx, value));
ASSERT_STREQ("a float", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_FLOAT, nix_get_type(ctx, value));
}
TEST_F(nix_api_expr_test, nix_value_set_get_bool)
{
ASSERT_EQ(false, nix_get_bool(ctx, nullptr));
ASSERT_DEATH(nix_get_bool(ctx, value), "");
bool myBool = true;
nix_init_bool(ctx, value, myBool);
ASSERT_EQ(myBool, nix_get_bool(ctx, value));
ASSERT_STREQ("a Boolean", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_BOOL, nix_get_type(ctx, value));
}
TEST_F(nix_api_expr_test, nix_value_set_get_string)
{
std::string string_value;
ASSERT_EQ(NIX_ERR_UNKNOWN, nix_get_string(ctx, nullptr, OBSERVE_STRING(string_value)));
ASSERT_DEATH(nix_get_string(ctx, value, OBSERVE_STRING(string_value)), "");
const char * myString = "some string";
nix_init_string(ctx, value, myString);
nix_get_string(ctx, value, OBSERVE_STRING(string_value));
ASSERT_STREQ(myString, string_value.c_str());
ASSERT_STREQ("a string", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(ctx, value));
}
TEST_F(nix_api_expr_test, nix_value_set_get_null)
{
ASSERT_DEATH(nix_get_typename(ctx, value), "");
nix_init_null(ctx, value);
ASSERT_STREQ("null", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_NULL, nix_get_type(ctx, value));
}
TEST_F(nix_api_expr_test, nix_value_set_get_path)
{
ASSERT_EQ(nullptr, nix_get_path_string(ctx, nullptr));
ASSERT_DEATH(nix_get_path_string(ctx, value), "");
const char * p = "/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname";
nix_init_path_string(ctx, state, value, p);
ASSERT_STREQ(p, nix_get_path_string(ctx, value));
ASSERT_STREQ("a path", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_PATH, nix_get_type(ctx, value));
}
TEST_F(nix_api_expr_test, nix_build_and_init_list)
{
ASSERT_EQ(nullptr, nix_get_list_byidx(ctx, nullptr, state, 0));
ASSERT_EQ(0, nix_get_list_size(ctx, nullptr));
ASSERT_DEATH(nix_get_list_byidx(ctx, value, state, 0), "");
ASSERT_DEATH(nix_get_list_size(ctx, value), "");
int size = 10;
ListBuilder * builder = nix_make_list_builder(ctx, state, size);
Value * intValue = nix_alloc_value(ctx, state);
nix_init_int(ctx, intValue, 42);
nix_list_builder_insert(ctx, builder, 0, intValue);
nix_make_list(ctx, builder, value);
nix_list_builder_free(builder);
ASSERT_EQ(42, nix_get_int(ctx, nix_get_list_byidx(ctx, value, state, 0)));
ASSERT_EQ(nullptr, nix_get_list_byidx(ctx, value, state, 1));
ASSERT_EQ(10, nix_get_list_size(ctx, value));
ASSERT_STREQ("a list", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_LIST, nix_get_type(ctx, value));
// Clean up
nix_gc_decref(ctx, intValue);
}
TEST_F(nix_api_expr_test, nix_build_and_init_attr)
{
ASSERT_EQ(nullptr, nix_get_attr_byname(ctx, nullptr, state, 0));
ASSERT_EQ(nullptr, nix_get_attr_byidx(ctx, nullptr, state, 0, nullptr));
ASSERT_EQ(nullptr, nix_get_attr_name_byidx(ctx, nullptr, state, 0));
ASSERT_EQ(0, nix_get_attrs_size(ctx, nullptr));
ASSERT_EQ(false, nix_has_attr_byname(ctx, nullptr, state, "no-value"));
ASSERT_DEATH(nix_get_attr_byname(ctx, value, state, 0), "");
ASSERT_DEATH(nix_get_attr_byidx(ctx, value, state, 0, nullptr), "");
ASSERT_DEATH(nix_get_attr_name_byidx(ctx, value, state, 0), "");
ASSERT_DEATH(nix_get_attrs_size(ctx, value), "");
ASSERT_DEATH(nix_has_attr_byname(ctx, value, state, "no-value"), "");
int size = 10;
const char ** out_name = (const char **) malloc(sizeof(char *));
BindingsBuilder * builder = nix_make_bindings_builder(ctx, state, size);
Value * intValue = nix_alloc_value(ctx, state);
nix_init_int(ctx, intValue, 42);
Value * stringValue = nix_alloc_value(ctx, state);
nix_init_string(ctx, stringValue, "foo");
nix_bindings_builder_insert(ctx, builder, "a", intValue);
nix_bindings_builder_insert(ctx, builder, "b", stringValue);
nix_make_attrs(ctx, value, builder);
nix_bindings_builder_free(builder);
ASSERT_EQ(2, nix_get_attrs_size(ctx, value));
Value * out_value = nix_get_attr_byname(ctx, value, state, "a");
ASSERT_EQ(42, nix_get_int(ctx, out_value));
nix_gc_decref(ctx, out_value);
out_value = nix_get_attr_byidx(ctx, value, state, 0, out_name);
ASSERT_EQ(42, nix_get_int(ctx, out_value));
ASSERT_STREQ("a", *out_name);
nix_gc_decref(ctx, out_value);
ASSERT_STREQ("a", nix_get_attr_name_byidx(ctx, value, state, 0));
ASSERT_EQ(true, nix_has_attr_byname(ctx, value, state, "b"));
ASSERT_EQ(false, nix_has_attr_byname(ctx, value, state, "no-value"));
out_value = nix_get_attr_byname(ctx, value, state, "b");
std::string string_value;
nix_get_string(ctx, out_value, OBSERVE_STRING(string_value));
ASSERT_STREQ("foo", string_value.c_str());
nix_gc_decref(nullptr, out_value);
out_value = nix_get_attr_byidx(ctx, value, state, 1, out_name);
nix_get_string(ctx, out_value, OBSERVE_STRING(string_value));
ASSERT_STREQ("foo", string_value.c_str());
ASSERT_STREQ("b", *out_name);
nix_gc_decref(nullptr, out_value);
ASSERT_STREQ("b", nix_get_attr_name_byidx(ctx, value, state, 1));
ASSERT_STREQ("a set", nix_get_typename(ctx, value));
ASSERT_EQ(NIX_TYPE_ATTRS, nix_get_type(ctx, value));
// Clean up
nix_gc_decref(ctx, intValue);
nix_gc_decref(ctx, stringValue);
free(out_name);
}
}

View file

@ -91,7 +91,7 @@ namespace nix {
}
TEST_F(PrimOpTest, getEnv) {
setenv("_NIX_UNIT_TEST_ENV_VALUE", "test value", 1);
setEnv("_NIX_UNIT_TEST_ENV_VALUE", "test value");
auto v = eval("builtins.getEnv \"_NIX_UNIT_TEST_ENV_VALUE\"");
ASSERT_THAT(v, IsStringEq("test value"));
}