1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-07 10:11:47 +02:00

libutil-tests-support: Add file/line to ctx errors

This commit is contained in:
Robert Hensing 2025-04-02 17:00:59 +02:00
parent 70dcd738ef
commit 0dc9b6b7c7

View file

@ -3,6 +3,7 @@
#include "nix_api_util.h" #include "nix_api_util.h"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <string_view>
namespace nixC { namespace nixC {
@ -24,7 +25,12 @@ protected:
nix_c_context * ctx; nix_c_context * ctx;
inline void assert_ctx_ok() inline std::string loc(const char * file, int line)
{
return std::string(file) + ":" + std::to_string(line);
}
inline void assert_ctx_ok(const char * file, int line)
{ {
if (nix_err_code(ctx) == NIX_OK) { if (nix_err_code(ctx) == NIX_OK) {
return; return;
@ -32,16 +38,18 @@ protected:
unsigned int n; unsigned int n;
const char * p = nix_err_msg(nullptr, ctx, &n); const char * p = nix_err_msg(nullptr, ctx, &n);
std::string msg(p, n); std::string msg(p, n);
throw std::runtime_error(std::string("nix_err_code(ctx) != NIX_OK, message: ") + msg); throw std::runtime_error(loc(file, line) + ": nix_err_code(ctx) != NIX_OK, message: " + msg);
} }
#define assert_ctx_ok() assert_ctx_ok(__FILE__, __LINE__)
inline void assert_ctx_err() inline void assert_ctx_err(const char * file, int line)
{ {
if (nix_err_code(ctx) != NIX_OK) { if (nix_err_code(ctx) != NIX_OK) {
return; return;
} }
throw std::runtime_error("Got NIX_OK, but expected an error!"); throw std::runtime_error(loc(file, line) + ": Got NIX_OK, but expected an error!");
} }
#define assert_ctx_err() assert_ctx_err(__FILE__, __LINE__)
}; };
} }