1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-26 20:01:15 +02:00

libexpr-c: Add nix_string_realise

This commit is contained in:
Robert Hensing 2024-04-05 16:08:18 +02:00
parent 62f8d8c9a9
commit 02c41aba5b
7 changed files with 235 additions and 10 deletions

View file

@ -7,6 +7,7 @@
#include "tests/nix_api_expr.hh"
#include "gmock/gmock.h"
#include <gtest/gtest.h>
namespace nixC {
@ -97,4 +98,88 @@ TEST_F(nix_api_expr_test, nix_build_drv)
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_view> 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);
names.push_back(p->path.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