mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2024-12-12 10:04:14 +02:00
Add error test harness/shim.
The hrnErrorThrowP() macro allows errors with specified fields to be generated, which simplifies testing. Update the common/exit test to use the new macro.
This commit is contained in:
parent
c5897007c4
commit
5e1a8e6895
@ -48,6 +48,10 @@ unit:
|
||||
- name: error
|
||||
total: 8
|
||||
feature: error
|
||||
harness:
|
||||
name: error
|
||||
shim:
|
||||
common/error: ~
|
||||
|
||||
coverage:
|
||||
- common/error
|
||||
|
26
test/src/common/harnessError.c
Normal file
26
test/src/common/harnessError.c
Normal file
@ -0,0 +1,26 @@
|
||||
/***********************************************************************************************************************************
|
||||
Harness for Loading Test Configurations
|
||||
***********************************************************************************************************************************/
|
||||
#include "build.auto.h"
|
||||
|
||||
#include "common/harnessError.h"
|
||||
#include "common/harnessDebug.h"
|
||||
#include "common/harnessLog.h"
|
||||
#include "common/harnessTest.h"
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Include shimmed C modules
|
||||
***********************************************************************************************************************************/
|
||||
{[SHIM_MODULE]}
|
||||
|
||||
/**********************************************************************************************************************************/
|
||||
void hrnErrorThrow(const HrnErrorThrowParam param)
|
||||
{
|
||||
errorContext.error.errorType = param.errorType != NULL ? param.errorType : &AssertError;
|
||||
errorContext.error.fileName = param.fileName != NULL ? param.fileName : "ERR_FILE";
|
||||
errorContext.error.functionName = param.functionName != NULL ? param.functionName : "ERR_FUNCTION";
|
||||
errorContext.error.fileLine = param.fileLine != 0 ? param.fileLine : 999;
|
||||
errorContext.error.message = param.message != NULL ? param.message : "ERR_MESSAGE";
|
||||
errorContext.error.stackTrace = param.stackTrace != NULL ? param.stackTrace : "ERR_STACK_TRACE";
|
||||
errorInternalPropagate();
|
||||
}
|
33
test/src/common/harnessError.h
Normal file
33
test/src/common/harnessError.h
Normal file
@ -0,0 +1,33 @@
|
||||
/***********************************************************************************************************************************
|
||||
Harness for Loading Test Configurations
|
||||
***********************************************************************************************************************************/
|
||||
#ifndef TEST_COMMON_HARNESS_ERROR_H
|
||||
#define TEST_COMMON_HARNESS_ERROR_H
|
||||
|
||||
#include "common/error.h"
|
||||
#include "common/type/param.h"
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Generate an error with specific field values
|
||||
|
||||
Useful for throwing errors with predictable output for fields that are set internally and subject to change, e.g. stackTrace. Note
|
||||
that all const char * fields should be set from constant strings, e.g. "ERROR", to ensure they don't get freed while the error is
|
||||
being propagated.
|
||||
***********************************************************************************************************************************/
|
||||
typedef struct HrnErrorThrowParam
|
||||
{
|
||||
VAR_PARAM_HEADER;
|
||||
const ErrorType *errorType; // Error type (defaults to AssertError)
|
||||
const char *fileName; // Source file where the error occurred (defaults to ERR_FILE)
|
||||
const char *functionName; // Function where the error occurred (defaults to ERR_FUNCTION)
|
||||
int fileLine; // Source file line where the error occurred (defaults to 999)
|
||||
const char *message; // Description of the error (defaults to ERR_MESSAGE)
|
||||
const char *stackTrace; // Stack trace (defaults to ERR_STACK_TRACE)
|
||||
} HrnErrorThrowParam;
|
||||
|
||||
#define hrnErrorThrowP(...) \
|
||||
hrnErrorThrow((HrnErrorThrowParam){VAR_PARAM_INIT, __VA_ARGS__})
|
||||
|
||||
void hrnErrorThrow(const HrnErrorThrowParam param) __attribute__((__noreturn__));
|
||||
|
||||
#endif
|
@ -7,6 +7,7 @@ Test Exit Routines
|
||||
#include "version.h"
|
||||
|
||||
#include "common/harnessConfig.h"
|
||||
#include "common/harnessError.h"
|
||||
#include "common/harnessFork.h"
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
@ -87,7 +88,7 @@ testRun(void)
|
||||
|
||||
TRY_BEGIN()
|
||||
{
|
||||
THROW(RuntimeError, "test debug error message");
|
||||
hrnErrorThrowP(.errorType = &RuntimeError, .message = "test debug error message");
|
||||
}
|
||||
CATCH_ANY()
|
||||
{
|
||||
@ -103,8 +104,7 @@ testRun(void)
|
||||
" options: --exec-id=1-test --process-max=4 --stanza=test\n"
|
||||
" \n"
|
||||
" stack trace:\n"
|
||||
" " TEST_PGB_PATH "/test/src/module/common/exitTest.c:testRun:90:(void)\n"
|
||||
" test.c:main:(argListSize: 1, argList: (char *[]))\n"
|
||||
" ERR_STACK_TRACE\n"
|
||||
" --------------------------------------------------------------------\n"
|
||||
"P00 INFO: archive-push:async command end: aborted with exception [122]\n"
|
||||
"P00 DEBUG: " TEST_PGB_PATH "/src/common/lock::lockRelease: (failOnNoLock: false)\n"
|
||||
@ -118,7 +118,7 @@ testRun(void)
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
TRY_BEGIN()
|
||||
{
|
||||
THROW(AssertError, "test assert message");
|
||||
hrnErrorThrowP(.message = "test assert message");
|
||||
}
|
||||
CATCH_ANY()
|
||||
{
|
||||
@ -133,8 +133,7 @@ testRun(void)
|
||||
" options: --exec-id=1-test --process-max=4 --stanza=test\n"
|
||||
" \n"
|
||||
" stack trace:\n"
|
||||
" " TEST_PGB_PATH "/test/src/module/common/exitTest.c:testRun:121:(void)\n"
|
||||
" test.c:main:(argListSize: 1, argList: (char *[]))\n"
|
||||
" ERR_STACK_TRACE\n"
|
||||
" --------------------------------------------------------------------\n"
|
||||
"P00 INFO: archive-push:async command end: aborted with exception [025]");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user